Skip to content

Commit b83c009

Browse files
authored
Rollup merge of rust-lang#112643 - compiler-errors:sized-obl-for-arg, r=wesleywiser
Always register sized obligation for argument Removes a "hack" that skips registering sized obligations for parameters that are simple identifiers. This doesn't seem to affect diagnostics because we're probably already being smart enough about deduplicating identical error messages anyways. Fixes rust-lang#112608
2 parents 54d6738 + 0d6da78 commit b83c009

7 files changed

+57
-22
lines changed

Diff for: compiler/rustc_hir_typeck/src/check.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,7 @@ pub(super) fn check_fn<'a, 'tcx>(
9292
fcx.check_pat_top(&param.pat, param_ty, ty_span, None);
9393

9494
// Check that argument is Sized.
95-
// The check for a non-trivial pattern is a hack to avoid duplicate warnings
96-
// for simple cases like `fn foo(x: Trait)`,
97-
// where we would error once on the parameter as a whole, and once on the binding `x`.
98-
if param.pat.simple_ident().is_none() && !params_can_be_unsized {
95+
if !params_can_be_unsized {
9996
fcx.require_type_is_sized(
10097
param_ty,
10198
param.pat.span,

Diff for: tests/ui/closures/cannot-call-unsized-via-ptr-2.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(unsized_fn_params)]
2+
3+
fn main() {
4+
// CoerceMany "LUB"
5+
let f = if true { |_a| {} } else { |_b| {} };
6+
//~^ ERROR the size for values of type `[u8]` cannot be known at compilation time
7+
//~| ERROR the size for values of type `[u8]` cannot be known at compilation time
8+
9+
let slice: Box<[u8]> = Box::new([1; 8]);
10+
f(*slice);
11+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
2+
--> $DIR/cannot-call-unsized-via-ptr-2.rs:5:24
3+
|
4+
LL | let f = if true { |_a| {} } else { |_b| {} };
5+
| ^^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `Sized` is not implemented for `[u8]`
8+
= note: all function arguments must have a statically known size
9+
10+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
11+
--> $DIR/cannot-call-unsized-via-ptr-2.rs:5:41
12+
|
13+
LL | let f = if true { |_a| {} } else { |_b| {} };
14+
| ^^ doesn't have a size known at compile-time
15+
|
16+
= help: the trait `Sized` is not implemented for `[u8]`
17+
= note: all function arguments must have a statically known size
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0277`.

Diff for: tests/ui/closures/cannot-call-unsized-via-ptr.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(unsized_fn_params)]
2+
3+
fn main() {
4+
// Simple coercion
5+
let f: fn([u8]) = |_result| {};
6+
//~^ ERROR the size for values of type `[u8]` cannot be known at compilation time
7+
8+
let slice: Box<[u8]> = Box::new([1; 8]);
9+
f(*slice);
10+
}

Diff for: tests/ui/closures/cannot-call-unsized-via-ptr.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
2+
--> $DIR/cannot-call-unsized-via-ptr.rs:5:24
3+
|
4+
LL | let f: fn([u8]) = |_result| {};
5+
| ^^^^^^^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `Sized` is not implemented for `[u8]`
8+
= note: all function arguments must have a statically known size
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0277`.

Diff for: tests/ui/issues/issue-5883.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ struct Struct {
66

77
fn new_struct(
88
r: dyn A + 'static //~ ERROR the size for values of type
9-
) -> Struct { //~ ERROR the size for values of type
9+
) -> Struct {
1010
Struct { r: r }
1111
}
1212

Diff for: tests/ui/issues/issue-5883.stderr

+1-17
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,6 @@ help: function arguments must have a statically known size, borrowed types alway
1515
LL | r: &dyn A + 'static
1616
| +
1717

18-
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time
19-
--> $DIR/issue-5883.rs:9:6
20-
|
21-
LL | ) -> Struct {
22-
| ^^^^^^ doesn't have a size known at compile-time
23-
LL | Struct { r: r }
24-
| --------------- this returned value is of type `Struct`
25-
|
26-
= help: within `Struct`, the trait `Sized` is not implemented for `(dyn A + 'static)`
27-
note: required because it appears within the type `Struct`
28-
--> $DIR/issue-5883.rs:3:8
29-
|
30-
LL | struct Struct {
31-
| ^^^^^^
32-
= note: the return type of a function must have a statically known size
33-
34-
error: aborting due to 2 previous errors
18+
error: aborting due to previous error
3519

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

0 commit comments

Comments
 (0)