-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Provide context when ?
can't be called because of Result<_, E>
#116496
Conversation
r? @wesleywiser (rustbot has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
LL | .map(|s| ()) | ||
| ----------- this can be annotated with `?` because it has type `Result<(), &str>` | ||
LL | .map_err(|_| ()) | ||
| --------------- this can't be annotated with `?` because it has type `Result<(), ()>` | ||
LL | .map(|()| "")?; | ||
| ------------^ the trait `From<()>` is not implemented for `String` | ||
| | | ||
| this can't be annotated with `?` because it has type `Result<&str, ()>` |
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 seems really heavy for long chains -- do i really care about ?
-ability 4 .map
s down?
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.
Probably not. I would like to add an extra check to only point at places where the E
changed (like we do for assoc types in method call chains), but wanted to keep the code down to a reviewable size.
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.
I'd rather just put a hard limit on the number of method chain to walk backwards. Maybe 2 is sensible.
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.
Let me know what you think of the new output.
Also, this doesn't really fix #72124, does it? The problem in that issue is a shortcoming of closure inference causing us to forgo suggesting removing |
I'm adding some checks for the specific methods that take closures and that can change the type of |
bd7a587
to
19f23b6
Compare
For what it's worth, I think those checks should be implemented in a separate PR to separate concerns. I'm mostly just noting that the PR shouldn't close (via |
bb20f12
to
b09ff86
Compare
This comment was marked as resolved.
This comment was marked as resolved.
9688267
to
c55e70e
Compare
This comment was marked as resolved.
This comment was marked as resolved.
c55e70e
to
0e8b80a
Compare
This comment was marked as resolved.
This comment was marked as resolved.
0e8b80a
to
eff708f
Compare
eff708f
to
cce82d8
Compare
@bors r+ rollup |
…text, r=compiler-errors Provide context when `?` can't be called because of `Result<_, E>` When a method chain ending in `?` causes an E0277 because the expression's `Result::Err` variant doesn't have a type that can be converted to the `Result<_, E>` type parameter in the return type, provide additional context of which parts of the chain can and can't support the `?` operator. ``` error[E0277]: `?` couldn't convert the error to `String` --> $DIR/question-mark-result-err-mismatch.rs:27:25 | LL | fn bar() -> Result<(), String> { | ------------------ expected `String` because of this LL | let x = foo(); | ----- this has type `Result<_, String>` ... LL | .map_err(|_| ())?; | ---------------^ the trait `From<()>` is not implemented for `String` | | | this can't be annotated with `?` because it has type `Result<_, ()>` | = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait = help: the following other types implement trait `From<T>`: <String as From<char>> <String as From<Box<str>>> <String as From<Cow<'a, str>>> <String as From<&str>> <String as From<&mut str>> <String as From<&String>> = note: required for `Result<(), String>` to implement `FromResidual<Result<Infallible, ()>>` ``` Fix rust-lang#72124.
…text, r=compiler-errors Provide context when `?` can't be called because of `Result<_, E>` When a method chain ending in `?` causes an E0277 because the expression's `Result::Err` variant doesn't have a type that can be converted to the `Result<_, E>` type parameter in the return type, provide additional context of which parts of the chain can and can't support the `?` operator. ``` error[E0277]: `?` couldn't convert the error to `String` --> $DIR/question-mark-result-err-mismatch.rs:27:25 | LL | fn bar() -> Result<(), String> { | ------------------ expected `String` because of this LL | let x = foo(); | ----- this has type `Result<_, String>` ... LL | .map_err(|_| ())?; | ---------------^ the trait `From<()>` is not implemented for `String` | | | this can't be annotated with `?` because it has type `Result<_, ()>` | = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait = help: the following other types implement trait `From<T>`: <String as From<char>> <String as From<Box<str>>> <String as From<Cow<'a, str>>> <String as From<&str>> <String as From<&mut str>> <String as From<&String>> = note: required for `Result<(), String>` to implement `FromResidual<Result<Infallible, ()>>` ``` Fix rust-lang#72124.
hir::intravisit::walk_expr(self, ex); | ||
} | ||
} | ||
let hir_id = self.tcx.hir().local_def_id_to_hir_id(obligation.cause.body_id); |
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 needs a rebase.
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.
please amend commit 2faaea9 after rebasing because local_def_id_to_hir_id
has moved onto TyCtxt
.
@bors r- r=me after fixing up that first commit |
(also if you're already changing this, pls squash the formatting commits down into more relevant ones) |
When a method chain ending in `?` causes an E0277 because the expression's `Result::Err` variant doesn't have a type that can be converted to the `Result<_, E>` type parameter in the return type, provide additional context of which parts of the chain can and can't support the `?` operator. ``` error[E0277]: `?` couldn't convert the error to `String` --> $DIR/question-mark-result-err-mismatch.rs:28:25 | LL | fn bar() -> Result<(), String> { | ------------------ expected `String` because of this LL | let x = foo(); | ----- this can be annotated with `?` because it has type `Result<String, String>` LL | let one = x LL | .map(|s| ()) | ----------- this can be annotated with `?` because it has type `Result<(), String>` LL | .map_err(|_| ())?; | ---------------^ the trait `From<()>` is not implemented for `String` | | | this can't be annotated with `?` because it has type `Result<(), ()>` | = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait = help: the following other types implement trait `From<T>`: <String as From<char>> <String as From<Box<str>>> <String as From<Cow<'a, str>>> <String as From<&str>> <String as From<&mut str>> <String as From<&String>> = note: required for `Result<(), String>` to implement `FromResidual<Result<Infallible, ()>>` ``` Fix rust-lang#72124.
cce82d8
to
23fa94f
Compare
Thanks! @bors r+ |
23fa94f
to
70fe624
Compare
Pushed again (squashed the comment adding commit too) @bors r=compiler-errors |
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#116496 (Provide context when `?` can't be called because of `Result<_, E>`) - rust-lang#117563 (docs: clarify explicitly freeing heap allocated memory) - rust-lang#117874 (`riscv32` platform support) - rust-lang#118516 (Add ADT variant infomation to StableMIR and finish implementing TyKind::internal()) - rust-lang#118650 (add comment about keeping flags in sync between bootstrap.py and bootstrap.rs) - rust-lang#118664 (docs: remove rust-lang#110800 from release notes) - rust-lang#118669 (library: fix comment about const assert in win api) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#116496 - estebank:question-method-chain-context, r=compiler-errors Provide context when `?` can't be called because of `Result<_, E>` When a method chain ending in `?` causes an E0277 because the expression's `Result::Err` variant doesn't have a type that can be converted to the `Result<_, E>` type parameter in the return type, provide additional context of which parts of the chain can and can't support the `?` operator. ``` error[E0277]: `?` couldn't convert the error to `String` --> $DIR/question-mark-result-err-mismatch.rs:27:25 | LL | fn bar() -> Result<(), String> { | ------------------ expected `String` because of this LL | let x = foo(); | ----- this has type `Result<_, String>` ... LL | .map_err(|_| ())?; | ---------------^ the trait `From<()>` is not implemented for `String` | | | this can't be annotated with `?` because it has type `Result<_, ()>` | = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait = help: the following other types implement trait `From<T>`: <String as From<char>> <String as From<Box<str>>> <String as From<Cow<'a, str>>> <String as From<&str>> <String as From<&mut str>> <String as From<&String>> = note: required for `Result<(), String>` to implement `FromResidual<Result<Infallible, ()>>` ``` Fix rust-lang#72124.
When a method chain ending in
?
causes an E0277 because the expression'sResult::Err
variant doesn't have a type that can be converted to theResult<_, E>
type parameter in the return type, provide additional context of which parts of the chain can and can't support the?
operator.Fix #72124.