Skip to content

Commit

Permalink
Rollup merge of #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 #61067.
  • Loading branch information
Centril authored Nov 23, 2019
2 parents 6618af2 + 34f03c0 commit 62a7839
Show file tree
Hide file tree
Showing 101 changed files with 684 additions and 354 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

error: aborting due to previous error

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,13 +2,17 @@ error[E0308]: mismatched types
--> $DIR/array-not-vector.rs:2:19
|
LL | let _x: i32 = [1, 2, 3];
| ^^^^^^^^^ expected `i32`, found array `[{integer}; 3]`
| --- ^^^^^^^^^ expected `i32`, found array `[{integer}; 3]`
| |
| expected due to this

error[E0308]: mismatched types
--> $DIR/array-not-vector.rs:7:20
|
LL | let _y: &i32 = x;
| ^ expected `i32`, found slice `[i32]`
| ---- ^ expected `i32`, found slice `[i32]`
| |
| expected due to this
|
= note: expected reference `&i32`
found reference `&[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 struct `Bar`
found associated 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 fn pointer `unsafe extern "C" fn(isize, u8)`
found fn item `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 fn pointer `extern "C" fn(isize, u8, ...)`
found fn item `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 fn pointer `fn(u8) -> u8`
found closure `[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 fn pointer `fn() -> u8`
found closure `[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:48:21
|
LL | let x: [!; 2] = [return, 22];
| ^^^^^^^^^^^^ expected `!`, found integer
| ------ ^^^^^^^^^^^^ expected `!`, found integer
| |
| expected due to this
|
= note: expected array `[!; 2]`
found array `[{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 `[{integer}; 1]`
| help: consider borrowing here: `&[0]`
| ------ ^^^
| | |
| | expected `&[i32]`, found array `[{integer}; 1]`
| | help: consider borrowing here: `&[0]`
| expected due to this

error: aborting due to previous error

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 struct `Checked<not_one>`
found struct `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 struct `Checked<generic::<u32>>`
found struct `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 struct `Const<{pointer}>`
found struct `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 struct `ConstString<"Hello">`
found struct `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 struct `ConstString<"ℇ㇈↦">`
found struct `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 struct `ConstBytes<b"AAA">`
found struct `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 struct `A<'_, _, 2u32, _>`
found struct `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 struct `A<'a, u16, _, _>`
found struct `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,37 +2,41 @@ 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 `&str`
| help: try using a conversion method: `"'Tis a fond Ambush—".to_string()`
| ------ ^^^^^^^^^^^^^^^^^^^^^
| | |
| | expected struct `std::string::String`, found `&str`
| | help: try using a conversion method: `"'Tis a fond Ambush—".to_string()`
| expected due to this

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 `&std::path::Path`
| help: try using a conversion method: `Path::new("/ern/her/own/surprise").to_path_buf()`
| ------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| | |
| | expected struct `std::path::PathBuf`, found `&std::path::Path`
| | help: try using a conversion method: `Path::new("/ern/her/own/surprise").to_path_buf()`
| expected due to this

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

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 `&[{integer}; 3]`
| help: try using a conversion method: `(&[1, 2, 3]).to_vec()`
| ---------- ^^^^^^^^^^
| | |
| | expected struct `std::vec::Vec`, found `&[{integer}; 3]`
| | help: try using a conversion method: `(&[1, 2, 3]).to_vec()`
| expected due to this
|
= note: expected struct `std::vec::Vec<usize>`
found reference `&[{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 reference `&dyn Trait`
found struct `std::boxed::Box<dyn Trait>`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,47 @@ error[E0308]: mismatched types
--> $DIR/issue-53280-expected-float-found-integer-literal.rs:2:24
|
LL | let sixteen: f32 = 16;
| ^^
| |
| expected `f32`, found integer
| help: use a float literal: `16.0`
| --- ^^
| | |
| | expected `f32`, found integer
| | help: use a float literal: `16.0`
| expected due to this

error[E0308]: mismatched types
--> $DIR/issue-53280-expected-float-found-integer-literal.rs:5:38
|
LL | let a_million_and_seventy: f64 = 1_000_070;
| ^^^^^^^^^
| |
| expected `f64`, found integer
| help: use a float literal: `1_000_070.0`
| --- ^^^^^^^^^
| | |
| | expected `f64`, found integer
| | help: use a float literal: `1_000_070.0`
| expected due to this

error[E0308]: mismatched types
--> $DIR/issue-53280-expected-float-found-integer-literal.rs:8:30
|
LL | let negative_nine: f32 = -9;
| ^^
| |
| expected `f32`, found integer
| help: use a float literal: `-9.0`
| --- ^^
| | |
| | expected `f32`, found integer
| | help: use a float literal: `-9.0`
| expected due to this

error[E0308]: mismatched types
--> $DIR/issue-53280-expected-float-found-integer-literal.rs:15:30
|
LL | let sixteen_again: f64 = 0x10;
| ^^^^ expected `f64`, found integer
| --- ^^^^ expected `f64`, found integer
| |
| expected due to this

error[E0308]: mismatched types
--> $DIR/issue-53280-expected-float-found-integer-literal.rs:17:30
|
LL | let and_once_more: f32 = 0o20;
| ^^^^ expected `f32`, found integer
| --- ^^^^ expected `f32`, found integer
| |
| expected due to this

error: aborting due to 5 previous errors

Expand Down
Loading

0 comments on commit 62a7839

Please sign in to comment.