@@ -28,10 +28,9 @@ use rustc_errors::codes::*;
2828use rustc_errors:: {
2929 Applicability , Diag , DiagCtxtHandle , ErrorGuaranteed , FatalError , struct_span_code_err,
3030} ;
31- use rustc_hir:: attrs:: AttributeKind ;
3231use rustc_hir:: def:: { CtorKind , CtorOf , DefKind , Res } ;
3332use rustc_hir:: def_id:: { DefId , LocalDefId } ;
34- use rustc_hir:: { self as hir, AnonConst , GenericArg , GenericArgs , HirId , find_attr } ;
33+ use rustc_hir:: { self as hir, AnonConst , GenericArg , GenericArgs , HirId } ;
3534use rustc_infer:: infer:: { InferCtxt , TyCtxtInferExt } ;
3635use rustc_infer:: traits:: DynCompatibilityViolation ;
3736use rustc_macros:: { TypeFoldable , TypeVisitable } ;
@@ -1423,14 +1422,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14231422 LowerTypeRelativePathMode :: Const ,
14241423 ) ? {
14251424 TypeRelativePath :: AssocItem ( def_id, args) => {
1426- if !find_attr ! ( self . tcx( ) . get_all_attrs( def_id) , AttributeKind :: TypeConst ( _) ) {
1427- let mut err = self . dcx ( ) . struct_span_err (
1428- span,
1429- "use of trait associated const without `#[type_const]`" ,
1430- ) ;
1431- err. note ( "the declaration in the trait must be marked with `#[type_const]`" ) ;
1432- return Err ( err. emit ( ) ) ;
1433- }
1425+ self . require_type_const_attribute ( def_id, span) ?;
14341426 let ct = Const :: new_unevaluated ( tcx, ty:: UnevaluatedConst :: new ( def_id, args) ) ;
14351427 let ct = self . check_param_uses_if_mcg ( ct, span, false ) ;
14361428 Ok ( ct)
@@ -1886,30 +1878,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
18861878 item_def_id : DefId ,
18871879 trait_segment : Option < & hir:: PathSegment < ' tcx > > ,
18881880 item_segment : & hir:: PathSegment < ' tcx > ,
1889- ) -> Const < ' tcx > {
1890- match self . lower_resolved_assoc_item_path (
1881+ ) -> Result < Const < ' tcx > , ErrorGuaranteed > {
1882+ let ( item_def_id , item_args ) = self . lower_resolved_assoc_item_path (
18911883 span,
18921884 opt_self_ty,
18931885 item_def_id,
18941886 trait_segment,
18951887 item_segment,
18961888 ty:: AssocTag :: Const ,
1897- ) {
1898- Ok ( ( item_def_id, item_args) ) => {
1899- if !find_attr ! ( self . tcx( ) . get_all_attrs( item_def_id) , AttributeKind :: TypeConst ( _) ) {
1900- let mut err = self . dcx ( ) . struct_span_err (
1901- span,
1902- "use of `const` in the type system without `#[type_const]`" ,
1903- ) ;
1904- err. note ( "the declaration must be marked with `#[type_const]`" ) ;
1905- return Const :: new_error ( self . tcx ( ) , err. emit ( ) ) ;
1906- }
1907-
1908- let uv = ty:: UnevaluatedConst :: new ( item_def_id, item_args) ;
1909- Const :: new_unevaluated ( self . tcx ( ) , uv)
1910- }
1911- Err ( guar) => Const :: new_error ( self . tcx ( ) , guar) ,
1912- }
1889+ ) ?;
1890+ self . require_type_const_attribute ( item_def_id, span) ?;
1891+ let uv = ty:: UnevaluatedConst :: new ( item_def_id, item_args) ;
1892+ Ok ( Const :: new_unevaluated ( self . tcx ( ) , uv) )
19131893 }
19141894
19151895 /// Lower a [resolved][hir::QPath::Resolved] (type-level) associated item path.
@@ -2669,6 +2649,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26692649 self . lower_const_param ( def_id, hir_id)
26702650 }
26712651 Res :: Def ( DefKind :: Const , did) => {
2652+ if let Err ( guar) = self . require_type_const_attribute ( did, span) {
2653+ return Const :: new_error ( self . tcx ( ) , guar) ;
2654+ }
2655+
26722656 assert_eq ! ( opt_self_ty, None ) ;
26732657 let [ leading_segments @ .., segment] = path. segments else { bug ! ( ) } ;
26742658 let _ = self
@@ -2719,6 +2703,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
27192703 trait_segment,
27202704 path. segments . last ( ) . unwrap ( ) ,
27212705 )
2706+ . unwrap_or_else ( |guar| Const :: new_error ( tcx, guar) )
27222707 }
27232708 Res :: Def ( DefKind :: Static { .. } , _) => {
27242709 span_bug ! ( span, "use of bare `static` ConstArgKind::Path's not yet supported" )
@@ -2844,6 +2829,25 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
28442829 . map ( |l| tcx. at ( expr. span ) . lit_to_const ( l) )
28452830 }
28462831
2832+ fn require_type_const_attribute (
2833+ & self ,
2834+ def_id : DefId ,
2835+ span : Span ,
2836+ ) -> Result < ( ) , ErrorGuaranteed > {
2837+ if self . tcx ( ) . is_type_const ( def_id) {
2838+ Ok ( ( ) )
2839+ } else {
2840+ let mut err = self
2841+ . dcx ( )
2842+ . struct_span_err ( span, "use of `const` in the type system without `#[type_const]`" ) ;
2843+ err. span_note (
2844+ self . tcx ( ) . def_span ( def_id) ,
2845+ "the declaration must be marked with `#[type_const]`" ,
2846+ ) ;
2847+ Err ( err. emit ( ) )
2848+ }
2849+ }
2850+
28472851 fn lower_delegation_ty ( & self , idx : hir:: InferDelegationKind ) -> Ty < ' tcx > {
28482852 let delegation_sig = self . tcx ( ) . inherit_sig_for_delegation_item ( self . item_def_id ( ) ) ;
28492853 match idx {
0 commit comments