@@ -743,6 +743,36 @@ 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+ scrut_hir_id,
749+ hir:: MatchSource :: TryDesugar ,
750+ ) => {
751+ if let Some ( ty:: error:: ExpectedFound { expected, .. } ) = exp_found {
752+ let scrut_expr = self . tcx . hir ( ) . expect_expr ( scrut_hir_id) ;
753+ let scrut_ty = if let hir:: ExprKind :: Call ( _, args) = & scrut_expr. kind {
754+ let arg_expr = args. first ( ) . expect ( "try desugaring call w/out arg" ) ;
755+ self . typeck_results . as_ref ( ) . and_then ( |typeck_results| {
756+ typeck_results. expr_ty_opt ( arg_expr)
757+ } )
758+ } else {
759+ bug ! ( "try desugaring w/out call expr as scrutinee" ) ;
760+ } ;
761+
762+ match scrut_ty {
763+ Some ( ty) if expected == ty => {
764+ let source_map = self . tcx . sess . source_map ( ) ;
765+ err. span_suggestion (
766+ source_map. end_point ( cause. span ( ) ) ,
767+ "try removing this `?`" ,
768+ "" ,
769+ Applicability :: MachineApplicable ,
770+ ) ;
771+ }
772+ _ => { }
773+ }
774+ }
775+ } ,
746776 ObligationCauseCode :: MatchExpressionArm ( box MatchExpressionArmCause {
747777 arm_block_id,
748778 arm_span,
@@ -1973,7 +2003,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
19732003 trace : & TypeTrace < ' tcx > ,
19742004 terr : TypeError < ' tcx > ,
19752005 ) -> Vec < TypeErrorAdditionalDiags > {
1976- use crate :: traits:: ObligationCauseCode :: MatchExpressionArm ;
2006+ use crate :: traits:: ObligationCauseCode :: { BlockTailExpression , MatchExpressionArm } ;
19772007 let mut suggestions = Vec :: new ( ) ;
19782008 let span = trace. cause . span ( ) ;
19792009 let values = self . resolve_vars_if_possible ( trace. values ) ;
@@ -1991,11 +2021,17 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
19912021 // specify a byte literal
19922022 ( ty:: Uint ( ty:: UintTy :: U8 ) , ty:: Char ) => {
19932023 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
2024+ && let Some ( code) =
2025+ code. strip_prefix ( '\'' ) . and_then ( |s| s. strip_suffix ( '\'' ) )
2026+ // forbid all Unicode escapes
2027+ && !code. starts_with ( "\\ u" )
2028+ // forbids literal Unicode characters beyond ASCII
2029+ && code. chars ( ) . next ( ) . is_some_and ( |c| c. is_ascii ( ) )
19972030 {
1998- suggestions. push ( TypeErrorAdditionalDiags :: MeantByteLiteral { span, code : escape_literal ( code) } )
2031+ suggestions. push ( TypeErrorAdditionalDiags :: MeantByteLiteral {
2032+ span,
2033+ code : escape_literal ( code) ,
2034+ } )
19992035 }
20002036 }
20012037 // If a character was expected and the found expression is a string literal
@@ -2006,7 +2042,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
20062042 && let Some ( code) = code. strip_prefix ( '"' ) . and_then ( |s| s. strip_suffix ( '"' ) )
20072043 && code. chars ( ) . count ( ) == 1
20082044 {
2009- suggestions. push ( TypeErrorAdditionalDiags :: MeantCharLiteral { span, code : escape_literal ( code) } )
2045+ suggestions. push ( TypeErrorAdditionalDiags :: MeantCharLiteral {
2046+ span,
2047+ code : escape_literal ( code) ,
2048+ } )
20102049 }
20112050 }
20122051 // If a string was expected and the found expression is a character literal,
@@ -2016,7 +2055,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
20162055 if let Some ( code) =
20172056 code. strip_prefix ( '\'' ) . and_then ( |s| s. strip_suffix ( '\'' ) )
20182057 {
2019- suggestions. push ( TypeErrorAdditionalDiags :: MeantStrLiteral { span, code : escape_literal ( code) } )
2058+ suggestions. push ( TypeErrorAdditionalDiags :: MeantStrLiteral {
2059+ span,
2060+ code : escape_literal ( code) ,
2061+ } )
20202062 }
20212063 }
20222064 }
@@ -2025,17 +2067,24 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
20252067 ( ty:: Bool , ty:: Tuple ( list) ) => if list. len ( ) == 0 {
20262068 suggestions. extend ( self . suggest_let_for_letchains ( & trace. cause , span) ) ;
20272069 }
2028- ( ty:: Array ( _, _) , ty:: Array ( _, _) ) => suggestions. extend ( self . suggest_specify_actual_length ( terr, trace, span) ) ,
2070+ ( ty:: Array ( _, _) , ty:: Array ( _, _) ) => {
2071+ suggestions. extend ( self . suggest_specify_actual_length ( terr, trace, span) )
2072+ }
20292073 _ => { }
20302074 }
20312075 }
20322076 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- }
2077+ if let & ( MatchExpressionArm ( box MatchExpressionArmCause { source, .. } )
2078+ | BlockTailExpression ( .., source)
2079+ ) = code
2080+ && let hir:: MatchSource :: TryDesugar = source
2081+ && let Some ( ( expected_ty, found_ty, _, _) ) = self . values_str ( trace. values )
2082+ {
2083+ suggestions. push ( TypeErrorAdditionalDiags :: TryCannotConvert {
2084+ found : found_ty. content ( ) ,
2085+ expected : expected_ty. content ( ) ,
2086+ } ) ;
2087+ }
20392088 suggestions
20402089 }
20412090
@@ -2905,6 +2954,9 @@ impl<'tcx> ObligationCauseExt<'tcx> for ObligationCause<'tcx> {
29052954 CompareImplItemObligation { kind : ty:: AssocKind :: Const , .. } => {
29062955 ObligationCauseFailureCode :: ConstCompat { span, subdiags }
29072956 }
2957+ BlockTailExpression ( .., hir:: MatchSource :: TryDesugar ) => {
2958+ ObligationCauseFailureCode :: TryCompat { span, subdiags }
2959+ }
29082960 MatchExpressionArm ( box MatchExpressionArmCause { source, .. } ) => match source {
29092961 hir:: MatchSource :: TryDesugar => {
29102962 ObligationCauseFailureCode :: TryCompat { span, subdiags }
0 commit comments