Skip to content

Commit ec63700

Browse files
Use the new attributes throughout the codebase
Signed-off-by: Jonathan Brouwer <[email protected]>
1 parent 2f50c78 commit ec63700

File tree

6 files changed

+60
-35
lines changed

6 files changed

+60
-35
lines changed

compiler/rustc_attr_parsing/src/attributes/proc_macro_attrs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,17 @@ fn parse_derive_like<S: Stage>(
6161
trait_name_mandatory: bool,
6262
) -> Option<(Option<Symbol>, ThinVec<Symbol>)> {
6363
let Some(list) = args.list() else {
64+
// For #[rustc_builtin_macro], it is permitted to leave out the trait name
65+
if args.no_args().is_ok() && !trait_name_mandatory {
66+
return Some((None, ThinVec::new()));
67+
}
6468
cx.expected_list(cx.attr_span);
6569
return None;
6670
};
6771
let mut items = list.mixed();
6872

6973
// Parse the name of the trait that is derived.
7074
let Some(trait_attr) = items.next() else {
71-
// For #[rustc_builtin_macro], it is permitted to leave out the trait name
72-
if !trait_name_mandatory {
73-
return None;
74-
}
7575
cx.expected_at_least_one_argument(list.span);
7676
return None;
7777
};

compiler/rustc_builtin_macros/src/proc_macro_harness.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
use std::mem;
1+
use std::{mem, slice};
22

33
use rustc_ast::ptr::P;
44
use rustc_ast::visit::{self, Visitor};
5-
use rustc_ast::{self as ast, NodeId, attr};
5+
use rustc_ast::{self as ast, HasNodeId, NodeId, attr};
66
use rustc_ast_pretty::pprust;
7+
use rustc_attr_data_structures::AttributeKind;
8+
use rustc_attr_parsing::AttributeParser;
79
use rustc_errors::DiagCtxtHandle;
8-
use rustc_expand::base::{ExtCtxt, ResolverExpand, parse_macro_name_and_helper_attrs};
10+
use rustc_expand::base::{ExtCtxt, ResolverExpand};
911
use rustc_expand::expand::{AstFragment, ExpansionConfig};
1012
use rustc_feature::Features;
1113
use rustc_session::Session;
@@ -22,7 +24,7 @@ struct ProcMacroDerive {
2224
trait_name: Symbol,
2325
function_ident: Ident,
2426
span: Span,
25-
attrs: Vec<Symbol>,
27+
attrs: ThinVec<Symbol>,
2628
}
2729

2830
struct ProcMacroDef {
@@ -41,6 +43,7 @@ struct CollectProcMacros<'a> {
4143
macros: Vec<ProcMacro>,
4244
in_root: bool,
4345
dcx: DiagCtxtHandle<'a>,
46+
session: &'a Session,
4447
source_map: &'a SourceMap,
4548
is_proc_macro_crate: bool,
4649
is_test_crate: bool,
@@ -63,6 +66,7 @@ pub fn inject(
6366
macros: Vec::new(),
6467
in_root: true,
6568
dcx,
69+
session: sess,
6670
source_map: sess.source_map(),
6771
is_proc_macro_crate,
6872
is_test_crate,
@@ -98,8 +102,18 @@ impl<'a> CollectProcMacros<'a> {
98102
function_ident: Ident,
99103
attr: &'a ast::Attribute,
100104
) {
101-
let Some((trait_name, proc_attrs)) =
102-
parse_macro_name_and_helper_attrs(self.dcx, attr, "derive")
105+
let Some(rustc_hir::Attribute::Parsed(AttributeKind::ProcMacroDerive {
106+
trait_name,
107+
helper_attrs,
108+
..
109+
})) = AttributeParser::parse_limited(
110+
self.session,
111+
slice::from_ref(attr),
112+
sym::proc_macro_derive,
113+
item.span,
114+
item.node_id(),
115+
None,
116+
)
103117
else {
104118
return;
105119
};
@@ -110,7 +124,7 @@ impl<'a> CollectProcMacros<'a> {
110124
span: item.span,
111125
trait_name,
112126
function_ident,
113-
attrs: proc_attrs,
127+
attrs: helper_attrs,
114128
}));
115129
} else {
116130
let msg = if !self.in_root {

compiler/rustc_expand/src/base.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ impl SyntaxExtension {
861861
/// | (unspecified) | no | if-ext | if-ext | yes |
862862
/// | external | no | if-ext | if-ext | yes |
863863
/// | yes | yes | yes | yes | yes |
864-
fn get_collapse_debuginfo(sess: &Session, attrs: &[impl AttributeExt], ext: bool) -> bool {
864+
fn get_collapse_debuginfo(sess: &Session, attrs: &[hir::Attribute], ext: bool) -> bool {
865865
let flag = sess.opts.cg.collapse_macro_debuginfo;
866866
let attr = ast::attr::find_by_name(attrs, sym::collapse_debuginfo)
867867
.and_then(|attr| {
@@ -872,7 +872,7 @@ impl SyntaxExtension {
872872
.ok()
873873
})
874874
.unwrap_or_else(|| {
875-
if ast::attr::contains_name(attrs, sym::rustc_builtin_macro) {
875+
if find_attr!(attrs, AttributeKind::RustcBuiltinMacro { .. }) {
876876
CollapseMacroDebuginfo::Yes
877877
} else {
878878
CollapseMacroDebuginfo::Unspecified
@@ -915,16 +915,18 @@ impl SyntaxExtension {
915915
let collapse_debuginfo = Self::get_collapse_debuginfo(sess, attrs, !is_local);
916916
tracing::debug!(?name, ?local_inner_macros, ?collapse_debuginfo, ?allow_internal_unsafe);
917917

918-
let (builtin_name, helper_attrs) = ast::attr::find_by_name(attrs, sym::rustc_builtin_macro)
919-
.map(|attr| {
920-
// Override `helper_attrs` passed above if it's a built-in macro,
921-
// marking `proc_macro_derive` macros as built-in is not a realistic use case.
922-
parse_macro_name_and_helper_attrs(sess.dcx(), attr, "built-in").map_or_else(
923-
|| (Some(name), Vec::new()),
924-
|(name, helper_attrs)| (Some(name), helper_attrs),
925-
)
926-
})
927-
.unwrap_or_else(|| (None, helper_attrs));
918+
let (builtin_name, helper_attrs) = match find_attr!(attrs, AttributeKind::RustcBuiltinMacro { builtin_name, helper_attrs, .. } => (builtin_name, helper_attrs))
919+
{
920+
// Override `helper_attrs` passed above if it's a built-in macro,
921+
// marking `proc_macro_derive` macros as built-in is not a realistic use case.
922+
Some((Some(name), helper_attrs)) => {
923+
(Some(*name), helper_attrs.iter().copied().collect())
924+
}
925+
Some((None, _)) => (Some(name), Vec::new()),
926+
927+
// Not a built-in macro
928+
None => (None, helper_attrs),
929+
};
928930

929931
let stability = find_attr!(attrs, AttributeKind::Stability { stability, .. } => *stability);
930932

compiler/rustc_hir/src/hir.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,17 @@ impl AttributeExt for Attribute {
13631363
_ => None,
13641364
}
13651365
}
1366+
1367+
fn is_proc_macro_attr(&self) -> bool {
1368+
matches!(
1369+
self,
1370+
Attribute::Parsed(
1371+
AttributeKind::ProcMacro(..)
1372+
| AttributeKind::ProcMacroAttribute(..)
1373+
| AttributeKind::ProcMacroDerive { .. }
1374+
)
1375+
)
1376+
}
13661377
}
13671378

13681379
// FIXME(fn_delegation): use function delegation instead of manually forwarding

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::io::{Read, Seek, Write};
55
use std::path::{Path, PathBuf};
66
use std::sync::Arc;
77

8-
use rustc_attr_data_structures::EncodeCrossCrate;
8+
use rustc_attr_data_structures::{AttributeKind, EncodeCrossCrate, find_attr};
99
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
1010
use rustc_data_structures::memmap::{Mmap, MmapMut};
1111
use rustc_data_structures::sync::{join, par_for_each_in};
@@ -1965,18 +1965,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19651965
// Proc-macros may have attributes like `#[allow_internal_unstable]`,
19661966
// so downstream crates need access to them.
19671967
let attrs = tcx.hir_attrs(proc_macro);
1968-
let macro_kind = if ast::attr::contains_name(attrs, sym::proc_macro) {
1968+
let macro_kind = if find_attr!(attrs, AttributeKind::ProcMacro(..)) {
19691969
MacroKind::Bang
1970-
} else if ast::attr::contains_name(attrs, sym::proc_macro_attribute) {
1970+
} else if find_attr!(attrs, AttributeKind::ProcMacroAttribute(..)) {
19711971
MacroKind::Attr
1972-
} else if let Some(attr) = ast::attr::find_by_name(attrs, sym::proc_macro_derive) {
1973-
// This unwrap chain should have been checked by the proc-macro harness.
1974-
name = attr.meta_item_list().unwrap()[0]
1975-
.meta_item()
1976-
.unwrap()
1977-
.ident()
1978-
.unwrap()
1979-
.name;
1972+
} else if let Some(trait_name) = find_attr!(attrs, AttributeKind::ProcMacroDerive { trait_name, ..} => trait_name)
1973+
{
1974+
name = *trait_name;
19801975
MacroKind::Derive
19811976
} else {
19821977
bug!("Unknown proc-macro type for item {:?}", id);

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2024,7 +2024,10 @@ impl<'tcx> TyCtxt<'tcx> {
20242024
&& let Some(def_id) = def_id.as_local()
20252025
&& let outer = self.def_span(def_id).ctxt().outer_expn_data()
20262026
&& matches!(outer.kind, ExpnKind::Macro(MacroKind::Derive, _))
2027-
&& self.has_attr(outer.macro_def_id.unwrap(), sym::rustc_builtin_macro)
2027+
&& find_attr!(
2028+
self.get_all_attrs(outer.macro_def_id.unwrap()),
2029+
AttributeKind::RustcBuiltinMacro { .. }
2030+
)
20282031
{
20292032
true
20302033
} else {

0 commit comments

Comments
 (0)