@@ -743,6 +743,36 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
743
743
ObligationCauseCode :: Pattern { origin_expr : false , span : Some ( span) , .. } => {
744
744
err. span_label ( span, "expected due to this" ) ;
745
745
}
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
+ } ,
746
776
ObligationCauseCode :: MatchExpressionArm ( box MatchExpressionArmCause {
747
777
arm_block_id,
748
778
arm_span,
@@ -1973,7 +2003,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
1973
2003
trace : & TypeTrace < ' tcx > ,
1974
2004
terr : TypeError < ' tcx > ,
1975
2005
) -> Vec < TypeErrorAdditionalDiags > {
1976
- use crate :: traits:: ObligationCauseCode :: MatchExpressionArm ;
2006
+ use crate :: traits:: ObligationCauseCode :: { BlockTailExpression , MatchExpressionArm } ;
1977
2007
let mut suggestions = Vec :: new ( ) ;
1978
2008
let span = trace. cause . span ( ) ;
1979
2009
let values = self . resolve_vars_if_possible ( trace. values ) ;
@@ -1991,11 +2021,17 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
1991
2021
// specify a byte literal
1992
2022
( ty:: Uint ( ty:: UintTy :: U8 ) , ty:: Char ) => {
1993
2023
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 ( ) )
1997
2030
{
1998
- suggestions. push ( TypeErrorAdditionalDiags :: MeantByteLiteral { span, code : escape_literal ( code) } )
2031
+ suggestions. push ( TypeErrorAdditionalDiags :: MeantByteLiteral {
2032
+ span,
2033
+ code : escape_literal ( code) ,
2034
+ } )
1999
2035
}
2000
2036
}
2001
2037
// If a character was expected and the found expression is a string literal
@@ -2006,7 +2042,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
2006
2042
&& let Some ( code) = code. strip_prefix ( '"' ) . and_then ( |s| s. strip_suffix ( '"' ) )
2007
2043
&& code. chars ( ) . count ( ) == 1
2008
2044
{
2009
- suggestions. push ( TypeErrorAdditionalDiags :: MeantCharLiteral { span, code : escape_literal ( code) } )
2045
+ suggestions. push ( TypeErrorAdditionalDiags :: MeantCharLiteral {
2046
+ span,
2047
+ code : escape_literal ( code) ,
2048
+ } )
2010
2049
}
2011
2050
}
2012
2051
// If a string was expected and the found expression is a character literal,
@@ -2016,7 +2055,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
2016
2055
if let Some ( code) =
2017
2056
code. strip_prefix ( '\'' ) . and_then ( |s| s. strip_suffix ( '\'' ) )
2018
2057
{
2019
- suggestions. push ( TypeErrorAdditionalDiags :: MeantStrLiteral { span, code : escape_literal ( code) } )
2058
+ suggestions. push ( TypeErrorAdditionalDiags :: MeantStrLiteral {
2059
+ span,
2060
+ code : escape_literal ( code) ,
2061
+ } )
2020
2062
}
2021
2063
}
2022
2064
}
@@ -2025,17 +2067,24 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
2025
2067
( ty:: Bool , ty:: Tuple ( list) ) => if list. len ( ) == 0 {
2026
2068
suggestions. extend ( self . suggest_let_for_letchains ( & trace. cause , span) ) ;
2027
2069
}
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
+ }
2029
2073
_ => { }
2030
2074
}
2031
2075
}
2032
2076
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
+ }
2039
2088
suggestions
2040
2089
}
2041
2090
@@ -2905,6 +2954,9 @@ impl<'tcx> ObligationCauseExt<'tcx> for ObligationCause<'tcx> {
2905
2954
CompareImplItemObligation { kind : ty:: AssocKind :: Const , .. } => {
2906
2955
ObligationCauseFailureCode :: ConstCompat { span, subdiags }
2907
2956
}
2957
+ BlockTailExpression ( .., hir:: MatchSource :: TryDesugar ) => {
2958
+ ObligationCauseFailureCode :: TryCompat { span, subdiags }
2959
+ }
2908
2960
MatchExpressionArm ( box MatchExpressionArmCause { source, .. } ) => match source {
2909
2961
hir:: MatchSource :: TryDesugar => {
2910
2962
ObligationCauseFailureCode :: TryCompat { span, subdiags }
0 commit comments