File tree Expand file tree Collapse file tree 2 files changed +50
-4
lines changed
crates/formality-check/src Expand file tree Collapse file tree 2 files changed +50
-4
lines changed Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashSet ;
2
+
3
+ use anyhow:: bail;
1
4
use formality_prove:: Env ;
2
5
use formality_rust:: grammar:: { Adt , AdtBoundData , Field , Variant } ;
3
6
use formality_types:: grammar:: Fallible ;
@@ -15,10 +18,17 @@ impl super::Check<'_> {
15
18
16
19
self . prove_where_clauses_well_formed ( & env, & where_clauses, & where_clauses) ?;
17
20
18
- // FIXME: check names are unique or integers from 0..n
19
-
20
- for Variant { name : _, fields } in & variants {
21
- for Field { name : _, ty } in fields {
21
+ // names is used to check that there are no name conflicts
22
+ let mut names = HashSet :: new ( ) ;
23
+ for Variant { name, fields } in & variants {
24
+ if !names. insert ( ( name, None ) ) {
25
+ bail ! ( "variant \" {name:?}\" defined multiple times" ) ;
26
+ }
27
+ let vname = name;
28
+ for Field { name, ty } in fields {
29
+ if !names. insert ( ( vname, Some ( name) ) ) {
30
+ bail ! ( "field \" {name:?}\" of variant \" {vname:?}\" defined multiple times" ) ;
31
+ }
22
32
self . prove_goal ( & env, & where_clauses, ty. well_formed ( ) ) ?;
23
33
}
24
34
}
Original file line number Diff line number Diff line change @@ -133,3 +133,39 @@ fn basic_where_clauses_fail() {
133
133
expression evaluated to an empty collection: `decls.trait_invariants()`"# ] ]
134
134
)
135
135
}
136
+
137
+ #[ test]
138
+ fn basic_adt_variant_dup ( ) {
139
+ crate :: assert_err!(
140
+ [
141
+ crate Foo {
142
+ enum Bar {
143
+ Baz { } ,
144
+ Baz { } ,
145
+ }
146
+ }
147
+ ]
148
+
149
+ [ /* TODO */ ]
150
+
151
+ expect_test:: expect![ [ r#"variant "Baz" defined multiple times"# ] ]
152
+ )
153
+ }
154
+
155
+ #[ test]
156
+ fn basic_adt_field_dup ( ) {
157
+ crate :: assert_err!(
158
+ [
159
+ crate Foo {
160
+ struct Bar {
161
+ baz: ( ) ,
162
+ baz: ( ) ,
163
+ }
164
+ }
165
+ ]
166
+
167
+ [ /* TODO */ ]
168
+
169
+ expect_test:: expect![ [ r#"field "baz" of variant "struct" defined multiple times"# ] ]
170
+ )
171
+ }
You can’t perform that action at this time.
0 commit comments