Skip to content

Wrong span for E0277 error message "trait bound is not satisfied" #98645

Description

@sharifhsn

Problem

I had an error in my code where I tried to convert a u32 to an enum using try_into without implementing it first. My code essentially looked like this:

enum Thing {
    First,
    Second,
}

struct BigThing {
    value: u32,
    other: u16,
    thing: Thing,
}

impl BigThing {
    fn new(value: u32, other: u16, thing: Thing) -> Self {
        BigThing {value, other,  thing}
    }
}

//-------------------------------------------VVV note that this is u32, not Thing
fn make_thing(value: u32, other: u16, thing: u32) -> BigThing {
    BigThing::new(value, other, thing.try_into().unwrap())
}

This code generates the following error message:

error[E0277]: the trait bound `Thing: From<u32>` is not satisfied
  --> src/starlet/iso/iso.rs:67:19
   |
67 |     BigThing::new(value, other, thing.try_into().unwrap())
   |     ------------- ^^^^^ the trait `From<u32>` is not implemented for `Thing`
   |     |
   |     required by a bound introduced by this call
   |
   = note: required because of the requirements on the impl of `std::convert::Into<Thing>` for `u32`
   = note: required because of the requirements on the impl of `TryFrom<u32>` for `Thing`
   = note: required because of the requirements on the impl of `TryInto<Thing>` for `u32`

The compiler correctly notes that I have not implemented TryInto on Thing for u32, but it tells me that I haven't satisfied that trait bound for value, not thing. This problem does not occur if I use a struct initializer, only if I use a function.

Steps

  1. Copy the above code.
  2. Run cargo check

Possible Solution(s)

My guess here is that when showing an error for trait bound in a function argument, the checker defaults to the first argument. I'm not familiar with cargo code though so that's just speculation.

Notes

I am using the nightly compiler, but the same problem occurs on stable.

Version

cargo 1.64.0-nightly (a5e08c470 2022-06-23)
release: 1.64.0-nightly
commit-hash: a5e08c4703f202e30cdaf80ca3e7c00baa59c496
commit-date: 2022-06-23
host: x86_64-unknown-linux-gnu
libgit2: 1.4.2 (sys:0.14.2 vendored)
libcurl: 7.83.1-DEV (sys:0.4.55+curl-7.83.1 vendored ssl:OpenSSL/1.1.1n)
os: Arch Linux Rolling Release [64-bit]

Activity

  1. transferred this issue fromrust-lang/cargoon Jun 28, 2022
  2. ehuss commented on Jun 28, 2022

    @ehuss
    Contributor

    Thanks for the report! I have transferred this to the rust-lang/rust repository as this is a rustc issue.

  3. ehuss commented on Jun 28, 2022

    @ehuss
    Contributor

    I bisected this to nightly-2021-10-27. There are several PRs which are candidates there 29b1248...e269e6b

    The most likely look like either #90282, #90299, or #90075.

    The previous error message looked like:

    error[E0277]: the trait bound `Thing: From<u32>` is not satisfied
      --> src/main.rs:21:33
       |
    21 |     BigThing::new(value, other, thing.try_into().unwrap())
       |     -------------               ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<u32>` is not implemented for `Thing`
       |     |
       |     required by a bound introduced by this call
       |
       = note: required because of the requirements on the impl of `Into<Thing>` for `u32`
       = note: required because of the requirements on the impl of `TryFrom<u32>` for `Thing`
       = note: required because of the requirements on the impl of `TryInto<Thing>` for `u32`
    
    For more information about this error, try `rustc --explain E0277`.
    
  4. changed the title [-]`cargo check` error message says that wrong variable doesn't satisfy trait bound[/-] [+]Wrong span for E0277 error message "trait bound is not satisfied"[/+] on Jun 28, 2022
  5. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jun 28, 2022
  6. compiler-errors commented on Jun 28, 2022

    @compiler-errors
    Contributor

    This is probably #90181

  7. compiler-errors commented on Jun 28, 2022

    @compiler-errors
    Contributor

    I was looking into point_at_arg_instead_of_call_if_possible recently in #98607 so I can look at this too.

    @rustbot claim

  8. added 2 commits that reference this issue on Jun 29, 2022
    bb8ad95
    fcbb2e8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.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