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

Error message suggests invalid Rust code with PartialEq<OtherType> and ToString #83320

Open
ranile opened this issue Mar 20, 2021 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ranile
Copy link
Contributor

ranile commented Mar 20, 2021

Given the following code:

enum ETest {}

impl ToString for ETest {
    fn to_string(&self) -> String { String::new() }
}

#[derive(PartialEq)]
struct Props {
    to: ETest
}

impl PartialEq<String> for ETest {
    fn eq(&self, other: &String) -> bool { true }
}

struct Test {
    props: Props
}

trait Foo {
    type TProps: PartialEq;
}

impl Foo for Test {
    type TProps = Props;
}

The current output is:

error[E0308]: mismatched types
 --> src/lib.rs:9:5
  |
9 |     to: ETest
  |     ^^^^^^^^^
  |     |
  |     expected struct `String`, found enum `ETest`
  |     help: try using a conversion method: `(to: ETest).to_string()`
  |
  = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

If we replace our code with compiler's suggestion, we end up with this code:

#[derive(PartialEq)]
struct Props {
    (to: ETest).to_string()
}

Which is invalid Rust.

I've only observed this happen with PartialEq<Type> and ToString being implemented.
Related to: #63564

#63564 makes this error message even worse as it isn't obvious what's actually wrong here.

I reproduced this with today's nightly build (2021-03-19 f5f33ec0e0455eefa72f)

Playground link: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=7ac536d0521cefa0cf1f088b9d88db1f

@ranile ranile 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 20, 2021
@JohnTitor JohnTitor added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Mar 20, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 16, 2023
Do not provide suggestions when the spans come from expanded code that doesn't point at user code

Hide invalid proc-macro suggestions and track spans
coming from proc-macros pointing at attribute.

Effectively, unless the proc-macro keeps user spans,
suggestions will not be produced for the code they
produce.

r? `@ghost`

Fix rust-lang#107113, fix rust-lang#107976, fix rust-lang#107977, fix rust-lang#108748, fix rust-lang#106720, fix rust-lang#90557.

Could potentially address rust-lang#50141, rust-lang#67373, rust-lang#55146, rust-lang#78862, rust-lang#74043, rust-lang#88514, rust-lang#83320, rust-lang#91520, rust-lang#104071. CC rust-lang#50122, rust-lang#76360.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 17, 2023
Do not provide suggestions when the spans come from expanded code that doesn't point at user code

Hide invalid proc-macro suggestions and track spans
coming from proc-macros pointing at attribute.

Effectively, unless the proc-macro keeps user spans,
suggestions will not be produced for the code they
produce.

r? ``@ghost``

Fix rust-lang#107113, fix rust-lang#107976, fix rust-lang#107977, fix rust-lang#108748, fix rust-lang#106720, fix rust-lang#90557.

Could potentially address rust-lang#50141, rust-lang#67373, rust-lang#55146, rust-lang#78862, rust-lang#74043, rust-lang#88514, rust-lang#83320, rust-lang#91520, rust-lang#104071. CC rust-lang#50122, rust-lang#76360.
@estebank
Copy link
Contributor

Triage, current output:

error[E0308]: mismatched types
 --> src/lib.rs:9:5
  |
7 | #[derive(PartialEq)]
  |          --------- in this derive macro expansion
8 | struct Props {
9 |     to: ETest
  |     ^^^^^^^^^- help: try using a conversion method: `.to_string()`
  |     |
  |     expected `String`, found `ETest`
  |
  = note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

This is caused by the desugaring of the derive, and the suggestion not really being applicable in its original context. There are a few different tickets for this category of errors. The general solution would be to stop showing suggestions at all that come from desugarings. We need a good general solution here.

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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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

3 participants