@@ -743,6 +743,35 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
743743 ObligationCauseCode :: Pattern { origin_expr : false , span : Some ( span) , .. } => {
744744 err. span_label ( span, "expected due to this" ) ;
745745 }
746+ ObligationCauseCode :: BlockTailExpression (
747+ _,
748+ hir:: MatchSource :: TryDesugar ( scrut_hir_id) ,
749+ ) => {
750+ if let Some ( ty:: error:: ExpectedFound { expected, .. } ) = exp_found {
751+ let scrut_expr = self . tcx . hir ( ) . expect_expr ( scrut_hir_id) ;
752+ let scrut_ty = if let hir:: ExprKind :: Call ( _, args) = & scrut_expr. kind {
753+ let arg_expr = args. first ( ) . expect ( "try desugaring call w/out arg" ) ;
754+ self . typeck_results . as_ref ( ) . and_then ( |typeck_results| {
755+ typeck_results. expr_ty_opt ( arg_expr)
756+ } )
757+ } else {
758+ bug ! ( "try desugaring w/out call expr as scrutinee" ) ;
759+ } ;
760+
761+ match scrut_ty {
762+ Some ( ty) if expected == ty => {
763+ let source_map = self . tcx . sess . source_map ( ) ;
764+ err. span_suggestion (
765+ source_map. end_point ( cause. span ( ) ) ,
766+ "try removing this `?`" ,
767+ "" ,
768+ Applicability :: MachineApplicable ,
769+ ) ;
770+ }
771+ _ => { }
772+ }
773+ }
774+ } ,
746775 ObligationCauseCode :: MatchExpressionArm ( box MatchExpressionArmCause {
747776 arm_block_id,
748777 arm_span,
@@ -752,12 +781,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
752781 prior_arm_ty,
753782 source,
754783 ref prior_arms,
755- scrut_hir_id,
756784 opt_suggest_box_span,
757785 scrut_span,
758786 ..
759787 } ) => match source {
760- hir:: MatchSource :: TryDesugar => {
788+ hir:: MatchSource :: TryDesugar ( scrut_hir_id ) => {
761789 if let Some ( ty:: error:: ExpectedFound { expected, .. } ) = exp_found {
762790 let scrut_expr = self . tcx . hir ( ) . expect_expr ( scrut_hir_id) ;
763791 let scrut_ty = if let hir:: ExprKind :: Call ( _, args) = & scrut_expr. kind {
@@ -1973,7 +2001,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
19732001 trace : & TypeTrace < ' tcx > ,
19742002 terr : TypeError < ' tcx > ,
19752003 ) -> Vec < TypeErrorAdditionalDiags > {
1976- use crate :: traits:: ObligationCauseCode :: MatchExpressionArm ;
2004+ use crate :: traits:: ObligationCauseCode :: { BlockTailExpression , MatchExpressionArm } ;
19772005 let mut suggestions = Vec :: new ( ) ;
19782006 let span = trace. cause . span ( ) ;
19792007 let values = self . resolve_vars_if_possible ( trace. values ) ;
@@ -1991,11 +2019,17 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
19912019 // specify a byte literal
19922020 ( ty:: Uint ( ty:: UintTy :: U8 ) , ty:: Char ) => {
19932021 if let Ok ( code) = self . tcx . sess ( ) . source_map ( ) . span_to_snippet ( span)
1994- && let Some ( code) = code. strip_prefix ( '\'' ) . and_then ( |s| s. strip_suffix ( '\'' ) )
1995- && !code. starts_with ( "\\ u" ) // forbid all Unicode escapes
1996- && code. chars ( ) . next ( ) . is_some_and ( |c| c. is_ascii ( ) ) // forbids literal Unicode characters beyond ASCII
2022+ && let Some ( code) =
2023+ code. strip_prefix ( '\'' ) . and_then ( |s| s. strip_suffix ( '\'' ) )
2024+ // forbid all Unicode escapes
2025+ && !code. starts_with ( "\\ u" )
2026+ // forbids literal Unicode characters beyond ASCII
2027+ && code. chars ( ) . next ( ) . is_some_and ( |c| c. is_ascii ( ) )
19972028 {
1998- suggestions. push ( TypeErrorAdditionalDiags :: MeantByteLiteral { span, code : escape_literal ( code) } )
2029+ suggestions. push ( TypeErrorAdditionalDiags :: MeantByteLiteral {
2030+ span,
2031+ code : escape_literal ( code) ,
2032+ } )
19992033 }
20002034 }
20012035 // If a character was expected and the found expression is a string literal
@@ -2006,7 +2040,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
20062040 && let Some ( code) = code. strip_prefix ( '"' ) . and_then ( |s| s. strip_suffix ( '"' ) )
20072041 && code. chars ( ) . count ( ) == 1
20082042 {
2009- suggestions. push ( TypeErrorAdditionalDiags :: MeantCharLiteral { span, code : escape_literal ( code) } )
2043+ suggestions. push ( TypeErrorAdditionalDiags :: MeantCharLiteral {
2044+ span,
2045+ code : escape_literal ( code) ,
2046+ } )
20102047 }
20112048 }
20122049 // If a string was expected and the found expression is a character literal,
@@ -2016,7 +2053,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
20162053 if let Some ( code) =
20172054 code. strip_prefix ( '\'' ) . and_then ( |s| s. strip_suffix ( '\'' ) )
20182055 {
2019- suggestions. push ( TypeErrorAdditionalDiags :: MeantStrLiteral { span, code : escape_literal ( code) } )
2056+ suggestions. push ( TypeErrorAdditionalDiags :: MeantStrLiteral {
2057+ span,
2058+ code : escape_literal ( code) ,
2059+ } )
20202060 }
20212061 }
20222062 }
@@ -2025,17 +2065,24 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
20252065 ( ty:: Bool , ty:: Tuple ( list) ) => if list. len ( ) == 0 {
20262066 suggestions. extend ( self . suggest_let_for_letchains ( & trace. cause , span) ) ;
20272067 }
2028- ( ty:: Array ( _, _) , ty:: Array ( _, _) ) => suggestions. extend ( self . suggest_specify_actual_length ( terr, trace, span) ) ,
2068+ ( ty:: Array ( _, _) , ty:: Array ( _, _) ) => {
2069+ suggestions. extend ( self . suggest_specify_actual_length ( terr, trace, span) )
2070+ }
20292071 _ => { }
20302072 }
20312073 }
20322074 let code = trace. cause . code ( ) ;
2033- if let & MatchExpressionArm ( box MatchExpressionArmCause { source, .. } ) = code
2034- && let hir:: MatchSource :: TryDesugar = source
2035- && let Some ( ( expected_ty, found_ty, _, _) ) = self . values_str ( trace. values )
2036- {
2037- suggestions. push ( TypeErrorAdditionalDiags :: TryCannotConvert { found : found_ty. content ( ) , expected : expected_ty. content ( ) } ) ;
2038- }
2075+ if let & ( MatchExpressionArm ( box MatchExpressionArmCause { source, .. } )
2076+ | BlockTailExpression ( .., source)
2077+ ) = code
2078+ && let hir:: MatchSource :: TryDesugar ( _) = source
2079+ && let Some ( ( expected_ty, found_ty, _, _) ) = self . values_str ( trace. values )
2080+ {
2081+ suggestions. push ( TypeErrorAdditionalDiags :: TryCannotConvert {
2082+ found : found_ty. content ( ) ,
2083+ expected : expected_ty. content ( ) ,
2084+ } ) ;
2085+ }
20392086 suggestions
20402087 }
20412088
@@ -2905,8 +2952,11 @@ impl<'tcx> ObligationCauseExt<'tcx> for ObligationCause<'tcx> {
29052952 CompareImplItemObligation { kind : ty:: AssocKind :: Const , .. } => {
29062953 ObligationCauseFailureCode :: ConstCompat { span, subdiags }
29072954 }
2955+ BlockTailExpression ( .., hir:: MatchSource :: TryDesugar ( _) ) => {
2956+ ObligationCauseFailureCode :: TryCompat { span, subdiags }
2957+ }
29082958 MatchExpressionArm ( box MatchExpressionArmCause { source, .. } ) => match source {
2909- hir:: MatchSource :: TryDesugar => {
2959+ hir:: MatchSource :: TryDesugar ( _ ) => {
29102960 ObligationCauseFailureCode :: TryCompat { span, subdiags }
29112961 }
29122962 _ => ObligationCauseFailureCode :: MatchCompat { span, subdiags } ,
0 commit comments