Skip to content

Commit 8b42623

Browse files
committed
Check arg/ret sizedness at ExprKind::Path.
1 parent 682b33a commit 8b42623

File tree

7 files changed

+48
-44
lines changed

7 files changed

+48
-44
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3956,6 +3956,31 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
39563956
tcx.types.err
39573957
};
39583958

3959+
if let ty::FnDef(..) = ty.sty {
3960+
let fn_sig = ty.fn_sig(tcx);
3961+
if !tcx.features().unsized_locals {
3962+
// We want to remove some Sized bounds from std functions,
3963+
// but don't want to expose the removal to stable Rust.
3964+
// i.e. we don't want to allow
3965+
//
3966+
// ```rust
3967+
// drop as fn(str);
3968+
// ```
3969+
//
3970+
// to work in stable even if the Sized bound on `drop` is relaxed.
3971+
for i in 0..fn_sig.inputs().skip_binder().len() {
3972+
let input = tcx.erase_late_bound_regions(&fn_sig.input(i));
3973+
self.require_type_is_sized_deferred(input, expr.span,
3974+
traits::SizedArgumentType);
3975+
}
3976+
}
3977+
// Here we want to prevent struct constructors from returning unsized types.
3978+
// There were two cases this happened: fn pointer coercion in stable
3979+
// and usual function call in presense of unsized_locals.
3980+
let output = tcx.erase_late_bound_regions(&fn_sig.output());
3981+
self.require_type_is_sized_deferred(output, expr.span, traits::SizedReturnType);
3982+
}
3983+
39593984
// We always require that the type provided as the value for
39603985
// a type parameter outlives the moment of instantiation.
39613986
let substs = self.tables.borrow().node_substs(expr.hir_id);

src/test/ui/issues/issue-30355.nll.stderr

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/test/ui/issues/issue-30355.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ pub struct X([u8]);
1313
pub static Y: &'static X = {
1414
const Y: &'static [u8] = b"";
1515
&X(*Y)
16-
//~^ ERROR cannot move out
17-
//~^^ ERROR cannot move a
18-
//~^^^ ERROR cannot move a
16+
//~^ ERROR E0277
1917
};
2018

2119
fn main() {}

src/test/ui/issues/issue-30355.stderr

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
error[E0161]: cannot move a value of type X: the size of X cannot be statically determined
1+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
22
--> $DIR/issue-30355.rs:15:6
33
|
44
LL | &X(*Y)
5-
| ^^^^^
6-
7-
error[E0161]: cannot move a value of type [u8]: the size of [u8] cannot be statically determined
8-
--> $DIR/issue-30355.rs:15:8
5+
| ^ doesn't have a size known at compile-time
96
|
10-
LL | &X(*Y)
11-
| ^^
12-
13-
error[E0507]: cannot move out of borrowed content
14-
--> $DIR/issue-30355.rs:15:8
15-
|
16-
LL | &X(*Y)
17-
| ^^ cannot move out of borrowed content
7+
= help: the trait `std::marker::Sized` is not implemented for `[u8]`
8+
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
9+
= note: all function arguments must have a statically known size
10+
= help: unsized locals are gated as an unstable feature
1811

19-
error: aborting due to 3 previous errors
12+
error: aborting due to previous error
2013

21-
Some errors occurred: E0161, E0507.
22-
For more information about an error, try `rustc --explain E0161`.
14+
For more information about this error, try `rustc --explain E0277`.

src/test/ui/unsized-locals/unsized-exprs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ fn main() {
2323
//~^ERROR E0277
2424
udrop::<A<[u8]>>(A { 0: *foo() });
2525
//~^ERROR E0277
26+
udrop::<A<[u8]>>(A(*foo()));
27+
//~^ERROR E0277
2628
}

src/test/ui/unsized-locals/unsized-exprs.stderr

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ LL | udrop::<A<[u8]>>(A { 0: *foo() });
2020
= note: required because it appears within the type `A<[u8]>`
2121
= note: structs must have a statically known size to be initialized
2222

23-
error: aborting due to 2 previous errors
23+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
24+
--> $DIR/unsized-exprs.rs:26:22
25+
|
26+
LL | udrop::<A<[u8]>>(A(*foo()));
27+
| ^ doesn't have a size known at compile-time
28+
|
29+
= help: within `A<[u8]>`, the trait `std::marker::Sized` is not implemented for `[u8]`
30+
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
31+
= note: required because it appears within the type `A<[u8]>`
32+
= note: the return type of a function must have a statically known size
33+
34+
error: aborting due to 3 previous errors
2435

2536
For more information about this error, try `rustc --explain E0277`.

src/test/ui/unsized-locals/unsized-exprs2.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,4 @@ impl std::ops::Add<i32> for A<[u8]> {
2121
fn main() {
2222
udrop::<[u8]>(foo()[..]);
2323
//~^ERROR cannot move out of indexed content
24-
// FIXME: should be error
25-
udrop::<A<[u8]>>(A(*foo()));
2624
}

0 commit comments

Comments
 (0)