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

+22-6
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

+3-1
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

+6-2
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

+3-1
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

+3-1
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

+6-2
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

+3-1
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

+3-1
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

+3-1
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

+5-4
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

src/test/ui/const-generics/fn-const-param-infer.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ error[E0308]: mismatched types
1010
--> $DIR/fn-const-param-infer.rs:16:31
1111
|
1212
LL | let _: Checked<not_one> = Checked::<not_two>;
13-
| ^^^^^^^^^^^^^^^^^^ expected `not_one`, found `not_two`
13+
| ---------------- ^^^^^^^^^^^^^^^^^^ expected `not_one`, found `not_two`
14+
| |
15+
| expected due to this
1416
|
1517
= note: expected struct `Checked<not_one>`
1618
found struct `Checked<not_two>`
@@ -34,7 +36,9 @@ error[E0308]: mismatched types
3436
--> $DIR/fn-const-param-infer.rs:25:40
3537
|
3638
LL | let _: Checked<{generic::<u32>}> = Checked::<{generic::<u16>}>;
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `generic::<u32>`, found `generic::<u16>`
39+
| ------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `generic::<u32>`, found `generic::<u16>`
40+
| |
41+
| expected due to this
3842
|
3943
= note: expected struct `Checked<generic::<u32>>`
4044
found struct `Checked<generic::<u16>>`

src/test/ui/const-generics/raw-ptr-const-param.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ error[E0308]: mismatched types
1010
--> $DIR/raw-ptr-const-param.rs:7:38
1111
|
1212
LL | let _: Const<{15 as *const _}> = Const::<{10 as *const _}>;
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{pointer}`, found `{pointer}`
13+
| ----------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{pointer}`, found `{pointer}`
14+
| |
15+
| expected due to this
1416
|
1517
= note: expected struct `Const<{pointer}>`
1618
found struct `Const<{pointer}>`

src/test/ui/const-generics/slice-const-param-mismatch.stderr

+9-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ error[E0308]: mismatched types
1010
--> $DIR/slice-const-param-mismatch.rs:9:35
1111
|
1212
LL | let _: ConstString<"Hello"> = ConstString::<"World">;
13-
| ^^^^^^^^^^^^^^^^^^^^^^ expected `"Hello"`, found `"World"`
13+
| -------------------- ^^^^^^^^^^^^^^^^^^^^^^ expected `"Hello"`, found `"World"`
14+
| |
15+
| expected due to this
1416
|
1517
= note: expected struct `ConstString<"Hello">`
1618
found struct `ConstString<"World">`
@@ -19,7 +21,9 @@ error[E0308]: mismatched types
1921
--> $DIR/slice-const-param-mismatch.rs:11:33
2022
|
2123
LL | let _: ConstString<"ℇ㇈↦"> = ConstString::<"ℇ㇈↥">;
22-
| ^^^^^^^^^^^^^^^^^^^^^ expected `"ℇ㇈↦"`, found `"ℇ㇈↥"`
24+
| ------------------- ^^^^^^^^^^^^^^^^^^^^^ expected `"ℇ㇈↦"`, found `"ℇ㇈↥"`
25+
| |
26+
| expected due to this
2327
|
2428
= note: expected struct `ConstString<"ℇ㇈↦">`
2529
found struct `ConstString<"ℇ㇈↥">`
@@ -28,7 +32,9 @@ error[E0308]: mismatched types
2832
--> $DIR/slice-const-param-mismatch.rs:13:33
2933
|
3034
LL | let _: ConstBytes<b"AAA"> = ConstBytes::<b"BBB">;
31-
| ^^^^^^^^^^^^^^^^^^^^ expected `b"AAA"`, found `b"BBB"`
35+
| ------------------ ^^^^^^^^^^^^^^^^^^^^ expected `b"AAA"`, found `b"BBB"`
36+
| |
37+
| expected due to this
3238
|
3339
= note: expected struct `ConstBytes<b"AAA">`
3440
found struct `ConstBytes<b"BBB">`

src/test/ui/const-generics/types-mismatch-const-args.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ error[E0308]: mismatched types
1010
--> $DIR/types-mismatch-const-args.rs:13:41
1111
|
1212
LL | let _: A<'a, u32, {2u32}, {3u32}> = A::<'a, u32, {4u32}, {3u32}> { data: PhantomData };
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `2u32`, found `4u32`
13+
| -------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `2u32`, found `4u32`
14+
| |
15+
| expected due to this
1416
|
1517
= note: expected struct `A<'_, _, 2u32, _>`
1618
found struct `A<'_, _, 4u32, _>`
@@ -19,7 +21,9 @@ error[E0308]: mismatched types
1921
--> $DIR/types-mismatch-const-args.rs:15:41
2022
|
2123
LL | let _: A<'a, u16, {2u32}, {3u32}> = A::<'b, u32, {2u32}, {3u32}> { data: PhantomData };
22-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u16`, found `u32`
24+
| -------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u16`, found `u32`
25+
| |
26+
| expected due to this
2327
|
2428
= note: expected struct `A<'a, u16, _, _>`
2529
found struct `A<'b, u32, _, _>`

src/test/ui/conversion-methods.stderr

