Skip to content

Commit 21bbb8b

Browse files
Use the new attribute parser throughout the codebase
1 parent d8b5c69 commit 21bbb8b

File tree

4 files changed

+35
-27
lines changed

4 files changed

+35
-27
lines changed

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::borrow::Cow;
12
use std::cell::RefCell;
23
use std::collections::BTreeMap;
34
use std::ops::{Deref, DerefMut};
@@ -707,9 +708,9 @@ impl<'sess> AttributeParser<'sess, Early> {
707708
target_node_id: NodeId,
708709
features: Option<&'sess Features>,
709710
emit_errors: ShouldEmit,
710-
parse_fn: fn(cx: &mut AcceptContext<'_, '_, Early>, item: &ArgParser<'_>) -> T,
711+
parse_fn: fn(cx: &mut AcceptContext<'_, '_, Early>, item: &ArgParser<'_>) -> Option<T>,
711712
template: &AttributeTemplate,
712-
) -> T {
713+
) -> Option<T> {
713714
let mut parser = Self {
714715
features,
715716
tools: Vec::new(),
@@ -720,7 +721,7 @@ impl<'sess> AttributeParser<'sess, Early> {
720721
let ast::AttrKind::Normal(normal_attr) = &attr.kind else {
721722
panic!("parse_single called on a doc attr")
722723
};
723-
let meta_parser = MetaItemParser::from_attr(normal_attr, parser.dcx());
724+
let meta_parser = MetaItemParser::from_attr(normal_attr, &sess.psess, emit_errors)?;
724725
let path = meta_parser.path();
725726
let args = meta_parser.args();
726727
let mut cx: AcceptContext<'_, 'sess, Early> = AcceptContext {
@@ -825,14 +826,22 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
825826
// }))
826827
// }
827828
ast::AttrKind::Normal(n) => {
828-
attr_paths.push(PathParser::Ast(&n.item.path));
829+
attr_paths.push(PathParser(Cow::Borrowed(&n.item.path)));
829830

830-
let parser = MetaItemParser::from_attr(n, self.dcx());
831-
let path = parser.path();
832-
let args = parser.args();
833-
let parts = path.segments().map(|i| i.name).collect::<Vec<_>>();
831+
let parts =
832+
n.item.path.segments.iter().map(|seg| seg.ident.name).collect::<Vec<_>>();
834833

835834
if let Some(accepts) = S::parsers().0.get(parts.as_slice()) {
835+
let Some(parser) = MetaItemParser::from_attr(
836+
n,
837+
&self.sess.psess,
838+
self.stage.should_emit(),
839+
) else {
840+
continue;
841+
};
842+
let path = parser.path();
843+
let args = parser.args();
844+
836845
for (template, accept) in accepts {
837846
let mut cx: AcceptContext<'_, 'sess, S> = AcceptContext {
838847
shared: SharedContext {

compiler/rustc_attr_parsing/src/validate_attr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ pub fn check_attr(psess: &ParseSess, attr: &Attribute, id: NodeId) {
3333
// Check input tokens for built-in and key-value attributes.
3434
match builtin_attr_info {
3535
// `rustc_dummy` doesn't have any restrictions specific to built-in attributes.
36-
Some(BuiltinAttribute { name, template, .. }) if *name != sym::rustc_dummy => {
36+
Some(BuiltinAttribute { name, template, .. }) => {
37+
if AttributeParser::<Late>::is_parsed_attribute(slice::from_ref(&name)) {
38+
return;
39+
}
3740
match parse_meta(psess, attr) {
3841
// Don't check safety again, we just did that
3942
Ok(meta) => {
@@ -259,9 +262,6 @@ pub fn check_builtin_meta_item(
259262
) {
260263
if !is_attr_template_compatible(&template, &meta.kind) {
261264
// attrs with new parsers are locally validated so excluded here
262-
if AttributeParser::<Late>::is_parsed_attribute(slice::from_ref(&name)) {
263-
return;
264-
}
265265
emit_malformed_attribute(psess, style, meta.span, name, template);
266266
}
267267

compiler/rustc_expand/src/config.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -415,16 +415,6 @@ impl<'a> StripUnconfigured<'a> {
415415
node: NodeId,
416416
emit_errors: ShouldEmit,
417417
) -> EvalConfigResult {
418-
// We need to run this to do basic validation of the attribute, such as that lits are valid, etc
419-
// FIXME(jdonszelmann) this should not be necessary in the future
420-
match validate_attr::parse_meta(&self.sess.psess, attr) {
421-
Ok(_) => {}
422-
Err(err) => {
423-
err.emit();
424-
return EvalConfigResult::True;
425-
}
426-
}
427-
428418
// Unsafety check needs to be done explicitly here because this attribute will be removed before the normal check
429419
deny_builtin_meta_unsafety(
430420
self.sess.dcx(),

compiler/rustc_expand/src/expand.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
use std::path::PathBuf;
22
use std::rc::Rc;
33
use std::sync::Arc;
4-
use std::{iter, mem};
4+
use std::{iter, mem, slice};
55

66
use rustc_ast::mut_visit::*;
77
use rustc_ast::ptr::P;
88
use rustc_ast::tokenstream::TokenStream;
99
use rustc_ast::visit::{self, AssocCtxt, Visitor, VisitorResult, try_visit, walk_list};
1010
use rustc_ast::{
11-
self as ast, AssocItemKind, AstNodeWrapper, AttrArgs, AttrStyle, AttrVec, DUMMY_NODE_ID,
12-
ExprKind, ForeignItemKind, HasAttrs, HasNodeId, Inline, ItemKind, MacStmtStyle, MetaItemInner,
13-
MetaItemKind, ModKind, NodeId, PatKind, StmtKind, TyKind, token,
11+
self as ast, AssocItemKind, AstNodeWrapper, AttrArgs, AttrStyle, AttrVec, CRATE_NODE_ID,
12+
DUMMY_NODE_ID, ExprKind, ForeignItemKind, HasAttrs, HasNodeId, Inline, ItemKind, MacStmtStyle,
13+
MetaItemInner, MetaItemKind, ModKind, NodeId, PatKind, StmtKind, TyKind, token,
1414
};
1515
use rustc_ast_pretty::pprust;
16-
use rustc_attr_parsing::{EvalConfigResult, ShouldEmit, validate_attr};
16+
use rustc_attr_parsing::{AttributeParser, EvalConfigResult, ShouldEmit, validate_attr};
1717
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
1818
use rustc_errors::PResult;
1919
use rustc_feature::Features;
@@ -2129,6 +2129,15 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
21292129
attr,
21302130
self.cx.current_expansion.lint_node_id,
21312131
);
2132+
AttributeParser::parse_limited_all(
2133+
self.cx.sess,
2134+
slice::from_ref(attr),
2135+
None,
2136+
call.span(),
2137+
CRATE_NODE_ID,
2138+
Some(self.cx.ecfg.features),
2139+
ShouldEmit::ErrorsAndLints,
2140+
);
21322141

21332142
let current_span = if let Some(sp) = span { sp.to(attr.span) } else { attr.span };
21342143
span = Some(current_span);

0 commit comments

Comments
 (0)