Skip to content

Commit aa19274

Browse files
committed
De-genericize try_define.
1 parent 59de7f8 commit aa19274

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
use macros::{InvocationData, LegacyScope};
1717
use resolve_imports::ImportDirective;
1818
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport};
19-
use {Resolver, Module, ModuleS, ModuleKind, NameBinding, NameBindingKind, ToNameBinding};
19+
use {Module, ModuleS, ModuleKind, NameBinding, NameBindingKind, ToNameBinding};
20+
use {Resolver, ResolverArenas};
2021
use Namespace::{self, TypeNS, ValueNS, MacroNS};
2122
use {resolve_error, resolve_struct_error, ResolutionError};
2223

@@ -45,24 +46,24 @@ use syntax::visit::{self, Visitor};
4546
use syntax_pos::{Span, DUMMY_SP};
4647

4748
impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, Mark) {
48-
fn to_name_binding(self) -> NameBinding<'a> {
49-
NameBinding {
49+
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
50+
arenas.alloc_name_binding(NameBinding {
5051
kind: NameBindingKind::Module(self.0),
5152
vis: self.1,
5253
span: self.2,
5354
expansion: self.3,
54-
}
55+
})
5556
}
5657
}
5758

5859
impl<'a> ToNameBinding<'a> for (Def, ty::Visibility, Span, Mark) {
59-
fn to_name_binding(self) -> NameBinding<'a> {
60-
NameBinding {
60+
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
61+
arenas.alloc_name_binding(NameBinding {
6162
kind: NameBindingKind::Def(self.0),
6263
vis: self.1,
6364
span: self.2,
6465
expansion: self.3,
65-
}
66+
})
6667
}
6768
}
6869

@@ -79,8 +80,8 @@ impl<'b> Resolver<'b> {
7980
fn define<T>(&mut self, parent: Module<'b>, ident: Ident, ns: Namespace, def: T)
8081
where T: ToNameBinding<'b>,
8182
{
82-
let binding = def.to_name_binding();
83-
if let Err(old_binding) = self.try_define(parent, ident, ns, binding.clone()) {
83+
let binding = def.to_name_binding(self.arenas);
84+
if let Err(old_binding) = self.try_define(parent, ident, ns, binding) {
8485
self.report_conflict(parent, ident, ns, old_binding, &binding);
8586
}
8687
}
@@ -238,8 +239,8 @@ impl<'b> Resolver<'b> {
238239
// n.b. we don't need to look at the path option here, because cstore already did
239240
let crate_id = self.session.cstore.extern_mod_stmt_cnum(item.id).unwrap();
240241
let module = self.get_extern_crate_root(crate_id);
241-
let binding = (module, ty::Visibility::Public, sp, expansion).to_name_binding();
242-
let binding = self.arenas.alloc_name_binding(binding);
242+
let binding =
243+
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.arenas);
243244
let directive = self.arenas.alloc_import_directive(ImportDirective {
244245
id: item.id,
245246
parent: parent,

src/librustc_resolve/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -875,11 +875,11 @@ pub struct NameBinding<'a> {
875875
}
876876

877877
pub trait ToNameBinding<'a> {
878-
fn to_name_binding(self) -> NameBinding<'a>;
878+
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a>;
879879
}
880880

881-
impl<'a> ToNameBinding<'a> for NameBinding<'a> {
882-
fn to_name_binding(self) -> NameBinding<'a> {
881+
impl<'a> ToNameBinding<'a> for &'a NameBinding<'a> {
882+
fn to_name_binding(self, _: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
883883
self
884884
}
885885
}

src/librustc_resolve/resolve_imports.rs

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

1313
use {AmbiguityError, Module, PerNS};
1414
use Namespace::{self, TypeNS, MacroNS};
15-
use {NameBinding, NameBindingKind, PathResult, PathScope, PrivacyError, ToNameBinding};
15+
use {NameBinding, NameBindingKind, PathResult, PathScope, PrivacyError};
1616
use Resolver;
1717
use {names_to_string, module_to_string};
1818
use {resolve_error, ResolutionError};
@@ -273,7 +273,7 @@ impl<'a> Resolver<'a> {
273273
// Given a binding and an import directive that resolves to it,
274274
// return the corresponding binding defined by the import directive.
275275
pub fn import(&mut self, binding: &'a NameBinding<'a>, directive: &'a ImportDirective<'a>)
276-
-> NameBinding<'a> {
276+
-> &'a NameBinding<'a> {
277277
let vis = if binding.pseudo_vis().is_at_least(directive.vis.get(), self) ||
278278
!directive.is_glob() && binding.is_extern_crate() { // c.f. `PRIVATE_IN_PUBLIC`
279279
directive.vis.get()
@@ -287,7 +287,7 @@ impl<'a> Resolver<'a> {
287287
}
288288
}
289289

290-
NameBinding {
290+
self.arenas.alloc_name_binding(NameBinding {
291291
kind: NameBindingKind::Import {
292292
binding: binding,
293293
directive: directive,
@@ -296,16 +296,17 @@ impl<'a> Resolver<'a> {
296296
span: directive.span,
297297
vis: vis,
298298
expansion: directive.expansion,
299-
}
299+
})
300300
}
301301

302302
// Define the name or return the existing binding if there is a collision.
303-
pub fn try_define<T>(&mut self, module: Module<'a>, ident: Ident, ns: Namespace, binding: T)
304-
-> Result<(), &'a NameBinding<'a>>
305-
where T: ToNameBinding<'a>
306-
{
303+
pub fn try_define(&mut self,
304+
module: Module<'a>,
305+
ident: Ident,
306+
ns: Namespace,
307+
binding: &'a NameBinding<'a>)
308+
-> Result<(), &'a NameBinding<'a>> {
307309
let ident = ident.unhygienize();
308-
let binding = self.arenas.alloc_name_binding(binding.to_name_binding());
309310
self.update_resolution(module, ident, ns, |this, resolution| {
310311
if let Some(old_binding) = resolution.binding {
311312
if binding.is_glob_import() {
@@ -389,7 +390,7 @@ impl<'a> Resolver<'a> {
389390
let dummy_binding = self.dummy_binding;
390391
let dummy_binding = self.import(dummy_binding, directive);
391392
self.per_ns(|this, ns| {
392-
let _ = this.try_define(directive.parent, target, ns, dummy_binding.clone());
393+
let _ = this.try_define(directive.parent, target, ns, dummy_binding);
393394
});
394395
}
395396
}
@@ -516,10 +517,11 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
516517
return
517518
};
518519

520+
let parent = directive.parent;
519521
match result[ns].get() {
520522
Err(Undetermined) => indeterminate = true,
521523
Err(Determined) => {
522-
this.update_resolution(directive.parent, target, ns, |_, resolution| {
524+
this.update_resolution(parent, target, ns, |_, resolution| {
523525
resolution.single_imports.directive_failed()
524526
});
525527
}
@@ -534,10 +536,9 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
534536
}
535537
Ok(binding) => {
536538
let imported_binding = this.import(binding, directive);
537-
let conflict = this.try_define(directive.parent, target, ns, imported_binding);
539+
let conflict = this.try_define(parent, target, ns, imported_binding);
538540
if let Err(old_binding) = conflict {
539-
let binding = &this.import(binding, directive);
540-
this.report_conflict(directive.parent, target, ns, binding, old_binding);
541+
this.report_conflict(parent, target, ns, imported_binding, old_binding);
541542
}
542543
}
543544
}

0 commit comments

Comments
 (0)