Skip to content

Commit 40d36b1

Browse files
committed
Defer ?Sized local binding error to get better suggestions
1 parent 669a126 commit 40d36b1

22 files changed

+229
-178
lines changed

compiler/rustc_typeck/src/check/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
813813
let lhs_ty = self.check_expr_with_needs(&lhs, Needs::MutPlace);
814814
let rhs_ty = self.check_expr_coercable_to_type(&rhs, lhs_ty, Some(lhs));
815815

816-
self.require_type_is_sized(lhs_ty, lhs.span, traits::AssignmentLhsSized);
816+
self.require_type_is_sized_deferred(lhs_ty, lhs.span, traits::AssignmentLhsSized);
817817

818818
if lhs_ty.references_error() || rhs_ty.references_error() {
819819
self.tcx.ty_error()

compiler/rustc_typeck/src/check/gather_locals.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
9494
let var_ty = self.assign(p.span, p.hir_id, None);
9595

9696
if !self.fcx.tcx.features().unsized_locals {
97-
self.fcx.require_type_is_sized(var_ty, p.span, traits::VariableType(p.hir_id));
97+
self.fcx.require_type_is_sized_deferred(
98+
var_ty,
99+
p.span,
100+
traits::VariableType(p.hir_id),
101+
);
98102
}
99103

100104
debug!(

src/test/ui/error-codes/E0282.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
fn main() {
2-
let x = "hello".chars().rev().collect(); //~ ERROR E0282
2+
None; //~ ERROR E0282
33
}

src/test/ui/error-codes/E0282.stderr

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
error[E0282]: type annotations needed
2-
--> $DIR/E0282.rs:2:9
2+
--> $DIR/E0282.rs:2:5
33
|
4-
LL | let x = "hello".chars().rev().collect();
5-
| ^ cannot infer type
6-
|
7-
help: consider giving this binding a type
8-
|
9-
LL | let x: Type = "hello".chars().rev().collect();
10-
| ^^^^^^
4+
LL | None;
5+
| ^^^^ cannot infer type for type parameter `T` declared on the enum `Option`
116

127
error: aborting due to previous error
138

src/test/ui/for/for-loop-unconstrained-element-type.stderr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0282]: type annotations needed
22
--> $DIR/for-loop-unconstrained-element-type.rs:8:14
33
|
44
LL | for i in Vec::new() { }
5-
| ^^^^^^^^^^ the element type for this iterator is not specified
5+
| ^^^^^^^^--
6+
| |
7+
| the element type for this iterator is not specified
8+
| cannot infer type for type parameter `T`
69

710
error: aborting due to previous error
811

src/test/ui/issues/issue-12187-1.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error[E0282]: type annotations needed for `&T`
2-
--> $DIR/issue-12187-1.rs:6:10
2+
--> $DIR/issue-12187-1.rs:6:14
33
|
44
LL | let &v = new();
5-
| ^ cannot infer type
5+
| ^^^ cannot infer type for type parameter `T` declared on the function `new`
66
|
7-
help: consider giving this binding the explicit type `&T`, with the type parameters specified
7+
help: consider giving this binding the explicit type `&T`, where the type parameter `T` is specified
88
|
99
LL | let &v: &T = new();
1010
| ^^^^

src/test/ui/issues/issue-12187-2.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error[E0282]: type annotations needed for `&T`
2-
--> $DIR/issue-12187-2.rs:6:10
2+
--> $DIR/issue-12187-2.rs:6:14
33
|
44
LL | let &v = new();
5-
| ^ cannot infer type
5+
| ^^^ cannot infer type for type parameter `T` declared on the function `new`
66
|
7-
help: consider giving this binding the explicit type `&T`, with the type parameters specified
7+
help: consider giving this binding the explicit type `&T`, where the type parameter `T` is specified
88
|
99
LL | let &v: &T = new();
1010
| ^^^^

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ fn closure_to_loc() {
66

77
fn closure_from_match() {
88
let x = match 1usize {
9-
1 => |c| c + 1,
9+
1 => |c| c + 1, //~ ERROR type annotations needed
1010
2 => |c| c - 1,
1111
_ => |c| c - 1
1212
};
13-
//~^^^^ ERROR type annotations needed
1413
}
1514

1615
fn main() { }

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@ LL | x = |c| c + 1;
1111
= note: no two closures, even if identical, have the same type
1212
= help: consider boxing your closure and/or using it as a trait object
1313

14-
error[E0282]: type annotations needed
15-
--> $DIR/issue-24036.rs:9:15
14+
error[E0282]: type annotations needed for `fn(_) -> _`
15+
--> $DIR/issue-24036.rs:9:18
1616
|
1717
LL | 1 => |c| c + 1,
18-
| ^ consider giving this closure parameter a type
18+
| ^ cannot infer type
19+
|
20+
help: consider giving this binding the explicit type `fn(_) -> _`, with the type parameters specified
21+
|
22+
LL | let x: fn(_) -> _ = match 1usize {
23+
| ^^^^^^^^^^^^
1924

2025
error: aborting due to 2 previous errors
2126

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
1-
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time
2-
--> $DIR/issue-5883.rs:7:15
3-
|
4-
LL | fn new_struct(r: dyn A + 'static)
5-
| ^ doesn't have a size known at compile-time
6-
|
7-
= help: the trait `Sized` is not implemented for `(dyn A + 'static)`
8-
= help: unsized locals are gated as an unstable feature
9-
help: function arguments must have a statically known size, borrowed types always have a known size
10-
|
11-
LL | fn new_struct(r: &dyn A + 'static)
12-
| ^
13-
141
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time
152
--> $DIR/issue-5883.rs:8:8
163
|
@@ -24,6 +11,19 @@ LL | Struct { r: r }
2411
= note: required because it appears within the type `Struct`
2512
= note: the return type of a function must have a statically known size
2613

14+
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time
15+
--> $DIR/issue-5883.rs:7:15
16+
|
17+
LL | fn new_struct(r: dyn A + 'static)
18+
| ^ doesn't have a size known at compile-time
19+
|
20+
= help: the trait `Sized` is not implemented for `(dyn A + 'static)`
21+
= help: unsized locals are gated as an unstable feature
22+
help: function arguments must have a statically known size, borrowed types always have a known size
23+
|
24+
LL | fn new_struct(r: &dyn A + 'static)
25+
| ^
26+
2727
error: aborting due to 2 previous errors
2828

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

0 commit comments

Comments
 (0)