Rollup merge of #57723 - estebank:fix, r=davidtwco

Point at cause for expectation in return type type error

Various improvements and fixes for type errors in return expressions.

Fix #57664.
This commit is contained in:
Mazdak Farrokhzad 2019-01-19 19:41:22 +01:00 committed by GitHub
commit 5b9e02a39c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 203 additions and 21 deletions

View file

@ -1,6 +1,8 @@
error[E0308]: mismatched types
--> $DIR/fully-qualified-type-name2.rs:12:12
|
LL | fn bar(x: x::Foo) -> y::Foo {
| ------ expected `y::Foo` because of return type
LL | return x;
| ^ expected enum `y::Foo`, found enum `x::Foo`
|

View file

@ -1,6 +1,8 @@
error[E0308]: mismatched types
--> $DIR/fully-qualified-type-name4.rs:6:12
|
LL | fn bar(x: usize) -> Option<usize> {
| ------------- expected `std::option::Option<usize>` because of return type
LL | return x;
| ^ expected enum `std::option::Option`, found usize
|

View file

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/liveness-forgot-ret.rs:3:19
|
LL | fn f(a: isize) -> isize { if god_exists(a) { return 5; }; }
| - ^^^^^ expected isize, found () - expected because of this statement
| - ^^^^^ expected isize, found ()
| |
| this function's body doesn't return
|

View file

@ -0,0 +1,17 @@
fn unrelated() -> Result<(), std::string::ParseError> { // #57664
let x = 0;
match x {
1 => {
let property_value_as_string = "a".parse()?;
}
2 => {
let value: &bool = unsafe { &42 };
//~^ ERROR mismatched types
}
};
Ok(())
}
fn main() {}

View file

@ -0,0 +1,12 @@
error[E0308]: mismatched types
--> $DIR/point-to-type-err-cause-on-impl-trait-return-2.rs:9:41
|
LL | let value: &bool = unsafe { &42 };
| ^^^ expected bool, found integer
|
= note: expected type `&bool`
found type `&{integer}`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.

View file

@ -0,0 +1,36 @@
fn foo() -> impl std::fmt::Display {
if false {
return 0i32;
}
1u32
//~^ ERROR mismatched types
}
fn bar() -> impl std::fmt::Display {
if false {
return 0i32;
} else {
return 1u32;
//~^ ERROR mismatched types
}
}
fn baz() -> impl std::fmt::Display {
if false {
//~^ ERROR mismatched types
return 0i32;
} else {
1u32
}
}
fn qux() -> impl std::fmt::Display {
if false {
0i32
} else {
1u32
//~^ ERROR if and else have incompatible types
}
}
fn main() {}

View file

@ -0,0 +1,58 @@
error[E0308]: mismatched types
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:5:5
|
LL | return 0i32;
| ---- expected because of this statement
LL | }
LL | 1u32
| ^^^^ expected i32, found u32
|
= note: expected type `i32`
found type `u32`
error[E0308]: mismatched types
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:13:16
|
LL | return 0i32;
| ---- expected because of this statement
LL | } else {
LL | return 1u32;
| ^^^^ expected i32, found u32
|
= note: expected type `i32`
found type `u32`
error[E0308]: mismatched types
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:19:5
|
LL | / if false {
LL | | //~^ ERROR mismatched types
LL | | return 0i32;
| | ---- expected because of this statement
LL | | } else {
LL | | 1u32
LL | | }
| |_____^ expected i32, found u32
|
= note: expected type `i32`
found type `u32`
error[E0308]: if and else have incompatible types
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:31:9
|
LL | / if false {
LL | | 0i32
| | ---- expected because of this
LL | | } else {
LL | | 1u32
| | ^^^^ expected i32, found u32
LL | | //~^ ERROR if and else have incompatible types
LL | | }
| |_____- if and else have incompatible types
|
= note: expected type `i32`
found type `u32`
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0308`.

View file

@ -15,6 +15,9 @@ LL | let x: usize = "hello";;;;; //~ ERROR mismatched types
error[E0308]: mismatched types
--> $DIR/span-preservation.rs:19:29
|
LL | fn b(x: Option<isize>) -> usize {
| ----- expected `usize` because of return type
LL | match x {
LL | Some(x) => { return x }, //~ ERROR mismatched types
| ^ expected usize, found isize

View file

@ -1,6 +1,8 @@
error[E0308]: mismatched types
--> $DIR/return-from-diverging.rs:4:12
|
LL | fn fail() -> ! {
| - expected `!` because of return type
LL | return "wow"; //~ ERROR mismatched types
| ^^^^^ expected !, found reference
|

View file

@ -2,7 +2,9 @@ error[E0308]: mismatched types
--> $DIR/tail-typeck.rs:3:26
|
LL | fn f() -> isize { return g(); }
| ^^^ expected isize, found usize
| ----- ^^^ expected isize, found usize
| |
| expected `isize` because of return type
error: aborting due to previous error

View file

@ -2,7 +2,9 @@ error[E0308]: mismatched types
--> $DIR/wrong-ret-type.rs:2:49
|
LL | fn mk_int() -> usize { let i: isize = 3; return i; }
| ^ expected usize, found isize
| ----- ^ expected usize, found isize
| |
| expected `usize` because of return type
error: aborting due to previous error