Skip to content

Extend rustc_on_implemented to improve more ? error messages #85596

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

Merged
merged 1 commit into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
};
let name = param.name;
flags.push((name, Some(value)));

if let GenericParamDefKind::Type { .. } = param.kind {
let param_ty = trait_ref.substs[param.index as usize].expect_ty();
if let Some(def) = param_ty.ty_adt_def() {
// We also want to be able to select the parameter's
// original signature with no type arguments resolved
flags.push((name, Some(self.tcx.type_of(def.did).to_string())));
}
}
}

if let Some(true) = self_ty.ty_adt_def().map(|def| def.did.is_local()) {
Expand Down
29 changes: 27 additions & 2 deletions library/core/src/ops/try_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,18 @@ pub trait Try: FromResidual {
label = "this `?` produces `{R}`, which is incompatible with `{Self}`",
enclosing_scope = "this function returns a `Result`"
),
on(
all(
from_method = "from_residual",
from_desugaring = "QuestionMark",
_Self = "std::option::Option<T>",
R = "std::result::Result<T, E>",
),
message = "the `?` operator can only be used on `Option`s, not `Result`s, \
in {ItemContext} that returns `Option`",
label = "use `.ok()?` if you want to discard the `{R}` error information",
enclosing_scope = "this function returns an `Option`"
),
on(
all(
from_method = "from_residual",
Expand All @@ -272,13 +284,26 @@ pub trait Try: FromResidual {
from_method = "from_residual",
from_desugaring = "QuestionMark",
_Self = "std::ops::ControlFlow<B, C>",
R = "std::ops::ControlFlow<B, C>",
),
message = "the `?` operator can only be used on `ControlFlow<B, _>`s \
in {ItemContext} that returns `ControlFlow<B, _>`",
message = "the `?` operator in {ItemContext} that returns `ControlFlow<B, _>` \
can only be used on other `ControlFlow<B, _>`s (with the same Break type)",
label = "this `?` produces `{R}`, which is incompatible with `{Self}`",
enclosing_scope = "this function returns a `ControlFlow`",
note = "unlike `Result`, there's no `From`-conversion performed for `ControlFlow`"
),
on(
all(
from_method = "from_residual",
from_desugaring = "QuestionMark",
_Self = "std::ops::ControlFlow<B, C>",
// `R` is not a `ControlFlow`, as that case was matched previously
),
message = "the `?` operator can only be used on `ControlFlow`s \
in {ItemContext} that returns `ControlFlow`",
label = "this `?` produces `{R}`, which is incompatible with `{Self}`",
enclosing_scope = "this function returns a `ControlFlow`",
),
on(
all(
from_method = "from_residual",
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/try-trait/bad-interconversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn control_flow_to_result() -> Result<u64, String> {

fn result_to_option() -> Option<u16> {
Some(Err("hello")?)
//~^ ERROR the `?` operator can only be used on `Option`s in a function that returns `Option`
//~^ ERROR the `?` operator can only be used on `Option`s, not `Result`s, in a function that returns `Option`
}

fn control_flow_to_option() -> Option<u64> {
Expand All @@ -30,18 +30,18 @@ fn control_flow_to_option() -> Option<u64> {

fn result_to_control_flow() -> ControlFlow<String> {
ControlFlow::Continue(Err("hello")?)
//~^ ERROR the `?` operator can only be used on `ControlFlow<B, _>`s in a function that returns `ControlFlow<B, _>`
//~^ ERROR the `?` operator can only be used on `ControlFlow`s in a function that returns `ControlFlow`
}

fn option_to_control_flow() -> ControlFlow<u64> {
Some(3)?;
//~^ ERROR the `?` operator can only be used on `ControlFlow<B, _>`s in a function that returns `ControlFlow<B, _>`
//~^ ERROR the `?` operator can only be used on `ControlFlow`s in a function that returns `ControlFlow`
ControlFlow::Break(10)
}

fn control_flow_to_control_flow() -> ControlFlow<i64> {
ControlFlow::Break(4_u8)?;
//~^ ERROR the `?` operator can only be used on `ControlFlow<B, _>`s in a function that returns `ControlFlow<B, _>`
//~^ ERROR the `?` operator in a function that returns `ControlFlow<B, _>` can only be used on other `ControlFlow<B, _>`s
ControlFlow::Continue(())
}

Expand Down
12 changes: 5 additions & 7 deletions src/test/ui/try-trait/bad-interconversion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ LL | | }
= help: the trait `FromResidual<ControlFlow<{integer}, Infallible>>` is not implemented for `Result<u64, String>`
= note: required by `from_residual`

error[E0277]: the `?` operator can only be used on `Option`s in a function that returns `Option`
error[E0277]: the `?` operator can only be used on `Option`s, not `Result`s, in a function that returns `Option`
--> $DIR/bad-interconversion.rs:22:22
|
LL | / fn result_to_option() -> Option<u16> {
LL | | Some(Err("hello")?)
| | ^ this `?` produces `Result<Infallible, &str>`, which is incompatible with `Option<u16>`
| | ^ use `.ok()?` if you want to discard the `Result<Infallible, &str>` error information
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be clearer if we mentioned something like if you want to discard the Result::Err(&str) or something along those lines 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the residual being "coloured" with the Try type does make it harder to put in this error message nicely. I guess it would be less bad with just Result<!, &str>, but Infallible is long enough to be distracting. 🤷

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm more worried about confusing newcomers than anything else, anyone that already knows about Result, Option and .ok() wouldn't be confused regardless of wording, but someone who doesn't yet have a good grasp of what an enum even or that hasn't internalized Result::Err is the error information might be helped with things being more spelled out. Either way, I'm ok with the current wording.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have been looking at the quadratic growth of the on-unimplemented attribute here and been wondering if it should turn into custom code for it at some point. If at some point in the future you get inspired with a great idea for how these error should look, feel free to assign me an issue. (Or, of course, just do it yourself if that's easier that answering my inevitable noob questions 😅)

LL | |
LL | | }
| |_- this function returns an `Option`
Expand All @@ -66,7 +66,7 @@ LL | | }
= help: the trait `FromResidual<ControlFlow<{integer}, Infallible>>` is not implemented for `Option<u64>`
= note: required by `from_residual`

error[E0277]: the `?` operator can only be used on `ControlFlow<B, _>`s in a function that returns `ControlFlow<B, _>`
error[E0277]: the `?` operator can only be used on `ControlFlow`s in a function that returns `ControlFlow`
--> $DIR/bad-interconversion.rs:32:39
|
LL | / fn result_to_control_flow() -> ControlFlow<String> {
Expand All @@ -77,10 +77,9 @@ LL | | }
| |_- this function returns a `ControlFlow`
|
= help: the trait `FromResidual<Result<Infallible, &str>>` is not implemented for `ControlFlow<String>`
= note: unlike `Result`, there's no `From`-conversion performed for `ControlFlow`
= note: required by `from_residual`

error[E0277]: the `?` operator can only be used on `ControlFlow<B, _>`s in a function that returns `ControlFlow<B, _>`
error[E0277]: the `?` operator can only be used on `ControlFlow`s in a function that returns `ControlFlow`
--> $DIR/bad-interconversion.rs:37:12
|
LL | / fn option_to_control_flow() -> ControlFlow<u64> {
Expand All @@ -92,10 +91,9 @@ LL | | }
| |_- this function returns a `ControlFlow`
|
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `ControlFlow<u64>`
= note: unlike `Result`, there's no `From`-conversion performed for `ControlFlow`
= note: required by `from_residual`

error[E0277]: the `?` operator can only be used on `ControlFlow<B, _>`s in a function that returns `ControlFlow<B, _>`
error[E0277]: the `?` operator in a function that returns `ControlFlow<B, _>` can only be used on other `ControlFlow<B, _>`s (with the same Break type)
--> $DIR/bad-interconversion.rs:43:29
|
LL | / fn control_flow_to_control_flow() -> ControlFlow<i64> {
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/try-trait/option-to-result.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ LL | | }
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `Result<(), ()>`
= note: required by `from_residual`

error[E0277]: the `?` operator can only be used on `Option`s in a function that returns `Option`
error[E0277]: the `?` operator can only be used on `Option`s, not `Result`s, in a function that returns `Option`
--> $DIR/option-to-result.rs:11:6
|
LL | / fn test_option() -> Option<i32>{
LL | | let a:Result<i32, i32> = Ok(5);
LL | | a?;
| | ^ this `?` produces `Result<Infallible, i32>`, which is incompatible with `Option<i32>`
| | ^ use `.ok()?` if you want to discard the `Result<Infallible, i32>` error information
LL | | Some(5)
LL | | }
| |_- this function returns an `Option`
Expand Down