Skip to content

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

Closed
@zackmdavis

Description

@zackmdavis

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.

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

    No branches or pull requests

    Issue actions