@@ -20,7 +20,7 @@ use rustc_hir as hir;
2020use rustc_hir:: def:: { CtorKind , DefKind , Res } ;
2121use rustc_hir:: def_id:: DefId ;
2222use rustc_hir:: lang_items:: LangItem ;
23- use rustc_hir:: { ExprKind , HirId , QPath , find_attr, is_range_literal} ;
23+ use rustc_hir:: { ConstContext , ExprKind , HirId , QPath , find_attr, is_range_literal} ;
2424use rustc_hir_analysis:: NoVariantNamed ;
2525use rustc_hir_analysis:: errors:: NoFieldOnType ;
2626use rustc_hir_analysis:: hir_ty_lowering:: HirTyLowerer as _;
@@ -44,8 +44,8 @@ use crate::errors::{
4444 AddressOfTemporaryTaken , BaseExpressionDoubleDot , BaseExpressionDoubleDotAddExpr ,
4545 BaseExpressionDoubleDotRemove , CantDereference , FieldMultiplySpecifiedInInitializer ,
4646 FunctionalRecordUpdateOnNonStruct , HelpUseLatestEdition , NakedAsmOutsideNakedFn ,
47- NoFieldOnVariant , ReturnLikeStatementKind , ReturnStmtOutsideOfFnBody , StructExprNonExhaustive ,
48- TypeMismatchFruTypo , YieldExprOutsideOfCoroutine ,
47+ NoFieldOnVariant , QuestionMarkInConst , ReturnLikeStatementKind , ReturnStmtOutsideOfFnBody ,
48+ StructExprNonExhaustive , TypeMismatchFruTypo , YieldExprOutsideOfCoroutine ,
4949} ;
5050use crate :: op:: contains_let_in_chain;
5151use crate :: {
@@ -862,12 +862,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
862862 expr : & ' tcx hir:: Expr < ' tcx > ,
863863 ) -> Ty < ' tcx > {
864864 if self . ret_coercion . is_none ( ) {
865- self . emit_return_outside_of_fn_body ( expr, ReturnLikeStatementKind :: Return ) ;
865+ let expectation = if let Some ( desugar_kind) = expr. span . desugaring_kind ( )
866+ && desugar_kind == DesugaringKind :: QuestionMark
867+ && let Some ( ccx) = self . tcx . hir_body_const_context ( self . body_id )
868+ && matches ! ( ccx, ConstContext :: Const { .. } | ConstContext :: Static ( _) )
869+ {
870+ let guaranteed = self
871+ . tcx
872+ . dcx ( )
873+ . emit_err ( QuestionMarkInConst { span : expr. span , keyword : ccx. keyword_name ( ) } ) ;
874+ // Suppresses incorrect and unnecessary "E0283: type annotations needed"
875+ ExpectHasType ( Ty :: new_error ( self . tcx , guaranteed) )
876+ } else {
877+ self . emit_return_outside_of_fn_body ( expr, ReturnLikeStatementKind :: Return ) ;
878+ NoExpectation
879+ } ;
866880
867881 if let Some ( e) = expr_opt {
868882 // We still have to type-check `e` (issue #86188), but calling
869883 // `check_return_expr` only works inside fn bodies.
870- self . check_expr ( e ) ;
884+ self . check_expr_with_expectation ( e , expectation ) ;
871885 }
872886 } else if let Some ( e) = expr_opt {
873887 if self . ret_coercion_span . get ( ) . is_none ( ) {
@@ -986,7 +1000,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9861000 ///
9871001 /// `expr` is the `return` (`become`) "statement", `kind` is the kind of the statement
9881002 /// either `Return` or `Become`.
989- fn emit_return_outside_of_fn_body ( & self , expr : & hir:: Expr < ' _ > , kind : ReturnLikeStatementKind ) {
1003+ fn emit_return_outside_of_fn_body (
1004+ & self ,
1005+ expr : & hir:: Expr < ' _ > ,
1006+ kind : ReturnLikeStatementKind ,
1007+ ) -> ErrorGuaranteed {
9901008 let mut err = ReturnStmtOutsideOfFnBody {
9911009 span : expr. span ,
9921010 encl_body_span : None ,
@@ -1027,7 +1045,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10271045 err. encl_fn_span = Some ( * encl_fn_span) ;
10281046 }
10291047
1030- self . dcx ( ) . emit_err ( err) ;
1048+ self . dcx ( ) . emit_err ( err)
10311049 }
10321050
10331051 fn point_at_return_for_opaque_ty_error (
0 commit comments