-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggest coercion of Result
using ?
#106583
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e477cf9
Suggest coercion of `Result` using `?`
estebank d5a1609
review comment: use `fcx.infcx`
estebank ddd9a9f
Add call in `emit_type_mismatch_suggestions`
estebank fcf0ed9
Do not erase regions
estebank df81147
Ensure suggestion correctness
estebank 62aff3b
tweak wording
estebank File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
struct A; | ||
struct B; | ||
impl From<A> for B { | ||
fn from(_: A) -> Self { B } | ||
} | ||
fn foo4(x: Result<(), A>) -> Result<(), B> { | ||
match true { | ||
true => x, //~ ERROR mismatched types | ||
false => x, | ||
} | ||
} | ||
fn foo5(x: Result<(), A>) -> Result<(), B> { | ||
match true { | ||
true => return x, //~ ERROR mismatched types | ||
false => return x, | ||
} | ||
} | ||
fn main() { | ||
let _ = foo4(Ok(())); | ||
let _ = foo5(Ok(())); | ||
let _: Result<(), B> = { //~ ERROR mismatched types | ||
Err(A); | ||
}; | ||
} |
47 changes: 47 additions & 0 deletions
47
tests/ui/type/type-check/coerce-result-return-value-2.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/coerce-result-return-value-2.rs:8:17 | ||
| | ||
LL | fn foo4(x: Result<(), A>) -> Result<(), B> { | ||
| ------------- expected `Result<(), B>` because of return type | ||
LL | match true { | ||
LL | true => x, | ||
| ^ expected struct `B`, found struct `A` | ||
| | ||
= note: expected enum `Result<_, B>` | ||
found enum `Result<_, A>` | ||
help: use `?` to coerce and return an appropriate `Err`, and wrap the resulting value in `Ok` so the expression remains of type `Result` | ||
| | ||
LL | true => Ok(x?), | ||
| +++ ++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/coerce-result-return-value-2.rs:14:24 | ||
| | ||
LL | fn foo5(x: Result<(), A>) -> Result<(), B> { | ||
| ------------- expected `Result<(), B>` because of return type | ||
LL | match true { | ||
LL | true => return x, | ||
| ^ expected struct `B`, found struct `A` | ||
| | ||
= note: expected enum `Result<_, B>` | ||
found enum `Result<_, A>` | ||
help: use `?` to coerce and return an appropriate `Err`, and wrap the resulting value in `Ok` so the expression remains of type `Result` | ||
| | ||
LL | true => return Ok(x?), | ||
| +++ ++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/coerce-result-return-value-2.rs:21:28 | ||
| | ||
LL | let _: Result<(), B> = { | ||
| ____________________________^ | ||
LL | | Err(A); | ||
LL | | }; | ||
| |_____^ expected enum `Result`, found `()` | ||
| | ||
= note: expected enum `Result<(), B>` | ||
found unit type `()` | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// run-rustfix | ||
struct A; | ||
struct B; | ||
impl From<A> for B { | ||
fn from(_: A) -> Self { B } | ||
} | ||
fn foo1(x: Result<(), A>) -> Result<(), B> { | ||
Ok(x?) //~ ERROR mismatched types | ||
} | ||
fn foo2(x: Result<(), A>) -> Result<(), B> { | ||
return Ok(x?); //~ ERROR mismatched types | ||
} | ||
fn foo3(x: Result<(), A>) -> Result<(), B> { | ||
if true { | ||
Ok(x?) //~ ERROR mismatched types | ||
} else { | ||
Ok(x?) //~ ERROR mismatched types | ||
} | ||
} | ||
fn main() { | ||
let _ = foo1(Ok(())); | ||
let _ = foo2(Ok(())); | ||
let _ = foo3(Ok(())); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// run-rustfix | ||
struct A; | ||
struct B; | ||
impl From<A> for B { | ||
fn from(_: A) -> Self { B } | ||
} | ||
fn foo1(x: Result<(), A>) -> Result<(), B> { | ||
x //~ ERROR mismatched types | ||
} | ||
fn foo2(x: Result<(), A>) -> Result<(), B> { | ||
return x; //~ ERROR mismatched types | ||
} | ||
fn foo3(x: Result<(), A>) -> Result<(), B> { | ||
if true { | ||
x //~ ERROR mismatched types | ||
} else { | ||
x //~ ERROR mismatched types | ||
} | ||
} | ||
fn main() { | ||
let _ = foo1(Ok(())); | ||
let _ = foo2(Ok(())); | ||
let _ = foo3(Ok(())); | ||
} |
65 changes: 65 additions & 0 deletions
65
tests/ui/type/type-check/coerce-result-return-value.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/coerce-result-return-value.rs:8:5 | ||
| | ||
LL | fn foo1(x: Result<(), A>) -> Result<(), B> { | ||
| ------------- expected `Result<(), B>` because of return type | ||
LL | x | ||
| ^ expected struct `B`, found struct `A` | ||
| | ||
= note: expected enum `Result<_, B>` | ||
found enum `Result<_, A>` | ||
help: use `?` to coerce and return an appropriate `Err`, and wrap the resulting value in `Ok` so the expression remains of type `Result` | ||
| | ||
LL | Ok(x?) | ||
| +++ ++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/coerce-result-return-value.rs:11:12 | ||
| | ||
LL | fn foo2(x: Result<(), A>) -> Result<(), B> { | ||
| ------------- expected `Result<(), B>` because of return type | ||
LL | return x; | ||
| ^ expected struct `B`, found struct `A` | ||
| | ||
= note: expected enum `Result<_, B>` | ||
found enum `Result<_, A>` | ||
help: use `?` to coerce and return an appropriate `Err`, and wrap the resulting value in `Ok` so the expression remains of type `Result` | ||
| | ||
LL | return Ok(x?); | ||
| +++ ++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/coerce-result-return-value.rs:15:9 | ||
| | ||
LL | fn foo3(x: Result<(), A>) -> Result<(), B> { | ||
| ------------- expected `Result<(), B>` because of return type | ||
LL | if true { | ||
LL | x | ||
| ^ expected struct `B`, found struct `A` | ||
| | ||
= note: expected enum `Result<_, B>` | ||
found enum `Result<_, A>` | ||
help: use `?` to coerce and return an appropriate `Err`, and wrap the resulting value in `Ok` so the expression remains of type `Result` | ||
| | ||
LL | Ok(x?) | ||
| +++ ++ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/coerce-result-return-value.rs:17:9 | ||
| | ||
LL | fn foo3(x: Result<(), A>) -> Result<(), B> { | ||
| ------------- expected `Result<(), B>` because of return type | ||
... | ||
LL | x | ||
| ^ expected struct `B`, found struct `A` | ||
| | ||
= note: expected enum `Result<_, B>` | ||
found enum `Result<_, A>` | ||
help: use `?` to coerce and return an appropriate `Err`, and wrap the resulting value in `Ok` so the expression remains of type `Result` | ||
| | ||
LL | Ok(x?) | ||
| +++ ++ | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test case has a mistake - you meant to test
Err(A)
notErr(A);
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works as a negative test :)
I don't recall now if I intended for that to be the case, which is likely not.