Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve/hygiene: macro_rules are not "legacy" #69989

Merged
merged 4 commits into from
Mar 16, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
@@ -3083,7 +3083,7 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn hygienic_eq(self, use_name: Ident, def_name: Ident, def_parent_def_id: DefId) -> bool {
// We could use `Ident::eq` here, but we deliberately don't. The name
// comparison fails frequently, and we want to avoid the expensive
// `modern()` calls required for the span comparison whenever possible.
// `normalize_to_macros_2_0()` calls required for the span comparison whenever possible.
use_name.name == def_name.name
&& use_name
.span
@@ -3099,7 +3099,7 @@ impl<'tcx> TyCtxt<'tcx> {
}

pub fn adjust_ident(self, mut ident: Ident, scope: DefId) -> Ident {
ident.span.modernize_and_adjust(self.expansion_that_defined(scope));
ident.span.normalize_to_macros_2_0_and_adjust(self.expansion_that_defined(scope));
ident
}

@@ -3109,12 +3109,14 @@ impl<'tcx> TyCtxt<'tcx> {
scope: DefId,
block: hir::HirId,
) -> (Ident, DefId) {
let scope = match ident.span.modernize_and_adjust(self.expansion_that_defined(scope)) {
Some(actual_expansion) => {
self.hir().definitions().parent_module_of_macro_def(actual_expansion)
}
None => self.parent_module(block),
};
let scope =
match ident.span.normalize_to_macros_2_0_and_adjust(self.expansion_that_defined(scope))
{
Some(actual_expansion) => {
self.hir().definitions().parent_module_of_macro_def(actual_expansion)
}
None => self.parent_module(block),
};
(ident, scope)
}

2 changes: 1 addition & 1 deletion src/librustc_ast_lowering/item.rs
Original file line number Diff line number Diff line change
@@ -115,7 +115,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
_ => &[],
};
let lt_def_names = parent_generics.iter().filter_map(|param| match param.kind {
hir::GenericParamKind::Lifetime { .. } => Some(param.name.modern()),
hir::GenericParamKind::Lifetime { .. } => Some(param.name.normalize_to_macros_2_0()),
_ => None,
});
self.in_scope_lifetimes.extend(lt_def_names);
13 changes: 8 additions & 5 deletions src/librustc_ast_lowering/lib.rs
Original file line number Diff line number Diff line change
@@ -153,7 +153,7 @@ struct LoweringContext<'a, 'hir: 'a> {
/// against this list to see if it is already in-scope, or if a definition
/// needs to be created for it.
///
/// We always store a `modern()` version of the param-name in this
/// We always store a `normalize_to_macros_2_0()` version of the param-name in this
/// vector.
in_scope_lifetimes: Vec<ParamName>,

@@ -805,14 +805,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
return;
}

