Skip to content

Commit 96b4dc4

Browse files
committed
Refactor away the fields id and is_public of ImportResolution and rename ImportResolution to NameResolution
1 parent 4428b1c commit 96b4dc4

File tree

3 files changed

+49
-65
lines changed

3 files changed

+49
-65
lines changed

src/librustc_resolve/build_reduced_graph.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use DefModifiers;
1717
use resolve_imports::ImportDirective;
1818
use resolve_imports::ImportDirectiveSubclass::{self, SingleImport, GlobImport};
19-
use resolve_imports::ImportResolution;
19+
use resolve_imports::NameResolution;
2020
use Module;
2121
use Namespace::{self, TypeNS, ValueNS};
2222
use {NameBinding, NameBindingKind};
@@ -703,13 +703,10 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
703703
let mut import_resolutions = module_.import_resolutions.borrow_mut();
704704
for &ns in [TypeNS, ValueNS].iter() {
705705
let mut resolution = import_resolutions.entry((target, ns)).or_insert(
706-
ImportResolution::new(id, is_public)
706+
NameResolution::default()
707707
);
708708

709709
resolution.outstanding_references += 1;
710-
// the source of this name is different now
711-
resolution.id = id;
712-
resolution.is_public = is_public;
713710
}
714711
}
715712
GlobImport => {

src/librustc_resolve/lib.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ use std::cell::{Cell, RefCell};
9292
use std::fmt;
9393
use std::mem::replace;
9494

95-
use resolve_imports::{ImportDirective, ImportResolution};
95+
use resolve_imports::{ImportDirective, NameResolution};
9696

9797
// NB: This module needs to be declared first so diagnostics are
9898
// registered before they are used.
@@ -346,8 +346,9 @@ fn resolve_struct_error<'b, 'a: 'b, 'tcx: 'a>(resolver: &'b Resolver<'a, 'tcx>,
346346
.import_resolutions
347347
.borrow()
348348
.get(&(name, ValueNS)) {
349-
let item = resolver.ast_map.expect_item(directive.id);
350-
err.span_note(item.span, "constant imported here");
349+
if let Some(binding) = directive.binding {
350+
err.span_note(binding.span.unwrap(), "constant imported here");
351+
}
351352
}
352353
err
353354
}
@@ -814,7 +815,7 @@ pub struct ModuleS<'a> {
814815
anonymous_children: RefCell<NodeMap<Module<'a>>>,
815816

816817
// The status of resolving each import in this module.
817-
import_resolutions: RefCell<HashMap<(Name, Namespace), ImportResolution<'a>>>,
818+
import_resolutions: RefCell<HashMap<(Name, Namespace), NameResolution<'a>>>,
818819

819820
// The number of unresolved globs that this module exports.
820821
glob_count: Cell<usize>,
@@ -1219,8 +1220,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12191220
}
12201221

