Skip to content

Commit c62e18f

Browse files
committed
Error on moving unsized values rather than ICE'ing
1 parent 13180c2 commit c62e18f

File tree

7 files changed

+147
-8
lines changed

7 files changed

+147
-8
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
242242
arg_expr.span,
243243
ObligationCauseCode::WellFormed(None),
244244
);
245+
246+
// Unsized function arguments must be place expressions, because we can't move them.
247+
self.check_place_expr_if_unsized(fn_input_ty, arg_expr);
245248
}
246249

247250
// First, let's unify the formal method signature with the expectation eagerly.
@@ -544,6 +547,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
544547
}
545548
}
546549

550+
/// If `unsized_fn_params` is active, check that unsized values are place
551+
/// expressions. This is needed because we can't move unsized values.
552+
///
553+
/// If `unsized_fn_params` is inactive, this will be checked in borrowck instead.
554+
fn check_place_expr_if_unsized(&self, ty: Ty<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
555+
if self.tcx.features().unsized_fn_params() && !expr.is_syntactic_place_expr() {
556+
self.require_type_is_sized(
557+
ty,
558+
expr.span,
559+
ObligationCauseCode::UnsizedNonPlaceExpr(expr.span),
560+
);
561+
}
562+
}
563+
547564
fn report_arg_errors(
548565
&self,
549566
compatibility_diagonal: IndexVec<ProvidedIdx, Compatibility<'tcx>>,
@@ -1870,7 +1887,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18701887
});
18711888
}
18721889
hir::StmtKind::Semi(expr) => {
1873-
self.check_expr(expr);
1890+
let ty = self.check_expr(expr);
1891+
self.check_place_expr_if_unsized(ty, expr);
18741892
}
18751893
}
18761894

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,10 @@ pub enum ObligationCauseCode<'tcx> {
406406

407407
/// Obligations emitted during the normalization of a free type alias.
408408
TypeAlias(ObligationCauseCodeHandle<'tcx>, Span, DefId),
409+
410+
/// Only reachable if the `unsized_fn_params` feature is used. Unsized function arguments must
411+
/// be place expressions, because we can't move them.
412+
UnsizedNonPlaceExpr(Span),
409413
}
410414

411415
/// Whether a value can be extracted into a const.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3607,6 +3607,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
36073607
);
36083608
suggest_remove_deref(err, &expr);
36093609
}
3610+
ObligationCauseCode::UnsizedNonPlaceExpr(span) => {
3611+
err.span_note(
3612+
span,
3613+
"unsized values must be place expressions and cannot be moved from",
3614+
);
3615+
}
36103616
}
36113617
}
36123618

tests/ui/unsized-locals/unsized-exprs-rpass.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ impl std::ops::Add<i32> for A<[u8]> {
1818
}
1919

