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

fix: error on trait impl generics count mismatch #5582

Merged
merged 5 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
53 changes: 37 additions & 16 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1418,22 +1418,13 @@ impl<'context> Elaborator<'context> {
trait_impl.resolved_generics = self.generics.clone();

// Fetch trait constraints here
let trait_generics = if let Some(trait_id) = trait_impl.trait_id {
let trait_def = self.interner.get_trait(trait_id);
let resolved_generics = trait_def.generics.clone();
assert_eq!(resolved_generics.len(), trait_impl.trait_generics.len());
trait_impl
.trait_generics
.iter()
.enumerate()
.map(|(i, generic)| {
self.resolve_type_inner(generic.clone(), &resolved_generics[i].kind)
})
.collect()
} else {
// We still resolve as to continue type checking
vecmap(&trait_impl.trait_generics, |generic| self.resolve_type(generic.clone()))
};
let trait_generics = trait_impl
.trait_id
.and_then(|trait_id| self.resolve_trait_impl_generics(trait_impl, trait_id))
.unwrap_or_else(|| {
// We still resolve as to continue type checking
vecmap(&trait_impl.trait_generics, |generic| self.resolve_type(generic.clone()))
});

trait_impl.resolved_trait_generics = trait_generics;

Expand Down Expand Up @@ -1461,6 +1452,36 @@ impl<'context> Elaborator<'context> {
}
}

fn resolve_trait_impl_generics(
&mut self,
trait_impl: &UnresolvedTraitImpl,
trait_id: TraitId,
) -> Option<Vec<Type>> {
let trait_def = self.interner.get_trait(trait_id);
let resolved_generics = trait_def.generics.clone();
if resolved_generics.len() != trait_impl.trait_generics.len() {
self.push_err(CompilationError::TypeError(TypeCheckError::GenericCountMismatch {
item: trait_def.name.to_string(),
expected: resolved_generics.len(),
found: trait_impl.trait_generics.len(),
span: trait_impl.trait_path.span(),
}));

return None;
}

Some(
trait_impl
.trait_generics
.iter()
.zip(resolved_generics.iter())
.map(|(generic, resolved_generic)| {
self.resolve_type_inner(generic.clone(), &resolved_generic.kind)
})
.collect(),
)
jfecher marked this conversation as resolved.
Show resolved Hide resolved
}

fn define_function_metas_for_functions(&mut self, function_set: &mut UnresolvedFunctions) {
self.file = function_set.file_id;

Expand Down
26 changes: 26 additions & 0 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2310,7 +2310,7 @@
}

#[test]
fn underflowing_u8() {

Check warning on line 2313 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (underflowing)
let src = r#"
fn main() {
let _: u8 = -1;
Expand Down Expand Up @@ -2348,7 +2348,7 @@
}

#[test]
fn underflowing_i8() {

Check warning on line 2351 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (underflowing)
let src = r#"
fn main() {
let _: i8 = -129;
Expand Down Expand Up @@ -2459,3 +2459,29 @@
assert_eq!(span.start(), 4);
assert_eq!(span.end(), 9);
}

#[test]
fn trait_impl_generics_count_mismatch() {
let src = r#"
trait Foo {}

impl Foo<()> for Field {}

fn main() {}"#;
let errors = get_program_errors(src);
assert_eq!(errors.len(), 1);

let CompilationError::TypeError(TypeCheckError::GenericCountMismatch {
item,
expected,
found,
..
}) = &errors[0].0
else {
panic!("Expected a generic count mismatch error, got {:?}", errors[0].0);
};

assert_eq!(item, "Foo");
assert_eq!(*expected, 0);
assert_eq!(*found, 1);
}
Loading