Skip to content

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

Closed

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.

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Apr 14, 2020
  2. added
    D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
    on Apr 26, 2020
  3. kper commented on May 2, 2020

    @kper
    Contributor

    @rustbot claim

  4. self-assigned this
    on May 2, 2020
  5. added a commit that references this issue on May 22, 2020
  6. kper commented on May 23, 2020

    @kper
    Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to 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