Skip to content

Commit 329d892

Browse files
stop assuming which namespace a thing has if we can assume there's just one
1 parent e60aa8b commit 329d892

File tree

20 files changed

+52
-43
lines changed

20 files changed

+52
-43
lines changed

src/librustc/hir/def.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use hir;
1717
use ty;
1818

1919
use std::iter::{once, Once, Chain};
20+
use std::fmt::Debug;
2021

2122
use self::Namespace::*;
2223

@@ -160,6 +161,14 @@ impl<T> PerNS<Option<T>> {
160161
pub fn is_empty(&self) -> bool {
161162
self.value_ns.is_none() && self.type_ns.is_none() && self.macro_ns.is_none()
162163
}
164+
165+
pub fn assert_single_ns(self) -> Option<T> where T: Debug {
166+
let mut res = self.into_iter().filter_map(|it| it).collect::<Vec<_>>();
167+
if res.len() > 1 {
168+
bug!("single item was requested but multiple were available: {:?}", res);
169+
}
170+
res.pop()
171+
}
163172
}
164173

165174
impl PerNS<Def> {

src/librustc/hir/lowering.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ impl<'a> LoweringContext<'a> {
921921
match destination {
922922
Some((id, label)) => {
923923
let target_id =
924-
if let Def::Label(loop_id) = self.expect_full_defs(id).value_ns {
924+
if let Def::Label(loop_id) = self.expect_full_defs(id).assert_single_ns() {
925925
Ok(self.lower_node_id(loop_id).node_id)
926926
} else {
927927
Err(hir::LoopIdError::UnresolvedLabel)
@@ -1940,7 +1940,7 @@ impl<'a> LoweringContext<'a> {
19401940
{
19411941
if let Some(Def::TyParam(def_id)) = self.resolver
19421942
.get_resolutions(bound_pred.bounded_ty.id)
1943-
.type_ns
1943+
.assert_single_ns()
19441944
.map(|d| d.base_def())
19451945
{
19461946
if let Some(node_id) =
@@ -2301,7 +2301,7 @@ impl<'a> LoweringContext<'a> {
23012301
});
23022302

23032303
if let Some(ref trait_ref) = trait_ref {
2304-
if let Def::Trait(def_id) = trait_ref.path.defs.type_ns {
2304+
if let Def::Trait(def_id) = trait_ref.path.defs.assert_single_ns() {
23052305
this.trait_impls.entry(def_id).or_insert(vec![]).push(id);
23062306
}
23072307
}
@@ -2830,7 +2830,7 @@ impl<'a> LoweringContext<'a> {
28302830
// `None` can occur in body-less function signatures
28312831
defs @ PerNS { value_ns: None, .. } |
28322832
defs @ PerNS { value_ns: Some(Def::Local(_)), .. } => {
2833-
let canonical_id = match defs.value_ns {
2833+
let canonical_id = match defs.assert_single_ns() {
28342834
Some(Def::Local(id)) => id,
28352835
_ => p.id,
28362836
};
@@ -4026,7 +4026,7 @@ impl<'a> LoweringContext<'a> {
40264026
let node = match qpath {
40274027
hir::QPath::Resolved(None, path) => {
40284028
// Turn trait object paths into `TyTraitObject` instead.
4029-
if let Def::Trait(_) = path.defs.type_ns {
4029+
if let Def::Trait(_) = path.defs.assert_single_ns() {
40304030
let principal = hir::PolyTraitRef {
40314031
bound_generic_params: hir::HirVec::new(),
40324032
trait_ref: hir::TraitRef {

src/librustc/hir/pat_util.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ impl hir::Pat {
6565
PatKind::Path(hir::QPath::Resolved(_, ref path)) |
6666
PatKind::TupleStruct(hir::QPath::Resolved(_, ref path), ..) |
6767
PatKind::Struct(hir::QPath::Resolved(_, ref path), ..) => {
68-
if let Def::Variant(..) = path.defs.type_ns { true }
69-
else if let Def::VariantCtor(..) = path.defs.value_ns { true }
68+
if let Def::Variant(..) = path.defs.assert_single_ns() { true }
69+
else if let Def::VariantCtor(..) = path.defs.assert_single_ns() { true }
7070
else { false }
7171
}
7272
PatKind::Slice(..) => true,
@@ -78,7 +78,7 @@ impl hir::Pat {
7878
match self.node {
7979
PatKind::Path(hir::QPath::TypeRelative(..)) => true,
8080
PatKind::Path(hir::QPath::Resolved(_, ref path)) => {
81-
match path.defs.value_ns {
81+
match path.defs.assert_single_ns() {
8282
Def::Const(..) | Def::AssociatedConst(..) => true,
8383
_ => false
8484
}
@@ -157,9 +157,9 @@ impl hir::Pat {
157157
PatKind::Path(hir::QPath::Resolved(_, ref path)) |
158158
PatKind::TupleStruct(hir::QPath::Resolved(_, ref path), ..) |
159159
PatKind::Struct(hir::QPath::Resolved(_, ref path), ..) => {
160-
if let Def::Variant(id) = path.defs.type_ns {
160+
if let Def::Variant(id) = path.defs.assert_single_ns() {
161161
variants.push(id);
162-
} else if let Def::VariantCtor(id, ..) = path.defs.value_ns {
162+
} else if let Def::VariantCtor(id, ..) = path.defs.assert_single_ns() {
163163
variants.push(id);
164164
}
165165
}

src/librustc/middle/liveness.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ fn visit_expr<'a, 'tcx>(ir: &mut IrMaps<'a, 'tcx>, expr: &'tcx Expr) {
458458
// live nodes required for uses or definitions of variables:
459459
hir::ExprPath(hir::QPath::Resolved(_, ref path)) => {
460460
debug!("expr {}: path that leads to {:?}", expr.id, path.defs);
461-
if let Def::Local(..) = path.defs.value_ns {
461+
if let Def::Local(..) = path.defs.assert_single_ns() {
462462
ir.add_live_node_for_node(expr.hir_id, ExprNode(expr.span));
463463
}
464464
intravisit::walk_expr(ir, expr);
@@ -1268,7 +1268,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
12681268

12691269
fn access_path(&mut self, hir_id: HirId, path: &hir::Path, succ: LiveNode, acc: u32)
12701270
-> LiveNode {
1271-
match path.defs.value_ns {
1271+
match path.defs.assert_single_ns() {
12721272
Def::Local(nid) => {
12731273
self.access_var(hir_id, nid, succ, acc, path.span)
12741274
}
@@ -1445,7 +1445,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
14451445
fn check_place(&mut self, expr: &'tcx Expr) {
14461446
match expr.node {
14471447
hir::ExprPath(hir::QPath::Resolved(_, ref path)) => {
1448-
if let Def::Local(nid) = path.defs.value_ns {
1448+
if let Def::Local(nid) = path.defs.assert_single_ns() {
14491449
// Assignment to an immutable variable or argument: only legal
14501450
// if there is no later assignment. If this local is actually
14511451
// mutable, then check for a reassignment to flag the mutability

src/librustc/middle/reachable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ impl<'a, 'tcx: 'a> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a,
348348
self.worklist.push(impl_item_ref.id.node_id);
349349
}
350350

351-
let trait_def_id = match trait_ref.path.defs.type_ns {
351+
let trait_def_id = match trait_ref.path.defs.assert_single_ns() {
352352
Def::Trait(def_id) => def_id,
353353
_ => unreachable!()
354354
};

src/librustc/middle/resolve_lifetime.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,7 @@ fn object_lifetime_defaults_for_item(
12301230
}
12311231

12321232
let def = match data.bounded_ty.node {
1233-
hir::TyPath(hir::QPath::Resolved(None, ref path)) => path.defs.type_ns,
1233+
hir::TyPath(hir::QPath::Resolved(None, ref path)) => path.defs.assert_single_ns(),
12341234
_ => continue,
12351235
};
12361236

@@ -1821,12 +1821,12 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
18211821
// to the way elision rules were originally specified.
18221822
let impl_self = impl_self.map(|ty| &ty.node);
18231823
if let Some(&hir::TyPath(hir::QPath::Resolved(None, ref path))) = impl_self {
1824-
match path.defs.type_ns {
1824+
match path.defs.assert_single_ns() {
18251825
// Whitelist the types that unambiguously always
18261826
// result in the same type constructor being used
18271827
// (it can't differ between `Self` and `self`).
18281828
Def::Struct(_) | Def::Union(_) | Def::Enum(_) | Def::PrimTy(_) => {
1829-
return def == path.defs.type_ns
1829+
return def == path.defs.assert_single_ns()
18301830
}
18311831
_ => {}
18321832
}
@@ -1837,7 +1837,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
18371837

18381838
if let hir::TyRptr(lifetime_ref, ref mt) = inputs[0].node {
18391839
if let hir::TyPath(hir::QPath::Resolved(None, ref path)) = mt.ty.node {
1840-
if is_self_ty(path.defs.type_ns) {
1840+
if is_self_ty(path.defs.assert_single_ns()) {
18411841
if let Some(&lifetime) = self.map.defs.get(&lifetime_ref.id) {
18421842
let scope = Scope::Elision {
18431843
elide: Elide::Exact(lifetime),

src/librustc/middle/stability.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
738738
// individually as it's possible to have a stable trait with unstable
739739
// items.
740740
hir::ItemImpl(.., Some(ref t), _, ref impl_item_refs) => {
741-
if let Def::Trait(trait_did) = t.path.defs.type_ns {
741+
if let Def::Trait(trait_did) = t.path.defs.assert_single_ns() {
742742
for impl_item_ref in impl_item_refs {
743743
let impl_item = self.tcx.hir.impl_item(impl_item_ref.id);
744744
let trait_item_def_id = self.tcx.associated_items(trait_did)

src/librustc/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl Visibility {
271271
match *visibility {
272272
hir::Public => Visibility::Public,
273273
hir::Visibility::Crate(_) => Visibility::Restricted(DefId::local(CRATE_DEF_INDEX)),
274-
hir::Visibility::Restricted { ref path, .. } => match path.defs.type_ns {
274+
hir::Visibility::Restricted { ref path, .. } => match path.defs.assert_single_ns() {
275275
// If there is no resolution, `resolve` will have already reported an error, so
276276
// assume that the visibility is public to avoid reporting more privacy errors.
277277
Def::Err => Visibility::Public,

src/librustc_lint/bad_style.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonUpperCaseGlobals {
401401
fn check_pat(&mut self, cx: &LateContext, p: &hir::Pat) {
402402
// Lint for constants that look like binding identifiers (#7526)
403403
if let PatKind::Path(hir::QPath::Resolved(None, ref path)) = p.node {
404-
if let Def::Const(..) = path.defs.value_ns {
404+
if let Def::Const(..) = path.defs.assert_single_ns() {
405405
if path.segments.len() == 1 {
406406
NonUpperCaseGlobals::check_upper_case(cx,
407407
"constant in pattern",

src/librustc_lint/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
413413
hir::ItemImpl(.., Some(ref trait_ref), _, ref impl_item_refs) => {
414414
// If the trait is private, add the impl items to private_traits so they don't get
415415
// reported for missing docs.
416-
let real_trait = trait_ref.path.defs.type_ns.def_id();
416+
let real_trait = trait_ref.path.defs.assert_single_ns().def_id();
417417
if let Some(node_id) = cx.tcx.hir.as_local_node_id(real_trait) {
418418
match cx.tcx.hir.find(node_id) {
419419
Some(hir_map::NodeItem(item)) => {
@@ -1391,7 +1391,7 @@ impl TypeAliasBounds {
13911391
// If this is a type variable, we found a `T::Assoc`.
13921392
match ty.node {
13931393
hir::TyPath(hir::QPath::Resolved(None, ref path)) => {
1394-
match path.defs.type_ns {
1394+
match path.defs.assert_single_ns() {
13951395
Def::TyParam(_) => true,
13961396
_ => false
13971397
}

src/librustc_mir/hair/cx/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
241241
let adt_data = if let hir::ExprPath(hir::QPath::Resolved(_, ref path)) = fun.node {
242242
// Tuple-like ADTs are represented as ExprCall. We convert them here.
243243
expr_ty.ty_adt_def().and_then(|adt_def| {
244-
match path.defs.value_ns {
244+
match path.defs.assert_single_ns() {
245245
Def::VariantCtor(variant_id, CtorKind::Fn) => {
246246
Some((adt_def, adt_def.variant_index_with_id(variant_id)))
247247
}
@@ -436,7 +436,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
436436
}
437437
AdtKind::Enum => {
438438
let def = match *qpath {
439-
hir::QPath::Resolved(_, ref path) => path.defs.type_ns,
439+
hir::QPath::Resolved(_, ref path) => path.defs.assert_single_ns(),
440440
hir::QPath::TypeRelative(..) => Def::Err,
441441
};
442442
match def {

src/librustc_privacy/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
11341134
let not_private_trait =
11351135
trait_ref.as_ref().map_or(true, // no trait counts as public trait
11361136
|tr| {
1137-
let did = tr.path.defs.type_ns.def_id();
1137+
let did = tr.path.defs.assert_single_ns().def_id();
11381138

11391139
if let Some(node_id) = self.tcx.hir.as_local_node_id(did) {
11401140
self.trait_is_public(node_id)

src/librustc_resolve/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,7 +2420,7 @@ impl<'a> Resolver<'a> {
24202420
pat.walk(&mut |pat| {
24212421
if let PatKind::Ident(binding_mode, ident, ref sub_pat) = pat.node {
24222422
if sub_pat.is_some() || match self.get_resolutions(pat.id)
2423-
.value_ns
2423+
.assert_single_ns()
24242424
.map(|res| res.base_def()) {
24252425
Some(Def::Local(..)) => true,
24262426
_ => false,
@@ -3591,7 +3591,7 @@ impl<'a> Resolver<'a> {
35913591
if filter_fn(Def::Local(ast::DUMMY_NODE_ID)) {
35923592
if let Some(node_id) = self.current_self_type.as_ref().and_then(extract_node_id) {
35933593
// Look for a field with the same name in the current self_type.
3594-
if let Some(resolution) = self.get_resolutions(node_id).type_ns {
3594+
if let Some(resolution) = self.get_resolutions(node_id).assert_single_ns() {
35953595
match resolution.base_def() {
35963596
Def::Struct(did) | Def::Union(did)
35973597
if resolution.unresolved_segments() == 0 => {

src/librustc_typeck/astconv.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
367367
/// Get the DefId of the given trait ref. It _must_ actually be a trait.
368368
fn trait_def_id(&self, trait_ref: &hir::TraitRef) -> DefId {
369369
let path = &trait_ref.path;
370-
match path.defs.type_ns {
370+
match path.defs.assert_single_ns() {
371371
Def::Trait(trait_def_id) => trait_def_id,
372372
Def::TraitAlias(alias_def_id) => alias_def_id,
373373
Def::Err => {
@@ -1008,7 +1008,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
10081008
path.defs, opt_self_ty, path.segments);
10091009

10101010
let span = path.span;
1011-
match path.defs.type_ns {
1011+
match path.defs.assert_single_ns() {
10121012
Def::Enum(did) | Def::TyAlias(did) | Def::Struct(did) |
10131013
Def::Union(did) | Def::TyForeign(did) => {
10141014
assert_eq!(opt_self_ty, None);
@@ -1130,7 +1130,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
11301130
let ty = self.ast_ty_to_ty(qself);
11311131

11321132
let def = if let hir::TyPath(hir::QPath::Resolved(_, ref path)) = qself.node {
1133-
path.defs.type_ns
1133+
path.defs.assert_single_ns()
11341134
} else {
11351135
Def::Err
11361136
};
@@ -1327,7 +1327,7 @@ fn split_auto_traits<'a, 'b, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
13271327
{
13281328
let (auto_traits, trait_bounds): (Vec<_>, _) = trait_bounds.iter().partition(|bound| {
13291329
// Checks whether `trait_did` is an auto trait and adds it to `auto_traits` if so.
1330-
match bound.trait_ref.path.defs.type_ns {
1330+
match bound.trait_ref.path.defs.assert_single_ns() {
13311331
Def::Trait(trait_did) if tcx.trait_is_auto(trait_did) => {
13321332
true
13331333
}
@@ -1336,7 +1336,7 @@ fn split_auto_traits<'a, 'b, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
13361336
});
13371337

13381338
let auto_traits = auto_traits.into_iter().map(|tr| {
1339-
if let Def::Trait(trait_did) = tr.trait_ref.path.defs.type_ns {
1339+
if let Def::Trait(trait_did) = tr.trait_ref.path.defs.assert_single_ns() {
13401340
trait_did
13411341
} else {
13421342
unreachable!()

src/librustc_typeck/check/compare_method.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ fn compare_synthetic_generics<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
822822
hir::intravisit::walk_ty(self, ty);
823823
match ty.node {
824824
hir::TyPath(hir::QPath::Resolved(None, ref path)) => {
825-
if let hir::def::Def::TyParam(def_id) = path.defs.type_ns {
825+
if let hir::def::Def::TyParam(def_id) = path.defs.assert_single_ns() {
826826
if def_id == self.1 {
827827
self.0 = Some(ty.span);
828828
}

src/librustc_typeck/check/demand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
212212
/// ```
213213
fn can_use_as_ref(&self, expr: &hir::Expr) -> Option<(Span, &'static str, String)> {
214214
if let hir::ExprPath(hir::QPath::Resolved(_, ref path)) = expr.node {
215-
if let hir::def::Def::Local(id) = path.defs.value_ns {
215+
if let hir::def::Def::Local(id) = path.defs.assert_single_ns() {
216216
let parent = self.tcx.hir.get_parent_node(id);
217217
if let Some(NodeExpr(hir::Expr {
218218
id,

src/librustc_typeck/check/method/suggest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
260260
}
261261
hir::ExprPath(ref qpath) => { // local binding
262262
if let &hir::QPath::Resolved(_, ref path) = &qpath {
263-
if let hir::def::Def::Local(node_id) = path.defs.value_ns {
263+
if let hir::def::Def::Local(node_id) = path.defs.assert_single_ns() {
264264
let span = tcx.hir.span(node_id);
265265
let snippet = tcx.sess.codemap().span_to_snippet(span)
266266
.unwrap();

0 commit comments

Comments
 (0)