6
6
//! It also defines a `validate_op_children` method for more complex tests that
7
7
//! require traversing the children.
8
8
9
+ use std:: collections:: HashMap ;
10
+
9
11
use itertools:: Itertools ;
10
12
use portgraph:: { NodeIndex , PortOffset } ;
11
13
use thiserror:: Error ;
@@ -60,6 +62,34 @@ impl ValidateOp for super::Module {
60
62
..Default :: default ( )
61
63
}
62
64
}
65
+
66
+ fn validate_op_children < ' a > (
67
+ & self ,
68
+ children : impl DoubleEndedIterator < Item = ( NodeIndex , & ' a OpType ) > ,
69
+ ) -> Result < ( ) , ChildrenValidationError > {
70
+ let mut ops_by_name = HashMap :: new ( ) ;
71
+ for ( node, op) in children {
72
+ let name = match op {
73
+ OpType :: FuncDecl ( fd) => Some ( & fd. name ) ,
74
+ OpType :: FuncDefn ( fd) => fd. public . then_some ( & fd. name ) ,
75
+ OpType :: Const ( c) => c. name . as_ref ( ) ,
76
+ _ => None ,
77
+ } ;
78
+ if let Some ( name) = name {
79
+ ops_by_name. entry ( name) . or_insert ( Vec :: new ( ) ) . push ( node)
80
+ }
81
+ }
82
+ for ( name, nodes) in ops_by_name {
83
+ let mut nodes = nodes. iter ( ) . copied ( ) ;
84
+ let fst = nodes. next ( ) . unwrap ( ) ;
85
+ let Some ( snd) = nodes. next ( ) else { continue } ;
86
+ return Err ( ChildrenValidationError :: DuplicateExternalNames {
87
+ name : name. clone ( ) ,
88
+ children : [ fst, snd] ,
89
+ } ) ;
90
+ }
91
+ Ok ( ( ) )
92
+ }
63
93
}
64
94
65
95
impl ValidateOp for super :: Conditional {
@@ -194,6 +224,14 @@ pub enum ChildrenValidationError {
194
224
expected_count : usize ,
195
225
actual_sum_rows : Vec < TypeRow > ,
196
226
} ,
227
+ /// Multiple nodes were exported using the same name from a [Module](super::Module)
228
+ #[ error( "Node is exported under same name {name} as earlier node {:?}" , children[ 0 ] ) ]
229
+ DuplicateExternalNames {
230
+ /// The name of a [FuncDecl], public [FuncDefn](super::FuncDefn) or [Const]
231
+ name : String ,
232
+ /// Two nodes node exported under that name
233
+ children : [ NodeIndex ; 2 ] ,
234
+ } ,
197
235
}
198
236
199
237
impl ChildrenValidationError {
@@ -205,6 +243,7 @@ impl ChildrenValidationError {
205
243
ChildrenValidationError :: ConditionalCaseSignature { child, .. } => * child,
206
244
ChildrenValidationError :: IOSignatureMismatch { child, .. } => * child,
207
245
ChildrenValidationError :: InvalidConditionalSum { child, .. } => * child,
246
+ ChildrenValidationError :: DuplicateExternalNames { children, .. } => children[ 1 ] ,
208
247
}
209
248
}
210
249
}
0 commit comments