Skip to content

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

Closed
@gardrek

Description

@gardrek

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.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.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