Skip to content

Commit a94f6e0

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

File tree

7 files changed

+63
-35
lines changed

7 files changed

+63
-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
@@ -864,7 +864,7 @@ impl SyntaxExtension {
864864
/// | (unspecified) | no | if-ext | if-ext | yes |
865865
/// | external | no | if-ext | if-ext | yes |
866866
/// | yes | yes | yes | yes | yes |
867-
fn get_collapse_debuginfo(sess: &Session, attrs: &[impl AttributeExt], ext: bool) -> bool {
867+
fn get_collapse_debuginfo(sess: &Session, attrs: &[hir::Attribute], ext: bool) -> bool {
868868
let flag = sess.opts.cg.collapse_macro_debuginfo;
869869
let attr = ast::attr::find_by_name(attrs, sym::collapse_debuginfo)
870870
.and_then(|attr| {
@@ -875,7 +875,7 @@ impl SyntaxExtension {
875875
.ok()
876876
})
877877
.unwrap_or_else(|| {
878-
if ast::attr::contains_name(attrs, sym::rustc_builtin_macro) {
878+
if find_attr!(attrs, AttributeKind::RustcBuiltinMacro { .. }) {
879879
CollapseMacroDebuginfo::Yes
880880
} else {
881881
CollapseMacroDebuginfo::Unspecified
@@ -918,16 +918,18 @@ impl SyntaxExtension {
918918
let collapse_debuginfo = Self::get_collapse_debuginfo(sess, attrs, !is_local);
919919
tracing::debug!(?name, ?local_inner_macros, ?collapse_debuginfo, ?allow_internal_unsafe);
920920

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

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

compiler/rustc_hir/src/hir.rs

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

13671378
// 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 {

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ pub fn check_builtin_meta_item(
306306
| sym::non_exhaustive
307307
| sym::omit_gdb_pretty_printer_section
308308
| sym::path
309+
| sym::proc_macro
310+
| sym::proc_macro_attribute
311+
| sym::proc_macro_derive
309312
| sym::ignore
310313
| sym::must_use
311314
| sym::track_caller

0 commit comments

Comments
 (0)