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: ensure where clauses propagated to trait default definitions #4894

Merged
merged 10 commits into from
Apr 29, 2024
11 changes: 6 additions & 5 deletions compiler/noirc_frontend/src/hir/resolution/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ fn resolve_trait_methods(

let mut resolver = Resolver::new(interner, &path_resolver, def_maps, file);
resolver.add_generics(generics);

resolver.add_existing_generics(&unresolved_trait.trait_def.generics, trait_generics);
resolver.add_existing_generic("Self", name_span, self_typevar);
resolver.set_self_type(Some(self_type.clone()));
Expand Down Expand Up @@ -207,16 +208,16 @@ fn collect_trait_impl_methods(

if overrides.is_empty() {
if let Some(default_impl) = &method.default_impl {
// copy 'where' clause from unresolved trait impl
let mut default_impl_clone = default_impl.clone();
default_impl_clone.def.where_clause = trait_impl.where_clause.clone();
jfecher marked this conversation as resolved.
Show resolved Hide resolved

let func_id = interner.push_empty_fn();
let module = ModuleId { local_id: trait_impl.module_id, krate: crate_id };
let location = Location::new(default_impl.def.span, trait_impl.file_id);
interner.push_function(func_id, &default_impl.def, module, location);
func_ids_in_trait.insert(func_id);
ordered_methods.push((
method.default_impl_module_id,
func_id,
*default_impl.clone(),
));
ordered_methods.push((method.default_impl_module_id, func_id, *default_impl_clone));
} else {
let error = DefCollectorErrorKind::TraitMissingMethod {
trait_name: interner.get_trait(trait_id).name.clone(),
Expand Down
22 changes: 22 additions & 0 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,28 @@ mod test {
}
}

#[test]
fn test_impl_self_within_default_def() {
let src = "
trait Bar {
fn ok(self) -> Self;

fn ref_ok(self) -> Self {
self.ok()
}
}

impl<T> Bar for (T, T) where T: Bar {
fn ok(self) -> Self {
self
}
}";

let errors = get_program_errors(src);
errors.iter().for_each(|err| println!("{:?}", err));
assert!(errors.is_empty());
}

fn get_program_captures(src: &str) -> Vec<Vec<String>> {
let (program, context, _errors) = get_program(src);
let interner = context.def_interner;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "impl_from_where_impl"
type = "bin"
authors = [""]
compiler_version = ">=0.27.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
trait Bar {
fn ok(self) -> Self;

fn ref_ok(self) -> Self {
self.ok()
}
}

impl<T> Bar for (T, T) where T: Bar {
fn ok(self) -> Self {
self
}
}

fn main() {}
Loading