Skip to content

conversion method suggestion is wrong given struct field init shorthand #52820

@zackmdavis

Description

@zackmdavis
Contributor

Consider this program:

struct Bravery {
    guts: String
}

fn main() {
    let guts = "mettle";
    let _ = Bravery { guts };
}

Currently (as of nightly 23 July), we suggest just tacking on a .to_string(), which isn't going to work.

error[E0308]: mismatched types
 --> shorthand_method.rs:7:23
  |
7 |     let _ = Bravery { guts };
  |                       ^^^^
  |                       |
  |                       expected struct `std::string::String`, found &str
  |                       help: try using a conversion method: `guts.to_string()`
  |
  = note: expected type `std::string::String`
             found type `&str`

error: aborting due to previous error

The conversion method suggestion was added in #46461. I fear it may be messier to remediate this than one might hope because demand_coerce takes a hir::Expr and would probably need another argument to know about the shorthand-ness, which lives on hir::FieldPat.

Activity

zackmdavis

zackmdavis commented on Mar 27, 2019

@zackmdavis
ContributorAuthor

#52965 (proposed solution in PR #59455) is similar.

added 5 commits that reference this issue on May 29, 2019
5579f01
459c713
cf42d11
8e16887
d2c927b
added a commit that references this issue on May 29, 2019
e83776f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @zackmdavis

      Issue actions

        conversion method suggestion is wrong given struct field init shorthand · Issue #52820 · rust-lang/rust