Skip to content

Help for associated constant with same name (ignoring case) as associated function #93844

Closed
@asquared31415

Description

@asquared31415

Given the following code:

struct MyS;

impl MyS {
    const FOO: i32 = 1;
    fn foo() -> MyS {MyS}
}

fn main() {
    let x: i32 = MyS::foo;
    
    let y: i32 = i32::max - 42;
    let z: i32 = i32::max;
}

playground

The current output is:

error[E0308]: mismatched types
 --> src/main.rs:9:18
  |
9 |     let x: i32 = MyS::foo;
  |            ---   ^^^^^^^^ expected `i32`, found fn item
  |            |
  |            expected due to this
  |
  = note: expected type `i32`
          found fn item `fn() -> MyS {MyS::foo}`

error[E0369]: cannot subtract `{integer}` from `fn(i32, i32) -> i32 {<i32 as Ord>::max}`
  --> src/main.rs:11:27
   |
11 |     let y: i32 = i32::max - 42;
   |                  -------- ^ -- {integer}
   |                  |
   |                  fn(i32, i32) -> i32 {<i32 as Ord>::max}

error[E0308]: mismatched types
  --> src/main.rs:12:18
   |
12 |     let z: i32 = i32::max;
   |            ---   ^^^^^^^^ expected `i32`, found fn item
   |            |
   |            expected due to this
   |
   = note: expected type `i32`
           found fn item `fn(i32, i32) -> i32 {<i32 as Ord>::max}`
help: use parentheses to call this function
   |
12 |     let z: i32 = i32::max(...);
   |                          +++++

Ideally the output of both errors should mention that the MyS::MAX or i32::MAX constants exists if the type of the constant would solve the type issues.

help: use `MyS::FOO` to refer to the constant of type MyS

or

help: use `i32::MAX` to refer to the constant of type i32

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.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