Skip to content

Method resolution not working for associated method under bool context #7398

@xunilrj

Description

@xunilrj

The example below demonstrates a miscompilation on method resolution under "bool" context.

trait A { fn f() -> bool; }

impl A for u64 { fn f () -> bool { true } }
impl A for bool { fn f () -> bool { false } } // comment this line and everything will work

fn ff<T>() -> bool where T: A {
    let v: bool = T::f();
    v
}

fn main() {
    if !(ff::<u64>()) { // we should return true, but we return false
        __revert(0);
    }
}

This last part, the context, seems to be important, because I think the problem lies at sway-core/src/semantic_analysis/type_check_context.rs:903. We are trying to aid type inference using the expected type in the context.

But the generic argument is not in the return type, and so should not be affected by the context.

 // Consider items from supersets indicated by the annotation return type.
 if !matches!(&*type_engine.get(annotation_type), TypeInfo::Unknown) && !type_id.is_concrete(self.engines, crate::TreatNumericAs::Concrete) {
        ...
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions