Skip to content

Commit 8732bc3

Browse files
authored
Merge pull request #179 from FullyNonlinear/main
Check trait items for duplicate function names or associated type names
2 parents 3d95004 + 1cff4c2 commit 8732bc3

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

crates/formality-check/src/traits.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use anyhow::bail;
12
use fn_error_context::context;
3+
use formality_core::Set;
24
use formality_prove::Env;
35
use formality_rust::grammar::{
46
AssociatedTy, AssociatedTyBoundData, Fn, Trait, TraitBoundData, TraitItem, WhereClause,
@@ -31,8 +33,27 @@ impl super::Check<'_> {
3133
Ok(())
3234
}
3335

34-
fn check_trait_items_have_unique_names(&self, _trait_items: &[TraitItem]) -> Fallible<()> {
35-
// FIXME:
36+
fn check_trait_items_have_unique_names(&self, trait_items: &[TraitItem]) -> Fallible<()> {
37+
let mut functions = Set::new();
38+
let mut associated_types = Set::new();
39+
for trait_item in trait_items {
40+
match trait_item {
41+
TraitItem::Fn(f) => {
42+
if !functions.insert(&f.id) {
43+
bail!("the function name `{:?}` is defined multiple times", f.id);
44+
}
45+
}
46+
TraitItem::AssociatedTy(associated_ty) => {
47+
let AssociatedTy { id, .. } = associated_ty;
48+
if !associated_types.insert(id) {
49+
bail!(
50+
"the associated type name `{:?}` is defined multiple times",
51+
id
52+
);
53+
}
54+
}
55+
}
56+
}
3657
Ok(())
3758
}
3859

src/test/mod.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,48 @@ fn basic_where_clauses_fail() {
134134
expression evaluated to an empty collection: `decls.trait_invariants()`"#]]
135135
)
136136
}
137+
138+
#[test]
139+
fn trait_items_with_duplicate_fn_names() {
140+
crate::assert_err!(
141+
[
142+
crate core {
143+
trait A {
144+
fn a() -> ();
145+
fn a() -> ();
146+
}
147+
}
148+
]
149+
150+
["the function name `a` is defined multiple times",]
151+
152+
expect_test::expect![[r#"
153+
check_trait(A)
154+
155+
Caused by:
156+
the function name `a` is defined multiple times"#]]
157+
158+
);
159+
}
160+
161+
#[test]
162+
fn trait_items_with_duplicate_associated_type_names() {
163+
crate::assert_err!(
164+
[
165+
crate core {
166+
trait A {
167+
type Assoc : [];
168+
type Assoc : [];
169+
}
170+
}
171+
]
172+
173+
["the associated type name `Assoc` is defined multiple times",]
174+
175+
expect_test::expect![[r#"
176+
check_trait(A)
177+
178+
Caused by:
179+
the associated type name `Assoc` is defined multiple times"#]]
180+
);
181+
}

0 commit comments

Comments
 (0)