Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
asquared31415 opened this issue Feb 10, 2022 · 2 comments · Fixed by #105843
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@asquared31415
Copy link
Contributor

asquared31415 commented Feb 10, 2022

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
@asquared31415 asquared31415 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 10, 2022
@compiler-errors compiler-errors added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Jun 20, 2022
@r-raymond
Copy link
Contributor

Let me know if anyone was working on this already. @rustbot claim

@atamakahere-git
Copy link

I would like to work on this issue with a little guidance. Can anyone help get me started? this is my first issue.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 22, 2022
Suggest associated const on possible capitalization mistake

Suggest `i32::MAX` if we typed `i32::max` without making a function call.

Fixes rust-lang#93844
@bors bors closed this as completed in ecdabb4 Dec 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants