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

Compiler erroneously suggests inserting a syntax error in struct definition #71136

Closed
gardrek opened this issue Apr 14, 2020 · 2 comments
Closed
Assignees
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

@gardrek
Copy link
Contributor

gardrek commented Apr 14, 2020

When defining a struct that derives Clone, that owns an instance of another struct, which is not Clone, an invalid suggestion is given to resolve the problem.

I tried this code:

struct Foo(u8);

#[derive(Clone)]
struct FooHolster {
    the_foos: Vec<Foo>,
}

I expected any suggestions to at least be valid Rust. Instead, the compiler suggested "borrowing" but put the & in the wrong place, which is invalid syntax.

error[E0277]: the trait bound `Foo: std::clone::Clone` is not satisfied
 --> src/main.rs:5:5
  |
5 |     the_foos: Vec<Foo>,
  |     ^^^^^^^^^^^^^^^^^^
  |     |
  |     expected an implementor of trait `std::clone::Clone`
  |     help: consider borrowing here: `&the_foos: Vec<Foo>`
  |
  = note: required because of the requirements on the impl of `std::clone::Clone` for `std::vec::Vec<Foo>`
  = note: required by `std::clone::Clone::clone`
  = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

I checked this in Stable 1.42.0 and "Nightly version: 1.44.0-nightly (2020-04-12 3712e11)" on Rust Playground

A link to the code in Playground:
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=0fcf1446729fefbbd984472106334097

This issue has been assigned to @kper via this comment.

@gardrek gardrek added the C-bug Category: This is a bug. label Apr 14, 2020
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 14, 2020
@estebank estebank added the D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. label Apr 26, 2020
@kper
Copy link
Contributor

kper commented May 2, 2020

@rustbot claim

@rustbot rustbot self-assigned this May 2, 2020
kper added a commit to kper/rust that referenced this issue May 3, 2020
RalfJung added a commit to RalfJung/rust that referenced this issue May 22, 2020
Fix suggestion to borrow in struct

The corresponding issue is rust-lang#71136.
The compiler suggests that borrowing `the_foos` might solve the problem. This is obviously incorrect.
```
struct Foo(u8);

#[derive(Clone)]
struct FooHolster {
    the_foos: Vec<Foo>,
}
```

I propose as fix to check if there is any colon in the span. However, there might a case where `my_method(B { a: 1, b : foo })` would be appropriate to show a suggestion for `&B ...`.  To fix that too, we can simply check if there is a bracket in the span. This is only possible because both spans are different.
Issue's span: `the_foos: Vec<Foo>`
other's span: `B { a : 1, b : foo }`
@kper
Copy link
Contributor

kper commented May 23, 2020

@gardrek can you close the issue? The PR has been merged

@gardrek gardrek closed this as completed May 31, 2020
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

5 participants