Skip to content

Commit b4b9433

Browse files
authored
feat!: hugr-model use explicit Option<Visibility>, with ::Unspecified in capnp (#2424)
BREAKING CHANGE: hugr-model: text syntax (functions *must* now have public or private specified), and renumbers capnp enum variants from #2413 (unreleased)
1 parent 1ba9ef6 commit b4b9433

32 files changed

+129
-90
lines changed

hugr-core/src/export.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ struct Context<'a> {
9595
// that ensures that the `node_to_id` and `id_to_node` maps stay in sync.
9696
}
9797

98+
const NO_VIS: Option<Visibility> = None;
99+
98100
impl<'a> Context<'a> {
99101
pub fn new(hugr: &'a Hugr, bump: &'a Bump) -> Self {
100102
let mut module = table::Module::default();
@@ -331,7 +333,7 @@ impl<'a> Context<'a> {
331333
OpType::FuncDefn(func) => self.with_local_scope(node_id, |this| {
332334
let symbol = this.export_poly_func_type(
333335
func.func_name(),
334-
func.visibility().clone().into(),
336+
Some(func.visibility().clone().into()),
335337
func.signature(),
336338
);
337339
regions = this.bump.alloc_slice_copy(&[this.export_dfg(
@@ -345,7 +347,7 @@ impl<'a> Context<'a> {
345347
OpType::FuncDecl(func) => self.with_local_scope(node_id, |this| {
346348
let symbol = this.export_poly_func_type(
347349
func.func_name(),
348-
func.visibility().clone().into(),
350+
Some(func.visibility().clone().into()),
349351
func.signature(),
350352
);
351353
table::Operation::DeclareFunc(symbol)
@@ -354,10 +356,8 @@ impl<'a> Context<'a> {
354356
OpType::AliasDecl(alias) => self.with_local_scope(node_id, |this| {
355357
// TODO: We should support aliases with different types and with parameters
356358
let signature = this.make_term_apply(model::CORE_TYPE, &[]);
357-
// Visibility is not spec'd in hugr-core
358-
let visibility = this.bump.alloc(Visibility::default()); // good to common up!?
359359
let symbol = this.bump.alloc(table::Symbol {
360-
visibility,
360+
visibility: &NO_VIS, // not spec'd in hugr-core
361361
name: &alias.name,
362362
params: &[],
363363
constraints: &[],
@@ -370,10 +370,8 @@ impl<'a> Context<'a> {
370370
let value = this.export_type(&alias.definition);
371371
// TODO: We should support aliases with different types and with parameters
372372
let signature = this.make_term_apply(model::CORE_TYPE, &[]);
373-
// Visibility is not spec'd in hugr-core
374-
let visibility = this.bump.alloc(Visibility::default()); // good to common up!?
375373
let symbol = this.bump.alloc(table::Symbol {
376-
visibility,
374+
visibility: &NO_VIS, // not spec'd in hugr-core
377375
name: &alias.name,
378376
params: &[],
379377
constraints: &[],
@@ -548,8 +546,7 @@ impl<'a> Context<'a> {
548546

549547
let symbol = self.with_local_scope(node, |this| {
550548
let name = this.make_qualified_name(opdef.extension_id(), opdef.name());
551-
// Visibility of OpDef's has no effect
552-
this.export_poly_func_type(name, Visibility::default(), poly_func_type)
549+
this.export_poly_func_type(name, None, poly_func_type)
553550
});
554551

555552
let meta = {
@@ -800,7 +797,7 @@ impl<'a> Context<'a> {
800797
pub fn export_poly_func_type<RV: MaybeRV>(
801798
&mut self,
802799
name: &'a str,
803-
visibility: Visibility,
800+
visibility: Option<Visibility>,
804801
t: &PolyFuncTypeBase<RV>,
805802
) -> &'a table::Symbol<'a> {
806803
let mut params = BumpVec::with_capacity_in(t.params().len(), self.bump);

hugr-core/src/import.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -938,12 +938,12 @@ impl<'a> Context<'a> {
938938
node_data: &'a table::Node<'a>,
939939
parent: Node,
940940
) -> Result<Node, ImportError> {
941+
let visibility = symbol.visibility.clone().ok_or(ImportErrorInner::Invalid(
942+
"No visibility for FuncDefn".to_string(),
943+
))?;
941944
self.import_poly_func_type(node_id, *symbol, |ctx, signature| {
942-
let optype = OpType::FuncDefn(FuncDefn::new_vis(
943-
symbol.name,
944-
signature,
945-
symbol.visibility.clone().into(),
946-
));
945+
let optype =
946+
OpType::FuncDefn(FuncDefn::new_vis(symbol.name, signature, visibility.into()));
947947

948948
let node = ctx.make_node(node_id, optype, parent)?;
949949

@@ -967,12 +967,12 @@ impl<'a> Context<'a> {
967967
symbol: &'a table::Symbol<'a>,
968968
parent: Node,
969969
) -> Result<Node, ImportError> {
970+
let visibility = symbol.visibility.clone().ok_or(ImportErrorInner::Invalid(
971+
"No visibility for FuncDecl".to_string(),
972+
))?;
970973
self.import_poly_func_type(node_id, *symbol, |ctx, signature| {
971-
let optype = OpType::FuncDecl(FuncDecl::new_vis(
972-
symbol.name,
973-
signature,
974-
symbol.visibility.clone().into(),
975-
));
974+
let optype =
975+
OpType::FuncDecl(FuncDecl::new_vis(symbol.name, signature, visibility.into()));
976976
let node = ctx.make_node(node_id, optype, parent)?;
977977
Ok(node)
978978
})

hugr-core/tests/snapshots/model__roundtrip_add.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ expression: ast
2525
"addition modulo 2^N (signed and unsigned versions are the same op)")))
2626

2727
(define-func
28+
public
2829
example.add
2930
(core.fn
3031
[(arithmetic.int.types.int 6) (arithmetic.int.types.int 6)]

hugr-core/tests/snapshots/model__roundtrip_call.snap

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

@@ -17,12 +17,14 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-call
1717
(import arithmetic.int.types.int)
1818

1919
(declare-func
20+
public
2021
example.callee
2122
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int])
2223
(meta (compat.meta_json "description" "\"This is a function declaration.\""))
2324
(meta (compat.meta_json "title" "\"Callee\"")))
2425

2526
(define-func
27+
private
2628
example.caller
2729
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int])
2830
(meta
@@ -41,6 +43,7 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-call
4143
(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int])))))
4244

4345
(define-func
46+
private
4447
example.load
4548
(core.fn [] [(core.fn [arithmetic.int.types.int] [arithmetic.int.types.int])])
4649
(dfg [] [%0]

hugr-core/tests/snapshots/model__roundtrip_cfg.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ expression: ast
1616

1717
(import core.adt)
1818

19-
(define-func example.cfg_loop (param ?0 core.type) (core.fn [?0] [?0])
19+
(define-func private example.cfg_loop (param ?0 core.type) (core.fn [?0] [?0])
2020
(dfg [%0] [%1]
2121
(signature (core.fn [?0] [?0]))
2222
(cfg [%0] [%1]
@@ -30,7 +30,7 @@ expression: ast
3030
((core.make_adt 0) [%4] [%5]
3131
(signature (core.fn [?0] [(core.adt [[?0] [?0]])])))))))))
3232

33-
(define-func example.cfg_order (param ?0 core.type) (core.fn [?0] [?0])
33+
(define-func public example.cfg_order (param ?0 core.type) (core.fn [?0] [?0])
3434
(dfg [%0] [%1]
3535
(signature (core.fn [?0] [?0]))
3636
(cfg [%0] [%1]

hugr-core/tests/snapshots/model__roundtrip_cond.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ expression: ast
2525
"negation modulo 2^N (signed and unsigned versions are the same op)")))
2626

2727
(define-func
28+
private
2829
example.cond
2930
(core.fn
3031
[(core.adt [[] []]) (arithmetic.int.types.int 6)]

hugr-core/tests/snapshots/model__roundtrip_const.snap

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

@@ -28,7 +28,10 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-cons
2828

2929
(import core.adt)
3030

31-
(define-func example.bools (core.fn [] [(core.adt [[] []]) (core.adt [[] []])])
31+
(define-func
32+
public
33+
example.bools
34+
(core.fn [] [(core.adt [[] []]) (core.adt [[] []])])
3235
(dfg [] [%0 %1]
3336
(signature (core.fn [] [(core.adt [[] []]) (core.adt [[] []])]))
3437
((core.load_const (core.const.adt [[] []] _ 0 [])) [] [%0]
@@ -37,6 +40,7 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-cons
3740
(signature (core.fn [] [(core.adt [[] []])])))))
3841

3942
(define-func
43+
public
4044
example.make-pair
4145
(core.fn
4246
[]
@@ -73,7 +77,10 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-cons
7377
[[(collections.array.array 5 (arithmetic.int.types.int 6))
7478
arithmetic.float.types.float64]])])))))
7579

76-
(define-func example.f64-json (core.fn [] [arithmetic.float.types.float64])
80+
(define-func
81+
public
82+
example.f64-json
83+
(core.fn [] [arithmetic.float.types.float64])
7784
(dfg [] [%0 %1]
7885
(signature
7986
(core.fn

hugr-core/tests/snapshots/model__roundtrip_constraints.snap

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

@@ -17,13 +17,15 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-cons
1717
(import core.fn)
1818

1919
(declare-func
20+
private
2021
array.replicate
2122
(param ?0 core.nat)
2223
(param ?1 core.type)
2324
(where (core.nonlinear ?1))
2425
(core.fn [?1] [(collections.array.array ?0 ?1)]))
2526

2627
(declare-func
28+
public
2729
array.copy
2830
(param ?0 core.nat)
2931
(param ?1 core.type)
@@ -33,6 +35,7 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-cons
3335
[(collections.array.array ?0 ?1) (collections.array.array ?0 ?1)]))
3436

3537
(define-func
38+
public
3639
util.copy
3740
(param ?0 core.type)
3841
(where (core.nonlinear ?0))

hugr-core/tests/snapshots/model__roundtrip_entrypoint.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ expression: ast
1010

1111
(import core.entrypoint)
1212

13-
(define-func main (core.fn [] [])
13+
(define-func public main (core.fn [] [])
1414
(dfg (signature (core.fn [] [])) (meta core.entrypoint)))
1515

1616
(mod)
@@ -19,7 +19,7 @@ expression: ast
1919

2020
(import core.entrypoint)
2121

22-
(define-func wrapper_dfg (core.fn [] [])
22+
(define-func private wrapper_dfg (core.fn [] [])
2323
(dfg (signature (core.fn [] [])) (meta core.entrypoint)))
2424

2525
(mod)
@@ -34,7 +34,7 @@ expression: ast
3434

3535
(import core.adt)
3636

37-
(define-func wrapper_cfg (core.fn [] [])
37+
(define-func public wrapper_cfg (core.fn [] [])
3838
(dfg
3939
(signature (core.fn [] []))
4040
(cfg

hugr-core/tests/snapshots/model__roundtrip_loop.snap

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

@@ -14,7 +14,7 @@ expression: "roundtrip(include_str!(\"../../hugr-model/tests/fixtures/model-loop
1414

1515
(import core.adt)
1616

17-
(define-func example.loop (param ?0 core.type) (core.fn [?0] [?0])
17+
(define-func private example.loop (param ?0 core.type) (core.fn [?0] [?0])
1818
(dfg [%0] [%1]
1919
(signature (core.fn [?0] [?0]))
2020
(tail-loop [%0] [%1]

0 commit comments

Comments
 (0)