@@ -11,6 +11,7 @@ use rustc_ast::{AttrStyle, LitKind, MetaItemInner, MetaItemKind, MetaItemLit, as
1111use rustc_data_structures:: fx:: FxHashMap ;
1212use rustc_errors:: { Applicability , DiagCtxtHandle , IntoDiagArg , MultiSpan , StashKey } ;
1313use rustc_feature:: { AttributeDuplicates , AttributeType , BUILTIN_ATTRIBUTE_MAP , BuiltinAttribute } ;
14+ use rustc_hir:: def:: DefKind ;
1415use rustc_hir:: def_id:: LocalModDefId ;
1516use rustc_hir:: intravisit:: { self , Visitor } ;
1617use rustc_hir:: {
@@ -246,6 +247,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
246247 [ sym:: coroutine, ..] => {
247248 self . check_coroutine ( attr, target) ;
248249 }
250+ [ sym:: type_const, ..] => {
251+ self . check_type_const ( hir_id, attr, target) ;
252+ }
249253 [ sym:: linkage, ..] => self . check_linkage ( attr, span, target) ,
250254 [ sym:: rustc_pub_transparent, ..] => self . check_rustc_pub_transparent ( attr. span , span, attrs) ,
251255 [
@@ -2456,6 +2460,23 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
24562460 }
24572461 }
24582462
2463+ fn check_type_const ( & self , hir_id : HirId , attr : & Attribute , target : Target ) {
2464+ let tcx = self . tcx ;
2465+ if target == Target :: AssocConst
2466+ && let parent = tcx. parent ( hir_id. expect_owner ( ) . to_def_id ( ) )
2467+ && self . tcx . def_kind ( parent) == DefKind :: Trait
2468+ {
2469+ return ;
2470+ } else {
2471+ self . dcx ( )
2472+ . struct_span_err (
2473+ attr. span ,
2474+ "`#[type_const]` must only be applied to trait associated constants" ,
2475+ )
2476+ . emit ( ) ;
2477+ }
2478+ }
2479+
24592480 fn check_linkage ( & self , attr : & Attribute , span : Span , target : Target ) {
24602481 match target {
24612482 Target :: Fn
0 commit comments