Skip to content

Commit 459c713

Browse files
authored
Rollup merge of rust-lang#61217 - estebank:issue-52820, r=Centril
Account for short-hand init structs when suggesting conversion Fix rust-lang#52820.
2 parents 3430c6f + 24b2e20 commit 459c713

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

src/librustc_typeck/check/demand.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
270270
None
271271
}
272272

273-
fn is_hir_id_from_struct_pattern_shorthand_field(&self, hir_id: hir::HirId, sp: Span) -> bool {
273+
crate fn is_hir_id_from_struct_pattern_shorthand_field(
274+
&self,
275+
hir_id: hir::HirId,
276+
sp: Span,
277+
) -> bool {
274278
let cm = self.sess().source_map();
275279
let parent_id = self.tcx.hir().get_parent_node_by_hir_id(hir_id);
276280
if let Some(parent) = self.tcx.hir().find_by_hir_id(parent_id) {

src/librustc_typeck/check/mod.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -5010,6 +5010,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
50105010
Applicability::MachineApplicable,
50115011
);
50125012
} else if !self.check_for_cast(err, expr, found, expected) {
5013+
let is_struct_pat_shorthand_field = self.is_hir_id_from_struct_pattern_shorthand_field(
5014+
expr.hir_id,
5015+
expr.span,
5016+
);
50135017
let methods = self.get_conversion_methods(expr.span, expected, found);
50145018
if let Ok(expr_text) = self.sess().source_map().span_to_snippet(expr.span) {
50155019
let mut suggestions = iter::repeat(&expr_text).zip(methods.iter())
@@ -5019,14 +5023,18 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
50195023
None // do not suggest code that is already there (#53348)
50205024
} else {
50215025
let method_call_list = [".to_vec()", ".to_string()"];
5022-
if receiver.ends_with(".clone()")
5026+
let sugg = if receiver.ends_with(".clone()")
50235027
&& method_call_list.contains(&method_call.as_str()) {
50245028
let max_len = receiver.rfind(".").unwrap();
5025-
Some(format!("{}{}", &receiver[..max_len], method_call))
5026-
}
5027-
else {
5028-
Some(format!("{}{}", receiver, method_call))
5029-
}
5029+
format!("{}{}", &receiver[..max_len], method_call)
5030+
} else {
5031+
format!("{}{}", receiver, method_call)
5032+
};
5033+
Some(if is_struct_pat_shorthand_field {
5034+
format!("{}: {}", receiver, sugg)
5035+
} else {
5036+
sugg
5037+
})
50305038
}
50315039
}).peekable();
50325040
if suggestions.peek().is_some() {
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
struct Bravery {
2+
guts: String,
3+
brains: String,
4+
}
5+
6+
fn main() {
7+
let guts = "mettle";
8+
let _ = Bravery {
9+
guts, //~ ERROR mismatched types
10+
brains: guts.clone(), //~ ERROR mismatched types
11+
};
12+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-52820.rs:9:9
3+
|
4+
LL | guts,
5+
| ^^^^
6+
| |
7+
| expected struct `std::string::String`, found &str
8+
| help: try using a conversion method: `guts: guts.to_string()`
9+
|
10+
= note: expected type `std::string::String`
11+
found type `&str`
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/issue-52820.rs:10:17
15+
|
16+
LL | brains: guts.clone(),
17+
| ^^^^^^^^^^^^
18+
| |
19+
| expected struct `std::string::String`, found &str
20+
| help: try using a conversion method: `guts.to_string()`
21+
|
22+
= note: expected type `std::string::String`
23+
found type `&str`
24+
25+
error: aborting due to 2 previous errors
26+
27+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)