11//! Meta-syntax validation logic of attributes for post-expansion.
22
33use std:: convert:: identity;
4- use std:: slice;
54
65use rustc_ast:: token:: Delimiter ;
76use rustc_ast:: tokenstream:: DelimSpan ;
87use rustc_ast:: {
98 self as ast, AttrArgs , Attribute , DelimArgs , MetaItem , MetaItemInner , MetaItemKind , Safety ,
109} ;
11- use rustc_errors:: { Applicability , FatalError , PResult } ;
12- use rustc_feature:: { AttributeTemplate , BUILTIN_ATTRIBUTE_MAP , BuiltinAttribute } ;
10+ use rustc_errors:: { Applicability , PResult } ;
11+ use rustc_feature:: { AttributeTemplate , BUILTIN_ATTRIBUTE_MAP } ;
1312use rustc_hir:: AttrPath ;
1413use rustc_hir:: lints:: AttributeLintKind ;
1514use rustc_parse:: parse_in;
@@ -19,43 +18,23 @@ use rustc_session::lint::builtin::ILL_FORMED_ATTRIBUTE_INPUT;
1918use rustc_session:: parse:: ParseSess ;
2019use rustc_span:: { Span , Symbol , sym} ;
2120
22- use crate :: { AttributeParser , Late , session_diagnostics as errors} ;
21+ use crate :: session_diagnostics as errors;
2322
2423pub fn check_attr ( psess : & ParseSess , attr : & Attribute ) {
25- if attr. is_doc_comment ( ) || attr. has_name ( sym:: cfg_trace) || attr. has_name ( sym:: cfg_attr_trace)
24+ // Built-in attributes are parsed in their respective attribute parsers, so can be ignored here
25+ if attr. is_doc_comment ( )
26+ || attr. name ( ) . is_some_and ( |name| BUILTIN_ATTRIBUTE_MAP . contains_key ( & name) )
2627 {
2728 return ;
2829 }
2930
30- let builtin_attr_info = attr. name ( ) . and_then ( |name| BUILTIN_ATTRIBUTE_MAP . get ( & name) ) ;
31-
32- // Check input tokens for built-in and key-value attributes.
33- match builtin_attr_info {
34- // `rustc_dummy` doesn't have any restrictions specific to built-in attributes.
35- Some ( BuiltinAttribute { name, template, .. } ) => {
36- if AttributeParser :: < Late > :: is_parsed_attribute ( slice:: from_ref ( & name) ) {
37- return ;
38- }
39- match parse_meta ( psess, attr) {
40- // Don't check safety again, we just did that
41- Ok ( meta) => {
42- check_builtin_meta_item ( psess, & meta, attr. style , * name, * template, false )
43- }
44- Err ( err) => {
45- err. emit ( ) ;
46- }
47- }
48- }
49- _ => {
50- let attr_item = attr. get_normal_item ( ) ;
51- if let AttrArgs :: Eq { .. } = attr_item. args . unparsed_ref ( ) . unwrap ( ) {
52- // All key-value attributes are restricted to meta-item syntax.
53- match parse_meta ( psess, attr) {
54- Ok ( _) => { }
55- Err ( err) => {
56- err. emit ( ) ;
57- }
58- }
31+ let attr_item = attr. get_normal_item ( ) ;
32+ if let AttrArgs :: Eq { .. } = attr_item. args . unparsed_ref ( ) . unwrap ( ) {
33+ // All key-value attributes are restricted to meta-item syntax.
34+ match parse_meta ( psess, attr) {
35+ Ok ( _) => { }
36+ Err ( err) => {
37+ err. emit ( ) ;
5938 }
6039 }
6140 }
@@ -170,7 +149,7 @@ pub fn check_builtin_meta_item(
170149 }
171150}
172151
173- fn emit_malformed_attribute (
152+ pub fn emit_malformed_attribute (
174153 psess : & ParseSess ,
175154 style : ast:: AttrStyle ,
176155 span : Span ,
@@ -232,15 +211,3 @@ fn emit_malformed_attribute(
232211 err. emit ( ) ;
233212 }
234213}
235-
236- pub fn emit_fatal_malformed_builtin_attribute (
237- psess : & ParseSess ,
238- attr : & Attribute ,
239- name : Symbol ,
240- ) -> ! {
241- let template = BUILTIN_ATTRIBUTE_MAP . get ( & name) . expect ( "builtin attr defined" ) . template ;
242- emit_malformed_attribute ( psess, attr. style , attr. span , name, template) ;
243- // This is fatal, otherwise it will likely cause a cascade of other errors
244- // (and an error here is expected to be very rare).
245- FatalError . raise ( )
246- }
0 commit comments