Skip to content

Commit 62a7839

Browse files
authored
Rollup merge of rust-lang#66539 - estebank:let-ty, r=Centril
Point at type in `let` assignment on type errors Fix rust-lang#61067.
2 parents 6618af2 + 34f03c0 commit 62a7839

File tree

101 files changed

+684
-354
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+684
-354
lines changed

src/librustc_typeck/check/demand.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
102102
// N.B., this code relies on `self.diverges` to be accurate. In
103103
// particular, assignments to `!` will be permitted if the
104104
// diverges flag is currently "always".
105-
pub fn demand_coerce_diag(&self,
106-
expr: &hir::Expr,
107-
checked_ty: Ty<'tcx>,
108-
expected: Ty<'tcx>,
109-
allow_two_phase: AllowTwoPhase)
110-
-> (Ty<'tcx>, Option<DiagnosticBuilder<'tcx>>) {
105+
pub fn demand_coerce_diag(
106+
&self,
107+
expr: &hir::Expr,
108+
checked_ty: Ty<'tcx>,
109+
expected: Ty<'tcx>,
110+
allow_two_phase: AllowTwoPhase,
111+
) -> (Ty<'tcx>, Option<DiagnosticBuilder<'tcx>>) {
111112
let expected = self.resolve_vars_with_obligations(expected);
112113

113114
let e = match self.try_coerce(expr, checked_ty, expected, allow_two_phase) {
@@ -126,6 +127,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
126127
return (expected, None)
127128
}
128129

130+
self.annotate_expected_due_to_let_ty(&mut err, expr);
129131
self.suggest_compatible_variants(&mut err, expr, expected, expr_ty);
130132
self.suggest_ref_or_into(&mut err, expr, expected, expr_ty);
131133
self.suggest_boxing_when_appropriate(&mut err, expr, expected, expr_ty);
@@ -134,6 +136,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
134136
(expected, Some(err))
135137
}
136138

139+
fn annotate_expected_due_to_let_ty(&self, err: &mut DiagnosticBuilder<'_>, expr: &hir::Expr) {
140+
let parent = self.tcx.hir().get_parent_node(expr.hir_id);
141+
if let Some(hir::Node::Local(hir::Local {
142+
ty: Some(ty),
143+
init: Some(init),
144+
..
145+
})) = self.tcx.hir().find(parent) {
146+
if init.hir_id == expr.hir_id {
147+
// Point at `let` assignment type.
148+
err.span_label(ty.span, "expected due to this");
149+
}
150+
}
151+
}
152+
137153
/// Returns whether the expected type is `bool` and the expression is `x = y`.
138154
pub fn is_assign_to_bool(&self, expr: &hir::Expr, expected: Ty<'tcx>) -> bool {
139155
if let hir::ExprKind::Assign(..) = expr.kind {

src/test/rustdoc-ui/failed-doctest-missing-codes.stdout

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ error[E0308]: mismatched types
99
--> $DIR/failed-doctest-missing-codes.rs:9:13
1010
|
1111
LL | let x: () = 5i32;
12-
| ^^^^ expected `()`, found `i32`
12+
| -- ^^^^ expected `()`, found `i32`
13+
| |
14+
| expected due to this
1315

1416
error: aborting due to previous error
1517

src/test/ui/array-not-vector.stderr

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ error[E0308]: mismatched types
22
--> $DIR/array-not-vector.rs:2:19
33
|
44
LL | let _x: i32 = [1, 2, 3];
5-
| ^^^^^^^^^ expected `i32`, found array `[{integer}; 3]`
5+
| --- ^^^^^^^^^ expected `i32`, found array `[{integer}; 3]`
6+
| |
7+
| expected due to this
68

79
error[E0308]: mismatched types
810
--> $DIR/array-not-vector.rs:7:20
911
|
1012
LL | let _y: &i32 = x;
11-
| ^ expected `i32`, found slice `[i32]`
13+
| ---- ^ expected `i32`, found slice `[i32]`
14+
| |
15+
| expected due to this
1216
|
1317
= note: expected reference `&i32`
1418
found reference `&[i32]`

src/test/ui/associated-types/associated-types-eq-3.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0308]: mismatched types
22
--> $DIR/associated-types-eq-3.rs:23:18
33
|
44
LL | let _: Bar = x.boo();
5-
| ^^^^^^^ expected struct `Bar`, found associated type
5+
| --- ^^^^^^^ expected struct `Bar`, found associated type
6+
| |
7+
| expected due to this
68
|
79
= note: expected struct `Bar`
810
found associated type `<I as Foo>::A`

src/test/ui/associated-types/associated-types-path-2.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ error[E0308]: mismatched types
4343
--> $DIR/associated-types-path-2.rs:41:18
4444
|
4545
LL | let _: i32 = f2(2i32);
46-
| ^^^^^^^^ expected `i32`, found `u32`
46+
| --- ^^^^^^^^ expected `i32`, found `u32`
47+
| |
48+
| expected due to this
4749
|
4850
help: you can convert an `u32` to `i32` and panic if the converted value wouldn't fit
4951
|

src/test/ui/c-variadic/variadic-ffi-1.stderr

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ error[E0308]: mismatched types
2626
--> $DIR/variadic-ffi-1.rs:19:56
2727
|
2828
LL | let x: unsafe extern "C" fn(f: isize, x: u8) = foo;
29-
| ^^^ expected non-variadic fn, found variadic function
29+
| ------------------------------------- ^^^ expected non-variadic fn, found variadic function
30+
| |
31+
| expected due to this
3032
|
3133
= note: expected fn pointer `unsafe extern "C" fn(isize, u8)`
3234
found fn item `unsafe extern "C" fn(isize, u8, ...) {foo}`
@@ -35,7 +37,9 @@ error[E0308]: mismatched types
3537
--> $DIR/variadic-ffi-1.rs:20:54
3638
|
3739
LL | let y: extern "C" fn(f: isize, x: u8, ...) = bar;
38-
| ^^^ expected variadic fn, found non-variadic function
40+
| ----------------------------------- ^^^ expected variadic fn, found non-variadic function
41+
| |
42+
| expected due to this
3943
|
4044
= note: expected fn pointer `extern "C" fn(isize, u8, ...)`
4145
found fn item `extern "C" fn(isize, u8) {bar}`

src/test/ui/closures/closure-no-fn-1.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0308]: mismatched types
22
--> $DIR/closure-no-fn-1.rs:6:29
33
|
44
LL | let foo: fn(u8) -> u8 = |v: u8| { a += v; a };
5-
| ^^^^^^^^^^^^^^^^^^^^^ expected fn pointer, found closure
5+
| ------------ ^^^^^^^^^^^^^^^^^^^^^ expected fn pointer, found closure
6+
| |
7+
| expected due to this
68
|
79
= note: expected fn pointer `fn(u8) -> u8`
810
found closure `[closure@$DIR/closure-no-fn-1.rs:6:29: 6:50 a:_]`

src/test/ui/closures/closure-no-fn-2.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0308]: mismatched types
22
--> $DIR/closure-no-fn-2.rs:6:27
33
|
44
LL | let bar: fn() -> u8 = || { b };
5-
| ^^^^^^^^ expected fn pointer, found closure
5+
| ---------- ^^^^^^^^ expected fn pointer, found closure
6+
| |
7+
| expected due to this
68
|
79
= note: expected fn pointer `fn() -> u8`
810
found closure `[closure@$DIR/closure-no-fn-2.rs:6:27: 6:35 b:_]`

src/test/ui/coercion/coerce-to-bang.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ error[E0308]: mismatched types
4747
--> $DIR/coerce-to-bang.rs:48:21
4848
|
4949
LL | let x: [!; 2] = [return, 22];
50-
| ^^^^^^^^^^^^ expected `!`, found integer
50+
| ------ ^^^^^^^^^^^^ expected `!`, found integer
51+
| |
52+
| expected due to this
5153
|
5254
= note: expected array `[!; 2]`
5355
found array `[{integer}; 2]`

src/test/ui/coercion/coercion-slice.stderr

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ error[E0308]: mismatched types
22
--> $DIR/coercion-slice.rs:4:21
33
|
44
LL | let _: &[i32] = [0];
5-
| ^^^
6-
| |
7-
| expected `&[i32]`, found array `[{integer}; 1]`
8-
| help: consider borrowing here: `&[0]`
5+
| ------ ^^^
6+
| | |
7+
| | expected `&[i32]`, found array `[{integer}; 1]`
8+
| | help: consider borrowing here: `&[0]`
9+
| expected due to this
910

1011
error: aborting due to previous error
1112

0 commit comments

Comments
 (0)