Skip to content

Commit

Permalink
Merge pull request #179 from FullyNonlinear/main
Browse files Browse the repository at this point in the history
Check trait items for duplicate function names or associated type names
  • Loading branch information
nikomatsakis authored Jul 1, 2024
2 parents 3d95004 + 1cff4c2 commit 8732bc3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
25 changes: 23 additions & 2 deletions crates/formality-check/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use anyhow::bail;
use fn_error_context::context;
use formality_core::Set;
use formality_prove::Env;
use formality_rust::grammar::{
AssociatedTy, AssociatedTyBoundData, Fn, Trait, TraitBoundData, TraitItem, WhereClause,
Expand Down Expand Up @@ -31,8 +33,27 @@ impl super::Check<'_> {
Ok(())
}

fn check_trait_items_have_unique_names(&self, _trait_items: &[TraitItem]) -> Fallible<()> {
// FIXME:
fn check_trait_items_have_unique_names(&self, trait_items: &[TraitItem]) -> Fallible<()> {
let mut functions = Set::new();
let mut associated_types = Set::new();
for trait_item in trait_items {
match trait_item {
TraitItem::Fn(f) => {
if !functions.insert(&f.id) {
bail!("the function name `{:?}` is defined multiple times", f.id);
}
}
TraitItem::AssociatedTy(associated_ty) => {
let AssociatedTy { id, .. } = associated_ty;
if !associated_types.insert(id) {
bail!(
"the associated type name `{:?}` is defined multiple times",
id
);
}
}
}
}
Ok(())
}

Expand Down
45 changes: 45 additions & 0 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,48 @@ fn basic_where_clauses_fail() {
expression evaluated to an empty collection: `decls.trait_invariants()`"#]]
)
}

#[test]
fn trait_items_with_duplicate_fn_names() {
crate::assert_err!(
[
crate core {
trait A {
fn a() -> ();
fn a() -> ();
}
}
]

["the function name `a` is defined multiple times",]

expect_test::expect![[r#"
check_trait(A)
Caused by:
the function name `a` is defined multiple times"#]]

);
}

#[test]
fn trait_items_with_duplicate_associated_type_names() {
crate::assert_err!(
[
crate core {
trait A {
type Assoc : [];
type Assoc : [];
}
}
]

["the associated type name `Assoc` is defined multiple times",]

expect_test::expect![[r#"
check_trait(A)
Caused by:
the associated type name `Assoc` is defined multiple times"#]]
);
}

0 comments on commit 8732bc3

Please sign in to comment.