Skip to content

Commit

Permalink
fix: wrong module to lookup trait when using crate or super (#6974)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored and Rumata888 committed Jan 8, 2025
1 parent e9c0fed commit df4b035
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
11 changes: 6 additions & 5 deletions compiler/noirc_frontend/src/elaborator/path_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ impl<'context> Elaborator<'context> {

// Check if namespace
let found_ns = if current_module_id_is_struct {
match self.resolve_struct_function(starting_module, current_module, current_ident) {
match self.resolve_struct_function(importing_module, current_module, current_ident)
{
StructMethodLookupResult::NotFound(vec) => {
if vec.is_empty() {
return Err(PathResolutionError::Unresolved(current_ident.clone()));
Expand All @@ -306,7 +307,7 @@ impl<'context> Elaborator<'context> {
StructMethodLookupResult::FoundStructMethod(per_ns) => per_ns,
StructMethodLookupResult::FoundTraitMethod(per_ns, trait_id) => {
let trait_ = self.interner.get_trait(trait_id);
self.usage_tracker.mark_as_used(starting_module, &trait_.name);
self.usage_tracker.mark_as_used(importing_module, &trait_.name);
per_ns
}
StructMethodLookupResult::FoundOneTraitMethodButNotInScope(
Expand All @@ -324,7 +325,7 @@ impl<'context> Elaborator<'context> {
StructMethodLookupResult::FoundMultipleTraitMethods(vec) => {
let traits = vecmap(vec, |trait_id| {
let trait_ = self.interner.get_trait(trait_id);
self.usage_tracker.mark_as_used(starting_module, &trait_.name);
self.usage_tracker.mark_as_used(importing_module, &trait_.name);
self.fully_qualified_trait_path(trait_)
});
return Err(PathResolutionError::MultipleTraitsInScope {
Expand Down Expand Up @@ -382,7 +383,7 @@ impl<'context> Elaborator<'context> {

fn resolve_struct_function(
&self,
starting_module_id: ModuleId,
importing_module_id: ModuleId,
current_module: &ModuleData,
ident: &Ident,
) -> StructMethodLookupResult {
Expand All @@ -402,7 +403,7 @@ impl<'context> Elaborator<'context> {
}

// Otherwise, the function could be defined in zero, one or more traits.
let starting_module = self.get_module(starting_module_id);
let starting_module = self.get_module(importing_module_id);

// Gather a list of items for which their trait is in scope.
let mut results = Vec::new();
Expand Down
32 changes: 31 additions & 1 deletion compiler/noirc_frontend/src/tests/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ fn calls_trait_function_if_it_is_in_scope() {
}

#[test]
fn calls_trait_function_it_is_only_candidate_in_scope() {
fn calls_trait_function_if_it_is_only_candidate_in_scope() {
let src = r#"
use private_mod::Foo;
Expand Down Expand Up @@ -753,6 +753,36 @@ fn calls_trait_function_it_is_only_candidate_in_scope() {
assert_no_errors(src);
}

#[test]
fn calls_trait_function_if_it_is_only_candidate_in_scope_in_nested_module_using_super() {
let src = r#"
mod moo {
use super::public_mod::Foo;
pub fn method() {
let _ = super::Bar::foo();
}
}
fn main() {}
pub struct Bar {}
pub mod public_mod {
pub trait Foo {
fn foo() -> i32;
}
impl Foo for super::Bar {
fn foo() -> i32 {
42
}
}
}
"#;
assert_no_errors(src);
}

#[test]
fn errors_if_trait_is_not_in_scope_for_function_call_and_there_are_multiple_candidates() {
let src = r#"
Expand Down

0 comments on commit df4b035

Please sign in to comment.