@@ -10,9 +10,9 @@ use rustc_errors::{Applicability, FatalError, PResult};
10
10
use rustc_feature:: { AttributeTemplate , BuiltinAttribute , BUILTIN_ATTRIBUTE_MAP } ;
11
11
use rustc_session:: lint:: builtin:: ILL_FORMED_ATTRIBUTE_INPUT ;
12
12
use rustc_session:: parse:: ParseSess ;
13
- use rustc_span:: { sym, Symbol } ;
13
+ use rustc_span:: { sym, Span , Symbol } ;
14
14
15
- pub fn check_meta ( sess : & ParseSess , attr : & Attribute ) {
15
+ pub fn check_attr ( sess : & ParseSess , attr : & Attribute ) {
16
16
if attr. is_doc_comment ( ) {
17
17
return ;
18
18
}
@@ -115,25 +115,34 @@ pub fn check_builtin_attribute(
115
115
name : Symbol ,
116
116
template : AttributeTemplate ,
117
117
) {
118
- // Some special attributes like `cfg` must be checked
119
- // before the generic check, so we skip them here.
120
- let should_skip = |name| name == sym:: cfg;
121
-
122
118
match parse_meta ( sess, attr) {
123
- Ok ( meta) => {
124
- if !should_skip ( name) && !is_attr_template_compatible ( & template, & meta. kind ) {
125
- emit_malformed_attribute ( sess, attr, name, template) ;
126
- }
127
- }
119
+ Ok ( meta) => check_builtin_meta_item ( sess, & meta, attr. style , name, template) ,
128
120
Err ( mut err) => {
129
121
err. emit ( ) ;
130
122
}
131
123
}
132
124
}
133
125
126
+ pub fn check_builtin_meta_item (
127
+ sess : & ParseSess ,
128
+ meta : & MetaItem ,
129
+ style : ast:: AttrStyle ,
130
+ name : Symbol ,
131
+ template : AttributeTemplate ,
132
+ ) {
133
+ // Some special attributes like `cfg` must be checked
134
+ // before the generic check, so we skip them here.
135
+ let should_skip = |name| name == sym:: cfg;
136
+
137
+ if !should_skip ( name) && !is_attr_template_compatible ( & template, & meta. kind ) {
138
+ emit_malformed_attribute ( sess, style, meta. span , name, template) ;
139
+ }
140
+ }
141
+
134
142
fn emit_malformed_attribute (
135
143
sess : & ParseSess ,
136
- attr : & Attribute ,
144
+ style : ast:: AttrStyle ,
145
+ span : Span ,
137
146
name : Symbol ,
138
147
template : AttributeTemplate ,
139
148
) {
@@ -147,7 +156,7 @@ fn emit_malformed_attribute(
147
156
let mut msg = "attribute must be of the form " . to_owned ( ) ;
148
157
let mut suggestions = vec ! [ ] ;
149
158
let mut first = true ;
150
- let inner = if attr . style == ast:: AttrStyle :: Inner { "!" } else { "" } ;
159
+ let inner = if style == ast:: AttrStyle :: Inner { "!" } else { "" } ;
151
160
if template. word {
152
161
first = false ;
153
162
let code = format ! ( "#{}[{}]" , inner, name) ;
@@ -172,12 +181,12 @@ fn emit_malformed_attribute(
172
181
suggestions. push ( code) ;
173
182
}
174
183
if should_warn ( name) {
175
- sess. buffer_lint ( & ILL_FORMED_ATTRIBUTE_INPUT , attr . span , ast:: CRATE_NODE_ID , & msg) ;
184
+ sess. buffer_lint ( & ILL_FORMED_ATTRIBUTE_INPUT , span, ast:: CRATE_NODE_ID , & msg) ;
176
185
} else {
177
186
sess. span_diagnostic
178
- . struct_span_err ( attr . span , & error_msg)
187
+ . struct_span_err ( span, & error_msg)
179
188
. span_suggestions (
180
- attr . span ,
189
+ span,
181
190
if suggestions. len ( ) == 1 {
182
191
"must be of the form"
183
192
} else {
@@ -196,7 +205,7 @@ pub fn emit_fatal_malformed_builtin_attribute(
196
205
name : Symbol ,
197
206
) -> ! {
198
207
let template = BUILTIN_ATTRIBUTE_MAP . get ( & name) . expect ( "builtin attr defined" ) . template ;
199
- emit_malformed_attribute ( sess, attr, name, template) ;
208
+ emit_malformed_attribute ( sess, attr. style , attr . span , name, template) ;
200
209
// This is fatal, otherwise it will likely cause a cascade of other errors
201
210
// (and an error here is expected to be very rare).
202
211
FatalError . raise ( )
0 commit comments