Skip to content

Method resolution is not respecting trait constraints #7394

@xunilrj

Description

@xunilrj

The code below does not compile because the compiler very strangely thinks it should search for the function in B, when it is not mentioned anywhere inside the function f.

trait A { fn run() -> bool; }
trait B { fn run() -> bool; }

impl A for bool { fn run() -> bool { true } }
impl B for bool { fn run() -> bool { true } }

fn f<T>() where T: A {
    if T::run() {
    }
}

fn main() {
    f::<bool>();
}

Fails with:

             |
           8 | 
           9 | fn f<T>() where T: A {
          10 |     if T::run() {
             |           ^^^ Multiple applicable items in scope. 
            Disambiguate the associated function for candidate #0
              <bool as A>::run
            Disambiguate the associated function for candidate #1
              <bool as B>::run
          11 |         
          12 |     }
             |

Now, if we do make f ambiguous like:

fn f<T>() where T: A + B {
    if T::run() {
    }
}

The error message also does not help, as it should suggest to write <T as A>::run() or <T as B>::run(), and not bool, which is the concrete type.

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