Skip to content

Commit

Permalink
Bugfix: determine correct bounds of custom types
Browse files Browse the repository at this point in the history
  • Loading branch information
zrho committed Jan 21, 2025
1 parent b38a2e8 commit 5786561
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
24 changes: 20 additions & 4 deletions hugr-core/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
types::{
type_param::TypeParam, type_row::TypeRowBase, CustomType, FuncTypeBase, MaybeRV,
PolyFuncType, PolyFuncTypeBase, RowVariable, Signature, Type, TypeArg, TypeBase, TypeBound,
TypeEnum, TypeRow,
TypeEnum, TypeName, TypeRow,
},
Direction, Hugr, HugrView, Node, Port,
};
Expand Down Expand Up @@ -52,6 +52,14 @@ pub enum ImportError {
/// The available extensions in the registry.
available: Vec<ExtensionId>,
},
/// An extension type is missing.
#[error("Importing the hugr requires extension {ext} to have a type named {name}, but it was not found.")]
ExtensionType {
/// The extension that is missing the type.
ext: ExtensionId,
/// The name of the missing type.
name: TypeName,
},
/// The model is not well-formed.
#[error("validate error: {0}")]
Model(#[from] model::ModelError),
Expand Down Expand Up @@ -1088,13 +1096,21 @@ impl<'a> Context<'a> {
available: self.extensions.ids().cloned().collect(),
})?;

let ext_type =
extension_ref
.get_type(&id)
.ok_or_else(|| ImportError::ExtensionType {
ext: extension.clone(),
name: id.clone(),
})?;

let bound = ext_type.bound(&args);

Ok(TypeBase::new_extension(CustomType::new(
id,
args,
extension,
// As part of the migration from `TypeBound`s to constraints, we pretend that all
// `TypeBound`s are copyable.
TypeBound::Copyable,
bound,
&Arc::downgrade(extension_ref),
)))
}
Expand Down
19 changes: 11 additions & 8 deletions hugr-core/tests/snapshots/model__roundtrip_constraints.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-cons
---
(hugr 0)

(import prelude.Array)
(import collections.array.array)

(declare-func array.replicate
(forall ?0 type)
(forall ?1 nat)
(where (nonlinear ?0))
[?0] [(@ prelude.Array ?0 ?1)] (ext))
(forall ?0 nat)
(forall ?1 type)
(where (nonlinear ?1))
[?1] [(@ collections.array.array ?0 ?1)] (ext))

(declare-func array.copy
(forall ?0 type)
(where (nonlinear ?0))
[(@ prelude.Array ?0)] [(@ prelude.Array ?0) (@ prelude.Array ?0)] (ext))
(forall ?0 nat)
(forall ?1 type)
(where (nonlinear ?1))
[(@ collections.array.array ?0 ?1)]
[(@ collections.array.array ?0 ?1) (@ collections.array.array ?0 ?1)]
(ext))

(define-func util.copy
(forall ?0 type)
Expand Down
7 changes: 4 additions & 3 deletions hugr-model/tests/fixtures/model-constraints.edn
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
(hugr 0)

(declare-func array.replicate
(forall ?t type)
(forall ?n nat)
(forall ?t type)
(where (nonlinear ?t))
[?t] [(@ prelude.Array ?t ?n)]
[?t] [(@ collections.array.array ?n ?t)]
(ext))

(declare-func array.copy
(forall ?n nat)
(forall ?t type)
(where (nonlinear ?t))
[(@ prelude.Array ?t)] [(@ prelude.Array ?t) (@ prelude.Array ?t)] (ext))
[(@ collections.array.array ?n ?t)] [(@ collections.array.array ?n ?t) (@ collections.array.array ?n ?t)] (ext))

(define-func util.copy
(forall ?t type)
Expand Down

0 comments on commit 5786561

Please sign in to comment.