if self.in_scope_lifetimes.contains(&ParamName::Plain(ident.modern())) {
if self.in_scope_lifetimes.contains(&ParamName::Plain(ident.normalize_to_macros_2_0())) {
return;
}

let hir_name = ParamName::Plain(ident);

if self.lifetimes_to_define.iter().any(|(_, lt_name)| lt_name.modern() == hir_name.modern())
{
if self.lifetimes_to_define.iter().any(|(_, lt_name)| {
lt_name.normalize_to_macros_2_0() == hir_name.normalize_to_macros_2_0()
}) {
return;
}

@@ -840,7 +841,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
) -> T {
let old_len = self.in_scope_lifetimes.len();
let lt_def_names = params.iter().filter_map(|param| match param.kind {
GenericParamKind::Lifetime { .. } => Some(ParamName::Plain(param.ident.modern())),
GenericParamKind::Lifetime { .. } => {
Some(ParamName::Plain(param.ident.normalize_to_macros_2_0()))
}
_ => None,
});
self.in_scope_lifetimes.extend(lt_def_names);
10 changes: 6 additions & 4 deletions src/librustc_hir/hir.rs
Original file line number Diff line number Diff line change
@@ -79,9 +79,9 @@ impl ParamName {
}
}

pub fn modern(&self) -> ParamName {
pub fn normalize_to_macros_2_0(&self) -> ParamName {
match *self {
ParamName::Plain(ident) => ParamName::Plain(ident.modern()),
ParamName::Plain(ident) => ParamName::Plain(ident.normalize_to_macros_2_0()),
param_name => param_name,
}
}
@@ -151,9 +151,11 @@ impl LifetimeName {
self == &LifetimeName::Static
}

pub fn modern(&self) -> LifetimeName {
pub fn normalize_to_macros_2_0(&self) -> LifetimeName {
match *self {
LifetimeName::Param(param_name) => LifetimeName::Param(param_name.modern()),
LifetimeName::Param(param_name) => {
LifetimeName::Param(param_name.normalize_to_macros_2_0())
}
lifetime_name => lifetime_name,
}
}
4 changes: 2 additions & 2 deletions src/librustc_mir/monomorphize/collector.rs
Original file line number Diff line number Diff line change
@@ -1088,9 +1088,9 @@ fn create_mono_items_for_default_impls<'tcx>(
let param_env = ty::ParamEnv::reveal_all();
let trait_ref = tcx.normalize_erasing_regions(param_env, trait_ref);
let overridden_methods: FxHashSet<_> =
items.iter().map(|iiref| iiref.ident.modern()).collect();
items.iter().map(|iiref| iiref.ident.normalize_to_macros_2_0()).collect();
for method in tcx.provided_trait_methods(trait_ref.def_id) {
if overridden_methods.contains(&method.ident.modern()) {
if overridden_methods.contains(&method.ident.normalize_to_macros_2_0()) {
continue;
}

15 changes: 9 additions & 6 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
@@ -645,7 +645,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
self.r.potentially_unused_imports.push(import);
let imported_binding = self.r.import(binding, import);
if ptr::eq(parent, self.r.graph_root) {
if let Some(entry) = self.r.extern_prelude.get(&ident.modern()) {
if let Some(entry) = self.r.extern_prelude.get(&ident.normalize_to_macros_2_0())
{
if expansion != ExpnId::root()
&& orig_name.is_some()
&& entry.extern_crate_item.is_none()
@@ -656,10 +657,12 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
}
}
let entry =
self.r.extern_prelude.entry(ident.modern()).or_insert(ExternPreludeEntry {
extern_crate_item: None,
introduced_by_item: true,
});
self.r.extern_prelude.entry(ident.normalize_to_macros_2_0()).or_insert(
ExternPreludeEntry {
extern_crate_item: None,
introduced_by_item: true,
},
);
entry.extern_crate_item = Some(imported_binding);
if orig_name.is_some() {
entry.introduced_by_item = true;
@@ -1119,7 +1122,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
self.r.local_macro_def_scopes.insert(item.id, parent_scope.module);

if macro_rules {
let ident = ident.modern();
let ident = ident.normalize_to_macros_2_0();
self.r.macro_names.insert(ident);
let is_macro_export = attr::contains_name(&item.attrs, sym::macro_export);
let vis = if is_macro_export {
2 changes: 1 addition & 1 deletion src/librustc_resolve/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -758,7 +758,7 @@ impl<'a> Resolver<'a> {
let msg = format!("unsafe traits like `{}` should be implemented explicitly", ident);
err.span_note(ident.span, &msg);
}
if self.macro_names.contains(&ident.modern()) {
if self.macro_names.contains(&ident.normalize_to_macros_2_0()) {
err.help("have you added the `#[macro_use]` on the module/import?");
}
}
3 changes: 2 additions & 1 deletion src/librustc_resolve/imports.rs
Original file line number Diff line number Diff line change
@@ -416,7 +416,8 @@ impl<'a> Resolver<'a> {
None => return Err((Undetermined, Weak::Yes)),
};
let tmp_parent_scope;
let (mut adjusted_parent_scope, mut ident) = (parent_scope, ident.modern());
let (mut adjusted_parent_scope, mut ident) =
(parent_scope, ident.normalize_to_macros_2_0());
match ident.span.glob_adjust(module.expansion, glob_import.span) {
Some(Some(def)) => {
tmp_parent_scope =
10 changes: 5 additions & 5 deletions src/librustc_resolve/late.rs
Original file line number Diff line number Diff line change
@@ -935,7 +935,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
_ => unreachable!(),
};

let ident = param.ident.modern();
let ident = param.ident.normalize_to_macros_2_0();
debug!("with_generic_param_rib: {}", param.id);

if seen_bindings.contains_key(&ident) {
@@ -1464,7 +1464,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
// Add the binding to the local ribs, if it doesn't already exist in the bindings map.
// (We must not add it if it's in the bindings map because that breaks the assumptions
// later passes make about or-patterns.)
let ident = ident.modern_and_legacy();
let ident = ident.normalize_to_macro_rules();

let mut bound_iter = bindings.iter().filter(|(_, set)| set.contains(&ident));
// Already bound in a product pattern? e.g. `(a, a)` which is not allowed.
@@ -1873,7 +1873,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
self.diagnostic_metadata.unused_labels.insert(id, label.ident.span);
}
self.with_label_rib(NormalRibKind, |this| {
let ident = label.ident.modern_and_legacy();
let ident = label.ident.normalize_to_macro_rules();
this.label_ribs.last_mut().unwrap().bindings.insert(ident, id);
f(this);
});
@@ -1949,7 +1949,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {

ExprKind::Break(Some(label), _) | ExprKind::Continue(Some(label)) => {
let node_id = self.search_label(label.ident, |rib, ident| {
rib.bindings.get(&ident.modern_and_legacy()).cloned()
rib.bindings.get(&ident.normalize_to_macro_rules()).cloned()
});
match node_id {
None => {
@@ -2115,7 +2115,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
}
}

ident.span = ident.span.modern();
ident.span = ident.span.normalize_to_macros_2_0();
let mut search_module = self.parent_scope.module;
loop {
self.get_traits_in_module_containing_item(ident, ns, search_module, &mut found_traits);
27 changes: 16 additions & 11 deletions src/librustc_resolve/late/lifetimes.rs
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@ impl RegionExt for Region {
let def_id = hir_map.local_def_id(param.hir_id);
let origin = LifetimeDefOrigin::from_param(param);
debug!("Region::early: index={} def_id={:?}", i, def_id);
(param.name.modern(), Region::EarlyBound(i, def_id, origin))
(param.name.normalize_to_macros_2_0(), Region::EarlyBound(i, def_id, origin))
}

fn late(hir_map: &Map<'_>, param: &GenericParam<'_>) -> (ParamName, Region) {
@@ -73,7 +73,7 @@ impl RegionExt for Region {
"Region::late: param={:?} depth={:?} def_id={:?} origin={:?}",
param, depth, def_id, origin,
);
(param.name.modern(), Region::LateBound(depth, def_id, origin))
(param.name.normalize_to_macros_2_0(), Region::LateBound(depth, def_id, origin))
}

fn late_anon(index: &Cell<u32>) -> Region {
@@ -1174,7 +1174,9 @@ fn extract_labels(ctxt: &mut LifetimeContext<'_, '_>, body: &hir::Body<'_>) {

Scope::Binder { ref lifetimes, s, .. } => {
// FIXME (#24278): non-hygienic comparison
if let Some(def) = lifetimes.get(&hir::ParamName::Plain(label.modern())) {
if let Some(def) =
lifetimes.get(&hir::ParamName::Plain(label.normalize_to_macros_2_0()))
{
let hir_id = tcx.hir().as_local_hir_id(def.id().unwrap()).unwrap();

signal_shadowing_problem(
@@ -1253,7 +1255,7 @@ fn object_lifetime_defaults_for_item(
fn add_bounds(set: &mut Set1<hir::LifetimeName>, bounds: &[hir::GenericBound<'_>]) {
for bound in bounds {
if let hir::GenericBound::Outlives(ref lifetime) = *bound {
set.insert(lifetime.name.modern());
set.insert(lifetime.name.normalize_to_macros_2_0());
}
}
}
@@ -1791,7 +1793,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
Scope::Binder { ref lifetimes, s, .. } => {
match lifetime_ref.name {
LifetimeName::Param(param_name) => {
if let Some(&def) = lifetimes.get(&param_name.modern()) {
if let Some(&def) = lifetimes.get(&param_name.normalize_to_macros_2_0())
{
break Some(def.shifted(late_depth));
}
}
@@ -2544,7 +2547,9 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
let lifetimes: Vec<_> = params
.iter()
.filter_map(|param| match param.kind {
GenericParamKind::Lifetime { .. } => Some((param, param.name.modern())),
GenericParamKind::Lifetime { .. } => {
Some((param, param.name.normalize_to_macros_2_0()))
}
_ => None,
})
.collect();
@@ -2661,7 +2666,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
}

Scope::Binder { ref lifetimes, s, .. } => {
if let Some(&def) = lifetimes.get(&param.name.modern()) {
if let Some(&def) = lifetimes.get(&param.name.normalize_to_macros_2_0()) {
let hir_id = self.tcx.hir().as_local_hir_id(def.id().unwrap()).unwrap();

signal_shadowing_problem(
@@ -2799,7 +2804,7 @@ fn insert_late_bound_lifetimes(
// `'a: 'b` means both `'a` and `'b` are referenced
appears_in_where_clause
.regions
.insert(hir::LifetimeName::Param(param.name.modern()));
.insert(hir::LifetimeName::Param(param.name.normalize_to_macros_2_0()));
}
}
}
@@ -2821,7 +2826,7 @@ fn insert_late_bound_lifetimes(
hir::GenericParamKind::Type { .. } | hir::GenericParamKind::Const { .. } => continue,
}

let lt_name = hir::LifetimeName::Param(param.name.modern());
let lt_name = hir::LifetimeName::Param(param.name.normalize_to_macros_2_0());
// appears in the where clauses? early-bound.
if appears_in_where_clause.regions.contains(&lt_name) {
continue;
@@ -2885,7 +2890,7 @@ fn insert_late_bound_lifetimes(
}

fn visit_lifetime(&mut self, lifetime_ref: &'v hir::Lifetime) {
self.regions.insert(lifetime_ref.name.modern());
self.regions.insert(lifetime_ref.name.normalize_to_macros_2_0());
}
}

@@ -2902,7 +2907,7 @@ fn insert_late_bound_lifetimes(
}

fn visit_lifetime(&mut self, lifetime_ref: &'v hir::Lifetime) {
self.regions.insert(lifetime_ref.name.modern());
self.regions.insert(lifetime_ref.name.normalize_to_macros_2_0());
}
}
}
Loading