Skip to content

Commit e39ab25

Browse files
authored
Rollup merge of #140989 - xizheyin:issue-139631, r=compiler-errors
Suggest replace f with f: Box<f> when expr field is short hand Fixes #139631 r? compiler
2 parents 8395461 + 32be459 commit e39ab25

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

compiler/rustc_hir_typeck/src/errors.rs

+12
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,18 @@ pub(crate) enum SuggestBoxing {
680680
hir_typeck_suggest_boxing_when_appropriate,
681681
applicability = "machine-applicable"
682682
)]
683+
ExprFieldShorthand {
684+
#[suggestion_part(code = "{ident}: Box::new(")]
685+
start: Span,
686+
#[suggestion_part(code = ")")]
687+
end: Span,
688+
ident: Ident,
689+
},
690+
#[note(hir_typeck_suggest_boxing_note)]
691+
#[multipart_suggestion(
692+
hir_typeck_suggest_boxing_when_appropriate,
693+
applicability = "machine-applicable"
694+
)]
683695
Other {
684696
#[suggestion_part(code = "Box::new(")]
685697
start: Span,

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+9
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
585585
{
586586
errors::SuggestBoxing::AsyncBody
587587
}
588+
_ if let Node::ExprField(expr_field) = self.tcx.parent_hir_node(hir_id)
589+
&& expr_field.is_shorthand =>
590+
{
591+
errors::SuggestBoxing::ExprFieldShorthand {
592+
start: span.shrink_to_lo(),
593+
end: span.shrink_to_hi(),
594+
ident: expr_field.ident,
595+
}
596+
}
588597
_ => errors::SuggestBoxing::Other {
589598
start: span.shrink_to_lo(),
590599
end: span.shrink_to_hi(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
struct X {
2+
a: Box<u32>,
3+
}
4+
5+
struct Y {
6+
y: Box<u32>,
7+
}
8+
9+
fn main() {
10+
let a = 8;
11+
let v2 = X { a }; //~ ERROR mismatched types [E0308]
12+
let v3 = Y { y: a }; //~ ERROR mismatched types [E0308]
13+
let v4 = Y { a }; //~ ERROR struct `Y` has no field named `a` [E0560]
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/suggest-box-for-expr-field-issue-139631.rs:11:18
3+
|
4+
LL | let v2 = X { a };
5+
| ^ expected `Box<u32>`, found integer
6+
|
7+
= note: expected struct `Box<u32>`
8+
found type `{integer}`
9+
= note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html
10+
help: store this in the heap by calling `Box::new`
11+
|
12+
LL | let v2 = X { a: Box::new(a) };
13+
| ++++++++++++ +
14+
15+
error[E0308]: mismatched types
16+
--> $DIR/suggest-box-for-expr-field-issue-139631.rs:12:21
17+
|
18+
LL | let v3 = Y { y: a };
19+
| ^ expected `Box<u32>`, found integer
20+
|
21+
= note: expected struct `Box<u32>`
22+
found type `{integer}`
23+
= note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html
24+
help: store this in the heap by calling `Box::new`
25+
|
26+
LL | let v3 = Y { y: Box::new(a) };
27+
| +++++++++ +
28+
29+
error[E0560]: struct `Y` has no field named `a`
30+
--> $DIR/suggest-box-for-expr-field-issue-139631.rs:13:18
31+
|
32+
LL | let v4 = Y { a };
33+
| ^ unknown field
34+
|
35+
help: a field with a similar name exists
36+
|
37+
LL - let v4 = Y { a };
38+
LL + let v4 = Y { y };
39+
|
40+
41+
error: aborting due to 3 previous errors
42+
43+
Some errors have detailed explanations: E0308, E0560.
44+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)