Skip to content

Commit

Permalink
Rollup merge of rust-lang#66539 - estebank:let-ty, r=Centril
Browse files Browse the repository at this point in the history
Point at type in `let` assignment on type errors

Fix rust-lang#61067.
  • Loading branch information
Centril authored Nov 21, 2019
2 parents 5585280 + bca64ce commit 6815e4b
Show file tree
Hide file tree
Showing 97 changed files with 672 additions and 350 deletions.
28 changes: 22 additions & 6 deletions src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// N.B., this code relies on `self.diverges` to be accurate. In
// particular, assignments to `!` will be permitted if the
// diverges flag is currently "always".
pub fn demand_coerce_diag(&self,
expr: &hir::Expr,
checked_ty: Ty<'tcx>,
expected: Ty<'tcx>,
allow_two_phase: AllowTwoPhase)
-> (Ty<'tcx>, Option<DiagnosticBuilder<'tcx>>) {
pub fn demand_coerce_diag(
&self,
expr: &hir::Expr,
checked_ty: Ty<'tcx>,
expected: Ty<'tcx>,
allow_two_phase: AllowTwoPhase,
) -> (Ty<'tcx>, Option<DiagnosticBuilder<'tcx>>) {
let expected = self.resolve_vars_with_obligations(expected);

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

self.annotate_expected_due_to_let_ty(&mut err, expr);
self.suggest_compatible_variants(&mut err, expr, expected, expr_ty);
self.suggest_ref_or_into(&mut err, expr, expected, expr_ty);
self.suggest_boxing_when_appropriate(&mut err, expr, expected, expr_ty);
Expand All @@ -134,6 +136,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
(expected, Some(err))
}

fn annotate_expected_due_to_let_ty(&self, err: &mut DiagnosticBuilder<'_>, expr: &hir::Expr) {
let parent = self.tcx.hir().get_parent_node(expr.hir_id);
if let Some(hir::Node::Local(hir::Local {
ty: Some(ty),
init: Some(init),
..
})) = self.tcx.hir().find(parent) {
if init.hir_id == expr.hir_id {
// Point at `let` assignment type.
err.span_label(ty.span, "expected due to this");
}
}
}

/// Returns whether the expected type is `bool` and the expression is `x = y`.
pub fn is_assign_to_bool(&self, expr: &hir::Expr, expected: Ty<'tcx>) -> bool {
if let hir::ExprKind::Assign(..) = expr.kind {
Expand Down
4 changes: 3 additions & 1 deletion src/test/rustdoc-ui/failed-doctest-missing-codes.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ error[E0308]: mismatched types
--> $DIR/failed-doctest-missing-codes.rs:9:13
|
LL | let x: () = 5i32;
| ^^^^ expected (), found i32
| -- ^^^^ expected (), found i32
| |
| expected due to this
|
= note: expected type `()`
found type `i32`
Expand Down
8 changes: 6 additions & 2 deletions src/test/ui/array-not-vector.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error[E0308]: mismatched types
--> $DIR/array-not-vector.rs:2:19
|
LL | let _x: i32 = [1, 2, 3];
| ^^^^^^^^^ expected i32, found array of 3 elements
| --- ^^^^^^^^^ expected i32, found array of 3 elements
| |
| expected due to this
|
= note: expected type `i32`
found type `[{integer}; 3]`
Expand All @@ -11,7 +13,9 @@ error[E0308]: mismatched types
--> $DIR/array-not-vector.rs:9:20
|
LL | let _y: &i32 = x;
| ^ expected i32, found slice
| ---- ^ expected i32, found slice
| |
| expected due to this
|
= note: expected type `&i32`
found type `&[i32]`
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/associated-types/associated-types-eq-3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error[E0308]: mismatched types
--> $DIR/associated-types-eq-3.rs:23:18
|
LL | let _: Bar = x.boo();
| ^^^^^^^ expected struct `Bar`, found associated type
| --- ^^^^^^^ expected struct `Bar`, found associated type
| |
| expected due to this
|
= note: expected type `Bar`
found type `<I as Foo>::A`
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/associated-types/associated-types-path-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ error[E0308]: mismatched types
--> $DIR/associated-types-path-2.rs:41:18
|
LL | let _: i32 = f2(2i32);
| ^^^^^^^^ expected i32, found u32
| --- ^^^^^^^^ expected i32, found u32
| |
| expected due to this
|
help: you can convert an `u32` to `i32` and panic if the converted value wouldn't fit
|
Expand Down
8 changes: 6 additions & 2 deletions src/test/ui/c-variadic/variadic-ffi-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ error[E0308]: mismatched types
--> $DIR/variadic-ffi-1.rs:19:56
|
LL | let x: unsafe extern "C" fn(f: isize, x: u8) = foo;
| ^^^ expected non-variadic fn, found variadic function
| ------------------------------------- ^^^ expected non-variadic fn, found variadic function
| |
| expected due to this
|
= note: expected type `unsafe extern "C" fn(isize, u8)`
found type `unsafe extern "C" fn(isize, u8, ...) {foo}`
Expand All @@ -35,7 +37,9 @@ error[E0308]: mismatched types
--> $DIR/variadic-ffi-1.rs:20:54
|
LL | let y: extern "C" fn(f: isize, x: u8, ...) = bar;
| ^^^ expected variadic fn, found non-variadic function
| ----------------------------------- ^^^ expected variadic fn, found non-variadic function
| |
| expected due to this
|
= note: expected type `extern "C" fn(isize, u8, ...)`
found type `extern "C" fn(isize, u8) {bar}`
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/closures/closure-no-fn-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error[E0308]: mismatched types
--> $DIR/closure-no-fn-1.rs:6:29
|
LL | let foo: fn(u8) -> u8 = |v: u8| { a += v; a };
| ^^^^^^^^^^^^^^^^^^^^^ expected fn pointer, found closure
| ------------ ^^^^^^^^^^^^^^^^^^^^^ expected fn pointer, found closure
| |
| expected due to this
|
= note: expected type `fn(u8) -> u8`
found type `[closure@$DIR/closure-no-fn-1.rs:6:29: 6:50 a:_]`
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/closures/closure-no-fn-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error[E0308]: mismatched types
--> $DIR/closure-no-fn-2.rs:6:27
|
LL | let bar: fn() -> u8 = || { b };
| ^^^^^^^^ expected fn pointer, found closure
| ---------- ^^^^^^^^ expected fn pointer, found closure
| |
| expected due to this
|
= note: expected type `fn() -> u8`
found type `[closure@$DIR/closure-no-fn-2.rs:6:27: 6:35 b:_]`
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/coercion/coerce-to-bang.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ error[E0308]: mismatched types
--> $DIR/coerce-to-bang.rs:50:21
|
LL | let x: [!; 2] = [return, 22];
| ^^^^^^^^^^^^ expected !, found integer
| ------ ^^^^^^^^^^^^ expected !, found integer
| |
| expected due to this
|
= note: expected type `[!; 2]`
found type `[{integer}; 2]`
Expand Down
9 changes: 5 additions & 4 deletions src/test/ui/coercion/coercion-slice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ error[E0308]: mismatched types
--> $DIR/coercion-slice.rs:4:21
|
LL | let _: &[i32] = [0];
| ^^^
| |
| expected &[i32], found array of 1 element
| help: consider borrowing here: `&[0]`
| ------ ^^^
| | |
| | expected &[i32], found array of 1 element
| | help: consider borrowing here: `&[0]`
| expected due to this
|
= note: expected type `&[i32]`
found type `[{integer}; 1]`
Expand Down
8 changes: 6 additions & 2 deletions src/test/ui/const-generics/fn-const-param-infer.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ error[E0308]: mismatched types
--> $DIR/fn-const-param-infer.rs:16:31
|
LL | let _: Checked<not_one> = Checked::<not_two>;
| ^^^^^^^^^^^^^^^^^^ expected `not_one`, found `not_two`
| ---------------- ^^^^^^^^^^^^^^^^^^ expected `not_one`, found `not_two`
| |
| expected due to this
|
= note: expected type `Checked<not_one>`
found type `Checked<not_two>`
Expand All @@ -34,7 +36,9 @@ error[E0308]: mismatched types
--> $DIR/fn-const-param-infer.rs:25:40
|
LL | let _: Checked<{generic::<u32>}> = Checked::<{generic::<u16>}>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `generic::<u32>`, found `generic::<u16>`
| ------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `generic::<u32>`, found `generic::<u16>`
| |
| expected due to this
|
= note: expected type `Checked<generic::<u32>>`
found type `Checked<generic::<u16>>`
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/const-generics/raw-ptr-const-param.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ error[E0308]: mismatched types
--> $DIR/raw-ptr-const-param.rs:7:38
|
LL | let _: Const<{15 as *const _}> = Const::<{10 as *const _}>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{pointer}`, found `{pointer}`
| ----------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `{pointer}`, found `{pointer}`
| |
| expected due to this
|
= note: expected type `Const<{pointer}>`
found type `Const<{pointer}>`
Expand Down
12 changes: 9 additions & 3 deletions src/test/ui/const-generics/slice-const-param-mismatch.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ error[E0308]: mismatched types
--> $DIR/slice-const-param-mismatch.rs:9:35
|
LL | let _: ConstString<"Hello"> = ConstString::<"World">;
| ^^^^^^^^^^^^^^^^^^^^^^ expected `"Hello"`, found `"World"`
| -------------------- ^^^^^^^^^^^^^^^^^^^^^^ expected `"Hello"`, found `"World"`
| |
| expected due to this
|
= note: expected type `ConstString<"Hello">`
found type `ConstString<"World">`
Expand All @@ -19,7 +21,9 @@ error[E0308]: mismatched types
--> $DIR/slice-const-param-mismatch.rs:11:33
|
LL | let _: ConstString<"ℇ㇈↦"> = ConstString::<"ℇ㇈↥">;
| ^^^^^^^^^^^^^^^^^^^^^ expected `"ℇ㇈↦"`, found `"ℇ㇈↥"`
| ------------------- ^^^^^^^^^^^^^^^^^^^^^ expected `"ℇ㇈↦"`, found `"ℇ㇈↥"`
| |
| expected due to this
|
= note: expected type `ConstString<"ℇ㇈↦">`
found type `ConstString<"ℇ㇈↥">`
Expand All @@ -28,7 +32,9 @@ error[E0308]: mismatched types
--> $DIR/slice-const-param-mismatch.rs:13:33
|
LL | let _: ConstBytes<b"AAA"> = ConstBytes::<b"BBB">;
| ^^^^^^^^^^^^^^^^^^^^ expected `b"AAA"`, found `b"BBB"`
| ------------------ ^^^^^^^^^^^^^^^^^^^^ expected `b"AAA"`, found `b"BBB"`
| |
| expected due to this
|
= note: expected type `ConstBytes<b"AAA">`
found type `ConstBytes<b"BBB">`
Expand Down
8 changes: 6 additions & 2 deletions src/test/ui/const-generics/types-mismatch-const-args.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ error[E0308]: mismatched types
--> $DIR/types-mismatch-const-args.rs:13:41
|
LL | let _: A<'a, u32, {2u32}, {3u32}> = A::<'a, u32, {4u32}, {3u32}> { data: PhantomData };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `2u32`, found `4u32`
| -------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `2u32`, found `4u32`
| |
| expected due to this
|
= note: expected type `A<'_, _, 2u32, _>`
found type `A<'_, _, 4u32, _>`
Expand All @@ -19,7 +21,9 @@ error[E0308]: mismatched types
--> $DIR/types-mismatch-const-args.rs:15:41
|
LL | let _: A<'a, u16, {2u32}, {3u32}> = A::<'b, u32, {2u32}, {3u32}> { data: PhantomData };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected u16, found u32
| -------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected u16, found u32
| |
| expected due to this
|
= note: expected type `A<'a, u16, _, _>`
found type `A<'b, u32, _, _>`
Expand Down
36 changes: 20 additions & 16 deletions src/test/ui/conversion-methods.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ error[E0308]: mismatched types
--> $DIR/conversion-methods.rs:5:41
|
LL | let _tis_an_instants_play: String = "'Tis a fond Ambush—";
| ^^^^^^^^^^^^^^^^^^^^^
| |
| expected struct `std::string::String`, found reference
| help: try using a conversion method: `"'Tis a fond Ambush—".to_string()`
| ------ ^^^^^^^^^^^^^^^^^^^^^
| | |
| | expected struct `std::string::String`, found reference
| | help: try using a conversion method: `"'Tis a fond Ambush—".to_string()`
| expected due to this
|
= note: expected type `std::string::String`
found type `&'static str`
Expand All @@ -14,10 +15,11 @@ error[E0308]: mismatched types
--> $DIR/conversion-methods.rs:6:40
|
LL | let _just_to_make_bliss: PathBuf = Path::new("/ern/her/own/surprise");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| expected struct `std::path::PathBuf`, found reference
| help: try using a conversion method: `Path::new("/ern/her/own/surprise").to_path_buf()`
| ------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| | |
| | expected struct `std::path::PathBuf`, found reference
| | help: try using a conversion method: `Path::new("/ern/her/own/surprise").to_path_buf()`
| expected due to this
|
= note: expected type `std::path::PathBuf`
found type `&std::path::Path`
Expand All @@ -26,10 +28,11 @@ error[E0308]: mismatched types
--> $DIR/conversion-methods.rs:9:40
|
LL | let _but_should_the_play: String = 2; // Perhaps surprisingly, we suggest .to_string() here
| ^
| |
| expected struct `std::string::String`, found integer
| help: try using a conversion method: `2.to_string()`
| ------ ^
| | |
| | expected struct `std::string::String`, found integer
| | help: try using a conversion method: `2.to_string()`
| expected due to this
|
= note: expected type `std::string::String`
found type `{integer}`
Expand All @@ -38,10 +41,11 @@ error[E0308]: mismatched types
--> $DIR/conversion-methods.rs:12:47
|
LL | let _prove_piercing_earnest: Vec<usize> = &[1, 2, 3];
| ^^^^^^^^^^
| |
| expected struct `std::vec::Vec`, found reference
| help: try using a conversion method: `(&[1, 2, 3]).to_vec()`
| ---------- ^^^^^^^^^^
| | |
| | expected struct `std::vec::Vec`, found reference
| | help: try using a conversion method: `(&[1, 2, 3]).to_vec()`
| expected due to this
|
= note: expected type `std::vec::Vec<usize>`
found type `&[{integer}; 3]`
Expand Down
9 changes: 5 additions & 4 deletions src/test/ui/cross/cross-borrow-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ error[E0308]: mismatched types
--> $DIR/cross-borrow-trait.rs:10:26
|
LL | let _y: &dyn Trait = x;
| ^
| |
| expected &dyn Trait, found struct `std::boxed::Box`
| help: consider borrowing here: `&x`
| ---------- ^
| | |
| | expected &dyn Trait, found struct `std::boxed::Box`
| | help: consider borrowing here: `&x`
| expected due to this
|
= note: expected type `&dyn Trait`
found type `std::boxed::Box<dyn Trait>`
Expand Down
Loading

0 comments on commit 6815e4b

Please sign in to comment.