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 1 commit
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
18 changes: 14 additions & 4 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 @@ -15,10 +18,17 @@ 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 {
// 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 &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");
}
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 @@ -133,3 +133,39 @@ fn basic_where_clauses_fail() {
expression evaluated to an empty collection: `decls.trait_invariants()`"#]]
)
}

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

[ /* TODO */ ]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add suggested messages in here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I copied this from pre-existing tests in this file, so I had no idea what to put here, but scrolled down and found some other examples. This is supposed to be some summary of what's wrong with the code?

Copy link
Contributor Author

@shua shua Jul 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah wait, maybe instead it's some relevant substring in the error output that shouldn't change even when you update the expect output? (guessing from assert_err! naming that tt must_have and the other expect)


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

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

[ /* TODO */ ]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here


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