Skip to content

Commit 8872d90

Browse files
authored
Rollup merge of #69989 - petrochenkov:nolegacy, r=eddyb,matthewjasper
resolve/hygiene: `macro_rules` are not "legacy" The "modern" vs "legacy" naming was introduced by jseyfried during initial implementation of macros 2.0. At this point it's clear that `macro_rules` are not going anywhere and won't be deprecated in the near future. So this PR changes the naming "legacy" (when it implies "macro_rules") to "macro_rules". This should also help people reading this code because it's wasn't obvious that "legacy" actually meant "macro_rules" in these contexts. The most contentious renaming here is probably ``` fn modern -> fn normalize_to_macros_2_0 fn modern_and_legacy -> fn normalize_to_macro_rules ``` Other alternatives that I could think of are `normalize_to_opaque`/`normalize_to_semitransparent`, or `strip_non_opaque`/`strip_transparent`, but they seemed less intuitive. The documentation to these functions can be found in `symbol.rs`. r? @matthewjasper
2 parents 8f2482b + db638bd commit 8872d90

File tree

30 files changed

+260
-211
lines changed

30 files changed

+260
-211
lines changed

src/librustc/ty/mod.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -3083,7 +3083,7 @@ impl<'tcx> TyCtxt<'tcx> {
30833083
pub fn hygienic_eq(self, use_name: Ident, def_name: Ident, def_parent_def_id: DefId) -> bool {
30843084
// We could use `Ident::eq` here, but we deliberately don't. The name
30853085
// comparison fails frequently, and we want to avoid the expensive
3086-
// `modern()` calls required for the span comparison whenever possible.
3086+
// `normalize_to_macros_2_0()` calls required for the span comparison whenever possible.
30873087
use_name.name == def_name.name
30883088
&& use_name
30893089
.span
@@ -3099,7 +3099,7 @@ impl<'tcx> TyCtxt<'tcx> {
30993099
}
31003100

31013101
pub fn adjust_ident(self, mut ident: Ident, scope: DefId) -> Ident {
3102-
ident.span.modernize_and_adjust(self.expansion_that_defined(scope));
3102+
ident.span.normalize_to_macros_2_0_and_adjust(self.expansion_that_defined(scope));
31033103
ident
31043104
}
31053105

@@ -3109,12 +3109,14 @@ impl<'tcx> TyCtxt<'tcx> {
31093109
scope: DefId,
31103110
block: hir::HirId,
31113111
) -> (Ident, DefId) {
3112-
let scope = match ident.span.modernize_and_adjust(self.expansion_that_defined(scope)) {
3113-
Some(actual_expansion) => {
3114-
self.hir().definitions().parent_module_of_macro_def(actual_expansion)
3115-
}
3116-
None => self.parent_module(block),
3117-
};
3112+
let scope =
3113+
match ident.span.normalize_to_macros_2_0_and_adjust(self.expansion_that_defined(scope))
3114+
{
3115+
Some(actual_expansion) => {
3116+
self.hir().definitions().parent_module_of_macro_def(actual_expansion)
3117+
}
3118+
None => self.parent_module(block),
3119+
};
31183120
(ident, scope)
31193121
}
31203122

src/librustc_ast/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ impl MacDelimiter {
14501450
pub struct MacroDef {
14511451
pub body: P<MacArgs>,
14521452
/// `true` if macro was defined with `macro_rules`.
1453-
pub legacy: bool,
1453+
pub macro_rules: bool,
14541454
}
14551455

14561456
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Copy, Hash, Eq, PartialEq)]

src/librustc_ast/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ pub fn noop_visit_mac<T: MutVisitor>(mac: &mut MacCall, vis: &mut T) {
591591
}
592592

593593
pub fn noop_visit_macro_def<T: MutVisitor>(macro_def: &mut MacroDef, vis: &mut T) {
594-
let MacroDef { body, legacy: _ } = macro_def;
594+
let MacroDef { body, macro_rules: _ } = macro_def;
595595
visit_mac_args(body, vis);
596596
}
597597

src/librustc_ast_lowering/item.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
115115
_ => &[],
116116
};
117117
let lt_def_names = parent_generics.iter().filter_map(|param| match param.kind {
118-
hir::GenericParamKind::Lifetime { .. } => Some(param.name.modern()),
118+
hir::GenericParamKind::Lifetime { .. } => Some(param.name.normalize_to_macros_2_0()),
119119
_ => None,
120120
});
121121
self.in_scope_lifetimes.extend(lt_def_names);
@@ -220,8 +220,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
220220
let mut vis = self.lower_visibility(&i.vis, None);
221221
let attrs = self.lower_attrs(&i.attrs);
222222

