Skip to content

Commit aceb5d9

Browse files
authored
Rollup merge of #111056 - JohnBobbo96:fix_box_suggestions, r=compiler-errors
Fix some suggestions where a `Box<T>` is expected. This fixes #111011, and also adds a suggestion for boxing a unit type when a `Box<T>` was expected and an empty block was found.
2 parents c75543d + 3598509 commit aceb5d9

File tree

10 files changed

+174
-31
lines changed

10 files changed

+174
-31
lines changed

compiler/rustc_hir_typeck/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,7 @@ hir_typeck_union_pat_dotdot = `..` cannot be used in union patterns
7575
7676
hir_typeck_arg_mismatch_indeterminate = argument type mismatch was detected, but rustc had trouble determining where
7777
.note = we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new
78+
79+
hir_typeck_suggest_boxing_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
80+
81+
hir_typeck_suggest_boxing_when_appropriate = store this in the heap by calling `Box::new`

compiler/rustc_hir_typeck/src/demand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5151
|| self.suggest_non_zero_new_unwrap(err, expr, expected, expr_ty)
5252
|| self.suggest_calling_boxed_future_when_appropriate(err, expr, expected, expr_ty)
5353
|| self.suggest_no_capture_closure(err, expected, expr_ty)
54-
|| self.suggest_boxing_when_appropriate(err, expr, expected, expr_ty)
54+
|| self.suggest_boxing_when_appropriate(err, expr.span, expr.hir_id, expected, expr_ty)
5555
|| self.suggest_block_to_brackets_peeling_refs(err, expr, expr_ty, expected)
5656
|| self.suggest_copied_or_cloned(err, expr, expr_ty, expected)
5757
|| self.suggest_clone_for_ref(err, expr, expr_ty, expected)

compiler/rustc_hir_typeck/src/errors.rs

