Skip to content

Name resolution does not work if associated type and constant use the same name #44247

Closed
@matklad

Description

@matklad

Consider this program:

trait T {
    type X;
    const X: Self::X;
}
fn foo<X: T>() {
    let _: X::X = X::X;
}

I believe it should compile OK, but currently (Rust 1.20) it fails to compile with a wrong error message:

Error
   Compiling example_project v0.1.0 (file:///home/matklad/projects/intellij-rust/exampleProject)
error[E0599]: no associated item named `X` found for type `X` in the current scope
 --> src/main.rs:6:19
  |
6 |     let _: X::X = X::X;
  |                   ^^^^
  |
  = note: found the following associated functions; to be used as methods, functions must have a `self` parameter
note: candidate #1 is defined in the trait `T`
 --> src/main.rs:2:5
  |
2 |     type X;
  |     ^^^^^^^
  = help: to disambiguate the method call, write `T::X(...)` instead
  = help: items from traits can only be used if the trait is implemented and in scope
  = note: the following trait defines an item `X`, perhaps you need to implement it:
          candidate #1: `T`

error: aborting due to previous error

error: Could not compile `example_project`.

To learn more, run the command again with --verbose.

For comparison, the following program builds just fine:

mod X {
    pub type X = i32;
    pub const X: self::X = 0;
}
fn foo() {
    let _: X::X = X::X;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.T-compilerRelevant to the compiler 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