Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

track adt variant and field names do not conflict #175

Merged
merged 5 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions crates/formality-check/src/adts.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::collections::HashSet;

use anyhow::bail;
use formality_prove::Env;
use formality_rust::grammar::{Adt, AdtBoundData, Field, Variant};
use formality_types::grammar::Fallible;
Expand All @@ -6,6 +9,20 @@ impl super::Check<'_> {
pub(super) fn check_adt(&self, adt: &Adt) -> Fallible<()> {
let Adt { id: _, binder } = adt;

// names is used to check that there are no name conflicts
let mut names = HashSet::new();
nikomatsakis marked this conversation as resolved.
Show resolved Hide resolved
for Variant { name, fields } in &adt.binder.peek().variants {
if !names.insert((name, None)) {
bail!("variant \"{name:?}\" defined multiple times");
}
let vname = name;
for Field { name, ty: _ } in fields {
if !names.insert((vname, Some(name))) {
bail!("field \"{name:?}\" of variant \"{vname:?}\" defined multiple times");
}
}
}

let mut env = Env::default();

let AdtBoundData {
Expand All @@ -15,8 +32,6 @@ impl super::Check<'_> {

self.prove_where_clauses_well_formed(&env, &where_clauses, &where_clauses)?;

// FIXME: check names are unique or integers from 0..n

for Variant { name: _, fields } in &variants {
for Field { name: _, ty } in fields {
self.prove_goal(&env, &where_clauses, ty.well_formed())?;
Expand Down
36 changes: 36 additions & 0 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,42 @@ fn basic_where_clauses_fail() {
)
}

#[test]
fn basic_adt_variant_dup() {
crate::assert_err!(
[
crate Foo {
enum Bar {
Baz{},
Baz{},
}
}
]

[ r#"variant "Baz" defined multiple times"#, ]

expect_test::expect![[r#"variant "Baz" defined multiple times"#]]
)
}

#[test]
fn basic_adt_field_dup() {
crate::assert_err!(
[
crate Foo {
struct Bar {
baz: (),
baz: (),
}
}
]

[ r#"field "baz" of variant "struct" defined multiple times"#, ]

expect_test::expect![[r#"field "baz" of variant "struct" defined multiple times"#]]
)
}

#[test]
fn trait_items_with_duplicate_fn_names() {
crate::assert_err!(
Expand Down
Loading