Skip to content

Commit 87ae68c

Browse files
committed
Refactor binding.def() to return a Def instead of an Option<Def>.
1 parent 691d10c commit 87ae68c

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

src/librustc_resolve/lib.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ fn resolve_struct_error<'b, 'a: 'b, 'c>(resolver: &'b Resolver<'a>,
459459
err
460460
}
461461
ResolutionError::BindingShadowsSomethingUnacceptable(what_binding, name, binding) => {
462-
let shadows_what = PathResolution::new(binding.def().unwrap()).kind_name();
462+
let shadows_what = PathResolution::new(binding.def()).kind_name();
463463
let mut err = struct_span_err!(resolver.session,
464464
span,
465465
E0530,
@@ -739,7 +739,7 @@ impl<'a> LexicalScopeBinding<'a> {
739739
fn local_def(self) -> LocalDef {
740740
match self {
741741
LexicalScopeBinding::LocalDef(local_def) => local_def,
742-
LexicalScopeBinding::Item(binding) => LocalDef::from_def(binding.def().unwrap()),
742+
LexicalScopeBinding::Item(binding) => LocalDef::from_def(binding.def()),
743743
}
744744
}
745745

@@ -877,10 +877,10 @@ impl<'a> NameBinding<'a> {
877877
}
878878
}
879879

880-
fn def(&self) -> Option<Def> {
880+
fn def(&self) -> Def {
881881
match self.kind {
882-
NameBindingKind::Def(def) => Some(def),
883-
NameBindingKind::Module(module) => module.def,
882+
NameBindingKind::Def(def) => def,
883+
NameBindingKind::Module(module) => module.def.unwrap(),
884884
NameBindingKind::Import { binding, .. } => binding.def(),
885885
}
886886
}
@@ -916,7 +916,7 @@ impl<'a> NameBinding<'a> {
916916
}
917917

918918
fn is_importable(&self) -> bool {
919-
match self.def().unwrap() {
919+
match self.def() {
920920
Def::AssociatedConst(..) | Def::Method(..) | Def::AssociatedTy(..) => false,
921921
_ => true,
922922
}
@@ -1097,7 +1097,7 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> {
10971097
fn resolve_generated_global_path(&mut self, path: &hir::Path, is_value: bool) -> Def {
10981098
let namespace = if is_value { ValueNS } else { TypeNS };
10991099
match self.resolve_crate_relative_path(path.span, &path.segments, namespace) {
1100-
Ok(binding) => binding.def().unwrap(),
1100+
Ok(binding) => binding.def(),
11011101
Err(true) => Def::Err,
11021102
Err(false) => {
11031103
let path_name = &format!("{}", path);
@@ -1693,7 +1693,7 @@ impl<'a> Resolver<'a> {
16931693
&prefix.segments,
16941694
TypeNS) {
16951695
Ok(binding) => {
1696-
let def = binding.def().unwrap();
1696+
let def = binding.def();
16971697
self.record_def(item.id, PathResolution::new(def));
16981698
}
16991699
Err(true) => self.record_def(item.id, err_path_resolution()),
@@ -2309,7 +2309,7 @@ impl<'a> Resolver<'a> {
23092309
// entity, then fall back to a fresh binding.
23102310
let binding = self.resolve_ident_in_lexical_scope(ident.node, ValueNS, None)
23112311
.and_then(LexicalScopeBinding::item);
2312-
let resolution = binding.and_then(NameBinding::def).and_then(|def| {
2312+
let resolution = binding.map(NameBinding::def).and_then(|def| {
23132313
let always_binding = !pat_src.is_refutable() || opt_pat.is_some() ||
23142314
bmode != BindingMode::ByValue(Mutability::Immutable);
23152315
match def {
@@ -2443,7 +2443,7 @@ impl<'a> Resolver<'a> {
24432443

24442444
if path.global {
24452445
let binding = self.resolve_crate_relative_path(span, segments, namespace);
2446-
return binding.map(|binding| mk_res(binding.def().unwrap()));
2446+
return binding.map(|binding| mk_res(binding.def()));
24472447
}
24482448

24492449
// Try to find a path to an item in a module.
@@ -2481,7 +2481,7 @@ impl<'a> Resolver<'a> {
24812481
let unqualified_def = resolve_identifier_with_fallback(self, None);
24822482
let qualified_binding = self.resolve_module_relative_path(span, segments, namespace);
24832483
match (qualified_binding, unqualified_def) {
2484-
(Ok(binding), Some(ref ud)) if binding.def().unwrap() == ud.def => {
2484+
(Ok(binding), Some(ref ud)) if binding.def() == ud.def => {
24852485
self.session
24862486
.add_lint(lint::builtin::UNUSED_QUALIFICATIONS,
24872487
id,
@@ -2491,7 +2491,7 @@ impl<'a> Resolver<'a> {
24912491
_ => {}
24922492
}
24932493

2494-
qualified_binding.map(|binding| mk_res(binding.def().unwrap()))
2494+
qualified_binding.map(|binding| mk_res(binding.def()))
24952495
}
24962496

24972497
// Resolve a single identifier
@@ -3114,15 +3114,15 @@ impl<'a> Resolver<'a> {
31143114
let mut collected_traits = Vec::new();
31153115
module.for_each_child(|name, ns, binding| {
31163116
if ns != TypeNS { return }
3117-
if let Some(Def::Trait(_)) = binding.def() {
3117+
if let Def::Trait(_) = binding.def() {
31183118
collected_traits.push((name, binding));
31193119
}
31203120
});
31213121
*traits = Some(collected_traits.into_boxed_slice());
31223122
}
31233123

31243124
for &(trait_name, binding) in traits.as_ref().unwrap().iter() {
3125-
let trait_def_id = binding.def().unwrap().def_id();
3125+
let trait_def_id = binding.def().def_id();
31263126
if this.trait_item_map.contains_key(&(name, trait_def_id)) {
31273127
let mut import_id = None;
31283128
if let NameBindingKind::Import { directive, .. } = binding.kind {
@@ -3181,8 +3181,8 @@ impl<'a> Resolver<'a> {
31813181
if name_binding.is_import() { return; }
31823182

31833183
// collect results based on the filter function
3184-
if let Some(def) = name_binding.def() {
3185-
if name == lookup_name && ns == namespace && filter_fn(def) {
3184+
if name == lookup_name && ns == namespace {
3185+
if filter_fn(name_binding.def()) {
31863186
// create the path
31873187
let ident = ast::Ident::with_empty_ctxt(name);
31883188
let params = PathParameters::none();
@@ -3302,7 +3302,7 @@ impl<'a> Resolver<'a> {
33023302
let msg = format!("extern crate `{}` is private", name);
33033303
self.session.add_lint(lint::builtin::INACCESSIBLE_EXTERN_CRATE, node_id, span, msg);
33043304
} else {
3305-
let def = binding.def().unwrap();
3305+
let def = binding.def();
33063306
self.session.span_err(span, &format!("{} `{}` is private", def.kind_name(), name));
33073307
}
33083308
}

src/librustc_resolve/resolve_imports.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -639,9 +639,9 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
639639
// Record what this import resolves to for later uses in documentation,
640640
// this may resolve to either a value or a type, but for documentation
641641
// purposes it's good enough to just favor one over the other.
642-
let def = match type_result.ok().and_then(NameBinding::def) {
642+
let def = match type_result.ok().map(NameBinding::def) {
643643
Some(def) => def,
644-
None => value_result.ok().and_then(NameBinding::def).unwrap(),
644+
None => value_result.ok().map(NameBinding::def).unwrap(),
645645
};
646646
let path_resolution = PathResolution::new(def);
647647
self.def_map.insert(directive.id, path_resolution);
@@ -714,9 +714,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
714714

715715
if binding.vis == ty::Visibility::Public &&
716716
(binding.is_import() || binding.is_extern_crate()) {
717-
if let Some(def) = binding.def() {
718-
reexports.push(Export { name: name, def_id: def.def_id() });
719-
}
717+
reexports.push(Export { name: name, def_id: binding.def().def_id() });
720718
}
721719

722720
if let NameBindingKind::Import { binding: orig_binding, directive, .. } = binding.kind {

0 commit comments

Comments
 (0)