@@ -479,7 +479,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
479479 . arena
480480 . alloc_from_iter ( impl_items. iter ( ) . map ( |item| self . lower_impl_item_ref ( item) ) ) ;
481481
482- let constness = self . lower_constness ( * constness) ;
482+ let constness = self . lower_constness ( attrs , * constness) ;
483483
484484 hir:: ItemKind :: Impl ( hir:: Impl {
485485 generics,
@@ -499,7 +499,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
499499 bounds,
500500 items,
501501 } ) => {
502- let constness = self . lower_constness ( * constness) ;
502+ let constness = self . lower_constness ( attrs , * constness) ;
503503 let impl_restriction = self . lower_impl_restriction ( impl_restriction) ;
504504 let ident = self . lower_ident ( * ident) ;
505505 let ( generics, ( safety, items, bounds) ) = self . lower_generics (
@@ -530,7 +530,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
530530 }
531531 }
532532 ItemKind :: TraitAlias ( TraitAlias { constness, ident, generics, bounds } ) => {
533- let constness = self . lower_constness ( * constness) ;
533+ let constness = self . lower_constness ( attrs , * constness) ;
534534 let ident = self . lower_ident ( * ident) ;
535535 let ( generics, bounds) = self . lower_generics (
536536 generics,
@@ -1702,21 +1702,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17021702 safety. into ( )
17031703 } ;
17041704
1705- let mut constness = self . lower_constness ( h. constness ) ;
1706- if let Some ( & attr_span) = find_attr ! ( attrs, RustcComptime ( span) => span) {
1707- match std:: mem:: replace ( & mut constness, rustc_hir:: Constness :: Const { always : true } ) {
1708- rustc_hir:: Constness :: Const { always : true } => {
1709- unreachable ! ( "lower_constness cannot produce comptime" )
1710- }
1711- // A function can't be `const` and `comptime` at the same time
1712- rustc_hir:: Constness :: Const { always : false } => {
1713- let Const :: Yes ( span) = h. constness else { unreachable ! ( ) } ;
1714- self . dcx ( ) . emit_err ( ConstComptimeFn { span, attr_span } ) ;
1715- }
1716- // Good
1717- rustc_hir:: Constness :: NotConst => { }
1718- }
1719- }
1705+ let constness = self . lower_constness ( attrs, h. constness ) ;
17201706
17211707 hir:: FnHeader { safety, asyncness, constness, abi : self . lower_extern ( h. ext ) }
17221708 }
@@ -1778,11 +1764,30 @@ impl<'hir> LoweringContext<'_, 'hir> {
17781764 } ) ;
17791765 }
17801766
1781- pub ( super ) fn lower_constness ( & mut self , c : Const ) -> hir:: Constness {
1782- match c {
1767+ /// Lowers constness or comptime attribute.
1768+ /// Whether `const` is allowed here is checked by ast validation.
1769+ /// Whether `comptime` is allowed here is checked by the `comptime` attribute parser.
1770+ pub ( super ) fn lower_constness ( & mut self , attrs : & [ hir:: Attribute ] , c : Const ) -> hir:: Constness {
1771+ let mut constness = match c {
17831772 Const :: Yes ( _) => hir:: Constness :: Const { always : false } ,
17841773 Const :: No => hir:: Constness :: NotConst ,
1774+ } ;
1775+
1776+ if let Some ( & attr_span) = find_attr ! ( attrs, RustcComptime ( span) => span) {
1777+ match std:: mem:: replace ( & mut constness, hir:: Constness :: Const { always : true } ) {
1778+ hir:: Constness :: Const { always : true } => {
1779+ unreachable ! ( "lower_constness cannot produce comptime" )
1780+ }
1781+ // A function can't be `const` and `comptime` at the same time
1782+ hir:: Constness :: Const { always : false } => {
1783+ let Const :: Yes ( span) = c else { unreachable ! ( ) } ;
1784+ self . dcx ( ) . emit_err ( ConstComptimeFn { span, attr_span } ) ;
1785+ }
1786+ // Good
1787+ hir:: Constness :: NotConst => { }
1788+ }
17851789 }
1790+ constness
17861791 }
17871792
17881793 pub ( super ) fn lower_safety ( & self , s : Safety , default : hir:: Safety ) -> hir:: Safety {
0 commit comments