Skip to content

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

Description

@mqudsi

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions