Skip to content

Commit f0608fc

Browse files
committed
Reduce verbosity for ? on non-Try expressions
1 parent f002610 commit f0608fc

File tree

4 files changed

+8
-15
lines changed

4 files changed

+8
-15
lines changed

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ pub enum ObligationCauseCode<'tcx> {
352352

353353
ForLoopIterator,
354354

355+
QuestionMark,
356+
355357
/// Well-formed checking. If a `WellFormedLoc` is provided,
356358
/// then it will be used to eprform HIR-based wf checking
357359
/// after an error occurs, in order to generate a more precise error span.

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,6 +1964,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
19641964
| ObligationCauseCode::BlockTailExpression(_)
19651965
| ObligationCauseCode::AwaitableExpr
19661966
| ObligationCauseCode::ForLoopIterator
1967+
| ObligationCauseCode::QuestionMark
19671968
| ObligationCauseCode::LetElse => {}
19681969
ObligationCauseCode::SliceOrArrayElem => {
19691970
err.note("slice and array elements must have `Sized` type");

compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
817817
match lang_item {
818818
hir::LangItem::FuturePoll => ObligationCauseCode::AwaitableExpr,
819819
hir::LangItem::IntoIterIntoIter => ObligationCauseCode::ForLoopIterator,
820-
// FIXME: This could also be used for `?`. See if there are others.
820+
hir::LangItem::TryTraitBranch => ObligationCauseCode::QuestionMark,
821821
_ => traits::ItemObligation(def_id),
822822
},
823823
);

library/core/src/ops/try_trait.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,14 @@ use crate::ops::ControlFlow;
115115
#[unstable(feature = "try_trait_v2", issue = "84277")]
116116
#[rustc_on_unimplemented(
117117
on(
118-
all(from_method = "from_output", from_desugaring = "TryBlock"),
118+
all(from_desugaring = "TryBlock"),
119119
message = "a `try` block must return `Result` or `Option` \
120120
(or another type that implements `{Try}`)",
121121
label = "could not wrap the final value of the block as `{Self}` doesn't implement `Try`",
122122
),
123123
on(
124-
all(from_method = "branch", from_desugaring = "QuestionMark"),
125-
message = "the `?` operator can only be applied to values \
126-
that implement `{Try}`",
124+
all(from_desugaring = "QuestionMark"),
125+
message = "the `?` operator can only be applied to values that implement `{Try}`",
127126
label = "the `?` operator cannot be applied to type `{Self}`"
128127
)
129128
)]
@@ -226,7 +225,6 @@ pub trait Try: FromResidual {
226225
#[rustc_on_unimplemented(
227226
on(
228227
all(
229-
from_method = "from_residual",
230228
from_desugaring = "QuestionMark",
231229
_Self = "std::result::Result<T, E>",
232230
R = "std::option::Option<std::convert::Infallible>"
@@ -238,7 +236,6 @@ pub trait Try: FromResidual {
238236
),
239237
on(
240238
all(
241-
from_method = "from_residual",
242239
from_desugaring = "QuestionMark",
243240
_Self = "std::result::Result<T, E>",
244241
),
@@ -252,7 +249,6 @@ pub trait Try: FromResidual {
252249
),
253250
on(
254251
all(
255-
from_method = "from_residual",
256252
from_desugaring = "QuestionMark",
257253
_Self = "std::option::Option<T>",
258254
R = "std::result::Result<T, E>",
@@ -264,7 +260,6 @@ pub trait Try: FromResidual {
264260
),
265261
on(
266262
all(
267-
from_method = "from_residual",
268263
from_desugaring = "QuestionMark",
269264
_Self = "std::option::Option<T>",
270265
),
@@ -277,7 +272,6 @@ pub trait Try: FromResidual {
277272
),
278273
on(
279274
all(
280-
from_method = "from_residual",
281275
from_desugaring = "QuestionMark",
282276
_Self = "std::ops::ControlFlow<B, C>",
283277
R = "std::ops::ControlFlow<B, C>",
@@ -290,7 +284,6 @@ pub trait Try: FromResidual {
290284
),
291285
on(
292286
all(
293-
from_method = "from_residual",
294287
from_desugaring = "QuestionMark",
295288
_Self = "std::ops::ControlFlow<B, C>",
296289
// `R` is not a `ControlFlow`, as that case was matched previously
@@ -301,10 +294,7 @@ pub trait Try: FromResidual {
301294
enclosing_scope = "this function returns a `ControlFlow`",
302295
),
303296
on(
304-
all(
305-
from_method = "from_residual",
306-
from_desugaring = "QuestionMark"
307-
),
297+
all(from_desugaring = "QuestionMark"),
308298
message = "the `?` operator can only be used in {ItemContext} \
309299
that returns `Result` or `Option` \
310300
(or another type that implements `{FromResidual}`)",

0 commit comments

Comments
 (0)