Skip to content

Commit b8fff95

Browse files
committed
Require a box expression's type to be Sized
1 parent ba83b39 commit b8fff95

File tree

4 files changed

+7
-3
lines changed
  • compiler

4 files changed

+7
-3
lines changed

compiler/rustc_middle/src/traits/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ pub enum ObligationCauseCode<'tcx> {
225225
SizedReturnType,
226226
/// Yield type must be `Sized`.
227227
SizedYieldType,
228+
/// Box expression result type must be `Sized`.
229+
SizedBoxType,
228230
/// Inline asm operand type must be `Sized`.
229231
InlineAsmSized,
230232
/// `[T, ..n]` implies that `T` must be `Copy`.

compiler/rustc_mir/src/borrow_check/type_check/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1893,9 +1893,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18931893

18941894
// While this is located in `nll::typeck` this error is not
18951895
// an NLL error, it's a required check to prevent creation
1896-
// of unsized rvalues in certain cases:
1897-
// * operand of a box expression
1898-
// * callee in a call expression
1896+
// of unsized rvalues in a call expression.
18991897
diag.emit();
19001898
}
19011899
}

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2072,6 +2072,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
20722072
ObligationCauseCode::SizedYieldType => {
20732073
err.note("the yield type of a generator must have a statically known size");
20742074
}
2075+
ObligationCauseCode::SizedBoxType => {
2076+
err.note("the type of a box expression must have a statically known size");
2077+
}
20752078
ObligationCauseCode::AssignmentLhsSized => {
20762079
err.note("the left-hand-side of an assignment must have a statically known size");
20772080
}

compiler/rustc_typeck/src/check/expr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
313313
_ => NoExpectation,
314314
});
315315
let referent_ty = self.check_expr_with_expectation(expr, expected_inner);
316+
self.require_type_is_sized(referent_ty, expr.span, traits::SizedBoxType);
316317
self.tcx.mk_box(referent_ty)
317318
}
318319

0 commit comments

Comments
 (0)