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

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

Closed
sharifhsn opened this issue Jun 28, 2022 · 4 comments · Fixed by #98607
Closed

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

sharifhsn opened this issue Jun 28, 2022 · 4 comments · Fixed by #98607
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@sharifhsn
Copy link

sharifhsn commented Jun 28, 2022

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]
@sharifhsn sharifhsn added the C-bug Category: This is a bug. label Jun 28, 2022
@ehuss ehuss transferred this issue from rust-lang/cargo Jun 28, 2022
@ehuss
Copy link
Contributor

ehuss commented Jun 28, 2022

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

@ehuss
Copy link
Contributor

ehuss commented Jun 28, 2022

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

@ehuss ehuss 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" Jun 28, 2022
@ehuss ehuss 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 Jun 28, 2022
@compiler-errors
Copy link
Member

This is probably #90181

@compiler-errors
Copy link
Member

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

@rustbot claim

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jun 29, 2022
…on, r=oli-obk

Clean up arg mismatch diagnostic, generalize tuple wrap suggestion

This is based on top of rust-lang#97542, so just look at the last commit which contains the relevant changes.

1. Remove `final_arg_types` which was one of the last places we were using raw (`usize`) indices instead of typed indices in the arg mismatch suggestion code.
2. Improve the tuple wrap suggestion, now we suggest things like `call(a, b, c, d)` -> `call(a, (b, c), d)` 😺
3. Folded in fix rust-lang#98645
@bors bors closed this as completed in fcbb2e8 Jun 29, 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 C-bug Category: This is a bug. 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.

3 participants