+28
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,31 @@ pub struct ArgMismatchIndeterminate {
267267
#[primary_span]
268268
pub span: Span,
269269
}
270+
271+
#[derive(Subdiagnostic)]
272+
pub enum SuggestBoxing {
273+
#[note(hir_typeck_suggest_boxing_note)]
274+
#[multipart_suggestion(
275+
hir_typeck_suggest_boxing_when_appropriate,
276+
applicability = "machine-applicable"
277+
)]
278+
Unit {
279+
#[suggestion_part(code = "Box::new(())")]
280+
start: Span,
281+
#[suggestion_part(code = "")]
282+
end: Span,
283+
},
284+
#[note(hir_typeck_suggest_boxing_note)]
285+
AsyncBody,
286+
#[note(hir_typeck_suggest_boxing_note)]
287+
#[multipart_suggestion(
288+
hir_typeck_suggest_boxing_when_appropriate,
289+
applicability = "machine-applicable"
290+
)]
291+
Other {
292+
#[suggestion_part(code = "Box::new(")]
293+
start: Span,
294+
#[suggestion_part(code = ")")]
295+
end: Span,
296+
},
297+
}

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15191519
// case we can ignore the tail expression (e.g., `'a: {
15201520
// break 'a 22; }` would not force the type of the block
15211521
// to be `()`).
1522-
let tail_expr = blk.expr.as_ref();
15231522
let coerce_to_ty = expected.coercion_target_type(self, blk.span);
15241523
let coerce = if blk.targeted_by_break {
15251524
CoerceMany::new(coerce_to_ty)
@@ -1537,13 +1536,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15371536

15381537
// check the tail expression **without** holding the
15391538
// `enclosing_breakables` lock below.
1540-
let tail_expr_ty = tail_expr.map(|t| self.check_expr_with_expectation(t, expected));
1539+
let tail_expr_ty =
1540+
blk.expr.map(|expr| (expr, self.check_expr_with_expectation(expr, expected)));
15411541

15421542
let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
15431543
let ctxt = enclosing_breakables.find_breakable(blk.hir_id);
15441544
let coerce = ctxt.coerce.as_mut().unwrap();
1545-
if let Some(tail_expr_ty) = tail_expr_ty {
1546-
let tail_expr = tail_expr.unwrap();
1545+
if let Some((tail_expr, tail_expr_ty)) = tail_expr_ty {
15471546
let span = self.get_expr_coercion_span(tail_expr);
15481547
let cause = self.cause(span, ObligationCauseCode::BlockTailExpression(blk.hir_id));
15491548
let ty_for_diagnostic = coerce.merged_ty();
@@ -1596,6 +1595,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15961595
&self.misc(sp),
15971596
&mut |err| {
15981597
if let Some(expected_ty) = expected.only_has_type(self) {
1598+
if blk.stmts.is_empty() && blk.expr.is_none() {
1599+
self.suggest_boxing_when_appropriate(
1600+
err,
1601+
blk.span,
1602+
blk.hir_id,
1603+
expected_ty,
1604+
self.tcx.mk_unit(),
1605+
);
1606+
}
15991607
if !self.consider_removing_semicolon(blk, expected_ty, err) {
16001608
self.err_ctxt().consider_returning_binding(
16011609
blk,
@@ -1608,7 +1616,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16081616
// silence this redundant error, as we already emit E0070.
16091617

16101618
// Our block must be a `assign desugar local; assignment`
1611-
if let Some(hir::Node::Block(hir::Block {
1619+
if let hir::Block {
16121620
stmts:
16131621
[
16141622
hir::Stmt {
@@ -1630,7 +1638,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16301638
},
16311639
],
16321640
..
1633-
})) = self.tcx.hir().find(blk.hir_id)
1641+
} = blk
16341642
{
16351643
self.comes_from_while_condition(blk.hir_id, |_| {
16361644
err.downgrade_to_delayed_bug();

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::FnCtxt;
22

3-
use crate::errors::{AddReturnTypeSuggestion, ExpectedReturnTypeLabel};
3+
use crate::errors::{AddReturnTypeSuggestion, ExpectedReturnTypeLabel, SuggestBoxing};
44
use crate::fluent_generated as fluent;
55
use crate::method::probe::{IsSuggestion, Mode, ProbeScope};
66
use rustc_ast::util::parser::{ExprPrecedence, PREC_POSTFIX};
@@ -9,7 +9,8 @@ use rustc_hir as hir;
99
use rustc_hir::def::{CtorKind, CtorOf, DefKind};
1010
use rustc_hir::lang_items::LangItem;
1111
use rustc_hir::{
12-
Expr, ExprKind, GenericBound, Node, Path, QPath, Stmt, StmtKind, TyKind, WherePredicate,
12+
AsyncGeneratorKind, Expr, ExprKind, GeneratorKind, GenericBound, HirId, Node, Path, QPath,
13+
Stmt, StmtKind, TyKind, WherePredicate,
1314
};
1415
use rustc_hir_analysis::astconv::AstConv;
1516
use rustc_infer::traits::{self, StatementAsExpression};
@@ -438,33 +439,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
438439
pub(in super::super) fn suggest_boxing_when_appropriate(
439440
&self,
440441
err: &mut Diagnostic,
441-
expr: &hir::Expr<'_>,
442+
span: Span,
443+
hir_id: HirId,
442444
expected: Ty<'tcx>,
443445
found: Ty<'tcx>,
444446
) -> bool {
445-
if self.tcx.hir().is_inside_const_context(expr.hir_id) {
446-
// Do not suggest `Box::new` in const context.
447-
return false;
448-
}
449-
if !expected.is_box() || found.is_box() {
447+
// Do not suggest `Box::new` in const context.
448+
if self.tcx.hir().is_inside_const_context(hir_id) || !expected.is_box() || found.is_box() {
450449
return false;
451450
}
452-
let boxed_found = self.tcx.mk_box(found);
453-
if self.can_coerce(boxed_found, expected) {
454-
err.multipart_suggestion(
455-
"store this in the heap by calling `Box::new`",
456-
vec![
457-
(expr.span.shrink_to_lo(), "Box::new(".to_string()),
458-
(expr.span.shrink_to_hi(), ")".to_string()),
459-
],
460-
Applicability::MachineApplicable,
461-
);
462-
err.note(
463-
"for more on the distinction between the stack and the heap, read \
464-
https://doc.rust-lang.org/book/ch15-01-box.html, \
465-
https://doc.rust-lang.org/rust-by-example/std/box.html, and \
466-
https://doc.rust-lang.org/std/boxed/index.html",
467-
);
451+
if self.can_coerce(self.tcx.mk_box(found), expected) {
452+
let suggest_boxing = match found.kind() {
453+
ty::Tuple(tuple) if tuple.is_empty() => {
454+
SuggestBoxing::Unit { start: span.shrink_to_lo(), end: span }
455+
}
456+
ty::Generator(def_id, ..)
457+
if matches!(
458+
self.tcx.generator_kind(def_id),
459+
Some(GeneratorKind::Async(AsyncGeneratorKind::Closure))
460+
) =>
461+
{
462+
SuggestBoxing::AsyncBody
463+
}
464+
_ => SuggestBoxing::Other { start: span.shrink_to_lo(), end: span.shrink_to_hi() },
465+
};
466+
err.subdiagnostic(suggest_boxing);
467+
468468
true
469469
} else {
470470
false
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(async_closure)]
2+
3+
// edition:2021
4+
5+
fn foo<X>(x: impl FnOnce() -> Box<X>) {}
6+
// just to make sure async closures can still be suggested for boxing.
7+
fn bar<X>(x: Box<dyn FnOnce() -> X>) {}
8+
9+
fn main() {
10+
foo(async move || {}); //~ ERROR mismatched types
11+
bar(async move || {}); //~ ERROR mismatched types
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-111011.rs:10:23
3+
|
4+
LL | foo(async move || {});
5+
| ^^ expected `Box<_>`, found `async` closure body
6+
|
7+
= note: expected struct `Box<_>`
8+
found `async` closure body `[async closure body@$DIR/issue-111011.rs:10:23: 10:25]`
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+
11+
error[E0308]: mismatched types
12+
--> $DIR/issue-111011.rs:11:9
13+
|
14+
LL | bar(async move || {});
15+
| --- ^^^^^^^^^^^^^^^^ expected `Box<dyn FnOnce() -> _>`, found closure
16+
| |
17+
| arguments to this function are incorrect
18+
|
19+
= note: expected struct `Box<(dyn FnOnce() -> _ + 'static)>`
20+
found closure `[closure@$DIR/issue-111011.rs:11:9: 11:22]`
21+
= 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
22+
note: function defined here
23+
--> $DIR/issue-111011.rs:7:4
24+
|
25+
LL | fn bar<X>(x: Box<dyn FnOnce() -> X>) {}
26+
| ^^^ -------------------------
27+
help: store this in the heap by calling `Box::new`
28+
|
29+
LL | bar(Box::new(async move || {}));
30+
| +++++++++ +
31+
32+
error: aborting due to 2 previous errors
33+
34+
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(async_closure)]
2+
3+
// edition:2021
4+
// run-rustfix
5+
6+
fn foo<T>(_: Box<T>) {}
7+
fn bar<T>(_: impl Fn() -> Box<T>) {}
8+
9+
fn main() {
10+
foo(Box::new(())); //~ ERROR mismatched types
11+
bar(|| Box::new(())); //~ ERROR mismatched types
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(async_closure)]
2+
3+
// edition:2021
4+
// run-rustfix
5+
6+
fn foo<T>(_: Box<T>) {}
7+
fn bar<T>(_: impl Fn() -> Box<T>) {}
8+
9+
fn main() {
10+
foo({}); //~ ERROR mismatched types
11+
bar(|| {}); //~ ERROR mismatched types
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/suggest-boxed-empty-block.rs:10:9
3+
|
4+
LL | foo({});
5+
| ^^ expected `Box<_>`, found `()`
6+
|
7+
= note: expected struct `Box<_>`
8+
found unit type `()`
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 - foo({});
13+
LL + foo(Box::new(()));
14+
|
15+
16+
error[E0308]: mismatched types
17+
--> $DIR/suggest-boxed-empty-block.rs:11:12
18+
|
19+
LL | bar(|| {});
20+
| ^^ expected `Box<_>`, found `()`
21+
|
22+
= note: expected struct `Box<_>`
23+
found unit type `()`
24+
= 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
25+
help: store this in the heap by calling `Box::new`
26+
|
27+
LL - bar(|| {});
28+
LL + bar(|| Box::new(()));
29+
|
30+
31+
error: aborting due to 2 previous errors
32+
33+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)