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

Regression in error reporting on {integer} type ambiguity between 1.61 and 1.62 #98404

Open
mqudsi opened this issue Jun 22, 2022 · 3 comments
Open
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Milestone

Comments

@mqudsi
Copy link
Contributor

mqudsi commented Jun 22, 2022

This is strongly related to #98357.

The presence of a Mul impl for both u8 and i32 introduces an ambiguity in main() where the type of the literal 7 cannot be determined. #98357 goes into why this is an error since it can be fixed by assigning a type (Foo) to prod, but this issue is about a regression in rustc 1.62 which drops information about the ambiguity in the literal integer's type from the error message/notes as compared to 1.61 (which does not).

use core::ops::Mul;

#[derive(Debug)]
struct Foo(i32);

impl Mul<Foo> for i32 {
    type Output = Foo;

    fn mul(self, other: Foo) -> Self::Output {
        Foo(self * other.0)
    }
}

impl Mul<Foo> for u8 {
    type Output = Foo;
    
    fn mul(self, other: Foo) -> Self::Output {
        Foo(self as i32 * other.0)
    }
}

fn main() {
    let prod = 7 * Foo(6);
    assert_eq!(prod.0, 42);
}

Playground link

The error message emitted by 1.61 (current stable):

error[[E0282]](https://doc.rust-lang.org/stable/error-index.html#E0282): type annotations needed
  --> src/main.rs:24:16
   |
23 |     let prod = 7 * Foo(6);
   |         ---- consider giving `prod` a type
24 |     assert_eq!(prod.0, 42);
   |                ^^^^ cannot infer type
   |
   = note: type must be known at this point

error[[E0283]](https://doc.rust-lang.org/stable/error-index.html#E0283): type annotations needed
  --> src/main.rs:23:18
   |
23 |     let prod = 7 * Foo(6);
   |                  ^ cannot infer type for type `{integer}`
   |
note: multiple `impl`s satisfying `{integer}: Mul<Foo>` found
  --> src/main.rs:6:1
   |
6  | impl Mul<Foo> for i32 {
   | ^^^^^^^^^^^^^^^^^^^^^
...
14 | impl Mul<Foo> for u8 {
   | ^^^^^^^^^^^^^^^^^^^^

Some errors have detailed explanations: E0282, E0283.
For more information about an error, try `rustc --explain E0282`.

The error message emitted by 1.62 (2022-06-13 1bc802e) (the current beta):

error[[E0282]](https://doc.rust-lang.org/beta/error-index.html#E0282): type annotations needed
  --> src/main.rs:24:16
   |
23 |     let prod = 7 * Foo(6);
   |         ---- consider giving `prod` a type
24 |     assert_eq!(prod.0, 42);
   |                ^^^^ cannot infer type
   |
   = note: type must be known at this point

For more information about this error, try `rustc --explain E0282`.

Notice that while one perhaps valid resolution has been provided, the actual cause of the error is completely obscured.

@mqudsi mqudsi added C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression. labels Jun 22, 2022
@rustbot rustbot added I-prioritize Issue: Indicates that prioritization has been requested for this issue. regression-from-stable-to-beta Performance or correctness regression from stable to beta. and removed regression-untriaged Untriaged performance or correctness regression. labels Jun 22, 2022
@apiraino
Copy link
Contributor

WG-prioritization assigning priority (Zulip discussion).

@rustbot label -I-prioritize +P-medium

@rustbot rustbot added P-medium Medium priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jun 23, 2022
@pietroalbini pietroalbini added this to the 1.62.0 milestone Jun 24, 2022
@workingjubilee workingjubilee added regression-from-stable-to-stable Performance or correctness regression from one stable version to another. and removed regression-from-stable-to-beta Performance or correctness regression from stable to beta. labels Jul 6, 2022
@mqudsi
Copy link
Contributor Author

mqudsi commented Jul 15, 2022

@rustbot label +A-diagnostics

@rustbot rustbot added the A-diagnostics Area: Messages for errors, warnings, and lints label Jul 15, 2022
@Noratrieb Noratrieb added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 5, 2023
@RodBurman
Copy link

The error reporting in 1.82 is better (showing where to add type to variable) but the obscuring of the error remains:

Compiling mulreg v0.1.0 (/Users/rod/code/rust/triage/mulreg)
errorsrc/main.rs:23:9
   |
23 |     let prod = 7 * Foo(6);
   |         ^^^^
24 |     assert_eq!(prod.0, 42);
   |                ---- type must be known at this point
   |
help: consider giving `prod` an explicit type
   |
23 |     let prod: /* Type */ = 7 * Foo(6);
   |             ++++++++++++

For more information about this error, try `rustc --explain E0282`.
error: could not compile `mulreg` (bin "mulreg") due to 1 previous error[E0282]: type annotations needed

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 C-bug Category: This is a bug. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants