Skip to content

Provide better compiler output when using ? on Option in fn returning Result or vice-versa? #71089

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
alvinhochun opened this issue Apr 13, 2020 · 3 comments · Fixed by #71141
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. F-on_unimplemented Error messages that can be tackled with `#[rustc_on_unimplemented]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@alvinhochun
Copy link

alvinhochun commented Apr 13, 2020

It seems that rustc should provide a better error message or hint when one tries to use the ? operator on an Option in a function returning a Result or vice-versa.

Currently this is the error you get:

error[E0277]: `?` couldn't convert the error to `MyErrorType`
  --> src/main.rs:10:19
   |
10 |     let a = foo(i)?;
   |                   ^ the trait `std::convert::From<std::option::NoneError>` is not implemented for `MyErrorType`
   |
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = note: required by `std::convert::From::from`

However, it would be better to suggest the use of Option::ok_or or Option::ok_or_else, or Result::ok, instead of referencing something dependent on the try_trait feature (NoneError).

This issue has been assigned to @Duddino via this comment.

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 13, 2020
@estebank estebank added the F-on_unimplemented Error messages that can be tackled with `#[rustc_on_unimplemented]` label Apr 14, 2020
@estebank
Copy link
Contributor

This error is emitted here:

let is_try = self
.tcx
.sess
.source_map()
.span_to_snippet(span)
.map(|s| &s == "?")
.unwrap_or(false);
let is_from = format!("{}", trait_ref.print_only_trait_path())
.starts_with("std::convert::From<");
let (message, note) = if is_try && is_from {
(
Some(format!(
"`?` couldn't convert the error to `{}`",
trait_ref.self_ty(),
)),
Some(
"the question mark operation (`?`) implicitly performs a \
conversion on the error value using the `From` trait"
.to_owned(),
),
)
} else {
(message, note)
};
let mut err = struct_span_err!(
self.tcx.sess,
span,
E0277,
"{}",
message.unwrap_or_else(|| format!(
"the trait bound `{}` is not satisfied{}",
trait_ref.without_const().to_predicate(),
post_message,
))
);

We could look at whether the trait_ref is std::convert::From<std::option::NoneError>, and if so, provide a more targeted error, including a structured suggestion for ok_or_else.

@estebank estebank added the D-papercut Diagnostics: An error or lint that needs small tweaks. label Apr 14, 2020
@Duddino
Copy link

Duddino commented Apr 14, 2020

Can I claim this issue?

@Duddino
Copy link

Duddino commented Apr 14, 2020

@rustbot claim

@rustbot rustbot self-assigned this Apr 14, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 15, 2020
Provide better compiler output when using `?` on `Option` in fn returning `Result` and vice-versa

Fixes rust-lang#71089
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 15, 2020
Provide better compiler output when using `?` on `Option` in fn returning `Result` and vice-versa

Fixes rust-lang#71089
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 16, 2020
Provide better compiler output when using `?` on `Option` in fn returning `Result` and vice-versa

Fixes rust-lang#71089
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 16, 2020
Provide better compiler output when using `?` on `Option` in fn returning `Result` and vice-versa

Fixes rust-lang#71089
@bors bors closed this as completed in e4ec796 Apr 16, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-papercut Diagnostics: An error or lint that needs small tweaks. F-on_unimplemented Error messages that can be tackled with `#[rustc_on_unimplemented]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
5 participants