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: let a trait impl that relies on another trait work #5646

Merged
merged 4 commits into from
Aug 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
14 changes: 10 additions & 4 deletions compiler/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
/// Stores the [Location] of a [Type] reference
pub(crate) type_ref_locations: Vec<(Type, Location)>,

/// In Noir's metaprogramming, a noir type has the type `Type`. When these are spliced

Check warning on line 197 in compiler/noirc_frontend/src/node_interner.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (metaprogramming)
/// into `quoted` expressions, we preserve the original type by assigning it a unique id
/// and creating a `Token::QuotedType(id)` from this id. We cannot create a token holding
/// the actual type since types do not implement Send or Sync.
Expand Down Expand Up @@ -1437,6 +1437,8 @@

let mut matching_impls = Vec::new();

let mut where_clause_errors = Vec::new();

for (existing_object_type2, impl_kind) in impls {
// Bug: We're instantiating only the object type's generics here, not all of the trait's generics like we need to
let (existing_object_type, instantiation_bindings) =
Expand Down Expand Up @@ -1471,14 +1473,17 @@
let trait_impl = self.get_trait_implementation(*impl_id);
let trait_impl = trait_impl.borrow();

if let Err(mut errors) = self.validate_where_clause(
if let Err(errors) = self.validate_where_clause(
&trait_impl.where_clause,
&mut fresh_bindings,
&instantiation_bindings,
recursion_limit,
) {
errors.push(make_constraint());
return Err(errors);
// Only keep the first errors we get from a failing where clause
if where_clause_errors.is_empty() {
where_clause_errors.extend(errors);
}
continue;
}
}

Expand All @@ -1491,7 +1496,8 @@
*type_bindings = fresh_bindings;
Ok(impl_)
} else if matching_impls.is_empty() {
Err(vec![make_constraint()])
where_clause_errors.push(make_constraint());
Err(where_clause_errors)
} else {
// multiple matching impls, type annotations needed
Err(vec![])
Expand Down
72 changes: 72 additions & 0 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2309,7 +2309,7 @@
}

#[test]
fn underflowing_u8() {

Check warning on line 2312 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 @@ -2347,7 +2347,7 @@
}

#[test]
fn underflowing_i8() {

Check warning on line 2350 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 @@ -2846,3 +2846,75 @@
CompilationError::TypeError(TypeCheckError::TypeMismatch { .. })
));
}

#[test]
fn trait_impl_for_a_type_that_implements_another_trait() {
let src = r#"
trait One {
fn one(self) -> i32;
}

impl One for i32 {
fn one(self) -> i32 {
self
}
}

trait Two {
fn two(self) -> i32;
}

impl<T> Two for T where T: One {
fn two(self) -> i32 {
self.one() + 1
}
}

fn use_it<T>(t: T) -> i32 where T: Two {
Two::two(t)
}

fn main() {}
"#;
assert_no_errors(src);
}

#[test]
fn trait_impl_for_a_type_that_implements_another_trait_with_another_impl_used() {
let src = r#"
trait One {
fn one(self) -> i32;
}

impl One for i32 {
fn one(self) -> i32 {
let _ = self;
1
}
}

trait Two {
fn two(self) -> i32;
}

impl<T> Two for T where T: One {
fn two(self) -> i32 {
self.one() + 1
}
}

impl Two for u32 {
fn two(self) -> i32 {
let _ = self;
0
}
}

fn use_it(t: u32) -> i32 {
Two::two(t)
}

fn main() {}
"#;
assert_no_errors(src);
}
Loading