Skip to content

Method resolution should obey usual shadowing rules for glob imports & prelude #51497

Open
@matklad

Description

@matklad

Today item import take precedence over glob imports which take precedence over prelude imports. This works for traits:

mod a {
    pub trait X {
        fn x(&self) { println!("a"); }
    }

    impl X for () {}
}

mod b {
    pub trait X {
        fn x(&self) { println!("b"); }
    }

    impl X for () {}
}

use a::X;
use b::*;

fn main() {
    ().x(); // Works and prints "a", b/c b is shadowed
}

However, this logic does not work for trait methods. If we rename b::X to b::Y, the code fails to compile:

mod a {
    pub trait X {
        fn x(&self) { println!("a"); }
    }

    impl X for () {}
}

mod b {
    pub trait Y {
        fn x(&self) { println!("b"); }
    }

    impl Y for () {}
}

use a::X;
use b::*;

fn main() {
    ().x(); // error: multiple `x` found
}

I think this problematic for two reasons:

Could we perhaps fix it?

r? @rust-lang/lang

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-resolveArea: Name/path resolution done by `rustc_resolve` specificallyA-trait-systemArea: Trait systemT-langRelevant to the language team, which will review and decide on the PR/issue.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions