提交 aa19274b 编写于 作者: J Jeffrey Seyfried

De-genericize `try_define`.

上级 59de7f8f
......@@ -16,7 +16,8 @@
use macros::{InvocationData, LegacyScope};
use resolve_imports::ImportDirective;
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport};
use {Resolver, Module, ModuleS, ModuleKind, NameBinding, NameBindingKind, ToNameBinding};
use {Module, ModuleS, ModuleKind, NameBinding, NameBindingKind, ToNameBinding};
use {Resolver, ResolverArenas};
use Namespace::{self, TypeNS, ValueNS, MacroNS};
use {resolve_error, resolve_struct_error, ResolutionError};
......@@ -45,24 +46,24 @@
use syntax_pos::{Span, DUMMY_SP};
impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, Mark) {
fn to_name_binding(self) -> NameBinding<'a> {
NameBinding {
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
arenas.alloc_name_binding(NameBinding {
kind: NameBindingKind::Module(self.0),
vis: self.1,
span: self.2,
expansion: self.3,
}
})
}
}
impl<'a> ToNameBinding<'a> for (Def, ty::Visibility, Span, Mark) {
fn to_name_binding(self) -> NameBinding<'a> {
NameBinding {
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
arenas.alloc_name_binding(NameBinding {
kind: NameBindingKind::Def(self.0),
vis: self.1,
span: self.2,
expansion: self.3,
}
})
}
}
......@@ -79,8 +80,8 @@ impl<'b> Resolver<'b> {
fn define<T>(&mut self, parent: Module<'b>, ident: Ident, ns: Namespace, def: T)
where T: ToNameBinding<'b>,
{
let binding = def.to_name_binding();
if let Err(old_binding) = self.try_define(parent, ident, ns, binding.clone()) {
let binding = def.to_name_binding(self.arenas);
if let Err(old_binding) = self.try_define(parent, ident, ns, binding) {
self.report_conflict(parent, ident, ns, old_binding, &binding);
}
}
......@@ -238,8 +239,8 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
// n.b. we don't need to look at the path option here, because cstore already did
let crate_id = self.session.cstore.extern_mod_stmt_cnum(item.id).unwrap();
let module = self.get_extern_crate_root(crate_id);
let binding = (module, ty::Visibility::Public, sp, expansion).to_name_binding();
let binding = self.arenas.alloc_name_binding(binding);
let binding =
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.arenas);
let directive = self.arenas.alloc_import_directive(ImportDirective {
id: item.id,
parent: parent,
......
......@@ -875,11 +875,11 @@ pub struct NameBinding<'a> {
}
pub trait ToNameBinding<'a> {
fn to_name_binding(self) -> NameBinding<'a>;
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a>;
}
impl<'a> ToNameBinding<'a> for NameBinding<'a> {
fn to_name_binding(self) -> NameBinding<'a> {
impl<'a> ToNameBinding<'a> for &'a NameBinding<'a> {
fn to_name_binding(self, _: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
self
}
}
......
......@@ -12,7 +12,7 @@
use {AmbiguityError, Module, PerNS};
use Namespace::{self, TypeNS, MacroNS};
use {NameBinding, NameBindingKind, PathResult, PathScope, PrivacyError, ToNameBinding};
use {NameBinding, NameBindingKind, PathResult, PathScope, PrivacyError};
use Resolver;
use {names_to_string, module_to_string};
use {resolve_error, ResolutionError};
......@@ -273,7 +273,7 @@ pub fn add_import_directive(&mut self,
// Given a binding and an import directive that resolves to it,
// return the corresponding binding defined by the import directive.
pub fn import(&mut self, binding: &'a NameBinding<'a>, directive: &'a ImportDirective<'a>)
-> NameBinding<'a> {
-> &'a NameBinding<'a> {
let vis = if binding.pseudo_vis().is_at_least(directive.vis.get(), self) ||
!directive.is_glob() && binding.is_extern_crate() { // c.f. `PRIVATE_IN_PUBLIC`
directive.vis.get()
......@@ -287,7 +287,7 @@ pub fn import(&mut self, binding: &'a NameBinding<'a>, directive: &'a ImportDire
}
}
NameBinding {
self.arenas.alloc_name_binding(NameBinding {
kind: NameBindingKind::Import {
binding: binding,
directive: directive,
......@@ -296,16 +296,17 @@ pub fn import(&mut self, binding: &'a NameBinding<'a>, directive: &'a ImportDire
span: directive.span,
vis: vis,
expansion: directive.expansion,
}
})
}
// Define the name or return the existing binding if there is a collision.
pub fn try_define<T>(&mut self, module: Module<'a>, ident: Ident, ns: Namespace, binding: T)
-> Result<(), &'a NameBinding<'a>>
where T: ToNameBinding<'a>
{
pub fn try_define(&mut self,
module: Module<'a>,
ident: Ident,
ns: Namespace,
binding: &'a NameBinding<'a>)
-> Result<(), &'a NameBinding<'a>> {
let ident = ident.unhygienize();
let binding = self.arenas.alloc_name_binding(binding.to_name_binding());
self.update_resolution(module, ident, ns, |this, resolution| {
if let Some(old_binding) = resolution.binding {
if binding.is_glob_import() {
......@@ -389,7 +390,7 @@ fn import_dummy_binding(&mut self, directive: &'a ImportDirective<'a>) {
let dummy_binding = self.dummy_binding;
let dummy_binding = self.import(dummy_binding, directive);
self.per_ns(|this, ns| {
let _ = this.try_define(directive.parent, target, ns, dummy_binding.clone());
let _ = this.try_define(directive.parent, target, ns, dummy_binding);
});
}
}
......@@ -516,10 +517,11 @@ fn resolve_import(&mut self, directive: &'b ImportDirective<'b>) -> bool {
return
};
let parent = directive.parent;
match result[ns].get() {
Err(Undetermined) => indeterminate = true,
Err(Determined) => {
this.update_resolution(directive.parent, target, ns, |_, resolution| {
this.update_resolution(parent, target, ns, |_, resolution| {
resolution.single_imports.directive_failed()
});
}
......@@ -534,10 +536,9 @@ fn resolve_import(&mut self, directive: &'b ImportDirective<'b>) -> bool {
}
Ok(binding) => {
let imported_binding = this.import(binding, directive);
let conflict = this.try_define(directive.parent, target, ns, imported_binding);
let conflict = this.try_define(parent, target, ns, imported_binding);
if let Err(old_binding) = conflict {
let binding = &this.import(binding, directive);
this.report_conflict(directive.parent, target, ns, binding, old_binding);
this.report_conflict(parent, target, ns, imported_binding, old_binding);
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册