Skip to content

Commit 092166e

Browse files
committed
Auto merge of rust-lang#85596 - scottmcm:more-on-unimplemented, r=estebank
Extend `rustc_on_implemented` to improve more `?` error messages `_Self` could match the generic definition; this adds that functionality for matching the generic definition of type parameters too. Your advice welcome on the wording of all these messages, and which things belong in the message/label/note. r? `@estebank`
2 parents e5d8bc2 + 058a8a4 commit 092166e

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

core/src/ops/try_trait.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,18 @@ pub trait Try: FromResidual {
254254
label = "this `?` produces `{R}`, which is incompatible with `{Self}`",
255255
enclosing_scope = "this function returns a `Result`"
256256
),
257+
on(
258+
all(
259+
from_method = "from_residual",
260+
from_desugaring = "QuestionMark",
261+
_Self = "std::option::Option<T>",
262+
R = "std::result::Result<T, E>",
263+
),
264+
message = "the `?` operator can only be used on `Option`s, not `Result`s, \
265+
in {ItemContext} that returns `Option`",
266+
label = "use `.ok()?` if you want to discard the `{R}` error information",
267+
enclosing_scope = "this function returns an `Option`"
268+
),
257269
on(
258270
all(
259271
from_method = "from_residual",
@@ -272,13 +284,26 @@ pub trait Try: FromResidual {
272284
from_method = "from_residual",
273285
from_desugaring = "QuestionMark",
274286
_Self = "std::ops::ControlFlow<B, C>",
287+
R = "std::ops::ControlFlow<B, C>",
275288
),
276-
message = "the `?` operator can only be used on `ControlFlow<B, _>`s \
277-
in {ItemContext} that returns `ControlFlow<B, _>`",
289+
message = "the `?` operator in {ItemContext} that returns `ControlFlow<B, _>` \
290+
can only be used on other `ControlFlow<B, _>`s (with the same Break type)",
278291
label = "this `?` produces `{R}`, which is incompatible with `{Self}`",
279292
enclosing_scope = "this function returns a `ControlFlow`",
280293
note = "unlike `Result`, there's no `From`-conversion performed for `ControlFlow`"
281294
),
295+
on(
296+
all(
297+
from_method = "from_residual",
298+
from_desugaring = "QuestionMark",
299+
_Self = "std::ops::ControlFlow<B, C>",
300+
// `R` is not a `ControlFlow`, as that case was matched previously
301+
),
302+
message = "the `?` operator can only be used on `ControlFlow`s \
303+
in {ItemContext} that returns `ControlFlow`",
304+
label = "this `?` produces `{R}`, which is incompatible with `{Self}`",
305+
enclosing_scope = "this function returns a `ControlFlow`",
306+
),
282307
on(
283308
all(
284309
from_method = "from_residual",

0 commit comments

Comments
 (0)