12211222
#[inline]
1222-
fn record_import_use(&mut self, name: Name, ns: Namespace, resolution: &ImportResolution<'a>) {
1223-
let import_id = resolution.id;
1223+
fn record_import_use(&mut self, name: Name, ns: Namespace, binding: &NameBinding<'a>) {
1224+
let import_id = match binding.kind {
1225+
NameBindingKind::Import { id, .. } => id,
1226+
_ => return,
1227+
};
1228+
12241229
self.used_imports.insert((import_id, ns));
12251230

12261231
if !self.make_glob_map {
@@ -1592,20 +1597,22 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15921597

15931598
// Check the list of resolved imports.
15941599
match module_.import_resolutions.borrow().get(&(name, namespace)) {
1595-
Some(import_resolution) if allow_private_imports || import_resolution.is_public => {
1596-
if import_resolution.is_public && import_resolution.outstanding_references != 0 {
1597-
debug!("(resolving name in module) import unresolved; bailing out");
1598-
return Indeterminate;
1599-
}
1600+
Some(import_resolution) => {
16001601
if let Some(binding) = import_resolution.binding {
1602+
if !allow_private_imports && binding.is_public() { return Failed(None) }
1603+
if binding.is_public() && import_resolution.outstanding_references != 0 {
1604+
debug!("(resolving name in module) import unresolved; bailing out");
1605+
return Indeterminate;
1606+
}
1607+
16011608
debug!("(resolving name in module) resolved to import");
16021609
if record_used {
1603-
self.record_import_use(name, namespace, &import_resolution);
1610+
self.record_import_use(name, namespace, binding);
16041611
}
16051612
return Success(binding);
16061613
}
16071614
}
1608-
Some(..) | None => {} // Continue.
1615+
None => {}
16091616
}
16101617

16111618
// We're out of luck.
@@ -3482,7 +3489,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
34823489
if self.trait_item_map.contains_key(&(name, did)) {
34833490
add_trait_info(&mut found_traits, did, name);
34843491
let trait_name = self.get_trait_name(did);
3485-
self.record_import_use(trait_name, TypeNS, &import);
3492+
self.record_import_use(trait_name, TypeNS, binding);
34863493
}
34873494
}
34883495

src/librustc_resolve/resolve_imports.rs

+26-46
Original file line numberDiff line numberDiff line change
@@ -101,39 +101,30 @@ impl ImportDirective {
101101
}
102102

103103
#[derive(Debug)]
104-
/// An ImportResolution records what we know about an imported name in a given namespace.
104+
/// An NameResolution records what we know about an imported name in a given namespace.
105105
/// More specifically, it records the number of unresolved `use` directives that import the name,
106106
/// the `use` directive importing the name in the namespace, and the `NameBinding` to which the
107107
/// name in the namespace resolves (if applicable).
108108
/// Different `use` directives may import the same name in different namespaces.
109-
pub struct ImportResolution<'a> {
109+
pub struct NameResolution<'a> {
110110
// When outstanding_references reaches zero, outside modules can count on the targets being
111111
// correct. Before then, all bets are off; future `use` directives could override the name.
112112
// Since shadowing is forbidden, the only way outstanding_references > 1 in a legal program
113113
// is if the name is imported by exactly two `use` directives, one of which resolves to a
114114
// value and the other of which resolves to a type.
115115
pub outstanding_references: usize,
116116

117-
/// Whether this resolution came from a `use` or a `pub use`.
118-
pub is_public: bool,
119-
120117
/// Resolution of the name in the namespace
121118
pub binding: Option<&'a NameBinding<'a>>,
122-
123-
/// The source node of the `use` directive
124-
pub id: NodeId,
125119
}
126120

127-
impl<'a> ImportResolution<'a> {
128-
pub fn new(id: NodeId, is_public: bool) -> Self {
129-
ImportResolution {
130-
outstanding_references: 0,
131-
id: id,
132-
binding: None,
133-
is_public: is_public,
134-
}
121+
impl<'a> Default for NameResolution<'a> {
122+
fn default() -> Self {
123+
NameResolution { outstanding_references: 0, binding: None }
135124
}
125+
}
136126

127+
impl<'a> NameResolution<'a> {
137128
pub fn shadowable(&self) -> Shadowable {
138129
match self.binding {
139130
Some(binding) if binding.defined_with(DefModifiers::PRELUDE) =>
@@ -216,8 +207,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
216207
debug!("(resolving import error) adding import resolution for `{}`",
217208
target);
218209

219-
ImportResolution::new(e.import_directive.id,
220-
e.import_directive.is_public)
210+
NameResolution::default()
221211
});
222212

223213
if resolution.binding.is_none() {
@@ -402,13 +392,13 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
402392

403393
// The name is an import which has been fully resolved, so we just follow it.
404394
Some(resolution) if resolution.outstanding_references == 0 => {
405-
// Import resolutions must be declared with "pub" in order to be exported.
406-
if !resolution.is_public {
407-
return Failed(None);
408-
}
409-
410395
if let Some(binding) = resolution.binding {
411-
self.resolver.record_import_use(name, ns, &resolution);
396+
// Import resolutions must be declared with "pub" in order to be exported.
397+
if !binding.is_public() {
398+
return Failed(None);
399+
}
400+
401+
self.resolver.record_import_use(name, ns, binding);
412402
Success(binding)
413403
} else {
414404
Failed(None)
@@ -549,10 +539,8 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
549539

550540
import_resolution.binding =
551541
Some(self.resolver.new_name_binding(directive.import(name_binding)));
552-
import_resolution.id = directive.id;
553-
import_resolution.is_public = directive.is_public;
554542

555-
self.add_export(module_, target, &import_resolution);
543+
self.add_export(module_, target, import_resolution.binding.unwrap());
556544
}
557545
Failed(_) => {
558546
// Continue.
@@ -644,7 +632,6 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
644632
lp: LastPrivate)
645633
-> ResolveResult<()> {
646634
let id = import_directive.id;
647-
let is_public = import_directive.is_public;
648635

649636
// This function works in a highly imperative manner; it eagerly adds
650637
// everything it can to the list of import resolutions of the module
@@ -679,19 +666,17 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
679666
let mut import_resolutions = module_.import_resolutions.borrow_mut();
680667
let mut dest_import_resolution =
681668
import_resolutions.entry((name, ns))
682-
.or_insert_with(|| ImportResolution::new(id, is_public));
669+
.or_insert_with(|| NameResolution::default());
683670

684671
match target_import_resolution.binding {
685-
Some(binding) if target_import_resolution.is_public => {
672+
Some(binding) if binding.is_public() => {
686673
self.check_for_conflicting_import(&dest_import_resolution,
687674
import_directive.span,
688675
name,
689676
ns);
690-
dest_import_resolution.id = id;
691-
dest_import_resolution.is_public = is_public;
692677
dest_import_resolution.binding =
693678
Some(self.resolver.new_name_binding(import_directive.import(binding)));
694-
self.add_export(module_, name, &dest_import_resolution);
679+
self.add_export(module_, name, dest_import_resolution.binding.unwrap());
695680
}
696681
_ => {}
697682
}
@@ -728,12 +713,11 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
728713
import_directive: &ImportDirective,
729714
(name, ns): (Name, Namespace),
730715
name_binding: &'b NameBinding<'b>) {
731-
let id = import_directive.id;
732716
let is_public = import_directive.is_public;
733717

734718
let mut import_resolutions = module_.import_resolutions.borrow_mut();
735719
let dest_import_resolution = import_resolutions.entry((name, ns)).or_insert_with(|| {
736-
ImportResolution::new(id, is_public)
720+
NameResolution::default()
737721
});
738722

739723
debug!("(resolving glob import) writing resolution `{}` in `{}` to `{}`",
@@ -767,9 +751,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
767751
} else {
768752
dest_import_resolution.binding =
769753
Some(self.resolver.new_name_binding(import_directive.import(name_binding)));
770-
dest_import_resolution.id = id;
771-
dest_import_resolution.is_public = is_public;
772-
self.add_export(module_, name, &dest_import_resolution);
754+
self.add_export(module_, name, dest_import_resolution.binding.unwrap());
773755
}
774756
}
775757

@@ -779,13 +761,13 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
779761
(name, ns));
780762
}
781763

782-
fn add_export(&mut self, module: Module<'b>, name: Name, resolution: &ImportResolution<'b>) {
783-
if !resolution.is_public { return }
764+
fn add_export(&mut self, module: Module<'b>, name: Name, binding: &NameBinding<'b>) {
765+
if !binding.is_public() { return }
784766
let node_id = match module.def_id() {
785767
Some(def_id) => self.resolver.ast_map.as_local_node_id(def_id).unwrap(),
786768
None => return,
787769
};
788-
let export = match resolution.binding.as_ref().unwrap().def() {
770+
let export = match binding.def() {
789771
Some(def) => Export { name: name, def_id: def.def_id() },
790772
None => return,
791773
};
@@ -794,7 +776,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
794776

795777
/// Checks that imported names and items don't have the same name.
796778
fn check_for_conflicting_import(&mut self,
797-
import_resolution: &ImportResolution,
779+
import_resolution: &NameResolution,
798780
import_span: Span,
799781
name: Name,
800782
namespace: Namespace) {
@@ -815,8 +797,6 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
815797
}
816798
ValueNS => "value",
817799
};
818-
let use_id = import_resolution.id;
819-
let item = self.resolver.ast_map.expect_item(use_id);
820800
let mut err = struct_span_err!(self.resolver.session,
821801
import_span,
822802
E0252,
@@ -825,7 +805,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
825805
ns_word,
826806
name);
827807
span_note!(&mut err,
828-
item.span,
808+
binding.span.unwrap(),
829809
"previous import of `{}` here",
830810
name);
831811
err.emit();
@@ -848,7 +828,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
848828
/// Checks that imported names and items don't have the same name.
849829
fn check_for_conflicts_between_imports_and_items(&mut self,
850830
module: Module<'b>,
851-
import: &ImportResolution<'b>,
831+
import: &NameResolution<'b>,
852832
import_span: Span,
853833
(name, ns): (Name, Namespace)) {
854834
// Check for item conflicts.

0 commit comments

Comments
 (0)