@@ -1423,14 +1423,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14231423 LowerTypeRelativePathMode :: Const ,
14241424 ) ? {
14251425 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- }
1426+ self . require_type_const_attribute ( def_id, span) ?;
14341427 let ct = Const :: new_unevaluated ( tcx, ty:: UnevaluatedConst :: new ( def_id, args) ) ;
14351428 let ct = self . check_param_uses_if_mcg ( ct, span, false ) ;
14361429 Ok ( ct)
@@ -1886,30 +1879,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
18861879 item_def_id : DefId ,
18871880 trait_segment : Option < & hir:: PathSegment < ' tcx > > ,
18881881 item_segment : & hir:: PathSegment < ' tcx > ,
1889- ) -> Const < ' tcx > {
1890- match self . lower_resolved_assoc_item_path (
1882+ ) -> Result < Const < ' tcx > , ErrorGuaranteed > {
1883+ let ( item_def_id , item_args ) = self . lower_resolved_assoc_item_path (
18911884 span,
18921885 opt_self_ty,
18931886 item_def_id,
18941887 trait_segment,
18951888 item_segment,
18961889 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- }
1890+ ) ?;
1891+ self . require_type_const_attribute ( item_def_id, span) ?;
1892+ let uv = ty:: UnevaluatedConst :: new ( item_def_id, item_args) ;
1893+ Ok ( Const :: new_unevaluated ( self . tcx ( ) , uv) )
19131894 }
19141895
19151896 /// Lower a [resolved][hir::QPath::Resolved] (type-level) associated item path.
@@ -2669,6 +2650,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26692650 self . lower_const_param ( def_id, hir_id)
26702651 }
26712652 Res :: Def ( DefKind :: Const , did) => {
2653+ if let Err ( guar) = self . require_type_const_attribute ( did, span) {
2654+ return Const :: new_error ( self . tcx ( ) , guar) ;
2655+ }
2656+
26722657 assert_eq ! ( opt_self_ty, None ) ;
26732658 let [ leading_segments @ .., segment] = path. segments else { bug ! ( ) } ;
26742659 let _ = self
@@ -2719,6 +2704,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
27192704 trait_segment,
27202705 path. segments . last ( ) . unwrap ( ) ,
27212706 )
2707+ . unwrap_or_else ( |guar| Const :: new_error ( tcx, guar) )
27222708 }
27232709 Res :: Def ( DefKind :: Static { .. } , _) => {
27242710 span_bug ! ( span, "use of bare `static` ConstArgKind::Path's not yet supported" )
@@ -2844,6 +2830,25 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
28442830 . map ( |l| tcx. at ( expr. span ) . lit_to_const ( l) )
28452831 }
28462832
2833+ fn require_type_const_attribute (
2834+ & self ,
2835+ def_id : DefId ,
2836+ span : Span ,
2837+ ) -> Result < ( ) , ErrorGuaranteed > {
2838+ if find_attr ! ( self . tcx( ) . get_all_attrs( def_id) , AttributeKind :: TypeConst ( _) ) {
2839+ Ok ( ( ) )
2840+ } else {
2841+ let mut err = self
2842+ . dcx ( )
2843+ . struct_span_err ( span, "use of `const` in the type system without `#[type_const]`" ) ;
2844+ err. span_note (
2845+ self . tcx ( ) . def_span ( def_id) ,
2846+ "the declaration must be marked with `#[type_const]`" ,
2847+ ) ;
2848+ Err ( err. emit ( ) )
2849+ }
2850+ }
2851+
28472852 fn lower_delegation_ty ( & self , idx : hir:: InferDelegationKind ) -> Ty < ' tcx > {
28482853 let delegation_sig = self . tcx ( ) . inherit_sig_for_delegation_item ( self . item_def_id ( ) ) ;
28492854 match idx {
0 commit comments