From caf0c1bb1cb939ab47f6216f0f3482a7da69da4e Mon Sep 17 00:00:00 2001 From: Esteban Kuber Date: Tue, 16 Nov 2021 02:17:57 +0000 Subject: [PATCH] Reduce verbosity for `?` on non-`Try` expressions --- compiler/rustc_middle/src/traits/mod.rs | 2 ++ .../src/traits/error_reporting/suggestions.rs | 1 + .../rustc_typeck/src/check/fn_ctxt/_impl.rs | 2 +- library/core/src/ops/try_trait.rs | 18 ++++-------------- 4 files changed, 8 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_middle/src/traits/mod.rs b/compiler/rustc_middle/src/traits/mod.rs index b6b88f6e551..079899ac494 100644 --- a/compiler/rustc_middle/src/traits/mod.rs +++ b/compiler/rustc_middle/src/traits/mod.rs @@ -352,6 +352,8 @@ pub enum ObligationCauseCode<'tcx> { ForLoopIterator, + QuestionMark, + /// Well-formed checking. If a `WellFormedLoc` is provided, /// then it will be used to eprform HIR-based wf checking /// after an error occurs, in order to generate a more precise error span. diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 20e0631b66b..9893e5143fc 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -1964,6 +1964,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { | ObligationCauseCode::BlockTailExpression(_) | ObligationCauseCode::AwaitableExpr | ObligationCauseCode::ForLoopIterator + | ObligationCauseCode::QuestionMark | ObligationCauseCode::LetElse => {} ObligationCauseCode::SliceOrArrayElem => { err.note("slice and array elements must have `Sized` type"); diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs b/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs index f51ab140a25..d45d76e7509 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs @@ -811,7 +811,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { match lang_item { hir::LangItem::FuturePoll => ObligationCauseCode::AwaitableExpr, hir::LangItem::IntoIterIntoIter => ObligationCauseCode::ForLoopIterator, - // FIXME: This could also be used for `?`. See if there are others. + hir::LangItem::TryTraitBranch => ObligationCauseCode::QuestionMark, _ => traits::ItemObligation(def_id), }, ); diff --git a/library/core/src/ops/try_trait.rs b/library/core/src/ops/try_trait.rs index f4f0a589809..6a414ae8c4b 100644 --- a/library/core/src/ops/try_trait.rs +++ b/library/core/src/ops/try_trait.rs @@ -115,15 +115,14 @@ use crate::ops::ControlFlow; #[unstable(feature = "try_trait_v2", issue = "84277")] #[rustc_on_unimplemented( on( - all(from_method = "from_output", from_desugaring = "TryBlock"), + all(from_desugaring = "TryBlock"), message = "a `try` block must return `Result` or `Option` \ (or another type that implements `{Try}`)", label = "could not wrap the final value of the block as `{Self}` doesn't implement `Try`", ), on( - all(from_method = "branch", from_desugaring = "QuestionMark"), - message = "the `?` operator can only be applied to values \ - that implement `{Try}`", + all(from_desugaring = "QuestionMark"), + message = "the `?` operator can only be applied to values that implement `{Try}`", label = "the `?` operator cannot be applied to type `{Self}`" ) )] @@ -226,7 +225,6 @@ pub trait Try: FromResidual { #[rustc_on_unimplemented( on( all( - from_method = "from_residual", from_desugaring = "QuestionMark", _Self = "std::result::Result", R = "std::option::Option" @@ -238,7 +236,6 @@ pub trait Try: FromResidual { ), on( all( - from_method = "from_residual", from_desugaring = "QuestionMark", _Self = "std::result::Result", ), @@ -252,7 +249,6 @@ pub trait Try: FromResidual { ), on( all( - from_method = "from_residual", from_desugaring = "QuestionMark", _Self = "std::option::Option", R = "std::result::Result", @@ -264,7 +260,6 @@ pub trait Try: FromResidual { ), on( all( - from_method = "from_residual", from_desugaring = "QuestionMark", _Self = "std::option::Option", ), @@ -277,7 +272,6 @@ pub trait Try: FromResidual { ), on( all( - from_method = "from_residual", from_desugaring = "QuestionMark", _Self = "std::ops::ControlFlow", R = "std::ops::ControlFlow", @@ -290,7 +284,6 @@ pub trait Try: FromResidual { ), on( all( - from_method = "from_residual", from_desugaring = "QuestionMark", _Self = "std::ops::ControlFlow", // `R` is not a `ControlFlow`, as that case was matched previously @@ -301,10 +294,7 @@ pub trait Try: FromResidual { enclosing_scope = "this function returns a `ControlFlow`", ), on( - all( - from_method = "from_residual", - from_desugaring = "QuestionMark" - ), + all(from_desugaring = "QuestionMark"), message = "the `?` operator can only be used in {ItemContext} \ that returns `Result` or `Option` \ (or another type that implements `{FromResidual}`)",