提交 87ae68c1 编写于 作者: J Jeffrey Seyfried

Refactor `binding.def()` to return a `Def` instead of an `Option<Def>`.

上级 691d10c3
......@@ -459,7 +459,7 @@ fn resolve_struct_error<'b, 'a: 'b, 'c>(resolver: &'b Resolver<'a>,
err
}
ResolutionError::BindingShadowsSomethingUnacceptable(what_binding, name, binding) => {
let shadows_what = PathResolution::new(binding.def().unwrap()).kind_name();
let shadows_what = PathResolution::new(binding.def()).kind_name();
let mut err = struct_span_err!(resolver.session,
span,
E0530,
......@@ -739,7 +739,7 @@ impl<'a> LexicalScopeBinding<'a> {
fn local_def(self) -> LocalDef {
match self {
LexicalScopeBinding::LocalDef(local_def) => local_def,
LexicalScopeBinding::Item(binding) => LocalDef::from_def(binding.def().unwrap()),
LexicalScopeBinding::Item(binding) => LocalDef::from_def(binding.def()),
}
}
......@@ -877,10 +877,10 @@ fn module(&self) -> Option<Module<'a>> {
}
}
fn def(&self) -> Option<Def> {
fn def(&self) -> Def {
match self.kind {
NameBindingKind::Def(def) => Some(def),
NameBindingKind::Module(module) => module.def,
NameBindingKind::Def(def) => def,
NameBindingKind::Module(module) => module.def.unwrap(),
NameBindingKind::Import { binding, .. } => binding.def(),
}
}
......@@ -916,7 +916,7 @@ fn is_glob_import(&self) -> bool {
}
fn is_importable(&self) -> bool {
match self.def().unwrap() {
match self.def() {
Def::AssociatedConst(..) | Def::Method(..) | Def::AssociatedTy(..) => false,
_ => true,
}
......@@ -1097,7 +1097,7 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> {
fn resolve_generated_global_path(&mut self, path: &hir::Path, is_value: bool) -> Def {
let namespace = if is_value { ValueNS } else { TypeNS };
match self.resolve_crate_relative_path(path.span, &path.segments, namespace) {
Ok(binding) => binding.def().unwrap(),
Ok(binding) => binding.def(),
Err(true) => Def::Err,
Err(false) => {
let path_name = &format!("{}", path);
......@@ -1693,7 +1693,7 @@ fn resolve_item(&mut self, item: &Item) {
&prefix.segments,
TypeNS) {
Ok(binding) => {
let def = binding.def().unwrap();
let def = binding.def();
self.record_def(item.id, PathResolution::new(def));
}
Err(true) => self.record_def(item.id, err_path_resolution()),
......@@ -2309,7 +2309,7 @@ fn resolve_pattern(&mut self,
// entity, then fall back to a fresh binding.
let binding = self.resolve_ident_in_lexical_scope(ident.node, ValueNS, None)
.and_then(LexicalScopeBinding::item);
let resolution = binding.and_then(NameBinding::def).and_then(|def| {
let resolution = binding.map(NameBinding::def).and_then(|def| {
let always_binding = !pat_src.is_refutable() || opt_pat.is_some() ||
bmode != BindingMode::ByValue(Mutability::Immutable);
match def {
......@@ -2443,7 +2443,7 @@ fn resolve_path(&mut self, id: NodeId, path: &Path, path_depth: usize, namespace
if path.global {
let binding = self.resolve_crate_relative_path(span, segments, namespace);
return binding.map(|binding| mk_res(binding.def().unwrap()));
return binding.map(|binding| mk_res(binding.def()));
}
// Try to find a path to an item in a module.
......@@ -2481,7 +2481,7 @@ fn resolve_path(&mut self, id: NodeId, path: &Path, path_depth: usize, namespace
let unqualified_def = resolve_identifier_with_fallback(self, None);
let qualified_binding = self.resolve_module_relative_path(span, segments, namespace);
match (qualified_binding, unqualified_def) {
(Ok(binding), Some(ref ud)) if binding.def().unwrap() == ud.def => {
(Ok(binding), Some(ref ud)) if binding.def() == ud.def => {
self.session
.add_lint(lint::builtin::UNUSED_QUALIFICATIONS,
id,
......@@ -2491,7 +2491,7 @@ fn resolve_path(&mut self, id: NodeId, path: &Path, path_depth: usize, namespace
_ => {}
}
qualified_binding.map(|binding| mk_res(binding.def().unwrap()))
qualified_binding.map(|binding| mk_res(binding.def()))
}
// Resolve a single identifier
......@@ -3114,7 +3114,7 @@ fn add_trait_info(found_traits: &mut Vec<TraitCandidate>,
let mut collected_traits = Vec::new();
module.for_each_child(|name, ns, binding| {
if ns != TypeNS { return }
if let Some(Def::Trait(_)) = binding.def() {
if let Def::Trait(_) = binding.def() {
collected_traits.push((name, binding));
}
});
......@@ -3122,7 +3122,7 @@ fn add_trait_info(found_traits: &mut Vec<TraitCandidate>,
}
for &(trait_name, binding) in traits.as_ref().unwrap().iter() {
let trait_def_id = binding.def().unwrap().def_id();
let trait_def_id = binding.def().def_id();
if this.trait_item_map.contains_key(&(name, trait_def_id)) {
let mut import_id = None;
if let NameBindingKind::Import { directive, .. } = binding.kind {
......@@ -3181,8 +3181,8 @@ fn lookup_candidates<FilterFn>(&mut self,
if name_binding.is_import() { return; }
// collect results based on the filter function
if let Some(def) = name_binding.def() {
if name == lookup_name && ns == namespace && filter_fn(def) {
if name == lookup_name && ns == namespace {
if filter_fn(name_binding.def()) {
// create the path
let ident = ast::Ident::with_empty_ctxt(name);
let params = PathParameters::none();
......@@ -3302,7 +3302,7 @@ fn report_privacy_errors(&self) {
let msg = format!("extern crate `{}` is private", name);
self.session.add_lint(lint::builtin::INACCESSIBLE_EXTERN_CRATE, node_id, span, msg);
} else {
let def = binding.def().unwrap();
let def = binding.def();
self.session.span_err(span, &format!("{} `{}` is private", def.kind_name(), name));
}
}
......
......@@ -639,9 +639,9 @@ fn finalize_import(&mut self, directive: &'b ImportDirective<'b>) -> ResolveResu
// Record what this import resolves to for later uses in documentation,
// this may resolve to either a value or a type, but for documentation
// purposes it's good enough to just favor one over the other.
let def = match type_result.ok().and_then(NameBinding::def) {
let def = match type_result.ok().map(NameBinding::def) {
Some(def) => def,
None => value_result.ok().and_then(NameBinding::def).unwrap(),
None => value_result.ok().map(NameBinding::def).unwrap(),
};
let path_resolution = PathResolution::new(def);
self.def_map.insert(directive.id, path_resolution);
......@@ -714,9 +714,7 @@ fn finalize_resolutions_in(&mut self, module: Module<'b>) {
if binding.vis == ty::Visibility::Public &&
(binding.is_import() || binding.is_extern_crate()) {
if let Some(def) = binding.def() {
reexports.push(Export { name: name, def_id: def.def_id() });
}
reexports.push(Export { name: name, def_id: binding.def().def_id() });
}
if let NameBindingKind::Import { binding: orig_binding, directive, .. } = binding.kind {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册