223-
if let ItemKind::MacroDef(MacroDef { ref body, legacy }) = i.kind {
224-
if !legacy || attr::contains_name(&i.attrs, sym::macro_export) {
223+
if let ItemKind::MacroDef(MacroDef { ref body, macro_rules }) = i.kind {
224+
if !macro_rules || attr::contains_name(&i.attrs, sym::macro_export) {
225225
let hir_id = self.lower_node_id(i.id);
226226
let body = P(self.lower_mac_args(body));
227227
self.exported_macros.push(hir::MacroDef {
@@ -230,7 +230,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
230230
attrs,
231231
hir_id,
232232
span: i.span,
233-
ast: MacroDef { body, legacy },
233+
ast: MacroDef { body, macro_rules },
234234
});
235235
} else {
236236
self.non_exported_macro_attrs.extend(attrs.iter().cloned());

src/librustc_ast_lowering/lib.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ struct LoweringContext<'a, 'hir: 'a> {
153153
/// against this list to see if it is already in-scope, or if a definition
154154
/// needs to be created for it.
155155
///
156-
/// We always store a `modern()` version of the param-name in this
156+
/// We always store a `normalize_to_macros_2_0()` version of the param-name in this
157157
/// vector.
158158
in_scope_lifetimes: Vec<ParamName>,
159159

@@ -803,14 +803,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
803803
return;
804804
}
805805

806-
if self.in_scope_lifetimes.contains(&ParamName::Plain(ident.modern())) {
806+
if self.in_scope_lifetimes.contains(&ParamName::Plain(ident.normalize_to_macros_2_0())) {
807807
return;
808808
}
809809

810810
let hir_name = ParamName::Plain(ident);
811811

812-
if self.lifetimes_to_define.iter().any(|(_, lt_name)| lt_name.modern() == hir_name.modern())
813-
{
812+
if self.lifetimes_to_define.iter().any(|(_, lt_name)| {
813+
lt_name.normalize_to_macros_2_0() == hir_name.normalize_to_macros_2_0()
814+
}) {
814815
return;
815816
}
816817

@@ -838,7 +839,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
838839
) -> T {
839840
let old_len = self.in_scope_lifetimes.len();
840841
let lt_def_names = params.iter().filter_map(|param| match param.kind {
841-
GenericParamKind::Lifetime { .. } => Some(ParamName::Plain(param.ident.modern())),
842+
GenericParamKind::Lifetime { .. } => {
843+
Some(ParamName::Plain(param.ident.normalize_to_macros_2_0()))
844+
}
842845
_ => None,
843846
});
844847
self.in_scope_lifetimes.extend(lt_def_names);

src/librustc_ast_passes/feature_gate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
366366
gate_feature_post!(&self, trait_alias, i.span, "trait aliases are experimental");
367367
}
368368

369-
ast::ItemKind::MacroDef(ast::MacroDef { legacy: false, .. }) => {
369+
ast::ItemKind::MacroDef(ast::MacroDef { macro_rules: false, .. }) => {
370370
let msg = "`macro` is experimental";
371371
gate_feature_post!(&self, decl_macro, i.span, msg);
372372
}

src/librustc_ast_pretty/pprust.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,7 @@ impl<'a> State<'a> {
12381238
}
12391239
}
12401240
ast::ItemKind::MacroDef(ref macro_def) => {
1241-
let (kw, has_bang) = if macro_def.legacy {
1241+
let (kw, has_bang) = if macro_def.macro_rules {
12421242
("macro_rules", true)
12431243
} else {
12441244
self.print_visibility(&item.vis);

src/librustc_attr/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ pub enum TransparencyError {
10241024

10251025
pub fn find_transparency(
10261026
attrs: &[Attribute],
1027-
is_legacy: bool,
1027+
macro_rules: bool,
10281028
) -> (Transparency, Option<TransparencyError>) {
10291029
let mut transparency = None;
10301030
let mut error = None;
@@ -1049,7 +1049,7 @@ pub fn find_transparency(
10491049
}
10501050
}
10511051
}
1052-
let fallback = if is_legacy { Transparency::SemiTransparent } else { Transparency::Opaque };
1052+
let fallback = if macro_rules { Transparency::SemiTransparent } else { Transparency::Opaque };
10531053
(transparency.map_or(fallback, |t| t.0), error)
10541054
}
10551055

src/librustc_expand/mbe/macro_check.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,10 @@ fn check_nested_occurrences(
419419
| (NestedMacroState::MacroName, &TokenTree::Delimited(_, ref del))
420420
if del.delim == DelimToken::Brace =>
421421
{
422-
let legacy = state == NestedMacroState::MacroRulesNotName;
422+
let macro_rules = state == NestedMacroState::MacroRulesNotName;
423423
state = NestedMacroState::Empty;
424424
let rest =
425-
check_nested_macro(sess, node_id, legacy, &del.tts, &nested_macros, valid);
425+
check_nested_macro(sess, node_id, macro_rules, &del.tts, &nested_macros, valid);
426426
// If we did not check the whole macro definition, then check the rest as if outside
427427
// the macro definition.
428428
check_nested_occurrences(
@@ -493,21 +493,21 @@ fn check_nested_occurrences(
493493
/// Arguments:
494494
/// - `sess` is used to emit diagnostics and lints
495495
/// - `node_id` is used to emit lints
496-
/// - `legacy` specifies whether the macro is legacy
496+
/// - `macro_rules` specifies whether the macro is `macro_rules`
497497
/// - `tts` is checked as a list of (LHS) => {RHS}
498498
/// - `macros` is the stack of outer macros
499499
/// - `valid` is set in case of errors
500500
fn check_nested_macro(
501501
sess: &ParseSess,
502502
node_id: NodeId,
503-
legacy: bool,
503+
macro_rules: bool,
504504
tts: &[TokenTree],
505505
macros: &Stack<'_, MacroState<'_>>,
506506
valid: &mut bool,
507507
) -> usize {
508508
let n = tts.len();
509509
let mut i = 0;
510-
let separator = if legacy { TokenKind::Semi } else { TokenKind::Comma };
510+
let separator = if macro_rules { TokenKind::Semi } else { TokenKind::Comma };
511511
loop {
512512
// We expect 3 token trees: `(LHS) => {RHS}`. The separator is checked after.
513513
if i + 2 >= n
@@ -522,7 +522,7 @@ fn check_nested_macro(
522522
let mut binders = Binders::default();
523523
check_binders(sess, node_id, lhs, macros, &mut binders, &Stack::Empty, valid);
524524
check_occurrences(sess, node_id, rhs, macros, &binders, &Stack::Empty, valid);
525-
// Since the last semicolon is optional for legacy macros and decl_macro are not terminated,
525+
// Since the last semicolon is optional for `macro_rules` macros and decl_macro are not terminated,
526526
// we increment our checked position by how many token trees we already checked (the 3
527527
// above) before checking for the separator.
528528
i += 3;

src/librustc_expand/mbe/macro_rules.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ pub fn compile_declarative_macro(
350350
let tt_spec = ast::Ident::new(sym::tt, def.span);
351351

352352
// Parse the macro_rules! invocation
353-
let (is_legacy, body) = match &def.kind {
354-
ast::ItemKind::MacroDef(macro_def) => (macro_def.legacy, macro_def.body.inner_tokens()),
353+
let (macro_rules, body) = match &def.kind {
354+
ast::ItemKind::MacroDef(def) => (def.macro_rules, def.body.inner_tokens()),
355355
_ => unreachable!(),
356356
};
357357

@@ -370,7 +370,7 @@ pub fn compile_declarative_macro(
370370
mbe::TokenTree::MetaVarDecl(def.span, rhs_nm, tt_spec),
371371
],
372372
separator: Some(Token::new(
373-
if is_legacy { token::Semi } else { token::Comma },
373+
if macro_rules { token::Semi } else { token::Comma },
374374
def.span,
375375
)),
376376
kleene: mbe::KleeneToken::new(mbe::KleeneOp::OneOrMore, def.span),
@@ -382,7 +382,7 @@ pub fn compile_declarative_macro(
382382
DelimSpan::dummy(),
383383
Lrc::new(mbe::SequenceRepetition {
384384
tts: vec![mbe::TokenTree::token(
385-
if is_legacy { token::Semi } else { token::Comma },
385+
if macro_rules { token::Semi } else { token::Comma },
386386
def.span,
387387
)],
388388
separator: None,
@@ -456,7 +456,7 @@ pub fn compile_declarative_macro(
456456
// that is not lint-checked and trigger the "failed to process buffered lint here" bug.
457457
valid &= macro_check::check_meta_variables(sess, ast::CRATE_NODE_ID, def.span, &lhses, &rhses);
458458

459-
let (transparency, transparency_error) = attr::find_transparency(&def.attrs, is_legacy);
459+
let (transparency, transparency_error) = attr::find_transparency(&def.attrs, macro_rules);
460460
match transparency_error {
461461
Some(TransparencyError::UnknownTransparency(value, span)) => {
462462
diag.span_err(span, &format!("unknown macro transparency: `{}`", value))

src/librustc_hir/hir.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ impl ParamName {
7979
}
8080
}
8181

82-
pub fn modern(&self) -> ParamName {
82+
pub fn normalize_to_macros_2_0(&self) -> ParamName {
8383
match *self {
84-
ParamName::Plain(ident) => ParamName::Plain(ident.modern()),
84+
ParamName::Plain(ident) => ParamName::Plain(ident.normalize_to_macros_2_0()),
8585
param_name => param_name,
8686
}
8787
}
@@ -151,9 +151,11 @@ impl LifetimeName {
151151
self == &LifetimeName::Static
152152
}
153153

154-
pub fn modern(&self) -> LifetimeName {
154+
pub fn normalize_to_macros_2_0(&self) -> LifetimeName {
155155
match *self {
156-
LifetimeName::Param(param_name) => LifetimeName::Param(param_name.modern()),
156+
LifetimeName::Param(param_name) => {
157+
LifetimeName::Param(param_name.normalize_to_macros_2_0())
158+
}
157159
lifetime_name => lifetime_name,
158160
}
159161
}

src/librustc_mir/monomorphize/collector.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1088,9 +1088,9 @@ fn create_mono_items_for_default_impls<'tcx>(
10881088
let param_env = ty::ParamEnv::reveal_all();
10891089
let trait_ref = tcx.normalize_erasing_regions(param_env, trait_ref);
10901090
let overridden_methods: FxHashSet<_> =
1091-
items.iter().map(|iiref| iiref.ident.modern()).collect();
1091+
items.iter().map(|iiref| iiref.ident.normalize_to_macros_2_0()).collect();
10921092
for method in tcx.provided_trait_methods(trait_ref.def_id) {
1093-
if overridden_methods.contains(&method.ident.modern()) {
1093+
if overridden_methods.contains(&method.ident.normalize_to_macros_2_0()) {
10941094
continue;
10951095
}
10961096

src/librustc_parse/parser/item.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,7 @@ impl<'a> Parser<'a> {
12601260
};
12611261

12621262
self.sess.gated_spans.gate(sym::decl_macro, lo.to(self.prev_token.span));
1263-
Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, legacy: false })))
1263+
Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, macro_rules: false })))
12641264
}
12651265

12661266
/// Is this unambiguously the start of a `macro_rules! foo` item defnition?
@@ -1270,7 +1270,7 @@ impl<'a> Parser<'a> {
12701270
&& self.look_ahead(2, |t| t.is_ident())
12711271
}
12721272

1273-
/// Parses a legacy `macro_rules! foo { ... }` declarative macro.
1273+
/// Parses a `macro_rules! foo { ... }` declarative macro.
12741274
fn parse_item_macro_rules(&mut self, vis: &Visibility) -> PResult<'a, ItemInfo> {
12751275
self.expect_keyword(kw::MacroRules)?; // `macro_rules`
12761276
self.expect(&token::Not)?; // `!`
@@ -1280,7 +1280,7 @@ impl<'a> Parser<'a> {
12801280
self.eat_semi_for_macro_if_needed(&body);
12811281
self.complain_if_pub_macro(vis, true);
12821282

1283-
Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, legacy: true })))
1283+
Ok((ident, ItemKind::MacroDef(ast::MacroDef { body, macro_rules: true })))
12841284
}
12851285

12861286
/// Item macro invocations or `macro_rules!` definitions need inherited visibility.

src/librustc_privacy/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
920920
}
921921

922922
fn visit_macro_def(&mut self, md: &'tcx hir::MacroDef<'tcx>) {
923-
if attr::find_transparency(&md.attrs, md.ast.legacy).0 != Transparency::Opaque {
923+
if attr::find_transparency(&md.attrs, md.ast.macro_rules).0 != Transparency::Opaque {
924924
self.update(md.hir_id, Some(AccessLevel::Public));
925925
return;
926926
}

0 commit comments

Comments
 (0)