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

Adding a trait impl for an integer type causes existing code to break with misleading error #138402

Open
zslayton opened this issue Mar 12, 2025 · 1 comment
Labels
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.

Comments

@zslayton
Copy link
Contributor

zslayton commented Mar 12, 2025

Code

Playground

#![allow(dead_code)]

// There's a type `Foo`...
struct Foo(pub usize);

// ...that you can easily create from a `usize`.
impl From<usize> for Foo {
    fn from(value: usize) -> Self {
        Foo(value)
    }
}

// There's also a trait `FooLike`...
trait FooLike {}
// ...that is implemented by anything we can turn into a `Foo`.
impl<T> FooLike for T where Foo: From<T> {}

// I have a generic function that accepts any implementation of `FooLike`.
fn use_foo_like(_f: impl FooLike) -> Result<(), ()> {
    Ok(())
}

fn main() -> Result<(), ()> {
    // If I invoke that function with the literal `0`,
    // the compiler correctly determines that it's a `usize`.
    use_foo_like(0)
}

// However, if you uncomment the code below, the program no longer compiles.
// The error is:
//    error[E0277]: the trait bound `Foo: From<i32>` is not satisfied

/*
impl From<u32> for Foo {
    fn from(value: u32) -> Self {
        Foo(value as usize)
    }
}
*/

Current output

error[E0277]: the trait bound `Foo: From<i32>` is not satisfied
  --> src/main.rs:26:18
   |
26 |     use_foo_like(0)
   |     ------------ ^ the trait `From<i32>` is not implemented for `Foo`
   |     |
   |     required by a bound introduced by this call
   |
   = help: the following other types implement trait `From<T>`:
             `Foo` implements `From<u32>`
             `Foo` implements `From<usize>`
note: required for `i32` to implement `FooLike`
  --> src/main.rs:16:9
   |
16 | impl<T> FooLike for T where Foo: From<T> {}
   |         ^^^^^^^     ^            ------- unsatisfied trait bound introduced here
note: required by a bound in `use_foo_like`
  --> src/main.rs:19:26
   |
19 | fn use_foo_like(_f: impl FooLike) -> Result<(), ()> {
   |                          ^^^^^^^ required by this bound in `use_foo_like`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Desired output

error[E0277]: the integer type of literal `0` is ambiguous
  --> src/main.rs:26:18
   |
26 |     use_foo_like(0)
   |     ------------ ^ trait `FooLike` is implemented for multiple integer types
   |     |
   |     required by a bound introduced by this call
   |
   = help: the following integer types implement trait `FooLike`:
             `u32` implements `FooLike`
             `usize` implements `FooLike`
note: when multiple integer types implement a trait, integer literals without a type hint are considered `i32`
hint: consider implementing `FooLike` for `i32`
note: required by a bound in `use_foo_like`
  --> src/main.rs:19:26
   |
19 | fn use_foo_like(_f: impl FooLike) -> Result<(), ()> {
   |                          ^^^^^^^ required by this bound in `use_foo_like`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Rationale and extra context

This appears to be an edge case in the type resolver. When only one integer type implements the necessary trait, the compiler can correctly infer that a bare integer literal must be of that type. However, when an implementation of that trait for a second integer type is added, it breaks existing user code because the type resolver is obligated to fall back to treating bare integer literals as i32.

The current diagnostic output doesn't explain why it's talking about i32, and users will likely be very surprised that adding a trait implementation breaks their build.

Other cases

Rust Version

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: aarch64-apple-darwin
release: 1.85.0
LLVM version: 19.1.7

Anything else?

No response

@zslayton zslayton 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 Mar 12, 2025
@juntyr
Copy link
Contributor

juntyr commented Mar 12, 2025

I think this might come from rust defaulting untyped integer literals to i32 when there is ambiguity

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 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

2 participants