Skip to content

Commit af3d9e5

Browse files
authored
Rollup merge of rust-lang#64623 - matthewjasper:underscore-imports, r=petrochenkov
Remove last uses of gensyms Underscore bindings now use unique `SyntaxContext`s to avoid collisions. This was the last use of gensyms in the compiler, so this PR also removes them. closes rust-lang#49300 cc rust-lang#60869 r? @petrochenkov
2 parents fcef4b1 + 4198df1 commit af3d9e5

File tree

12 files changed

+247
-154
lines changed

12 files changed

+247
-154
lines changed

src/librustc/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ impl<F: fmt::Write> FmtPrinter<'_, 'tcx, F> {
14831483
}
14841484

14851485
// Replace any anonymous late-bound regions with named
1486-
// variants, using gensym'd identifiers, so that we can
1486+
// variants, using new unique identifiers, so that we can
14871487
// clearly differentiate between named and unnamed regions in
14881488
// the output. We'll probably want to tweak this over time to
14891489
// decide just how much information to give.

src/librustc_resolve/build_reduced_graph.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ impl<'a> Resolver<'a> {
9494
where T: ToNameBinding<'a>,
9595
{
9696
let binding = def.to_name_binding(self.arenas);
97-
if let Err(old_binding) = self.try_define(parent, ident, ns, binding) {
97+
let key = self.new_key(ident, ns);
98+
if let Err(old_binding) = self.try_define(parent, key, binding) {
9899
self.report_conflict(parent, ident, ns, old_binding, &binding);
99100
}
100101
}
@@ -349,9 +350,12 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
349350

350351
self.r.indeterminate_imports.push(directive);
351352
match directive.subclass {
353+
// Don't add unresolved underscore imports to modules
354+
SingleImport { target: Ident { name: kw::Underscore, .. }, .. } => {}
352355
SingleImport { target, type_ns_only, .. } => {
353356
self.r.per_ns(|this, ns| if !type_ns_only || ns == TypeNS {
354-
let mut resolution = this.resolution(current_module, target, ns).borrow_mut();
357+
let key = this.new_key(target, ns);
358+
let mut resolution = this.resolution(current_module, key).borrow_mut();
355359
resolution.add_single_import(directive);
356360
});
357361
}
@@ -407,7 +411,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
407411
};
408412
match use_tree.kind {
409413
ast::UseTreeKind::Simple(rename, ..) => {
410-
let mut ident = use_tree.ident().gensym_if_underscore();
414+
let mut ident = use_tree.ident();
411415
let mut module_path = prefix;
412416
let mut source = module_path.pop().unwrap();
413417
let mut type_ns_only = false;
@@ -585,7 +589,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
585589
let parent_scope = &self.parent_scope;
586590
let parent = parent_scope.module;
587591
let expansion = parent_scope.expansion;
588-
let ident = item.ident.gensym_if_underscore();
592+
let ident = item.ident;
589593
let sp = item.span;
590594
let vis = self.resolve_visibility(&item.vis);
591595

@@ -851,10 +855,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
851855
fn build_reduced_graph_for_external_crate_res(&mut self, child: Export<NodeId>) {
852856
let parent = self.parent_scope.module;
853857
let Export { ident, res, vis, span } = child;
854-
// FIXME: We shouldn't create the gensym here, it should come from metadata,
855-
// but metadata cannot encode gensyms currently, so we create it here.
856-
// This is only a guess, two equivalent idents may incorrectly get different gensyms here.
857-
let ident = ident.gensym_if_underscore();
858858
let expansion = ExpnId::root(); // FIXME(jseyfried) intercrate hygiene
859859
// Record primary definitions.
860860
match res {

src/librustc_resolve/diagnostics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ impl<'a> Resolver<'a> {
8080
names: &mut Vec<TypoSuggestion>,
8181
filter_fn: &impl Fn(Res) -> bool,
8282
) {
83-
for (&(ident, _), resolution) in self.resolutions(module).borrow().iter() {
83+
for (key, resolution) in self.resolutions(module).borrow().iter() {
8484
if let Some(binding) = resolution.borrow().binding {
8585
let res = binding.res();
8686
if filter_fn(res) {
87-
names.push(TypoSuggestion::from_res(ident.name, res));
87+
names.push(TypoSuggestion::from_res(key.ident.name, res));
8888
}
8989
}
9090
}
@@ -849,7 +849,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
849849
}
850850

851851
let resolutions = self.r.resolutions(crate_module).borrow();
852-
let resolution = resolutions.get(&(ident, MacroNS))?;
852+
let resolution = resolutions.get(&self.r.new_key(ident, MacroNS))?;
853853
let binding = resolution.borrow().binding()?;
854854
if let Res::Def(DefKind::Macro(MacroKind::Bang), _) = binding.res() {
855855
let module_name = crate_module.kind.name().unwrap();

src/librustc_resolve/lib.rs

+33-5
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,22 @@ impl ModuleKind {
432432
}
433433
}
434434

435-
type Resolutions<'a> = RefCell<FxIndexMap<(Ident, Namespace), &'a RefCell<NameResolution<'a>>>>;
435+
/// A key that identifies a binding in a given `Module`.
436+
///
437+
/// Multiple bindings in the same module can have the same key (in a valid
438+
/// program) if all but one of them come from glob imports.
439+
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
440+
struct BindingKey {
441+
/// The identifier for the binding, aways the `modern` version of the
442+
/// identifier.
443+
ident: Ident,
444+
ns: Namespace,
445+
/// 0 if ident is not `_`, otherwise a value that's unique to the specific
446+
/// `_` in the expanded AST that introduced this binding.
447+
disambiguator: u32,
448+
}
449+
450+
type Resolutions<'a> = RefCell<FxIndexMap<BindingKey, &'a RefCell<NameResolution<'a>>>>;
436451

437452
/// One node in the tree of modules.
438453
pub struct ModuleData<'a> {
@@ -492,8 +507,8 @@ impl<'a> ModuleData<'a> {
492507
fn for_each_child<R, F>(&'a self, resolver: &mut R, mut f: F)
493508
where R: AsMut<Resolver<'a>>, F: FnMut(&mut R, Ident, Namespace, &'a NameBinding<'a>)
494509
{
495-
for (&(ident, ns), name_resolution) in resolver.as_mut().resolutions(self).borrow().iter() {
496-
name_resolution.borrow().binding.map(|binding| f(resolver, ident, ns, binding));
510+
for (key, name_resolution) in resolver.as_mut().resolutions(self).borrow().iter() {
511+
name_resolution.borrow().binding.map(|binding| f(resolver, key.ident, key.ns, binding));
497512
}
498513
}
499514

@@ -882,6 +897,7 @@ pub struct Resolver<'a> {
882897
module_map: FxHashMap<DefId, Module<'a>>,
883898
extern_module_map: FxHashMap<DefId, Module<'a>>,
884899
binding_parent_modules: FxHashMap<PtrKey<'a, NameBinding<'a>>, Module<'a>>,
900+
underscore_disambiguator: u32,
885901

886902
/// Maps glob imports to the names of items actually imported.
887903
pub glob_map: GlobMap,
@@ -1160,6 +1176,7 @@ impl<'a> Resolver<'a> {
11601176
extern_crate_map: Default::default(),
11611177
export_map: FxHashMap::default(),
11621178
trait_map: Default::default(),
1179+
underscore_disambiguator: 0,
11631180
empty_module,
11641181
module_map,
11651182
block_map: Default::default(),
@@ -1284,6 +1301,17 @@ impl<'a> Resolver<'a> {
12841301
self.arenas.alloc_module(module)
12851302
}
12861303

1304+
fn new_key(&mut self, ident: Ident, ns: Namespace) -> BindingKey {
1305+
let ident = ident.modern();
1306+
let disambiguator = if ident.name == kw::Underscore {
1307+
self.underscore_disambiguator += 1;
1308+
self.underscore_disambiguator
1309+
} else {
1310+
0
1311+
};
1312+
BindingKey { ident, ns, disambiguator }
1313+
}
1314+
12871315
fn resolutions(&mut self, module: Module<'a>) -> &'a Resolutions<'a> {
12881316
if module.populate_on_access.get() {
12891317
module.populate_on_access.set(false);
@@ -1292,9 +1320,9 @@ impl<'a> Resolver<'a> {
12921320
&module.lazy_resolutions
12931321
}
12941322

1295-
fn resolution(&mut self, module: Module<'a>, ident: Ident, ns: Namespace)
1323+
fn resolution(&mut self, module: Module<'a>, key: BindingKey)
12961324
-> &'a RefCell<NameResolution<'a>> {
1297-
*self.resolutions(module).borrow_mut().entry((ident.modern(), ns))
1325+
*self.resolutions(module).borrow_mut().entry(key)
12981326
.or_insert_with(|| self.arenas.alloc_name_resolution())
12991327
}
13001328

0 commit comments

Comments
 (0)