1+ use std:: mem;
2+
13use rustc_abi:: ExternAbi ;
24use rustc_ast:: visit:: AssocCtxt ;
35use rustc_ast:: * ;
@@ -345,6 +347,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
345347 body. as_deref ( ) ,
346348 attrs,
347349 contract. as_deref ( ) ,
350+ header. constness ,
348351 ) ;
349352
350353 let itctx = ImplTraitContext :: Universal ;
@@ -1024,6 +1027,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10241027 Some ( body) ,
10251028 attrs,
10261029 contract. as_deref ( ) ,
1030+ sig. header . constness ,
10271031 ) ;
10281032 let ( generics, sig) = self . lower_method_sig (
10291033 generics,
@@ -1217,6 +1221,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12171221 body. as_deref ( ) ,
12181222 attrs,
12191223 contract. as_deref ( ) ,
1224+ sig. header . constness ,
12201225 ) ;
12211226 let ( generics, sig) = self . lower_method_sig (
12221227 generics,
@@ -1346,11 +1351,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
13461351 f : impl FnOnce ( & mut Self ) -> ( & ' hir [ hir:: Param < ' hir > ] , hir:: Expr < ' hir > ) ,
13471352 ) -> hir:: BodyId {
13481353 let prev_coroutine_kind = self . coroutine_kind . take ( ) ;
1354+ let prev_is_in_const_context = mem:: take ( & mut self . is_in_const_context ) ;
13491355 let task_context = self . task_context . take ( ) ;
13501356 let ( parameters, result) = f ( self ) ;
13511357 let body_id = self . record_body ( parameters, result) ;
13521358 self . task_context = task_context;
13531359 self . coroutine_kind = prev_coroutine_kind;
1360+ self . is_in_const_context = prev_is_in_const_context;
13541361 body_id
13551362 }
13561363
@@ -1369,9 +1376,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
13691376 & mut self ,
13701377 decl : & FnDecl ,
13711378 contract : Option < & FnContract > ,
1379+ constness : Const ,
13721380 body : impl FnOnce ( & mut Self ) -> hir:: Expr < ' hir > ,
13731381 ) -> hir:: BodyId {
13741382 self . lower_body ( |this| {
1383+ if let Const :: Yes ( _) = constness {
1384+ this. is_in_const_context = true ;
1385+ }
13751386 let params =
13761387 this. arena . alloc_from_iter ( decl. inputs . iter ( ) . map ( |x| this. lower_param ( x) ) ) ;
13771388
@@ -1389,16 +1400,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
13891400 decl : & FnDecl ,
13901401 body : & Block ,
13911402 contract : Option < & FnContract > ,
1403+ constness : Const ,
13921404 ) -> hir:: BodyId {
1393- self . lower_fn_body ( decl, contract, |this| this. lower_block_expr ( body) )
1405+ self . lower_fn_body ( decl, contract, constness , |this| this. lower_block_expr ( body) )
13941406 }
13951407
13961408 pub ( super ) fn lower_const_body ( & mut self , span : Span , expr : Option < & Expr > ) -> hir:: BodyId {
13971409 self . lower_body ( |this| {
13981410 (
13991411 & [ ] ,
14001412 match expr {
1401- Some ( expr) => this. lower_expr_mut ( expr) ,
1413+ Some ( expr) => {
1414+ this. is_in_const_context = true ;
1415+ this. lower_expr_mut ( expr)
1416+ }
14021417 None => this. expr_err ( span, this. dcx ( ) . span_delayed_bug ( span, "no block" ) ) ,
14031418 } ,
14041419 )
@@ -1417,12 +1432,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
14171432 body : Option < & Block > ,
14181433 attrs : & ' hir [ hir:: Attribute ] ,
14191434 contract : Option < & FnContract > ,
1435+ constness : Const ,
14201436 ) -> hir:: BodyId {
14211437 let Some ( body) = body else {
14221438 // Functions without a body are an error, except if this is an intrinsic. For those we
14231439 // create a fake body so that the entire rest of the compiler doesn't have to deal with
14241440 // this as a special case.
1425- return self . lower_fn_body ( decl, contract, |this| {
1441+ return self . lower_fn_body ( decl, contract, constness , |this| {
14261442 if find_attr ! ( attrs, RustcIntrinsic ) || this. tcx . is_sdylib_interface_build ( ) {
14271443 let span = this. lower_span ( span) ;
14281444 let empty_block = hir:: Block {
@@ -1447,7 +1463,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14471463 } ;
14481464 let Some ( coroutine_kind) = coroutine_kind else {
14491465 // Typical case: not a coroutine.
1450- return self . lower_fn_body_block ( decl, body, contract) ;
1466+ return self . lower_fn_body_block ( decl, body, contract, constness ) ;
14511467 } ;
14521468 // FIXME(contracts): Support contracts on async fn.
14531469 self . lower_body ( |this| {
0 commit comments