Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 14 additions & 12 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
pub(crate) fn plant_decl_into_local_module(
&mut self,
parent: Module<'ra>,
ident: Ident,
ident: Macros20NormalizedIdent,
ns: Namespace,
decl: Decl<'ra>,
) {
if let Err(old_decl) = self.try_plant_decl_into_local_module(parent, ident, ns, decl, false)
{
self.report_conflict(parent, ident, ns, old_decl, decl);
self.report_conflict(parent, ident.0, ns, old_decl, decl);
}
}

Expand All @@ -71,14 +71,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
expn_id: LocalExpnId,
) {
let decl = self.arenas.new_def_decl(res, vis.to_def_id(), span, expn_id);
let ident = Macros20NormalizedIdent::new(ident);
self.plant_decl_into_local_module(parent, ident, ns, decl);
}

/// Create a name definitinon from the given components, and put it into the extern module.
fn define_extern(
&self,
parent: Module<'ra>,
ident: Ident,
ident: Macros20NormalizedIdent,
ns: Namespace,
child_index: usize,
res: Res,
Expand Down Expand Up @@ -280,6 +281,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
)
};
let ModChild { ident, res, vis, ref reexport_chain } = *child;
let ident = Macros20NormalizedIdent::new(ident);
let span = child_span(self, reexport_chain, res);
let res = res.expect_non_local();
let expansion = parent_scope.expansion;
Expand Down Expand Up @@ -532,7 +534,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
if target.name != kw::Underscore {
self.r.per_ns(|this, ns| {
if !type_ns_only || ns == TypeNS {
let key = BindingKey::new(target, ns);
let key = BindingKey::new(Macros20NormalizedIdent::new(target), ns);
this.resolution_or_default(current_module, key)
.borrow_mut(this)
.single_imports
Expand Down Expand Up @@ -1023,11 +1025,11 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
}
self.r.potentially_unused_imports.push(import);
let import_decl = self.r.new_import_decl(decl, import);
let ident = Macros20NormalizedIdent::new(ident);
if ident.name != kw::Underscore && parent == self.r.graph_root {
let norm_ident = Macros20NormalizedIdent::new(ident);
// FIXME: this error is technically unnecessary now when extern prelude is split into
// two scopes, remove it with lang team approval.
if let Some(entry) = self.r.extern_prelude.get(&norm_ident)
if let Some(entry) = self.r.extern_prelude.get(&ident)
&& expansion != LocalExpnId::ROOT
&& orig_name.is_some()
&& entry.item_decl.is_none()
Expand All @@ -1038,7 +1040,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
}

use indexmap::map::Entry;
match self.r.extern_prelude.entry(norm_ident) {
match self.r.extern_prelude.entry(ident) {
Entry::Occupied(mut occupied) => {
let entry = occupied.get_mut();
if entry.item_decl.is_some() {
Expand Down Expand Up @@ -1290,8 +1292,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
self.r.local_macro_def_scopes.insert(def_id, parent_scope.module);

if macro_rules {
let ident = ident.normalize_to_macros_2_0();
self.r.macro_names.insert(ident);
let ident = Macros20NormalizedIdent::new(ident);
self.r.macro_names.insert(ident.0);
let is_macro_export = ast::attr::contains_name(&item.attrs, sym::macro_export);
let vis = if is_macro_export {
Visibility::Public
Expand Down Expand Up @@ -1320,8 +1322,8 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
let import_decl = self.r.new_import_decl(decl, import);
self.r.plant_decl_into_local_module(self.r.graph_root, ident, MacroNS, import_decl);
} else {
self.r.check_reserved_macro_name(ident, res);
self.insert_unused_macro(ident, def_id, item.id);
self.r.check_reserved_macro_name(ident.0, res);
self.insert_unused_macro(ident.0, def_id, item.id);
}
self.r.feed_visibility(feed, vis);
let scope = self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Def(
Expand Down Expand Up @@ -1488,7 +1490,7 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
{
// Don't add underscore names, they cannot be looked up anyway.
let impl_def_id = self.r.tcx.local_parent(local_def_id);
let key = BindingKey::new(ident, ns);
let key = BindingKey::new(Macros20NormalizedIdent::new(ident), ns);
self.r.impl_binding_keys.entry(impl_def_id).or_default().insert(key);
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
.get(&expn_id)
.into_iter()
.flatten()
.map(|(ident, _)| TypoSuggestion::typo_from_ident(*ident, res)),
.map(|(ident, _)| TypoSuggestion::typo_from_ident(ident.0, res)),
);
}
}
Expand All @@ -1199,7 +1199,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let res = macro_rules_def.decl.res();
if filter_fn(res) {
suggestions
.push(TypoSuggestion::typo_from_ident(macro_rules_def.ident, res))
.push(TypoSuggestion::typo_from_ident(macro_rules_def.ident.0, res))
}
}
}
Expand Down Expand Up @@ -2892,7 +2892,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
return None;
}

let binding_key = BindingKey::new(ident, MacroNS);
let binding_key = BindingKey::new(Macros20NormalizedIdent::new(ident), MacroNS);
let binding = self.resolution(crate_module, binding_key)?.binding()?;
let Res::Def(DefKind::Macro(kinds), _) = binding.res() else {
return None;
Expand Down
53 changes: 26 additions & 27 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use rustc_middle::{bug, span_bug};
use rustc_session::lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK;
use rustc_session::parse::feature_err;
use rustc_span::hygiene::{ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContext};
use rustc_span::{Ident, Span, kw, sym};
use rustc_span::{Ident, Macros20NormalizedIdent, Span, kw, sym};
use smallvec::SmallVec;
use tracing::{debug, instrument};

use crate::errors::{ParamKindInEnumDiscriminant, ParamKindInNonTrivialAnonConst};
Expand Down Expand Up @@ -50,7 +51,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
mut self: CmResolver<'r, 'ra, 'tcx>,
scope_set: ScopeSet<'ra>,
parent_scope: &ParentScope<'ra>,
ctxt: SyntaxContext,
orig_ctxt: SyntaxContext,
derive_fallback_lint_id: Option<NodeId>,
mut visitor: impl FnMut(
&mut CmResolver<'r, 'ra, 'tcx>,
Expand Down Expand Up @@ -100,7 +101,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
// 4c. Standard library prelude (de-facto closed, controlled).
// 6. Language prelude: builtin attributes (closed, controlled).

let rust_2015 = ctxt.edition().is_rust_2015();
let (ns, macro_kind) = match scope_set {
ScopeSet::All(ns)
| ScopeSet::Module(ns, _)
Expand All @@ -123,7 +123,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
TypeNS | ValueNS => Scope::ModuleNonGlobs(module, None),
MacroNS => Scope::DeriveHelpers(parent_scope.expansion),
};
let mut ctxt = ctxt.normalize_to_macros_2_0();
let mut ctxt = orig_ctxt.normalize_to_macros_2_0();
let mut use_prelude = !module.no_implicit_prelude;

loop {
Expand All @@ -148,7 +148,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
true
}
Scope::ModuleNonGlobs(..) | Scope::ModuleGlobs(..) => true,
Scope::MacroUsePrelude => use_prelude || rust_2015,
Scope::MacroUsePrelude => use_prelude || orig_ctxt.edition().is_rust_2015(),
Scope::BuiltinAttrs => true,
Scope::ExternPreludeItems | Scope::ExternPreludeFlags => {
use_prelude || module_and_extern_prelude || extern_prelude
Expand Down Expand Up @@ -397,7 +397,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
assert!(force || finalize.is_none()); // `finalize` implies `force`

// Make sure `self`, `super` etc produce an error when passed to here.
if orig_ident.is_path_segment_keyword() && !matches!(scope_set, ScopeSet::Module(..)) {
if !matches!(scope_set, ScopeSet::Module(..)) && orig_ident.is_path_segment_keyword() {
return Err(Determinacy::Determined);
}

Expand All @@ -424,7 +424,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
// }
// So we have to save the innermost solution and continue searching in outer scopes
// to detect potential ambiguities.
let mut innermost_results: Vec<(Decl<'_>, Scope<'_>)> = Vec::new();
let mut innermost_results: SmallVec<[(Decl<'_>, Scope<'_>); 2]> = SmallVec::new();
let mut determinacy = Determinacy::Determined;

// Go through all the scopes and try to resolve the name.
Expand All @@ -434,12 +434,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
orig_ident.span.ctxt(),
derive_fallback_lint_id,
|this, scope, use_prelude, ctxt| {
let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt));
// The passed `ctxt` is already normalized, so avoid expensive double normalization.
let ident = Macros20NormalizedIdent(ident);
let res = match this.reborrow().resolve_ident_in_scope(
orig_ident,
ident,
ns,
scope,
use_prelude,
ctxt,
scope_set,
parent_scope,
// Shadowed decls don't need to be marked as used or non-speculatively loaded.
Expand Down Expand Up @@ -508,19 +510,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {

fn resolve_ident_in_scope<'r>(
mut self: CmResolver<'r, 'ra, 'tcx>,
orig_ident: Ident,
ident: Macros20NormalizedIdent,
ns: Namespace,
scope: Scope<'ra>,
use_prelude: UsePrelude,
ctxt: SyntaxContext,
scope_set: ScopeSet<'ra>,
parent_scope: &ParentScope<'ra>,
finalize: Option<Finalize>,
force: bool,
ignore_decl: Option<Decl<'ra>>,
ignore_import: Option<Import<'ra>>,
) -> Result<Decl<'ra>, ControlFlow<Determinacy, Determinacy>> {
let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt));
let ret = match scope {
Scope::DeriveHelpers(expn_id) => {
if let Some(decl) = self
Expand Down Expand Up @@ -599,11 +599,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
self.get_mut().lint_buffer.buffer_lint(
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
lint_id,
orig_ident.span,
ident.span,
errors::ProcMacroDeriveResolutionFallback {
span: orig_ident.span,
span: ident.span,
ns_descr: ns.descr(),
ident,
ident: ident.0,
},
);
}
Expand Down Expand Up @@ -649,11 +649,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
self.get_mut().lint_buffer.buffer_lint(
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
lint_id,
orig_ident.span,
ident.span,
errors::ProcMacroDeriveResolutionFallback {
span: orig_ident.span,
span: ident.span,
ns_descr: ns.descr(),
ident,
ident: ident.0,
},
);
}
Expand Down Expand Up @@ -699,7 +699,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let mut result = Err(Determinacy::Determined);
if let Some(prelude) = self.prelude
&& let Ok(decl) = self.reborrow().resolve_ident_in_scope_set(
ident,
ident.0,
ScopeSet::Module(ns, prelude),
parent_scope,
None,
Expand Down Expand Up @@ -983,7 +983,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
fn resolve_ident_in_module_non_globs_unadjusted<'r>(
mut self: CmResolver<'r, 'ra, 'tcx>,
module: Module<'ra>,
ident: Ident,
ident: Macros20NormalizedIdent,
ns: Namespace,
parent_scope: &ParentScope<'ra>,
shadowing: Shadowing,
Expand All @@ -1006,7 +1006,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {

if let Some(finalize) = finalize {
return self.get_mut().finalize_module_binding(
ident,
ident.0,
binding,
parent_scope,
module,
Expand Down Expand Up @@ -1046,7 +1046,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
fn resolve_ident_in_module_globs_unadjusted<'r>(
mut self: CmResolver<'r, 'ra, 'tcx>,
module: Module<'ra>,
ident: Ident,
ident: Macros20NormalizedIdent,
ns: Namespace,
parent_scope: &ParentScope<'ra>,
shadowing: Shadowing,
Expand All @@ -1067,7 +1067,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {

if let Some(finalize) = finalize {
return self.get_mut().finalize_module_binding(
ident,
ident.0,
binding,
parent_scope,
module,
Expand Down Expand Up @@ -1136,9 +1136,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
None => return Err(ControlFlow::Continue(Undetermined)),
};
let tmp_parent_scope;
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) {
let (mut adjusted_parent_scope, mut ident) = (parent_scope, ident);
match ident.0.span.glob_adjust(module.expansion, glob_import.span) {
Some(Some(def)) => {
tmp_parent_scope =
ParentScope { module: self.expn_def_scope(def), ..*parent_scope };
Expand All @@ -1148,7 +1147,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
None => continue,
};
let result = self.reborrow().resolve_ident_in_scope_set(
ident,
ident.0,
ScopeSet::Module(ns, module),
adjusted_parent_scope,
None,
Expand Down
Loading
Loading