Skip to content

Commit 6815e4b

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 5585280 + bca64ce commit 6815e4b

File tree

97 files changed

+672
-350
lines changed

Some content is hidden

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

97 files changed

+672
-350
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
= note: expected type `()`
1517
found type `i32`

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ 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 of 3 elements
5+
| --- ^^^^^^^^^ expected i32, found array of 3 elements
6+
| |
7+
| expected due to this
68
|
79
= note: expected type `i32`
810
found type `[{integer}; 3]`
@@ -11,7 +13,9 @@ error[E0308]: mismatched types
1113
--> $DIR/array-not-vector.rs:9:20
1214
|
1315
LL | let _y: &i32 = x;
14-
| ^ expected i32, found slice
16+
| ---- ^ expected i32, found slice
17+
| |
18+
| expected due to this
1519
|
1620
= note: expected type `&i32`
1721
found type `&[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 type `Bar`
810
found 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 type `unsafe extern "C" fn(isize, u8)`
3234
found type `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 type `extern "C" fn(isize, u8, ...)`
4145
found type `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 type `fn(u8) -> u8`
810
found type `[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 type `fn() -> u8`
810
found type `[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:50: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 type `[!; 2]`
5355
found type `[{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 of 1 element
8-
| help: consider borrowing here: `&[0]`
5+
| ------ ^^^
6+
| | |
7+
| | expected &[i32], found array of 1 element
8+
| | help: consider borrowing here: `&[0]`
9+
| expected due to this
910
|
1011
= note: expected type `&[i32]`
1112
found type `[{integer}; 1]`

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 type `Checked<not_one>`
1618
found type `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 type `Checked<generic::<u32>>`
4044
found type `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 type `Const<{pointer}>`
1618
found type `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 type `ConstString<"Hello">`
1618
found type `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 type `ConstString<"ℇ㇈↦">`
2529
found type `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 type `ConstBytes<b"AAA">`
3440
found type `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 type `A<'_, _, 2u32, _>`
1618
found type `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 type `A<'a, u16, _, _>`
2529
found type `A<'b, u32, _, _>`

src/test/ui/conversion-methods.stderr

+20-16
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ 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 reference
8-
| help: try using a conversion method: `"'Tis a fond Ambush—".to_string()`
5+
| ------ ^^^^^^^^^^^^^^^^^^^^^
6+
| | |
7+
| | expected struct `std::string::String`, found reference
8+
| | help: try using a conversion method: `"'Tis a fond Ambush—".to_string()`
9+
| expected due to this
910
|
1011
= note: expected type `std::string::String`
1112
found type `&'static str`
@@ -14,10 +15,11 @@ error[E0308]: mismatched types
1415
--> $DIR/conversion-methods.rs:6:40
1516
|
1617
LL | let _just_to_make_bliss: PathBuf = Path::new("/ern/her/own/surprise");
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18-
| |
19-
| expected struct `std::path::PathBuf`, found reference
20-
| help: try using a conversion method: `Path::new("/ern/her/own/surprise").to_path_buf()`
18+
| ------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
| | |
20+
| | expected struct `std::path::PathBuf`, found reference
21+
| | help: try using a conversion method: `Path::new("/ern/her/own/surprise").to_path_buf()`
22+
| expected due to this
2123
|
2224
= note: expected type `std::path::PathBuf`
2325
found type `&std::path::Path`
@@ -26,10 +28,11 @@ error[E0308]: mismatched types
2628
--> $DIR/conversion-methods.rs:9:40
2729
|
2830
LL | let _but_should_the_play: String = 2; // Perhaps surprisingly, we suggest .to_string() here
29-
| ^
30-
| |
31-
| expected struct `std::string::String`, found integer
32-
| help: try using a conversion method: `2.to_string()`
31+
| ------ ^
32+
| | |
33+
| | expected struct `std::string::String`, found integer
34+
| | help: try using a conversion method: `2.to_string()`
35+
| expected due to this
3336
|
3437
= note: expected type `std::string::String`
3538
found type `{integer}`
@@ -38,10 +41,11 @@ error[E0308]: mismatched types
3841
--> $DIR/conversion-methods.rs:12:47
3942
|
4043
LL | let _prove_piercing_earnest: Vec<usize> = &[1, 2, 3];
41-
| ^^^^^^^^^^
42-
| |
43-
| expected struct `std::vec::Vec`, found reference
44-
| help: try using a conversion method: `(&[1, 2, 3]).to_vec()`
44+
| ---------- ^^^^^^^^^^
45+
| | |
46+
| | expected struct `std::vec::Vec`, found reference
47+
| | help: try using a conversion method: `(&[1, 2, 3]).to_vec()`
48+
| expected due to this
4549
|
4650
= note: expected type `std::vec::Vec<usize>`
4751
found type `&[{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 type `&dyn Trait`
1112
found type `std::boxed::Box<dyn Trait>`

0 commit comments

Comments
 (0)