Skip to content

Commit 1de70d3

Browse files
committed
Auto merge of #31461 - jseyfried:remove_import_resolutions, r=nrc
This PR adds to `NameBinding` so it can more fully represent bindings from imports as well from items, refactors away `Target`, generalizes `ImportResolution` to a simpler type `NameResolution`, and uses a single `NameResolution`-valued map in place the existing maps `children` and `import_resolutions` (of `NameBinding`s and `ImportResolution`s, respectively), simplifying duplicate checking and name resolution. It also unifies the `resolve_name_in_module` in `lib.rs` with its namesake in `resolve_imports.rs`, clarifying and improving the core logic (fixes #31403 and fixes #31404) while maintaining clear future-comparability with shadowable globs (i.e., never reporting that a resolution is a `Success` or is `Failing` unless this would also be knowable with shadowable globs). Since it fixes #31403, this is technically a [breaking-change], but it is exceedingly unlikely to cause breakage in practice. The following is an example of code that would break: ```rust mod foo { pub mod bar {} // This defines bar in the type namespace pub use alpha::bar; // This defines bar in the value namespace // This should define baz in both namespaces, but it only defines baz in the type namespace. pub use self::bar as baz; pub fn baz() {} // This should collide with baz, but now it does not. } pub fn f() {} mod alpha { pub use self::f as bar; // Changing this to `pub fn bar() {}` causes the collision right now. pub use super::*; } ``` r? @nrc
2 parents 106070b + 3df40c0 commit 1de70d3

File tree

5 files changed

+377
-677
lines changed

5 files changed

+377
-677
lines changed

src/librustc_resolve/build_reduced_graph.rs

+10-21
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
use DefModifiers;
1717
use resolve_imports::ImportDirective;
1818
use resolve_imports::ImportDirectiveSubclass::{self, SingleImport, GlobImport};
19-
use resolve_imports::ImportResolution;
2019
use Module;
2120
use Namespace::{self, TypeNS, ValueNS};
22-
use {NameBinding, DefOrModule};
21+
use {NameBinding, NameBindingKind};
2322
use {names_to_string, module_to_string};
2423
use ParentLink::{ModuleParentLink, BlockParentLink};
2524
use Resolver;
@@ -82,8 +81,8 @@ impl<'a> ToNameBinding<'a> for (Module<'a>, Span) {
8281

8382
impl<'a> ToNameBinding<'a> for (Def, Span, DefModifiers) {
8483
fn to_name_binding(self) -> NameBinding<'a> {
85-
let def = DefOrModule::Def(self.0);
86-
NameBinding { modifiers: self.2, def_or_module: def, span: Some(self.1) }
84+
let kind = NameBindingKind::Def(self.0);
85+
NameBinding { modifiers: self.2, kind: kind, span: Some(self.1) }
8786
}
8887
}
8988

@@ -101,16 +100,16 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
101100
fn try_define<T>(&self, parent: Module<'b>, name: Name, ns: Namespace, def: T)
102101
where T: ToNameBinding<'b>
103102
{
104-
parent.try_define_child(name, ns, def.to_name_binding());
103+
let _ = parent.try_define_child(name, ns, self.new_name_binding(def.to_name_binding()));
105104
}
106105

107106
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
108107
/// otherwise, reports an error.
109108
fn define<T: ToNameBinding<'b>>(&self, parent: Module<'b>, name: Name, ns: Namespace, def: T) {
110-
let binding = def.to_name_binding();
111-
let old_binding = match parent.try_define_child(name, ns, binding.clone()) {
112-
Some(old_binding) => old_binding,
113-
None => return,
109+
let binding = self.new_name_binding(def.to_name_binding());
110+
let old_binding = match parent.try_define_child(name, ns, binding) {
111+
Ok(()) => return,
112+
Err(old_binding) => old_binding,
114113
};
115114

116115
let span = binding.span.unwrap_or(DUMMY_SP);
@@ -699,18 +698,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
699698
debug!("(building import directive) building import directive: {}::{}",
700699
names_to_string(&module_.imports.borrow().last().unwrap().module_path),
701700
target);
702-
703-
let mut import_resolutions = module_.import_resolutions.borrow_mut();
704-
for &ns in [TypeNS, ValueNS].iter() {
705-
let mut resolution = import_resolutions.entry((target, ns)).or_insert(
706-
ImportResolution::new(id, is_public)
707-
);
708-
709-
resolution.outstanding_references += 1;
710-
// the source of this name is different now
711-
resolution.id = id;
712-
resolution.is_public = is_public;
713-
}
701+
module_.increment_outstanding_references_for(target, ValueNS);
702+
module_.increment_outstanding_references_for(target, TypeNS);
714703
}
715704
GlobImport => {
716705
// Set the glob flag. This tells us that we don't know the

0 commit comments

Comments
 (0)