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 trait interface method name resolution during typechecking of impl #5141

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
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,33 @@ fn type_check_trait_implementation(
let method = decl_engine.get_trait_fn(decl_ref);
let name = method.name.clone();
method_checklist.insert(name.clone(), method);
interface_item_refs.insert((name, self_type), item.clone());

// After monomorphization (and typechecking trait implementation
// works on a monomorphized version of a trait declaration), the
// newly produced [decl_id]s for interface methods can only be
// traced back to the original (non-monomorphized) trait
// declaration, which means that during [decl_id] substitutions
// of trait interface dummy functions for the corresponding
// interface method _implementations_, we need to refer to the
// original [decl_id]s of the non-monomorphized trait
// declaration.
let interface_item_parents = decl_engine.find_all_parents(engines, decl_ref.id());
match interface_item_parents.len() {
0 => { interface_item_refs.insert((name, self_type), item.clone()); },
1 => match interface_item_parents[0] {
AssociatedItemDeclId::TraitFn(parent_decl_id) => {
let parent_interface_item =
TyTraitInterfaceItem::TraitFn(DeclRef::new(name.clone(), parent_decl_id, decl_ref.span()));
interface_item_refs.insert((name, self_type), parent_interface_item);
}
_ => return Err(handler.emit_err(CompileError::Internal(
"A trait interface method's parent is expected to be another trait interface method",
name.span())))
}
_ => return Err(handler.emit_err(CompileError::Internal(
"A trait interface method is expected to have zero parents or one parent in the Declaration Engine",
name.span())))
}
}
TyTraitInterfaceItem::Constant(decl_ref) => {
let constant = decl_engine.get_constant(decl_ref);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = "name_resolution_after_monomorphization"
source = "member"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "name_resolution_after_monomorphization"
implicit-std = false

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
script;

trait Eq<T> {
fn eq(self, other: Self) -> bool;
} {
fn neq(self, other: Self) -> bool {
__eq((self.eq(other)), false)
}
}

impl Eq<u64> for u64 {
fn eq(self, other: Self) -> bool {
__eq(self, other)
}
}

fn main() -> u64 {
// block const evaluation for `x` (it does not currently support asm-blocks)
let x = asm(x: 42u64) { x: u64 };
let y = 1u64;
if x.neq(y) {
2
} else {
101
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
category = "run"
expected_result = { action = "return", value = 2 }
validate_abi = false
expected_warnings = 1