1use rustc_ast::token::Delimiter;
4use rustc_ast::tokenstream::DelimSpan;
5use rustc_ast::{
6 self as ast, AttrArgs, Attribute, DelimArgs, MetaItem, MetaItemInner, MetaItemKind, NodeId,
7 Safety,
8};
9use rustc_errors::{Applicability, FatalError, PResult};
10use rustc_feature::{AttributeSafety, AttributeTemplate, BUILTIN_ATTRIBUTE_MAP, BuiltinAttribute};
11use rustc_session::errors::report_lit_error;
12use rustc_session::lint::BuiltinLintDiag;
13use rustc_session::lint::builtin::{ILL_FORMED_ATTRIBUTE_INPUT, UNSAFE_ATTR_OUTSIDE_UNSAFE};
14use rustc_session::parse::ParseSess;
15use rustc_span::{Span, Symbol, sym};
16
17use crate::{errors, parse_in};
18
19pub fn check_attr(psess: &ParseSess, attr: &Attribute, id: NodeId) {
20 if attr.is_doc_comment() || attr.has_name(sym::cfg_trace) || attr.has_name(sym::cfg_attr_trace)
21 {
22 return;
23 }
24
25 let builtin_attr_info = attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name));
26
27 let builtin_attr_safety = builtin_attr_info.map(|x| x.safety);
28 check_attribute_safety(psess, builtin_attr_safety, attr, id);
29
30 match builtin_attr_info {
32 Some(BuiltinAttribute { name, template, .. }) if *name != sym::rustc_dummy => {
34 match parse_meta(psess, attr) {
35 Ok(meta) => {
37 check_builtin_meta_item(psess, &meta, attr.style, *name, *template, false)
38 }
39 Err(err) => {
40 err.emit();
41 }
42 }
43 }
44 _ => {
45 let attr_item = attr.get_normal_item();
46 if let AttrArgs::Eq { .. } = attr_item.args {
47 match parse_meta(psess, attr) {
49 Ok(_) => {}
50 Err(err) => {
51 err.emit();
52 }
53 }
54 }
55 }
56 }
57}
58
59pub fn parse_meta<'a>(psess: &'a ParseSess, attr: &Attribute) -> PResult<'a, MetaItem> {
60 let item = attr.get_normal_item();
61 Ok(MetaItem {
62 unsafety: item.unsafety,
63 span: attr.span,
64 path: item.path.clone(),
65 kind: match &item.args {
66 AttrArgs::Empty => MetaItemKind::Word,
67 AttrArgs::Delimited(DelimArgs { dspan, delim, tokens }) => {
68 check_meta_bad_delim(psess, *dspan, *delim);
69 let nmis =
70 parse_in(psess, tokens.clone(), "meta list", |p| p.parse_meta_seq_top())?;
71 MetaItemKind::List(nmis)
72 }
73 AttrArgs::Eq { expr, .. } => {
74 if let ast::ExprKind::Lit(token_lit) = expr.kind {
75 let res = ast::MetaItemLit::from_token_lit(token_lit, expr.span);
76 let res = match res {
77 Ok(lit) => {
78 if token_lit.suffix.is_some() {
79 let mut err = psess.dcx().struct_span_err(
80 expr.span,
81 "suffixed literals are not allowed in attributes",
82 );
83 err.help(
84 "instead of using a suffixed literal (`1u8`, `1.0f32`, etc.), \
85 use an unsuffixed version (`1`, `1.0`, etc.)",
86 );
87 return Err(err);
88 } else {
89 MetaItemKind::NameValue(lit)
90 }
91 }
92 Err(err) => {
93 let guar = report_lit_error(psess, err, token_lit, expr.span);
94 let lit = ast::MetaItemLit {
95 symbol: token_lit.symbol,
96 suffix: token_lit.suffix,
97 kind: ast::LitKind::Err(guar),
98 span: expr.span,
99 };
100 MetaItemKind::NameValue(lit)
101 }
102 };
103 res
104 } else {
105 let msg = "attribute value must be a literal";
112 let mut err = psess.dcx().struct_span_err(expr.span, msg);
113 if let ast::ExprKind::Err(_) = expr.kind {
114 err.downgrade_to_delayed_bug();
115 }
116 return Err(err);
117 }
118 }
119 },
120 })
121}
122
123fn check_meta_bad_delim(psess: &ParseSess, span: DelimSpan, delim: Delimiter) {
124 if let Delimiter::Parenthesis = delim {
125 return;
126 }
127 psess.dcx().emit_err(errors::MetaBadDelim {
128 span: span.entire(),
129 sugg: errors::MetaBadDelimSugg { open: span.open, close: span.close },
130 });
131}
132
133pub(super) fn check_cfg_attr_bad_delim(psess: &ParseSess, span: DelimSpan, delim: Delimiter) {
134 if let Delimiter::Parenthesis = delim {
135 return;
136 }
137 psess.dcx().emit_err(errors::CfgAttrBadDelim {
138 span: span.entire(),
139 sugg: errors::MetaBadDelimSugg { open: span.open, close: span.close },
140 });
141}
142
143fn is_attr_template_compatible(template: &AttributeTemplate, meta: &ast::MetaItemKind) -> bool {
145 let is_one_allowed_subword = |items: &[MetaItemInner]| match items {
146 [item] => item.is_word() && template.one_of.iter().any(|&word| item.has_name(word)),
147 _ => false,
148 };
149 match meta {
150 MetaItemKind::Word => template.word,
151 MetaItemKind::List(items) => template.list.is_some() || is_one_allowed_subword(items),
152 MetaItemKind::NameValue(lit) if lit.kind.is_str() => template.name_value_str.is_some(),
153 MetaItemKind::NameValue(..) => false,
154 }
155}
156
157pub fn check_attribute_safety(
158 psess: &ParseSess,
159 builtin_attr_safety: Option<AttributeSafety>,
160 attr: &Attribute,
161 id: NodeId,
162) {
163 let attr_item = attr.get_normal_item();
164 match (builtin_attr_safety, attr_item.unsafety) {
165 (Some(AttributeSafety::Unsafe { .. }), Safety::Unsafe(..)) => {
168 }
170
171 (Some(AttributeSafety::Unsafe { unsafe_since }), Safety::Default) => {
174 let path_span = attr_item.path.span;
175
176 let diag_span = attr_item.span();
181
182 let emit_error = match unsafe_since {
189 None => true,
190 Some(unsafe_since) => path_span.edition() >= unsafe_since,
191 };
192
193 if emit_error {
194 psess.dcx().emit_err(errors::UnsafeAttrOutsideUnsafe {
195 span: path_span,
196 suggestion: errors::UnsafeAttrOutsideUnsafeSuggestion {
197 left: diag_span.shrink_to_lo(),
198 right: diag_span.shrink_to_hi(),
199 },
200 });
201 } else {
202 psess.buffer_lint(
203 UNSAFE_ATTR_OUTSIDE_UNSAFE,
204 path_span,
205 id,
206 BuiltinLintDiag::UnsafeAttrOutsideUnsafe {
207 attribute_name_span: path_span,
208 sugg_spans: (diag_span.shrink_to_lo(), diag_span.shrink_to_hi()),
209 },
210 );
211 }
212 }
213
214 (Some(AttributeSafety::Normal) | None, Safety::Unsafe(unsafe_span)) => {
218 psess.dcx().emit_err(errors::InvalidAttrUnsafe {
219 span: unsafe_span,
220 name: attr_item.path.clone(),
221 });
222 }
223
224 (Some(AttributeSafety::Normal), Safety::Default) => {
227 }
229
230 (None, Safety::Default) => {
233 }
235
236 (
237 Some(AttributeSafety::Unsafe { .. } | AttributeSafety::Normal) | None,
238 Safety::Safe(..),
239 ) => {
240 psess.dcx().span_delayed_bug(
241 attr_item.span(),
242 "`check_attribute_safety` does not expect `Safety::Safe` on attributes",
243 );
244 }
245 }
246}
247
248pub fn deny_builtin_meta_unsafety(psess: &ParseSess, meta: &MetaItem) {
251 if let Safety::Unsafe(unsafe_span) = meta.unsafety {
255 psess
256 .dcx()
257 .emit_err(errors::InvalidAttrUnsafe { span: unsafe_span, name: meta.path.clone() });
258 }
259}
260
261pub fn check_builtin_meta_item(
262 psess: &ParseSess,
263 meta: &MetaItem,
264 style: ast::AttrStyle,
265 name: Symbol,
266 template: AttributeTemplate,
267 deny_unsafety: bool,
268) {
269 if !is_attr_template_compatible(&template, &meta.kind) {
270 emit_malformed_attribute(psess, style, meta.span, name, template);
271 }
272
273 if deny_unsafety {
274 deny_builtin_meta_unsafety(psess, meta);
275 }
276}
277
278fn emit_malformed_attribute(
279 psess: &ParseSess,
280 style: ast::AttrStyle,
281 span: Span,
282 name: Symbol,
283 template: AttributeTemplate,
284) {
285 if matches!(
287 name,
288 sym::inline
289 | sym::rustc_force_inline
290 | sym::rustc_confusables
291 | sym::repr
292 | sym::align
293 | sym::deprecated
294 | sym::optimize
295 ) {
296 return;
297 }
298
299 let should_warn =
302 |name| matches!(name, sym::doc | sym::ignore | sym::link | sym::test | sym::bench);
303
304 let error_msg = format!("malformed `{name}` attribute input");
305 let mut suggestions = vec![];
306 let inner = if style == ast::AttrStyle::Inner { "!" } else { "" };
307 if template.word {
308 suggestions.push(format!("#{inner}[{name}]"));
309 }
310 if let Some(descr) = template.list {
311 suggestions.push(format!("#{inner}[{name}({descr})]"));
312 }
313 suggestions.extend(template.one_of.iter().map(|&word| format!("#{inner}[{name}({word})]")));
314 if let Some(descr) = template.name_value_str {
315 suggestions.push(format!("#{inner}[{name} = \"{descr}\"]"));
316 }
317 if should_warn(name) {
318 psess.buffer_lint(
319 ILL_FORMED_ATTRIBUTE_INPUT,
320 span,
321 ast::CRATE_NODE_ID,
322 BuiltinLintDiag::IllFormedAttributeInput { suggestions: suggestions.clone() },
323 );
324 } else {
325 suggestions.sort();
326 psess
327 .dcx()
328 .struct_span_err(span, error_msg)
329 .with_span_suggestions(
330 span,
331 if suggestions.len() == 1 {
332 "must be of the form"
333 } else {
334 "the following are the possible correct uses"
335 },
336 suggestions,
337 Applicability::HasPlaceholders,
338 )
339 .emit();
340 }
341}
342
343pub fn emit_fatal_malformed_builtin_attribute(
344 psess: &ParseSess,
345 attr: &Attribute,
346 name: Symbol,
347) -> ! {
348 let template = BUILTIN_ATTRIBUTE_MAP.get(&name).expect("builtin attr defined").template;
349 emit_malformed_attribute(psess, attr.style, attr.span, name, template);
350 FatalError.raise()
353}