Skip to content

Commit 2bfeaae

Browse files
authored
feat: Packages do not include the hugr extensions by default (#2187)
Closes #2185, by shifting the responsibility to the user. It also helps greatly reduce payload size, as we don't encode the prelude extension anymore :)
1 parent 0f78fab commit 2bfeaae

File tree

2 files changed

+10
-36
lines changed

2 files changed

+10
-36
lines changed

hugr-cli/tests/validate.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ fn test_package_validation(package_string: String, mut val_cmd: Command) {
193193
// package with float extension and hugr that uses floats can validate
194194
val_cmd.write_stdin(package_string);
195195
val_cmd.arg("-");
196-
val_cmd.arg("--no-std");
197196

198197
val_cmd.assert().success().stderr(contains(VALID_PRINT));
199198
}

hugr-core/src/package.rs

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
//! Bundles of hugr modules along with the extension required to load them.
22
33
use derive_more::{Display, Error, From};
4-
use itertools::Itertools;
54
use std::io;
65

76
use crate::envelope::{read_envelope, write_envelope, EnvelopeConfig, EnvelopeError};
8-
use crate::extension::{ExtensionId, ExtensionRegistry, PRELUDE_REGISTRY};
7+
use crate::extension::{ExtensionRegistry, PRELUDE_REGISTRY};
98
use crate::hugr::{HugrView, ValidationError};
109
use crate::{Hugr, Node};
1110

@@ -23,24 +22,25 @@ pub struct Package {
2322
impl Package {
2423
/// Create a new package from a list of hugrs.
2524
///
26-
/// Collects the extensions used in the modules and stores them in top-level
27-
/// `extensions` attribute.
25+
/// The hugr extensions are not automatically added to the package. Make
26+
/// sure to manually include any non-standard extensions to
27+
/// [`Package::extensions`].
2828
pub fn new(modules: impl IntoIterator<Item = Hugr>) -> Self {
2929
let modules: Vec<Hugr> = modules.into_iter().collect();
30-
let mut extensions = ExtensionRegistry::default();
31-
for module in &modules {
32-
extensions.extend(module.extensions());
33-
}
3430
Self {
3531
modules,
36-
extensions,
32+
extensions: ExtensionRegistry::default(),
3733
}
3834
}
3935

4036
/// Create a new package containing a single HUGR.
37+
///
38+
/// The hugr extensions are not automatically added to the package. Make
39+
/// sure to manually include any non-standard extensions to
40+
/// [`Package::extensions`].
4141
pub fn from_hugr(hugr: Hugr) -> Self {
4242
Package {
43-
extensions: hugr.extensions().clone(),
43+
extensions: ExtensionRegistry::default(),
4444
modules: vec![hugr],
4545
}
4646
}
@@ -51,19 +51,6 @@ impl Package {
5151
pub fn validate(&self) -> Result<(), PackageValidationError> {
5252
for hugr in self.modules.iter() {
5353
hugr.validate()?;
54-
55-
let missing_exts = hugr
56-
.extensions()
57-
.ids()
58-
.filter(|id| !self.extensions.contains(id))
59-
.cloned()
60-
.collect_vec();
61-
if !missing_exts.is_empty() {
62-
return Err(PackageValidationError::MissingExtension {
63-
missing: missing_exts,
64-
available: self.extensions.ids().cloned().collect(),
65-
});
66-
}
6754
}
6855
Ok(())
6956
}
@@ -126,18 +113,6 @@ impl AsRef<[Hugr]> for Package {
126113
#[derive(Debug, Display, From, Error)]
127114
#[non_exhaustive]
128115
pub enum PackageValidationError {
129-
/// Error raised while processing the package extensions.
130-
#[display("The package modules use the extension{} {} not present in the defined set. The declared extensions are {}",
131-
if missing.len() > 1 {"s"} else {""},
132-
missing.iter().map(|id| id.to_string()).collect::<Vec<_>>().join(", "),
133-
available.iter().map(|id| id.to_string()).collect::<Vec<_>>().join(", "),
134-
)]
135-
MissingExtension {
136-
/// The missing extensions.
137-
missing: Vec<ExtensionId>,
138-
/// The available extensions.
139-
available: Vec<ExtensionId>,
140-
},
141116
/// Error raised while validating the package hugrs.
142117
Validation(ValidationError<Node>),
143118
}

0 commit comments

Comments
 (0)