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

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

Closed
zackmdavis opened this issue Jul 29, 2018 · 1 comment · Fixed by #61217
Closed

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

zackmdavis opened this issue Jul 29, 2018 · 1 comment · Fixed by #61217
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@zackmdavis
Copy link
Member

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.

@zackmdavis zackmdavis added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 12, 2018
@zackmdavis
Copy link
Member Author

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

Centril added a commit to Centril/rust that referenced this issue May 29, 2019
Account for short-hand init structs when suggesting conversion

Fix rust-lang#52820.
Centril added a commit to Centril/rust that referenced this issue May 29, 2019
Account for short-hand init structs when suggesting conversion

Fix rust-lang#52820.
Centril added a commit to Centril/rust that referenced this issue May 29, 2019
Account for short-hand init structs when suggesting conversion

Fix rust-lang#52820.
Centril added a commit to Centril/rust that referenced this issue May 29, 2019
Account for short-hand init structs when suggesting conversion

Fix rust-lang#52820.
Centril added a commit to Centril/rust that referenced this issue May 29, 2019
Account for short-hand init structs when suggesting conversion

Fix rust-lang#52820.
oli-obk added a commit to oli-obk/rust that referenced this issue May 29, 2019
Account for short-hand init structs when suggesting conversion

Fix rust-lang#52820.
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant