Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: determine correct bounds of custom types #1888

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading