Skip to content

Commit 8a17e7c

Browse files
authored
fix: Model import should perform extension resolution (#2326)
This PR adds extension resolution to Rust model import. Closes #2320.
1 parent 9649a48 commit 8a17e7c

File tree

7 files changed

+162
-99
lines changed

7 files changed

+162
-99
lines changed

hugr-core/src/import.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use std::sync::Arc;
77

88
use crate::{
99
Direction, Hugr, HugrView, Node, Port,
10-
extension::{ExtensionId, ExtensionRegistry, SignatureError},
10+
extension::{
11+
ExtensionId, ExtensionRegistry, SignatureError, resolution::ExtensionResolutionError,
12+
},
1113
hugr::{HugrMut, NodeMetadata},
1214
ops::{
1315
AliasDecl, AliasDefn, CFG, Call, CallIndirect, Case, Conditional, Const, DFG,
@@ -60,16 +62,20 @@ enum ImportErrorInner {
6062
Context(#[source] Box<ImportErrorInner>, String),
6163

6264
/// A signature mismatch was detected during import.
63-
#[error("signature error: {0}")]
65+
#[error("signature error")]
6466
Signature(#[from] SignatureError),
6567

6668
/// An error relating to the loaded extension registry.
67-
#[error("extension error: {0}")]
69+
#[error("extension error")]
6870
Extension(#[from] ExtensionError),
6971

7072
/// Incorrect order hints.
71-
#[error("incorrect order hint: {0}")]
73+
#[error("incorrect order hint")]
7274
OrderHint(#[from] OrderHintError),
75+
76+
/// Extension resolution.
77+
#[error("extension resolution error")]
78+
ExtensionResolution(#[from] ExtensionResolutionError),
7379
}
7480

7581
#[derive(Debug, Clone, Error)]
@@ -186,6 +192,10 @@ pub fn import_hugr(
186192
ctx.link_ports()?;
187193
ctx.link_static_ports()?;
188194

195+
ctx.hugr
196+
.resolve_extension_defs(extensions)
197+
.map_err(ImportErrorInner::ExtensionResolution)?;
198+
189199
Ok(ctx.hugr)
190200
}
191201

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
11
---
22
source: hugr-core/tests/model.rs
3-
expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-add.edn\"))"
3+
expression: ast
44
---
55
(hugr 0)
66

77
(mod)
88

9-
(import core.fn)
9+
(import core.meta.description)
10+
11+
(import core.nat)
1012

11-
(import arithmetic.int.iadd)
13+
(import core.fn)
1214

1315
(import arithmetic.int.types.int)
1416

17+
(declare-operation
18+
arithmetic.int.iadd
19+
(param ?0 core.nat)
20+
(core.fn
21+
[(arithmetic.int.types.int ?0) (arithmetic.int.types.int ?0)]
22+
[(arithmetic.int.types.int ?0)])
23+
(meta
24+
(core.meta.description
25+
"addition modulo 2^N (signed and unsigned versions are the same op)")))
26+
1527
(define-func
1628
example.add
1729
(core.fn
18-
[arithmetic.int.types.int arithmetic.int.types.int]
19-
[arithmetic.int.types.int])
30+
[(arithmetic.int.types.int 6) (arithmetic.int.types.int 6)]
31+
[(arithmetic.int.types.int 6)])
2032
(dfg [%0 %1] [%2]
2133
(signature
2234
(core.fn
23-
[arithmetic.int.types.int arithmetic.int.types.int]
24-
[arithmetic.int.types.int]))
25-
(arithmetic.int.iadd [%0 %1] [%2]
35+
[(arithmetic.int.types.int 6) (arithmetic.int.types.int 6)]
36+
[(arithmetic.int.types.int 6)]))
37+
((arithmetic.int.iadd 6) [%0 %1] [%2]
2638
(signature
2739
(core.fn
28-
[arithmetic.int.types.int arithmetic.int.types.int]
29-
[arithmetic.int.types.int])))))
40+
[(arithmetic.int.types.int 6) (arithmetic.int.types.int 6)]
41+
[(arithmetic.int.types.int 6)])))))
Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,56 @@
11
---
22
source: hugr-core/tests/model.rs
3-
expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-cond.edn\"))"
3+
expression: ast
44
---
55
(hugr 0)
66

77
(mod)
88

9+
(import core.meta.description)
10+
11+
(import core.nat)
12+
913
(import core.fn)
1014

1115
(import core.adt)
1216

1317
(import arithmetic.int.types.int)
1418

15-
(import arithmetic.int.ineg)
19+
(declare-operation
20+
arithmetic.int.ineg
21+
(param ?0 core.nat)
22+
(core.fn [(arithmetic.int.types.int ?0)] [(arithmetic.int.types.int ?0)])
23+
(meta
24+
(core.meta.description
25+
"negation modulo 2^N (signed and unsigned versions are the same op)")))
1626

1727
(define-func
1828
example.cond
1929
(core.fn
20-
[(core.adt [[] []]) arithmetic.int.types.int]
21-
[arithmetic.int.types.int])
30+
[(core.adt [[] []]) (arithmetic.int.types.int 6)]
31+
[(arithmetic.int.types.int 6)])
2232
(dfg [%0 %1] [%2]
2333
(signature
2434
(core.fn
25-
[(core.adt [[] []]) arithmetic.int.types.int]
26-
[arithmetic.int.types.int]))
35+
[(core.adt [[] []]) (arithmetic.int.types.int 6)]
36+
[(arithmetic.int.types.int 6)]))
2737
(cond [%0 %1] [%2]
2838
(signature
2939
(core.fn
30-
[(core.adt [[] []]) arithmetic.int.types.int]
31-
[arithmetic.int.types.int]))
40+
[(core.adt [[] []]) (arithmetic.int.types.int 6)]
41+
[(arithmetic.int.types.int 6)]))
3242
(dfg [%3] [%3]
3343
(signature
34-
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int])))
44+
(core.fn
45+
[(arithmetic.int.types.int 6)]
46+
[(arithmetic.int.types.int 6)])))
3547
(dfg [%4] [%5]
3648
(signature
37-
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int]))
38-
(arithmetic.int.ineg [%4] [%5]
49+
(core.fn
50+
[(arithmetic.int.types.int 6)]
51+
[(arithmetic.int.types.int 6)]))
52+
((arithmetic.int.ineg 6) [%4] [%5]
3953
(signature
4054
(core.fn
41-
[arithmetic.int.types.int]
42-
[arithmetic.int.types.int])))))))
55+
[(arithmetic.int.types.int 6)]
56+
[(arithmetic.int.types.int 6)])))))))
Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
---
22
source: hugr-core/tests/model.rs
3-
expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-order.edn\"))"
3+
expression: ast
44
---
55
(hugr 0)
66

77
(mod)
88

9+
(import core.meta.description)
10+
11+
(import core.nat)
12+
913
(import core.order_hint.key)
1014

1115
(import core.fn)
@@ -14,47 +18,53 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-orde
1418

1519
(import arithmetic.int.types.int)
1620

17-
(import arithmetic.int.ineg)
21+
(declare-operation
22+
arithmetic.int.ineg
23+
(param ?0 core.nat)
24+
(core.fn [(arithmetic.int.types.int ?0)] [(arithmetic.int.types.int ?0)])
25+
(meta
26+
(core.meta.description
27+
"negation modulo 2^N (signed and unsigned versions are the same op)")))
1828

1929
(define-func
2030
main
2131
(core.fn
22-
[arithmetic.int.types.int
23-
arithmetic.int.types.int
24-
arithmetic.int.types.int
25-
arithmetic.int.types.int]
26-
[arithmetic.int.types.int
27-
arithmetic.int.types.int
28-
arithmetic.int.types.int
29-
arithmetic.int.types.int])
32+
[(arithmetic.int.types.int 6)
33+
(arithmetic.int.types.int 6)
34+
(arithmetic.int.types.int 6)
35+
(arithmetic.int.types.int 6)]
36+
[(arithmetic.int.types.int 6)
37+
(arithmetic.int.types.int 6)
38+
(arithmetic.int.types.int 6)
39+
(arithmetic.int.types.int 6)])
3040
(dfg [%0 %1 %2 %3] [%4 %5 %6 %7]
3141
(signature
3242
(core.fn
33-
[arithmetic.int.types.int
34-
arithmetic.int.types.int
35-
arithmetic.int.types.int
36-
arithmetic.int.types.int]
37-
[arithmetic.int.types.int
38-
arithmetic.int.types.int
39-
arithmetic.int.types.int
40-
arithmetic.int.types.int]))
43+
[(arithmetic.int.types.int 6)
44+
(arithmetic.int.types.int 6)
45+
(arithmetic.int.types.int 6)
46+
(arithmetic.int.types.int 6)]
47+
[(arithmetic.int.types.int 6)
48+
(arithmetic.int.types.int 6)
49+
(arithmetic.int.types.int 6)
50+
(arithmetic.int.types.int 6)]))
4151
(meta (core.order_hint.order 4 7))
4252
(meta (core.order_hint.order 5 6))
4353
(meta (core.order_hint.order 5 4))
4454
(meta (core.order_hint.order 6 7))
45-
(arithmetic.int.ineg [%0] [%4]
55+
((arithmetic.int.ineg 6) [%0] [%4]
4656
(signature
47-
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int]))
57+
(core.fn [(arithmetic.int.types.int 6)] [(arithmetic.int.types.int 6)]))
4858
(meta (core.order_hint.key 4)))
49-
(arithmetic.int.ineg [%1] [%5]
59+
((arithmetic.int.ineg 6) [%1] [%5]
5060
(signature
51-
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int]))
61+
(core.fn [(arithmetic.int.types.int 6)] [(arithmetic.int.types.int 6)]))
5262
(meta (core.order_hint.key 5)))
53-
(arithmetic.int.ineg [%2] [%6]
63+
((arithmetic.int.ineg 6) [%2] [%6]
5464
(signature
55-
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int]))
65+
(core.fn [(arithmetic.int.types.int 6)] [(arithmetic.int.types.int 6)]))
5666
(meta (core.order_hint.key 6)))
57-
(arithmetic.int.ineg [%3] [%7]
67+
((arithmetic.int.ineg 6) [%3] [%7]
5868
(signature
59-
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int]))
69+
(core.fn [(arithmetic.int.types.int 6)] [(arithmetic.int.types.int 6)]))
6070
(meta (core.order_hint.key 7)))))

hugr-model/tests/fixtures/model-add.edn

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
(mod)
44

5-
(define-func example.add
5+
(define-func
6+
example.add
67
(core.fn
7-
[arithmetic.int.types.int arithmetic.int.types.int]
8-
[arithmetic.int.types.int])
9-
(dfg
10-
[%0 %1]
11-
[%2]
12-
(signature (core.fn [arithmetic.int.types.int arithmetic.int.types.int] [arithmetic.int.types.int]))
13-
(arithmetic.int.iadd
14-
[%0 %1] [%2]
15-
(signature (core.fn [arithmetic.int.types.int arithmetic.int.types.int] [arithmetic.int.types.int])))))
8+
[(arithmetic.int.types.int 6) (arithmetic.int.types.int 6)]
9+
[(arithmetic.int.types.int 6)])
10+
(dfg [%0 %1] [%2]
11+
(signature
12+
(core.fn
13+
[(arithmetic.int.types.int 6) (arithmetic.int.types.int 6)]
14+
[(arithmetic.int.types.int 6)]))
15+
((arithmetic.int.iadd 6) [%0 %1] [%2]
16+
(signature
17+
(core.fn
18+
[(arithmetic.int.types.int 6) (arithmetic.int.types.int 6)]
19+
[(arithmetic.int.types.int 6)])))))

hugr-model/tests/fixtures/model-cond.edn

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,29 @@
22

33
(mod)
44

5-
(define-func example.cond
6-
(core.fn [(core.adt [[] []]) arithmetic.int.types.int]
7-
[arithmetic.int.types.int])
5+
(define-func
6+
example.cond
7+
(core.fn
8+
[(core.adt [[] []]) (arithmetic.int.types.int 6)]
9+
[(arithmetic.int.types.int 6)])
810
(dfg [%0 %1] [%2]
9-
(signature (core.fn [(core.adt [[] []]) arithmetic.int.types.int] [arithmetic.int.types.int]))
10-
(cond [%0 %1] [%2]
11-
(signature (core.fn [(core.adt [[] []]) arithmetic.int.types.int] [arithmetic.int.types.int]))
12-
(dfg [%3] [%3]
13-
(signature (core.fn [arithmetic.int.types.int] [arithmetic.int.types.int])))
14-
(dfg [%4] [%5]
15-
(signature (core.fn [arithmetic.int.types.int] [arithmetic.int.types.int]))
16-
(arithmetic.int.ineg [%4] [%5]
17-
(signature (core.fn [arithmetic.int.types.int] [arithmetic.int.types.int])))))))
11+
(signature
12+
(core.fn
13+
[(core.adt [[] []]) (arithmetic.int.types.int 6)]
14+
[(arithmetic.int.types.int 6)]))
15+
(cond [%0 %1] [%2]
16+
(signature
17+
(core.fn
18+
[(core.adt [[] []]) (arithmetic.int.types.int 6)]
19+
[(arithmetic.int.types.int 6)]))
20+
(dfg [%3] [%3]
21+
(signature
22+
(core.fn [(arithmetic.int.types.int 6)] [(arithmetic.int.types.int 6)])))
23+
(dfg [%4] [%5]
24+
(signature
25+
(core.fn [(arithmetic.int.types.int 6)] [(arithmetic.int.types.int 6)]))
26+
((arithmetic.int.ineg 6) [%4] [%5]
27+
(signature
28+
(core.fn
29+
[(arithmetic.int.types.int 6)]
30+
[(arithmetic.int.types.int 6)])))))))

0 commit comments

Comments
 (0)