@@ -1422,14 +1422,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14221422 LowerTypeRelativePathMode :: Const ,
14231423 ) ? {
14241424 TypeRelativePath :: AssocItem ( def_id, args) => {
1425- if !self . tcx ( ) . is_type_const ( def_id) {
1426- let mut err = self . dcx ( ) . struct_span_err (
1427- span,
1428- "use of trait associated const without `#[type_const]`" ,
1429- ) ;
1430- err. note ( "the declaration in the trait must be marked with `#[type_const]`" ) ;
1431- return Err ( err. emit ( ) ) ;
1432- }
1425+ self . require_type_const_attribute ( def_id, span) ?;
14331426 let ct = Const :: new_unevaluated ( tcx, ty:: UnevaluatedConst :: new ( def_id, args) ) ;
14341427 let ct = self . check_param_uses_if_mcg ( ct, span, false ) ;
14351428 Ok ( ct)
@@ -1885,30 +1878,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
18851878 item_def_id : DefId ,
18861879 trait_segment : Option < & hir:: PathSegment < ' tcx > > ,
18871880 item_segment : & hir:: PathSegment < ' tcx > ,
1888- ) -> Const < ' tcx > {
1889- 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 (
18901883 span,
18911884 opt_self_ty,
18921885 item_def_id,
18931886 trait_segment,
18941887 item_segment,
18951888 ty:: AssocTag :: Const ,
1896- ) {
1897- Ok ( ( item_def_id, item_args) ) => {
1898- if !self . tcx ( ) . is_type_const ( item_def_id) {
1899- let mut err = self . dcx ( ) . struct_span_err (
1900- span,
1901- "use of `const` in the type system without `#[type_const]`" ,
1902- ) ;
1903- err. note ( "the declaration must be marked with `#[type_const]`" ) ;
1904- return Const :: new_error ( self . tcx ( ) , err. emit ( ) ) ;
1905- }
1906-
1907- let uv = ty:: UnevaluatedConst :: new ( item_def_id, item_args) ;
1908- Const :: new_unevaluated ( self . tcx ( ) , uv)
1909- }
1910- Err ( guar) => Const :: new_error ( self . tcx ( ) , guar) ,
1911- }
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) )
19121893 }
19131894
19141895 /// Lower a [resolved][hir::QPath::Resolved] (type-level) associated item path.
@@ -2668,6 +2649,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26682649 self . lower_const_param ( def_id, hir_id)
26692650 }
26702651 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+
26712656 assert_eq ! ( opt_self_ty, None ) ;
26722657 let [ leading_segments @ .., segment] = path. segments else { bug ! ( ) } ;
26732658 let _ = self
@@ -2718,6 +2703,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
27182703 trait_segment,
27192704 path. segments . last ( ) . unwrap ( ) ,
27202705 )
2706+ . unwrap_or_else ( |guar| Const :: new_error ( tcx, guar) )
27212707 }
27222708 Res :: Def ( DefKind :: Static { .. } , _) => {
27232709 span_bug ! ( span, "use of bare `static` ConstArgKind::Path's not yet supported" )
@@ -2843,6 +2829,33 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
28432829 . map ( |l| tcx. at ( expr. span ) . lit_to_const ( l) )
28442830 }
28452831
2832+ fn require_type_const_attribute (
2833+ & self ,
2834+ def_id : DefId ,
2835+ span : Span ,
2836+ ) -> Result < ( ) , ErrorGuaranteed > {
2837+ let tcx = self . tcx ( ) ;
2838+ if tcx. is_type_const ( def_id) {
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+ if def_id. is_local ( ) {
2845+ let name = tcx. def_path_str ( def_id) ;
2846+ err. span_suggestion (
2847+ tcx. def_span ( def_id) . shrink_to_lo ( ) ,
2848+ format ! ( "add `#[type_const]` attribute to `{name}`" ) ,
2849+ format ! ( "#[type_const]\n " ) ,
2850+ Applicability :: MaybeIncorrect ,
2851+ ) ;
2852+ } else {
2853+ err. note ( "only consts marked with `#[type_const]` may be used in types" ) ;
2854+ }
2855+ Err ( err. emit ( ) )
2856+ }
2857+ }
2858+
28462859 fn lower_delegation_ty ( & self , idx : hir:: InferDelegationKind ) -> Ty < ' tcx > {
28472860 let delegation_sig = self . tcx ( ) . inherit_sig_for_delegation_item ( self . item_def_id ( ) ) ;
28482861 match idx {
0 commit comments