2020
fn main() {
21-
udrop::<[u8]>(loop {
22-
break *foo();
23-
});
24-
udrop::<[u8]>(if true { *foo() } else { *foo() });
25-
udrop::<[u8]>({ *foo() });
2621
udrop::<[u8]>((*foo()));
2722
*afoo() + 42;
2823
udrop as fn([u8]);

tests/ui/unsized-locals/unsized-exprs.stderr

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ note: required because it appears within the type `A<[u8]>`
1010
|
1111
LL | struct A<X: ?Sized>(X);
1212
| ^
13-
= note: structs must have a statically known size to be initialized
13+
note: unsized values must be place expressions and cannot be moved from
14+
--> $DIR/unsized-exprs.rs:19:22
15+
|
16+
LL | udrop::<A<[u8]>>(A { 0: *foo() });
17+
| ^^^^^^^^^^^^^^^
1418

1519
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
1620
--> $DIR/unsized-exprs.rs:21:22
@@ -24,7 +28,11 @@ note: required because it appears within the type `A<[u8]>`
2428
|
2529
LL | struct A<X: ?Sized>(X);
2630
| ^
27-
= note: the return type of a function must have a statically known size
31+
note: unsized values must be place expressions and cannot be moved from
32+
--> $DIR/unsized-exprs.rs:21:22
33+
|
34+
LL | udrop::<A<[u8]>>(A(*foo()));
35+
| ^^^^^^^^^
2836

2937
error: aborting due to 2 previous errors
3038

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//! `#![feature(unsized_fn_params)]` lets you use unsized function parameters. In particular this
2+
//! is load bearing for `Box<dyn FnOnce()>: FnOnce()`. To do that, borrowck relaxes the requirement
3+
//! that certain places must be `Sized`. But in #142911 we removed alloca support, so these
4+
//! arguments can't move (or ICE at codegen) That means when `unsized_fn_params` is enabled, we must
5+
//! explicitly check that unsized function arguments are place expressions.
6+
//!
7+
//! Also see tests/ui/unsized_locals/unsized-exprs-rpass.rs
8+
9+
#![feature(unsized_fn_params)]
10+
11+
fn foo() -> Box<[u8]> {
12+
Box::new(*b"foo")
13+
}
14+
15+
fn udrop<T: ?Sized>(_x: T) {}
16+
17+
fn main(){
18+
// NB The ordering of the following operations matters, otherwise errors get swallowed somehow.
19+
20+
udrop::<[u8]>(if true { *foo() } else { *foo() }); //~ERROR the size for values of type `[u8]` cannot be known at compilation time
21+
udrop::<[u8]>({ *foo() }); //~ERROR the size for values of type `[u8]` cannot be known at compilation time
22+
udrop(match foo() { x => *x }); //~ERROR the size for values of type `[u8]` cannot be known at compilation time
23+
udrop::<[u8]>({ loop { break *foo(); } }); //~ERROR the size for values of type `[u8]` cannot be known at compilation time
24+
25+
{ *foo() }; //~ERROR the size for values of type `[u8]` cannot be known at compilation time
26+
{ loop { break *foo(); } }; //~ERROR the size for values of type `[u8]` cannot be known at compilation time
27+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
2+
--> $DIR/unsized-non-place-exprs.rs:20:19
3+
|
4+
LL | udrop::<[u8]>(if true { *foo() } else { *foo() });
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `Sized` is not implemented for `[u8]`
8+
note: unsized values must be place expressions and cannot be moved from
9+
--> $DIR/unsized-non-place-exprs.rs:20:19
10+
|
11+
LL | udrop::<[u8]>(if true { *foo() } else { *foo() });
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
14+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
15+
--> $DIR/unsized-non-place-exprs.rs:21:19
16+
|
17+
LL | udrop::<[u8]>({ *foo() });
18+
| ^^^^^^^^^^ doesn't have a size known at compile-time
19+
|
20+
= help: the trait `Sized` is not implemented for `[u8]`
21+
note: unsized values must be place expressions and cannot be moved from
22+
--> $DIR/unsized-non-place-exprs.rs:21:19
23+
|
24+
LL | udrop::<[u8]>({ *foo() });
25+
| ^^^^^^^^^^
26+
27+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
28+
--> $DIR/unsized-non-place-exprs.rs:22:11
29+
|
30+
LL | udrop(match foo() { x => *x });
31+
| ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
32+
|
33+
= help: the trait `Sized` is not implemented for `[u8]`
34+
note: unsized values must be place expressions and cannot be moved from
35+
--> $DIR/unsized-non-place-exprs.rs:22:11
36+
|
37+
LL | udrop(match foo() { x => *x });
38+
| ^^^^^^^^^^^^^^^^^^^^^^^
39+
40+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
41+
--> $DIR/unsized-non-place-exprs.rs:23:19
42+
|
43+
LL | udrop::<[u8]>({ loop { break *foo(); } });
44+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
45+
|
46+
= help: the trait `Sized` is not implemented for `[u8]`
47+
note: unsized values must be place expressions and cannot be moved from
48+
--> $DIR/unsized-non-place-exprs.rs:23:19
49+
|
50+
LL | udrop::<[u8]>({ loop { break *foo(); } });
51+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
52+
53+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
54+
--> $DIR/unsized-non-place-exprs.rs:25:5
55+
|
56+
LL | { *foo() };
57+
| ^^^^^^^^^^ doesn't have a size known at compile-time
58+
|
59+
= help: the trait `Sized` is not implemented for `[u8]`
60+
note: unsized values must be place expressions and cannot be moved from
61+
--> $DIR/unsized-non-place-exprs.rs:25:5
62+
|
63+
LL | { *foo() };
64+
| ^^^^^^^^^^
65+
66+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
67+
--> $DIR/unsized-non-place-exprs.rs:26:5
68+
|
69+
LL | { loop { break *foo(); } };
70+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
71+
|
72+
= help: the trait `Sized` is not implemented for `[u8]`
73+
note: unsized values must be place expressions and cannot be moved from
74+
--> $DIR/unsized-non-place-exprs.rs:26:5
75+
|
76+
LL | { loop { break *foo(); } };
77+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
78+
79+
error: aborting due to 6 previous errors
80+
81+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)