+20-16
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,41 @@ error[E0308]: mismatched types
22
--> $DIR/conversion-methods.rs:5:41
33
|
44
LL | let _tis_an_instants_play: String = "'Tis a fond Ambush—";
5-
| ^^^^^^^^^^^^^^^^^^^^^
6-
| |
7-
| expected struct `std::string::String`, found `&str`
8-
| help: try using a conversion method: `"'Tis a fond Ambush—".to_string()`
5+
| ------ ^^^^^^^^^^^^^^^^^^^^^
6+
| | |
7+
| | expected struct `std::string::String`, found `&str`
8+
| | help: try using a conversion method: `"'Tis a fond Ambush—".to_string()`
9+
| expected due to this
910

1011
error[E0308]: mismatched types
1112
--> $DIR/conversion-methods.rs:6:40
1213
|
1314
LL | let _just_to_make_bliss: PathBuf = Path::new("/ern/her/own/surprise");
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15-
| |
16-
| expected struct `std::path::PathBuf`, found `&std::path::Path`
17-
| help: try using a conversion method: `Path::new("/ern/her/own/surprise").to_path_buf()`
15+
| ------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
| | |
17+
| | expected struct `std::path::PathBuf`, found `&std::path::Path`
18+
| | help: try using a conversion method: `Path::new("/ern/her/own/surprise").to_path_buf()`
19+
| expected due to this
1820

1921
error[E0308]: mismatched types
2022
--> $DIR/conversion-methods.rs:9:40
2123
|
2224
LL | let _but_should_the_play: String = 2; // Perhaps surprisingly, we suggest .to_string() here
23-
| ^
24-
| |
25-
| expected struct `std::string::String`, found integer
26-
| help: try using a conversion method: `2.to_string()`
25+
| ------ ^
26+
| | |
27+
| | expected struct `std::string::String`, found integer
28+
| | help: try using a conversion method: `2.to_string()`
29+
| expected due to this
2730

2831
error[E0308]: mismatched types
2932
--> $DIR/conversion-methods.rs:12:47
3033
|
3134
LL | let _prove_piercing_earnest: Vec<usize> = &[1, 2, 3];
32-
| ^^^^^^^^^^
33-
| |
34-
| expected struct `std::vec::Vec`, found `&[{integer}; 3]`
35-
| help: try using a conversion method: `(&[1, 2, 3]).to_vec()`
35+
| ---------- ^^^^^^^^^^
36+
| | |
37+
| | expected struct `std::vec::Vec`, found `&[{integer}; 3]`
38+
| | help: try using a conversion method: `(&[1, 2, 3]).to_vec()`
39+
| expected due to this
3640
|
3741
= note: expected struct `std::vec::Vec<usize>`
3842
found reference `&[{integer}; 3]`

src/test/ui/cross/cross-borrow-trait.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ error[E0308]: mismatched types
22
--> $DIR/cross-borrow-trait.rs:10:26
33
|
44
LL | let _y: &dyn Trait = x;
5-
| ^
6-
| |
7-
| expected `&dyn Trait`, found struct `std::boxed::Box`
8-
| help: consider borrowing here: `&x`
5+
| ---------- ^
6+
| | |
7+
| | expected `&dyn Trait`, found struct `std::boxed::Box`
8+
| | help: consider borrowing here: `&x`
9+
| expected due to this
910
|
1011
= note: expected reference `&dyn Trait`
1112
found struct `std::boxed::Box<dyn Trait>`

src/test/ui/did_you_mean/issue-53280-expected-float-found-integer-literal.stderr

+21-14
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,47 @@ error[E0308]: mismatched types
22
--> $DIR/issue-53280-expected-float-found-integer-literal.rs:2:24
33
|
44
LL | let sixteen: f32 = 16;
5-
| ^^
6-
| |
7-
| expected `f32`, found integer
8-
| help: use a float literal: `16.0`
5+
| --- ^^
6+
| | |
7+
| | expected `f32`, found integer
8+
| | help: use a float literal: `16.0`
9+
| expected due to this
910

1011
error[E0308]: mismatched types
1112
--> $DIR/issue-53280-expected-float-found-integer-literal.rs:5:38
1213
|
1314
LL | let a_million_and_seventy: f64 = 1_000_070;
14-
| ^^^^^^^^^
15-
| |
16-
| expected `f64`, found integer
17-
| help: use a float literal: `1_000_070.0`
15+
| --- ^^^^^^^^^
16+
| | |
17+
| | expected `f64`, found integer
18+
| | help: use a float literal: `1_000_070.0`
19+
| expected due to this
1820

1921
error[E0308]: mismatched types
2022
--> $DIR/issue-53280-expected-float-found-integer-literal.rs:8:30
2123
|
2224
LL | let negative_nine: f32 = -9;
23-
| ^^
24-
| |
25-
| expected `f32`, found integer
26-
| help: use a float literal: `-9.0`
25+
| --- ^^
26+
| | |
27+
| | expected `f32`, found integer
28+
| | help: use a float literal: `-9.0`
29+
| expected due to this
2730

2831
error[E0308]: mismatched types
2932
--> $DIR/issue-53280-expected-float-found-integer-literal.rs:15:30
3033
|
3134
LL | let sixteen_again: f64 = 0x10;
32-
| ^^^^ expected `f64`, found integer
35+
| --- ^^^^ expected `f64`, found integer
36+
| |
37+
| expected due to this
3338

3439
error[E0308]: mismatched types
3540
--> $DIR/issue-53280-expected-float-found-integer-literal.rs:17:30
3641
|
3742
LL | let and_once_more: f32 = 0o20;
38-
| ^^^^ expected `f32`, found integer
43+
| --- ^^^^ expected `f32`, found integer
44+
| |
45+
| expected due to this
3946

4047
error: aborting due to 5 previous errors
4148

0 commit comments

Comments
 (0)