Skip to content

Commit b107a4e

Browse files
committed
Refactor module.try_define_child(..) -> resolver.try_define(module, ..).
1 parent 096c3ed commit b107a4e

File tree

3 files changed

+75
-69
lines changed

3 files changed

+75
-69
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport};
1717
use Module;
1818
use Namespace::{self, TypeNS, ValueNS};
19-
use {NameBinding, NameBindingKind};
19+
use {NameBinding, NameBindingKind, ToNameBinding};
2020
use ParentLink::{ModuleParentLink, BlockParentLink};
2121
use Resolver;
2222
use {resolve_error, resolve_struct_error, ResolutionError};
@@ -39,10 +39,6 @@ use syntax::visit::{self, Visitor};
3939

4040
use syntax_pos::{Span, DUMMY_SP};
4141

42-
trait ToNameBinding<'a> {
43-
fn to_name_binding(self) -> NameBinding<'a>;
44-
}
45-
4642
impl<'a> ToNameBinding<'a> for (Module<'a>, Span, ty::Visibility) {
4743
fn to_name_binding(self) -> NameBinding<'a> {
4844
NameBinding { kind: NameBindingKind::Module(self.0), span: self.1, vis: self.2 }
@@ -68,18 +64,13 @@ impl<'b> Resolver<'b> {
6864
visit::walk_crate(&mut visitor, krate);
6965
}
7066

71-
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined.
72-
fn try_define<T>(&self, parent: Module<'b>, name: Name, ns: Namespace, def: T)
73-
where T: ToNameBinding<'b>
74-
{
75-
let _ = parent.try_define_child(name, ns, def.to_name_binding());
76-
}
77-
7867
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
7968
/// otherwise, reports an error.
80-
fn define<T: ToNameBinding<'b>>(&self, parent: Module<'b>, name: Name, ns: Namespace, def: T) {
69+
fn define<T>(&mut self, parent: Module<'b>, name: Name, ns: Namespace, def: T)
70+
where T: ToNameBinding<'b>,
71+
{
8172
let binding = def.to_name_binding();
82-
if let Err(old_binding) = parent.try_define_child(name, ns, binding.clone()) {
73+
if let Err(old_binding) = self.try_define(parent, name, ns, binding.clone()) {
8374
self.report_conflict(parent, name, ns, old_binding, &binding);
8475
}
8576
}
@@ -399,14 +390,14 @@ impl<'b> Resolver<'b> {
399390
name, vis);
400391
let parent_link = ModuleParentLink(parent, name);
401392
let module = self.new_module(parent_link, Some(def), true);
402-
self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
393+
let _ = self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
403394
}
404395
Def::Variant(_, variant_id) => {
405396
debug!("(building reduced graph for external crate) building variant {}", name);
406397
// Variants are always treated as importable to allow them to be glob used.
407398
// All variants are defined in both type and value namespaces as future-proofing.
408-
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
409-
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
399+
let _ = self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
400+
let _ = self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
410401
if self.session.cstore.variant_kind(variant_id) == Some(VariantKind::Struct) {
411402
// Not adding fields for variants as they are not accessed with a self receiver
412403
self.structs.insert(variant_id, Vec::new());
@@ -419,7 +410,7 @@ impl<'b> Resolver<'b> {
419410
Def::Method(..) => {
420411
debug!("(building reduced graph for external crate) building value (fn/static) {}",
421412
name);
422-
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
413+
let _ = self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
423414
}
424415
Def::Trait(def_id) => {
425416
debug!("(building reduced graph for external crate) building type {}", name);
@@ -441,20 +432,20 @@ impl<'b> Resolver<'b> {
441432

442433
let parent_link = ModuleParentLink(parent, name);
443434
let module = self.new_module(parent_link, Some(def), true);
444-
self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
435+
let _ = self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
445436
}
446437
Def::TyAlias(..) | Def::AssociatedTy(..) => {
447438
debug!("(building reduced graph for external crate) building type {}", name);
448-
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
439+
let _ = self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
449440
}
450441
Def::Struct(def_id)
451442
if self.session.cstore.tuple_struct_definition_if_ctor(def_id).is_none() => {
452443
debug!("(building reduced graph for external crate) building type and value for {}",
453444
name);
454-
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
445+
let _ = self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
455446
if let Some(ctor_def_id) = self.session.cstore.struct_ctor_def_id(def_id) {
456447
let def = Def::Struct(ctor_def_id);
457-
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
448+
let _ = self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
458449
}
459450

460451
// Record the def ID and fields of this struct.

src/librustc_resolve/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,16 @@ pub struct NameBinding<'a> {
818818
vis: ty::Visibility,
819819
}
820820

821+
pub trait ToNameBinding<'a> {
822+
fn to_name_binding(self) -> NameBinding<'a>;
823+
}
824+
825+
impl<'a> ToNameBinding<'a> for NameBinding<'a> {
826+
fn to_name_binding(self) -> NameBinding<'a> {
827+
self
828+
}
829+
}
830+
821831
#[derive(Clone, Debug)]
822832
enum NameBindingKind<'a> {
823833
Def(Def),

src/librustc_resolve/resolve_imports.rs

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use self::ImportDirectiveSubclass::*;
1212

1313
use Module;
1414
use Namespace::{self, TypeNS, ValueNS};
15-
use {NameBinding, NameBindingKind, PrivacyError};
15+
use {NameBinding, NameBindingKind, PrivacyError, ToNameBinding};
1616
use ResolveResult;
1717
use ResolveResult::*;
1818
use Resolver;
@@ -226,28 +226,6 @@ impl<'a> ::ModuleS<'a> {
226226
Failed(None)
227227
}
228228

229-
// Define the name or return the existing binding if there is a collision.
230-
pub fn try_define_child(&self, name: Name, ns: Namespace, binding: NameBinding<'a>)
231-
-> Result<(), &'a NameBinding<'a>> {
232-
let binding = self.arenas.alloc_name_binding(binding);
233-
self.update_resolution(name, ns, |resolution| {
234-
if let Some(old_binding) = resolution.binding {
235-
if binding.is_glob_import() {
236-
resolution.duplicate_globs.push(binding);
237-
} else if old_binding.is_glob_import() {
238-
resolution.duplicate_globs.push(old_binding);
239-
resolution.binding = Some(binding);
240-
} else {
241-
return Err(old_binding);
242-
}
243-
} else {
244-
resolution.binding = Some(binding);
245-
}
246-
247-
Ok(())
248-
})
249-
}
250-
251229
pub fn add_import_directive(&self,
252230
module_path: Vec<Name>,
253231
subclass: ImportDirectiveSubclass,
@@ -277,19 +255,45 @@ impl<'a> ::ModuleS<'a> {
277255
GlobImport { .. } => self.globs.borrow_mut().push(directive),
278256
}
279257
}
258+
}
280259

281-
// Use `update` to mutate the resolution for the name.
260+
impl<'a> Resolver<'a> {
261+
// Define the name or return the existing binding if there is a collision.
262+
pub fn try_define<T>(&mut self, module: Module<'a>, name: Name, ns: Namespace, binding: T)
263+
-> Result<(), &'a NameBinding<'a>>
264+
where T: ToNameBinding<'a>
265+
{
266+
let binding = self.arenas.alloc_name_binding(binding.to_name_binding());
267+
self.update_resolution(module, name, ns, |_, resolution| {
268+
if let Some(old_binding) = resolution.binding {
269+
if binding.is_glob_import() {
270+
resolution.duplicate_globs.push(binding);
271+
} else if old_binding.is_glob_import() {
272+
resolution.duplicate_globs.push(old_binding);
273+
resolution.binding = Some(binding);
274+
} else {
275+
return Err(old_binding);
276+
}
277+
} else {
278+
resolution.binding = Some(binding);
279+
}
280+
281+
Ok(())
282+
})
283+
}
284+
285+
// Use `f` to mutate the resolution of the name in the module.
282286
// If the resolution becomes a success, define it in the module's glob importers.
283-
fn update_resolution<T, F>(&self, name: Name, ns: Namespace, update: F) -> T
284-
where F: FnOnce(&mut NameResolution<'a>) -> T
287+
fn update_resolution<T, F>(&mut self, module: Module<'a>, name: Name, ns: Namespace, f: F) -> T
288+
where F: FnOnce(&mut Resolver<'a>, &mut NameResolution<'a>) -> T
285289
{
286-
// Ensure that `resolution` isn't borrowed during `define_in_glob_importers`,
287-
// where it might end up getting re-defined via a glob cycle.
290+
// Ensure that `resolution` isn't borrowed when defining in the module's glob importers,
291+
// during which the resolution might end up getting re-defined via a glob cycle.
288292
let (new_binding, t) = {
289-
let mut resolution = &mut *self.resolution(name, ns).borrow_mut();
293+
let mut resolution = &mut *module.resolution(name, ns).borrow_mut();
290294
let was_known = resolution.binding().is_some();
291295

292-
let t = update(resolution);
296+
let t = f(self, resolution);
293297

294298
if was_known { return t; }
295299
match resolution.binding() {
@@ -298,15 +302,14 @@ impl<'a> ::ModuleS<'a> {
298302
}
299303
};
300304

301-
self.define_in_glob_importers(name, ns, new_binding);
302-
t
303-
}
304-
305-
fn define_in_glob_importers(&self, name: Name, ns: Namespace, binding: &'a NameBinding<'a>) {
306-
if !binding.is_importable() || !binding.is_pseudo_public() { return }
307-
for &(importer, directive) in self.glob_importers.borrow_mut().iter() {
308-
let _ = importer.try_define_child(name, ns, directive.import(binding));
305+
// Define `new_binding` in `module`s glob importers.
306+
if new_binding.is_importable() && new_binding.is_pseudo_public() {
307+
for &(importer, directive) in module.glob_importers.borrow_mut().iter() {
308+
let _ = self.try_define(importer, name, ns, directive.import(new_binding));
309+
}
309310
}
311+
312+
t
310313
}
311314
}
312315

@@ -396,7 +399,9 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
396399

397400
// Define a "dummy" resolution containing a Def::Err as a placeholder for a
398401
// failed resolution
399-
fn import_dummy_binding(&self, source_module: Module<'b>, directive: &'b ImportDirective<'b>) {
402+
fn import_dummy_binding(&mut self,
403+
source_module: Module<'b>,
404+
directive: &'b ImportDirective<'b>) {
400405
if let SingleImport { target, .. } = directive.subclass {
401406
let dummy_binding = self.arenas.alloc_name_binding(NameBinding {
402407
kind: NameBindingKind::Def(Def::Err),
@@ -405,14 +410,14 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
405410
});
406411
let dummy_binding = directive.import(dummy_binding);
407412

408-
let _ = source_module.try_define_child(target, ValueNS, dummy_binding.clone());
409-
let _ = source_module.try_define_child(target, TypeNS, dummy_binding);
413+
let _ = self.try_define(source_module, target, ValueNS, dummy_binding.clone());
414+
let _ = self.try_define(source_module, target, TypeNS, dummy_binding);
410415
}
411416
}
412417

413418
/// Resolves an `ImportResolvingError` into the correct enum discriminant
414419
/// and passes that on to `resolve_error`.
415-
fn import_resolving_error(&self, e: ImportResolvingError<'b>) {
420+
fn import_resolving_error(&mut self, e: ImportResolvingError<'b>) {
416421
// If the error is a single failed import then create a "fake" import
417422
// resolution for it so that later resolve stages won't complain.
418423
self.import_dummy_binding(e.source_module, e.import_directive);
@@ -492,7 +497,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
492497
match *result {
493498
Failed(..) if !determined.get() => {
494499
determined.set(true);
495-
module_.update_resolution(target, ns, |resolution| {
500+
self.update_resolution(module_, target, ns, |_, resolution| {
496501
resolution.single_imports.directive_failed()
497502
});
498503
}
@@ -508,7 +513,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
508513
Success(binding) if !determined.get() => {
509514
determined.set(true);
510515
let imported_binding = directive.import(binding);
511-
let conflict = module_.try_define_child(target, ns, imported_binding);
516+
let conflict = self.try_define(module_, target, ns, imported_binding);
512517
if let Err(old_binding) = conflict {
513518
let binding = &directive.import(binding);
514519
self.report_conflict(module_, target, ns, binding, old_binding);
@@ -551,7 +556,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
551556
for &(ns, result) in &[(ValueNS, &value_result), (TypeNS, &type_result)] {
552557
let binding = match *result { Success(binding) => binding, _ => continue };
553558
self.privacy_errors.push(PrivacyError(directive.span, source, binding));
554-
let _ = module_.try_define_child(target, ns, directive.import(binding));
559+
let _ = self.try_define(module_, target, ns, directive.import(binding));
555560
}
556561
}
557562

@@ -626,14 +631,14 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
626631
// Add to target_module's glob_importers
627632
target_module.glob_importers.borrow_mut().push((module_, directive));
628633

629-
// Ensure that `resolutions` isn't borrowed during `try_define_child`,
634+
// Ensure that `resolutions` isn't borrowed during `try_define`,
630635
// since it might get updated via a glob cycle.
631636
let bindings = target_module.resolutions.borrow().iter().filter_map(|(name, resolution)| {
632637
resolution.borrow().binding().map(|binding| (*name, binding))
633638
}).collect::<Vec<_>>();
634639
for ((name, ns), binding) in bindings {
635640
if binding.is_importable() && binding.is_pseudo_public() {
636-
let _ = module_.try_define_child(name, ns, directive.import(binding));
641+
let _ = self.try_define(module_, name, ns, directive.import(binding));
637642
}
638643
}
639644

0 commit comments

Comments
 (0)