Skip to content

Commit

Permalink
fix: error on incorrect generic count for impl and type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Jul 29, 2024
1 parent 28211a3 commit 0ac3bf1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
28 changes: 20 additions & 8 deletions compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ impl<'context> Elaborator<'context> {

if let Some(type_alias) = self.lookup_type_alias(path.clone()) {
let type_alias = type_alias.borrow();
let actual_generic_count = args.len();
let expected_generic_count = type_alias.generics.len();
let type_alias_string = type_alias.to_string();
let id = type_alias.id;
Expand All @@ -247,9 +248,13 @@ impl<'context> Elaborator<'context> {
self.resolve_type_inner(arg, &generic.kind)
});

self.verify_generics_count(expected_generic_count, &mut args, span, || {
type_alias_string
});
self.verify_generics_count(
expected_generic_count,
actual_generic_count,
&mut args,
span,
|| type_alias_string,
);

if let Some(item) = self.current_item {
self.interner.add_type_alias_dependency(item, id);
Expand Down Expand Up @@ -279,6 +284,8 @@ impl<'context> Elaborator<'context> {
}

let expected_generic_count = struct_type.borrow().generics.len();
let actual_generic_count = args.len();

if !self.in_contract()
&& self
.interner
Expand All @@ -296,9 +303,13 @@ impl<'context> Elaborator<'context> {
self.resolve_type_inner(arg, &generic.kind)
});

self.verify_generics_count(expected_generic_count, &mut args, span, || {
struct_type.borrow().to_string()
});
self.verify_generics_count(
expected_generic_count,
actual_generic_count,
&mut args,
span,
|| struct_type.borrow().to_string(),
);

if let Some(current_item) = self.current_item {
let dependency_id = struct_type.borrow().id;
Expand Down Expand Up @@ -333,15 +344,16 @@ impl<'context> Elaborator<'context> {
fn verify_generics_count(
&mut self,
expected_count: usize,
actual_count: usize,
args: &mut Vec<Type>,
span: Span,
type_name: impl FnOnce() -> String,
) {
if args.len() != expected_count {
if actual_count != expected_count {
self.push_err(ResolverError::IncorrectGenericCount {
span,
item_name: type_name(),
actual: args.len(),
actual: actual_count,
expected: expected_count,
});

Expand Down
52 changes: 52 additions & 0 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2610,3 +2610,55 @@ fn turbofish_in_middle_of_variable_unsupported_yet() {
CompilationError::TypeError(TypeCheckError::UnsupportedTurbofishUsage { .. }),
));
}

#[test]
fn incorrect_generic_count_on_struct_impl() {
let src = r#"
struct Foo {}
impl <T> Foo<T> {}
fn main() {}
"#;

let errors = get_program_errors(src);
assert_eq!(errors.len(), 1);

let CompilationError::ResolverError(ResolverError::IncorrectGenericCount {
actual,
expected,
..
}) = errors[0].0
else {
panic!("Expected an incorrect generic count mismatch error, got {:?}", errors[0].0);
};

assert_eq!(actual, 1);
assert_eq!(expected, 0);
}

#[test]
fn incorrect_generic_count_on_type_alias() {
let src = r#"
struct Foo {}
type Bar = Foo<i32>;
fn main() {}
"#;

let errors = get_program_errors(src);
assert_eq!(errors.len(), 1);

let CompilationError::ResolverError(ResolverError::IncorrectGenericCount {
actual,
expected,
..
}) = errors[0].0
else {
panic!("Expected an incorrect generic count mismatch error, got {:?}", errors[0].0);
};

assert_eq!(actual, 1);
assert_eq!(expected, 0);
}

0 comments on commit 0ac3bf1

Please sign in to comment.