Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 75d2715

Browse files
Use the new parsed proc macro attributes throughout the codebase
Signed-off-by: Jonathan Brouwer <jonathantbrouwer@gmail.com>
1 parent 7b23769 commit 75d2715

File tree

9 files changed

+84
-70
lines changed

9 files changed

+84
-70
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: 20 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,17 @@ 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+
)
103116
else {
104117
return;
105118
};
@@ -110,7 +123,7 @@ impl<'a> CollectProcMacros<'a> {
110123
span: item.span,
111124
trait_name,
112125
function_ident,
113-
attrs: proc_attrs,
126+
attrs: helper_attrs,
114127
}));
115128
} else {
116129
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
@@ -844,7 +844,7 @@ impl SyntaxExtension {
844844
/// | (unspecified) | no | if-ext | if-ext | yes |
845845
/// | external | no | if-ext | if-ext | yes |
846846
/// | yes | yes | yes | yes | yes |
847-
fn get_collapse_debuginfo(sess: &Session, attrs: &[implAttributeExt], ext: bool) -> bool {
847+
fn get_collapse_debuginfo(sess: &Session, attrs: &[hir::Attribute], ext: bool) -> bool {
848848
let flag = sess.opts.cg.collapse_macro_debuginfo;
849849
let attr = ast::attr::find_by_name(attrs, sym::collapse_debuginfo)
850850
.and_then(|attr| {
@@ -855,7 +855,7 @@ impl SyntaxExtension {
855855
.ok()
856856
})
857857
.unwrap_or_else(|| {
858-
if ast::attr::contains_name(attrs, sym::rustc_builtin_macro) {
858+
if find_attr!(attrs, AttributeKind::RustcBuiltinMacro{ .. }) {
859859
CollapseMacroDebuginfo::Yes
860860
} else {
861861
CollapseMacroDebuginfo::Unspecified
@@ -898,16 +898,18 @@ impl SyntaxExtension {
898898
let collapse_debuginfo = Self::get_collapse_debuginfo(sess, attrs, !is_local);
899899
tracing::debug!(?name, ?local_inner_macros, ?collapse_debuginfo, ?allow_internal_unsafe);
900900

901-
let (builtin_name, helper_attrs) = ast::attr::find_by_name(attrs, sym::rustc_builtin_macro)
902-
.map(|attr| {
903-
// Override `helper_attrs` passed above if it's a built-in macro,
904-
// marking `proc_macro_derive` macros as built-in is not a realistic use case.
905-
parse_macro_name_and_helper_attrs(sess.dcx(), attr, "built-in").map_or_else(
906-
|| (Some(name), Vec::new()),
907-
|(name, helper_attrs)| (Some(name), helper_attrs),
908-
)
909-
})
910-
.unwrap_or_else(|| (None, helper_attrs));
901+
let (builtin_name, helper_attrs) = match find_attr!(attrs, AttributeKind::RustcBuiltinMacro { builtin_name, helper_attrs, .. } => (builtin_name, helper_attrs))
902+
{
903+
// Override `helper_attrs` passed above if it's a built-in macro,
904+
// marking `proc_macro_derive` macros as built-in is not a realistic use case.
905+
Some((Some(name), helper_attrs)) => {
906+
(Some(*name), helper_attrs.iter().copied().collect())
907+
}
908+
Some((None, _)) => (Some(name), Vec::new()),
909+
910+
// Not a built-in macro
911+
None => (None, helper_attrs),
912+
};
911913

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

‎compiler/rustc_hir/src/hir.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,17 @@ impl AttributeExt for Attribute {
13561356
_ => None,
13571357
}
13581358
}
1359+
1360+
fn is_proc_macro_attr(&self) -> bool {
1361+
matches!(
1362+
self,
1363+
Attribute::Parsed(
1364+
AttributeKind::ProcMacro(..)
1365+
| AttributeKind::ProcMacroAttribute(..)
1366+
| AttributeKind::ProcMacroDerive { .. }
1367+
)
1368+
)
1369+
}
13591370
}
13601371

13611372
// 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};
@@ -1976,18 +1976,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19761976
// Proc-macros may have attributes like `#[allow_internal_unstable]`,
19771977
// so downstream crates need access to them.
19781978
let attrs = tcx.hir_attrs(proc_macro);
1979-
let macro_kind = if ast::attr::contains_name(attrs, sym::proc_macro) {
1979+
let macro_kind = if find_attr!(attrs, AttributeKind::ProcMacro(..)) {
19801980
MacroKind::Bang
1981-
} else if ast::attr::contains_name(attrs, sym::proc_macro_attribute) {
1981+
} else if find_attr!(attrs, AttributeKind::ProcMacroAttribute(..)) {
19821982
MacroKind::Attr
1983-
} else if let Some(attr) = ast::attr::find_by_name(attrs, sym::proc_macro_derive) {
1984-
// This unwrap chain should have been checked by the proc-macro harness.
1985-
name = attr.meta_item_list().unwrap()[0]
1986-
.meta_item()
1987-
.unwrap()
1988-
.ident()
1989-
.unwrap()
1990-
.name;
1983+
} else if let Some(trait_name) = find_attr!(attrs, AttributeKind::ProcMacroDerive { trait_name, ..} => trait_name)
1984+
{
1985+
name = *trait_name;
19911986
MacroKind::Derive
19921987
} else {
19931988
bug!("Unknown proc-macro type for item {:?}", id);

‎compiler/rustc_middle/src/ty/mod.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_abi::{Align, FieldIdx, Integer, IntegerType, ReprFlags, ReprOptions, V
2828
use rustc_ast::expand::StrippedCfgItem;
2929
use rustc_ast::node_id::NodeMap;
3030
pub use rustc_ast_ir::{Movability, Mutability, try_visit};
31-
use rustc_attr_data_structures::AttributeKind;
31+
use rustc_attr_data_structures::{AttributeKind, find_attr};
3232
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
3333
use rustc_data_structures::intern::Interned;
3434
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -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
@@ -300,6 +300,9 @@ pub fn check_builtin_meta_item(
300300
| sym::rustc_layout_scalar_valid_range_start
301301
| sym::rustc_layout_scalar_valid_range_end
302302
| sym::no_implicit_prelude
303+
| sym::proc_macro
304+
| sym::proc_macro_attribute
305+
| sym::proc_macro_derive
303306
) {
304307
return;
305308
}

‎src/librustdoc/clean/mod.rs‎

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use std::mem;
3636

3737
use rustc_ast::token::{Token, TokenKind};
3838
use rustc_ast::tokenstream::{TokenStream, TokenTree};
39+
use rustc_attr_data_structures::{AttributeKind, find_attr};
3940
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet, IndexEntry};
4041
use rustc_errors::codes::*;
4142
use rustc_errors::{FatalError, struct_span_code_err};
@@ -978,28 +979,17 @@ fn clean_proc_macro<'tcx>(
978979
kind: MacroKind,
979980
cx: &mut DocContext<'tcx>,
980981
) -> ItemKind {
981-
let attrs = cx.tcx.hir_attrs(item.hir_id());
982-
if kind == MacroKind::Derive
983-
&& let Some(derive_name) =
984-
hir_attr_lists(attrs, sym::proc_macro_derive).find_map(|mi| mi.ident())
985-
{
986-
*name = derive_name.name;
982+
if kind != MacroKind::Derive {
983+
return ProcMacroItem(ProcMacro { kind, helpers: vec![] });
987984
}
985+
let attrs = cx.tcx.hir_attrs(item.hir_id());
986+
let Some((trait_name, helper_attrs)) = find_attr!(attrs, AttributeKind::ProcMacroDerive { trait_name, helper_attrs, ..} => (*trait_name, helper_attrs))
987+
else {
988+
return ProcMacroItem(ProcMacro { kind, helpers: vec![] });
989+
};
990+
*name = trait_name;
991+
let helpers = helper_attrs.iter().copied().collect();
988992

989-
let mut helpers = Vec::new();
990-
for mi in hir_attr_lists(attrs, sym::proc_macro_derive) {
991-
if !mi.has_name(sym::attributes) {
992-
continue;
993-
}
994-
995-
if let Some(list) = mi.meta_item_list() {
996-
for inner_mi in list {
997-
if let Some(ident) = inner_mi.ident() {
998-
helpers.push(ident.name);
999-
}
1000-
}
1001-
}
1002-
}
1003993
ProcMacroItem(ProcMacro { kind, helpers })
1004994
}
1005995

@@ -1012,17 +1002,16 @@ fn clean_fn_or_proc_macro<'tcx>(
10121002
cx: &mut DocContext<'tcx>,
10131003
) -> ItemKind {
10141004
let attrs = cx.tcx.hir_attrs(item.hir_id());
1015-
let macro_kind = attrs.iter().find_map(|a| {
1016-
if a.has_name(sym::proc_macro) {
1017-
Some(MacroKind::Bang)
1018-
} else if a.has_name(sym::proc_macro_derive) {
1019-
Some(MacroKind::Derive)
1020-
} else if a.has_name(sym::proc_macro_attribute) {
1021-
Some(MacroKind::Attr)
1022-
} else {
1023-
None
1024-
}
1025-
});
1005+
let macro_kind = if find_attr!(attrs, AttributeKind::ProcMacro(..)) {
1006+
Some(MacroKind::Bang)
1007+
} else if find_attr!(attrs, AttributeKind::ProcMacroDerive { .. }) {
1008+
Some(MacroKind::Derive)
1009+
} else if find_attr!(attrs, AttributeKind::ProcMacroAttribute(..)) {
1010+
Some(MacroKind::Attr)
1011+
} else {
1012+
None
1013+
};
1014+
10261015
match macro_kind {
10271016
Some(kind) => clean_proc_macro(item, name, kind, cx),
10281017
None => {

‎src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
308308
/// Functions marked with these attributes must have the exact signature.
309309
pub(crate) fn requires_exact_signature(attrs: &[Attribute]) -> bool {
310310
attrs.iter().any(|attr| {
311-
[sym::proc_macro, sym::proc_macro_attribute, sym::proc_macro_derive]
312-
.iter()
313-
.any(|&allow| attr.has_name(allow))
311+
attr.is_proc_macro_attr()
314312
})
315313
}
316314

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /