Skip to content

Commit

Permalink
Other legacy -> macro_rules
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Mar 15, 2020
1 parent 3fbb254 commit 8c9a38f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/librustc_attr/builtin.rs
Expand Up @@ -1024,7 +1024,7 @@ pub enum TransparencyError {

pub fn find_transparency(
attrs: &[Attribute],
is_legacy: bool,
macro_rules: bool,
) -> (Transparency, Option<TransparencyError>) {
let mut transparency = None;
let mut error = None;
Expand All @@ -1049,7 +1049,7 @@ pub fn find_transparency(
}
}
}
let fallback = if is_legacy { Transparency::SemiTransparent } else { Transparency::Opaque };
let fallback = if macro_rules { Transparency::SemiTransparent } else { Transparency::Opaque };
(transparency.map_or(fallback, |t| t.0), error)
}

Expand Down
12 changes: 6 additions & 6 deletions src/librustc_expand/mbe/macro_check.rs
Expand Up @@ -419,10 +419,10 @@ fn check_nested_occurrences(
| (NestedMacroState::MacroName, &TokenTree::Delimited(_, ref del))
if del.delim == DelimToken::Brace =>
{
let legacy = state == NestedMacroState::MacroRulesNotName;
let macro_rules = state == NestedMacroState::MacroRulesNotName;
state = NestedMacroState::Empty;
let rest =
check_nested_macro(sess, node_id, legacy, &del.tts, &nested_macros, valid);
check_nested_macro(sess, node_id, macro_rules, &del.tts, &nested_macros, valid);
// If we did not check the whole macro definition, then check the rest as if outside
// the macro definition.
check_nested_occurrences(
Expand Down Expand Up @@ -493,21 +493,21 @@ fn check_nested_occurrences(
/// Arguments:
/// - `sess` is used to emit diagnostics and lints
/// - `node_id` is used to emit lints
/// - `legacy` specifies whether the macro is legacy
/// - `macro_rules` specifies whether the macro is `macro_rules`
/// - `tts` is checked as a list of (LHS) => {RHS}
/// - `macros` is the stack of outer macros
/// - `valid` is set in case of errors
fn check_nested_macro(
sess: &ParseSess,
node_id: NodeId,
legacy: bool,
macro_rules: bool,
tts: &[TokenTree],
macros: &Stack<'_, MacroState<'_>>,
valid: &mut bool,
) -> usize {
let n = tts.len();
let mut i = 0;
let separator = if legacy { TokenKind::Semi } else { TokenKind::Comma };
let separator = if macro_rules { TokenKind::Semi } else { TokenKind::Comma };
loop {
// We expect 3 token trees: `(LHS) => {RHS}`. The separator is checked after.
if i + 2 >= n
Expand All @@ -522,7 +522,7 @@ fn check_nested_macro(
let mut binders = Binders::default();
check_binders(sess, node_id, lhs, macros, &mut binders, &Stack::Empty, valid);
check_occurrences(sess, node_id, rhs, macros, &binders, &Stack::Empty, valid);
// Since the last semicolon is optional for legacy macros and decl_macro are not terminated,
// Since the last semicolon is optional for `macro_rules` macros and decl_macro are not terminated,
// we increment our checked position by how many token trees we already checked (the 3
// above) before checking for the separator.
i += 3;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_parse/parser/item.rs
Expand Up @@ -1270,7 +1270,7 @@ impl<'a> Parser<'a> {
&& self.look_ahead(2, |t| t.is_ident())
}

/// Parses a legacy `macro_rules! foo { ... }` declarative macro.
/// Parses a `macro_rules! foo { ... }` declarative macro.
fn parse_item_macro_rules(&mut self, vis: &Visibility) -> PResult<'a, ItemInfo> {
self.expect_keyword(kw::MacroRules)?; // `macro_rules`
self.expect(&token::Not)?; // `!`
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -624,7 +624,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
self.r.get_module(DefId { krate: crate_id, index: CRATE_DEF_INDEX })
};

let used = self.process_legacy_macro_imports(item, module);
let used = self.process_macro_use_imports(item, module);
let binding =
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.r.arenas);
let import = self.r.arenas.alloc_import(Import {
Expand Down Expand Up @@ -913,7 +913,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}
}

fn legacy_import_macro(
fn add_macro_use_binding(
&mut self,
name: ast::Name,
binding: &'a NameBinding<'a>,
Expand All @@ -929,7 +929,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}

/// Returns `true` if we should consider the underlying `extern crate` to be used.
fn process_legacy_macro_imports(&mut self, item: &Item, module: Module<'a>) -> bool {
fn process_macro_use_imports(&mut self, item: &Item, module: Module<'a>) -> bool {
let mut import_all = None;
let mut single_imports = Vec::new();
for attr in &item.attrs {
Expand Down Expand Up @@ -1004,7 +1004,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
module.for_each_child(self, |this, ident, ns, binding| {
if ns == MacroNS {
let imported_binding = this.r.import(binding, import);
this.legacy_import_macro(ident.name, imported_binding, span, allow_shadowing);
this.add_macro_use_binding(ident.name, imported_binding, span, allow_shadowing);
}
});
} else {
Expand All @@ -1021,7 +1021,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
let import = macro_use_import(self, ident.span);
self.r.potentially_unused_imports.push(import);
let imported_binding = self.r.import(binding, import);
self.legacy_import_macro(
self.add_macro_use_binding(
ident.name,
imported_binding,
ident.span,
Expand Down
28 changes: 15 additions & 13 deletions src/librustc_resolve/lib.rs
Expand Up @@ -618,7 +618,7 @@ enum AmbiguityKind {
Import,
BuiltinAttr,
DeriveHelper,
LegacyVsModern,
MacroRulesVsModularized,
GlobVsOuter,
GlobVsGlob,
GlobVsExpanded,
Expand All @@ -631,7 +631,9 @@ impl AmbiguityKind {
AmbiguityKind::Import => "name vs any other name during import resolution",
AmbiguityKind::BuiltinAttr => "built-in attribute vs any other name",
AmbiguityKind::DeriveHelper => "derive helper attribute vs any other name",
AmbiguityKind::LegacyVsModern => "`macro_rules` vs non-`macro_rules` from other module",
AmbiguityKind::MacroRulesVsModularized => {
"`macro_rules` vs non-`macro_rules` from other module"
}
AmbiguityKind::GlobVsOuter => {
"glob import vs any other name from outer scope during import/macro resolution"
}
Expand Down Expand Up @@ -1473,7 +1475,7 @@ impl<'a> Resolver<'a> {
// derives (you need to resolve the derive first to add helpers into scope), but they
// should be available before the derive is expanded for compatibility.
// It's mess in general, so we are being conservative for now.
// 1-3. `macro_rules` (open, not controlled), loop through legacy scopes. Have higher
// 1-3. `macro_rules` (open, not controlled), loop through `macro_rules` scopes. Have higher
// priority than prelude macros, but create ambiguities with macros in modules.
// 1-3. Names in modules (both normal `mod`ules and blocks), loop through hygienic parents
// (open, not controlled). Have higher priority than prelude macros, but create
Expand Down Expand Up @@ -1639,7 +1641,7 @@ impl<'a> Resolver<'a> {
for i in (0..ribs.len()).rev() {
debug!("walk rib\n{:?}", ribs[i].bindings);
// Use the rib kind to determine whether we are resolving parameters
// (modern hygiene) or local variables (legacy hygiene).
// (modern hygiene) or local variables (`macro_rules` hygiene).
let rib_ident = if ribs[i].kind.contains_params() { modern_ident } else { ident };
if let Some(res) = ribs[i].bindings.get(&rib_ident).cloned() {
// The ident resolves to a type parameter or local variable.
Expand Down Expand Up @@ -1898,7 +1900,7 @@ impl<'a> Resolver<'a> {
break;
}
}
// Then find the last legacy mark from the end if it exists.
// Then find the last semi-transparent mark from the end if it exists.
for (mark, transparency) in iter {
if transparency == Transparency::SemiTransparent {
result = Some(mark);
Expand Down Expand Up @@ -2423,21 +2425,21 @@ impl<'a> Resolver<'a> {
}
}

fn disambiguate_legacy_vs_modern(
fn disambiguate_macro_rules_vs_modularized(
&self,
legacy: &'a NameBinding<'a>,
modern: &'a NameBinding<'a>,
macro_rules: &'a NameBinding<'a>,
modularized: &'a NameBinding<'a>,
) -> bool {
// Some non-controversial subset of ambiguities "modern macro name" vs "macro_rules"
// is disambiguated to mitigate regressions from macro modularization.
// Scoping for `macro_rules` behaves like scoping for `let` at module level, in general.
match (
self.binding_parent_modules.get(&PtrKey(legacy)),
self.binding_parent_modules.get(&PtrKey(modern)),
self.binding_parent_modules.get(&PtrKey(macro_rules)),
self.binding_parent_modules.get(&PtrKey(modularized)),
) {
(Some(legacy), Some(modern)) => {
legacy.normal_ancestor_id == modern.normal_ancestor_id
&& modern.is_ancestor_of(legacy)
(Some(macro_rules), Some(modularized)) => {
macro_rules.normal_ancestor_id == modularized.normal_ancestor_id
&& modularized.is_ancestor_of(macro_rules)
}
_ => false,
}
Expand Down
10 changes: 6 additions & 4 deletions src/librustc_resolve/macros.rs
Expand Up @@ -761,16 +761,18 @@ impl<'a> Resolver<'a> {
Some(AmbiguityKind::DeriveHelper)
} else if innermost_flags.contains(Flags::MACRO_RULES)
&& flags.contains(Flags::MODULE)
&& !this
.disambiguate_legacy_vs_modern(innermost_binding, binding)
&& !this.disambiguate_macro_rules_vs_modularized(
innermost_binding,
binding,
)
|| flags.contains(Flags::MACRO_RULES)
&& innermost_flags.contains(Flags::MODULE)
&& !this.disambiguate_legacy_vs_modern(
&& !this.disambiguate_macro_rules_vs_modularized(
binding,
innermost_binding,
)
{
Some(AmbiguityKind::LegacyVsModern)
Some(AmbiguityKind::MacroRulesVsModularized)
} else if innermost_binding.is_glob_import() {
Some(AmbiguityKind::GlobVsOuter)
} else if innermost_binding
Expand Down

0 comments on commit 8c9a38f

Please sign in to comment.