1use std::convert::identity;
2
3use rustc_ast as ast;
4use rustc_ast::token::DocFragmentKind;
5use rustc_ast::{AttrItemKind, AttrStyle, CRATE_NODE_ID, NodeId, Safety};
6use rustc_data_structures::sync::{DynSend, DynSync};
7use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, Level, MultiSpan};
8use rustc_feature::{AttributeTemplate, Features};
9use rustc_hir::attrs::AttributeKind;
10use rustc_hir::{AttrArgs, AttrItem, AttrPath, Attribute, HashIgnoredAttrId, Target};
11use rustc_lint_defs::RegisteredTools;
12use rustc_session::Session;
13use rustc_session::lint::LintId;
14use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
15
16use crate::attributes::AttributeSafety;
17use crate::context::{
18 ATTRIBUTE_PARSERS, AcceptContext, FinalizeContext, FinalizeFn, SharedContext,
19};
20use crate::early_parsed::{EARLY_PARSED_ATTRIBUTES, EarlyParsedState};
21use crate::parser::{AllowExprMetavar, ArgParser, PathParser, RefPathParser};
22use crate::session_diagnostics::ParsedDescription;
23use crate::{OmitDoc, ShouldEmit};
24
25pub struct EmitAttribute(
26 pub Box<
27 dyn for<'a> FnOnce(DiagCtxtHandle<'a>, Level, &Session) -> Diag<'a, ()>
28 + DynSend
29 + DynSync
30 + 'static,
31 >,
32);
33
34pub struct AttributeParser<'sess> {
37 pub(crate) tools: Option<&'sess RegisteredTools>,
38 pub(crate) features: Option<&'sess Features>,
39 pub(crate) sess: &'sess Session,
40 pub(crate) should_emit: ShouldEmit,
41
42 parse_only: Option<&'static [Symbol]>,
46}
47
48impl<'sess> AttributeParser<'sess> {
49 pub fn parse_limited(
66 sess: &'sess Session,
67 attrs: &[ast::Attribute],
68 sym: &'static [Symbol],
69 ) -> Option<Attribute> {
70 Self::parse_limited_should_emit(
71 sess,
72 attrs,
73 sym,
74 DUMMY_SP,
76 CRATE_NODE_ID,
77 Target::Crate,
78 None,
79 ShouldEmit::Nothing,
80 )
81 }
82
83 pub fn parse_limited_should_emit(
88 sess: &'sess Session,
89 attrs: &[ast::Attribute],
90 sym: &'static [Symbol],
91 target_span: Span,
92 target_node_id: NodeId,
93 target: Target,
94 features: Option<&'sess Features>,
95 should_emit: ShouldEmit,
96 ) -> Option<Attribute> {
97 let mut parsed = Self::parse_limited_all(
98 sess,
99 attrs,
100 Some(sym),
101 target,
102 target_span,
103 target_node_id,
104 features,
105 should_emit,
106 None,
107 );
108 if !(parsed.len() <= 1) {
::core::panicking::panic("assertion failed: parsed.len() <= 1")
};assert!(parsed.len() <= 1);
109 parsed.pop()
110 }
111
112 pub fn parse_limited_all(
120 sess: &'sess Session,
121 attrs: &[ast::Attribute],
122 parse_only: Option<&'static [Symbol]>,
123 target: Target,
124 target_span: Span,
125 target_node_id: NodeId,
126 features: Option<&'sess Features>,
127 should_emit: ShouldEmit,
128 tools: Option<&'sess RegisteredTools>,
129 ) -> Vec<Attribute> {
130 let mut p = Self { features, tools, parse_only, sess, should_emit };
131 p.parse_attribute_list(
132 attrs,
133 target_span,
134 target,
135 OmitDoc::Skip,
136 std::convert::identity,
137 |lint_id, span, kind| {
138 sess.psess.dyn_buffer_lint_sess(lint_id.lint, span, target_node_id, kind.0)
139 },
140 )
141 }
142
143 pub fn parse_single<T>(
146 sess: &'sess Session,
147 attr: &ast::Attribute,
148 target_span: Span,
149 target_node_id: NodeId,
150 target: Target,
151 features: Option<&'sess Features>,
152 emit_errors: ShouldEmit,
153 parse_fn: fn(cx: &mut AcceptContext<'_, '_>, item: &ArgParser) -> Option<T>,
154 template: &AttributeTemplate,
155 allow_expr_metavar: AllowExprMetavar,
156 expected_safety: AttributeSafety,
157 ) -> Option<T> {
158 let ast::AttrKind::Normal(normal_attr) = &attr.kind else {
159 {
::core::panicking::panic_fmt(format_args!("parse_single called on a doc attr"));
}panic!("parse_single called on a doc attr")
160 };
161 let parts =
162 normal_attr.item.path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>();
163
164 let path = AttrPath::from_ast(&normal_attr.item.path, identity);
165 let args = ArgParser::from_attr_args(
166 &normal_attr.item.args.unparsed_ref().unwrap(),
167 &parts,
168 &sess.psess,
169 emit_errors,
170 allow_expr_metavar,
171 )?;
172 Self::parse_single_args(
173 sess,
174 attr.span,
175 normal_attr.item.span(),
176 attr.style,
177 path,
178 Some(normal_attr.item.unsafety),
179 expected_safety,
180 ParsedDescription::Attribute,
181 target_span,
182 target_node_id,
183 target,
184 features,
185 emit_errors,
186 &args,
187 parse_fn,
188 template,
189 )
190 }
191
192 pub fn parse_single_args<T, I>(
195 sess: &'sess Session,
196 attr_span: Span,
197 inner_span: Span,
198 attr_style: AttrStyle,
199 attr_path: AttrPath,
200 attr_safety: Option<Safety>,
201 expected_safety: AttributeSafety,
202 parsed_description: ParsedDescription,
203 target_span: Span,
204 target_node_id: NodeId,
205 target: Target,
206 features: Option<&'sess Features>,
207 should_emit: ShouldEmit,
208 args: &I,
209 parse_fn: fn(cx: &mut AcceptContext<'_, '_>, item: &I) -> T,
210 template: &AttributeTemplate,
211 ) -> T {
212 let mut parser = Self { features, tools: None, parse_only: None, sess, should_emit };
213 let mut emit_lint = |lint_id: LintId, span: MultiSpan, kind: EmitAttribute| {
214 sess.psess.dyn_buffer_lint_sess(lint_id.lint, span, target_node_id, kind.0)
215 };
216 if let Some(safety) = attr_safety {
217 parser.check_attribute_safety(
218 &attr_path,
219 inner_span,
220 safety,
221 expected_safety,
222 &mut emit_lint,
223 );
224 }
225 let mut cx: AcceptContext<'_, 'sess> = AcceptContext {
226 shared: SharedContext {
227 cx: &mut parser,
228 target_span,
229 target,
230 emit_lint: &mut emit_lint,
231 },
232 attr_span,
233 inner_span,
234 attr_style,
235 parsed_description,
236 template,
237 attr_safety: attr_safety.unwrap_or(Safety::Default),
238 attr_path,
239 };
240 parse_fn(&mut cx, args)
241 }
242}
243
244impl<'sess> AttributeParser<'sess> {
245 pub fn new(
246 sess: &'sess Session,
247 features: &'sess Features,
248 tools: &'sess RegisteredTools,
249 should_emit: ShouldEmit,
250 ) -> Self {
251 Self { features: Some(features), tools: Some(tools), parse_only: None, sess, should_emit }
252 }
253
254 pub(crate) fn sess(&self) -> &'sess Session {
255 &self.sess
256 }
257
258 pub(crate) fn features(&self) -> &'sess Features {
259 self.features.expect("features not available at this point in the compiler")
260 }
261
262 pub(crate) fn features_option(&self) -> Option<&'sess Features> {
263 self.features
264 }
265
266 pub(crate) fn dcx(&self) -> DiagCtxtHandle<'sess> {
267 self.sess().dcx()
268 }
269
270 pub(crate) fn emit_err(&self, diag: impl for<'x> Diagnostic<'x>) -> ErrorGuaranteed {
271 self.should_emit.emit_err(self.sess.dcx().create_err(diag))
272 }
273
274 pub fn parse_attribute_list(
279 &mut self,
280 attrs: &[ast::Attribute],
281 target_span: Span,
282 target: Target,
283 omit_doc: OmitDoc,
284 lower_span: impl Copy + Fn(Span) -> Span,
285 mut emit_lint: impl FnMut(LintId, MultiSpan, EmitAttribute),
286 ) -> Vec<Attribute> {
287 let mut attributes = Vec::new();
288 let mut attr_paths: Vec<RefPathParser<'_>> = Vec::new();
289 let mut early_parsed_state = EarlyParsedState::default();
290
291 let mut finalizers: Vec<FinalizeFn> = Vec::with_capacity(attrs.len());
292
293 for attr in attrs {
294 if let Some(expected) = self.parse_only {
296 if !attr.path_matches(expected) {
297 continue;
298 }
299 }
300
301 let is_doc_attribute = attr.has_name(sym::doc);
307 if omit_doc == OmitDoc::Skip && is_doc_attribute {
308 continue;
309 }
310
311 let attr_span = lower_span(attr.span);
312 match &attr.kind {
313 ast::AttrKind::DocComment(comment_kind, symbol) => {
314 if omit_doc == OmitDoc::Skip {
315 continue;
316 }
317
318 attributes.push(Attribute::Parsed(AttributeKind::DocComment {
319 style: attr.style,
320 kind: DocFragmentKind::Sugared(*comment_kind),
321 span: attr_span,
322 comment: *symbol,
323 }));
324 }
325 ast::AttrKind::Normal(n) => {
326 attr_paths.push(PathParser(&n.item.path));
327 let attr_path = AttrPath::from_ast(&n.item.path, lower_span);
328
329 let args = match &n.item.args {
330 AttrItemKind::Unparsed(args) => args,
331 AttrItemKind::Parsed(parsed) => {
332 early_parsed_state
333 .accept_early_parsed_attribute(attr_span, lower_span, parsed);
334 continue;
335 }
336 };
337
338 let parts =
339 n.item.path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>();
340 let inner_span = lower_span(n.item.span());
341
342 if let Some(accept) = ATTRIBUTE_PARSERS.accepters.get(parts.as_slice()) {
343 self.check_attribute_safety(
344 &attr_path,
345 inner_span,
346 n.item.unsafety,
347 accept.safety,
348 &mut emit_lint,
349 );
350
351 let Some(args) = ArgParser::from_attr_args(
352 args,
353 &parts,
354 &self.sess.psess,
355 self.should_emit,
356 AllowExprMetavar::No,
357 ) else {
358 continue;
359 };
360
361 if is_doc_attribute
377 && let ArgParser::NameValue(nv) = &args
378 && let Some(comment) = nv.value_as_str()
382 {
383 attributes.push(Attribute::Parsed(AttributeKind::DocComment {
384 style: attr.style,
385 kind: DocFragmentKind::Raw(nv.value_span),
386 span: attr_span,
387 comment,
388 }));
389 continue;
390 }
391
392 let mut cx: AcceptContext<'_, 'sess> = AcceptContext {
393 shared: SharedContext {
394 cx: self,
395 target_span,
396 target,
397 emit_lint: &mut emit_lint,
398 },
399 attr_span,
400 inner_span,
401 attr_style: attr.style,
402 parsed_description: ParsedDescription::Attribute,
403 template: &accept.template,
404 attr_safety: n.item.unsafety,
405 attr_path: attr_path.clone(),
406 };
407
408 (accept.accept_fn)(&mut cx, &args);
409 finalizers.push(accept.finalizer);
410
411 Self::check_target(&accept.allowed_targets, &mut cx);
412 } else {
413 let attr = AttrItem {
414 path: attr_path.clone(),
415 args: self
416 .lower_attr_args(n.item.args.unparsed_ref().unwrap(), lower_span),
417 id: HashIgnoredAttrId { attr_id: attr.id },
418 style: attr.style,
419 span: attr_span,
420 };
421
422 self.check_attribute_safety(
423 &attr_path,
424 inner_span,
425 n.item.unsafety,
426 AttributeSafety::Normal,
427 &mut emit_lint,
428 );
429
430 if !#[allow(non_exhaustive_omitted_patterns)] match self.should_emit {
ShouldEmit::Nothing => true,
_ => false,
}matches!(self.should_emit, ShouldEmit::Nothing)
431 && target == Target::Crate
432 {
433 self.check_invalid_crate_level_attr_item(&attr, inner_span);
434 }
435
436 attributes.push(Attribute::Unparsed(Box::new(attr)));
437 };
438 }
439 }
440 }
441
442 early_parsed_state.finalize_early_parsed_attributes(&mut attributes);
443 for f in &finalizers {
444 if let Some(attr) = f(&mut FinalizeContext {
445 shared: SharedContext { cx: self, target_span, target, emit_lint: &mut emit_lint },
446 all_attrs: &attr_paths,
447 }) {
448 attributes.push(Attribute::Parsed(attr));
449 }
450 }
451
452 if !#[allow(non_exhaustive_omitted_patterns)] match self.should_emit {
ShouldEmit::Nothing => true,
_ => false,
}matches!(self.should_emit, ShouldEmit::Nothing) && target == Target::WherePredicate {
453 self.check_invalid_where_predicate_attrs(attributes.iter());
454 }
455
456 attributes
457 }
458
459 pub fn is_parsed_attribute(path: &[Symbol]) -> bool {
461 const SPECIAL_ATTRIBUTES: &[&[Symbol]] = &[
464 &[sym::cfg],
466 &[sym::cfg_attr],
467 ];
468
469 ATTRIBUTE_PARSERS.accepters.contains_key(path)
470 || EARLY_PARSED_ATTRIBUTES.contains(&path)
471 || SPECIAL_ATTRIBUTES.contains(&path)
472 }
473
474 fn lower_attr_args(&self, args: &ast::AttrArgs, lower_span: impl Fn(Span) -> Span) -> AttrArgs {
475 match args {
476 ast::AttrArgs::Empty => AttrArgs::Empty,
477 ast::AttrArgs::Delimited(args) => AttrArgs::Delimited(args.clone()),
478 ast::AttrArgs::Eq { eq_span, expr } => {
482 let lit = if let ast::ExprKind::Lit(token_lit) = expr.kind
485 && let Ok(lit) =
486 ast::MetaItemLit::from_token_lit(token_lit, lower_span(expr.span))
487 {
488 lit
489 } else {
490 let guar = self.dcx().span_delayed_bug(
491 args.span().unwrap_or(DUMMY_SP),
492 "expr in place where literal is expected (builtin attr parsing)",
493 );
494 ast::MetaItemLit {
495 symbol: sym::dummy,
496 suffix: None,
497 kind: ast::LitKind::Err(guar),
498 span: DUMMY_SP,
499 }
500 };
501 AttrArgs::Eq { eq_span: lower_span(*eq_span), expr: lit }
502 }
503 }
504 }
505}