Skip to content

Commit 79ff90f

Browse files
committed
Fix @nikomatsakis' nits in typeck.
1 parent 8904842 commit 79ff90f

File tree

3 files changed

+27
-36
lines changed

3 files changed

+27
-36
lines changed

src/librustc_typeck/astconv.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ pub fn instantiate_trait_ref<'tcx>(
639639
-> Rc<ty::TraitRef<'tcx>>
640640
{
641641
let path = &trait_ref.path;
642-
match ::lookup_def_tcx(this.tcx(), path.span, trait_ref.ref_id) {
642+
match ::lookup_full_def(this.tcx(), path.span, trait_ref.ref_id) {
643643
def::DefTrait(trait_def_id) => {
644644
let trait_ref = ast_path_to_trait_ref(this,
645645
rscope,
@@ -916,7 +916,10 @@ fn ast_ty_to_trait_ref<'tcx>(this: &AstConv<'tcx>,
916916

917917
match ty.node {
918918
ast::TyPath(None, ref path) => {
919-
let def = this.tcx().def_map.borrow().get(&ty.id).map(|d| d.full_def());
919+
let def = match this.tcx().def_map.borrow().get(&ty.id) {
920+
Some(&def::PathResolution { base_def, depth: 0, .. }) => Some(base_def),
921+
_ => None
922+
};
920923
match def {
921924
Some(def::DefTrait(trait_def_id)) => {
922925
let mut projection_bounds = Vec::new();
@@ -1219,7 +1222,10 @@ pub fn finish_resolving_def_to_ty<'tcx>(this: &AstConv<'tcx>,
12191222
segments.last().unwrap())
12201223
}
12211224
def::DefMod(id) => {
1222-
// Used as sentinel by callers to indicate the `<T>::a::b::c` form.
1225+
// Used as sentinel by callers to indicate the `<T>::A::B::C` form.
1226+
// FIXME(#22519) This part of the resolution logic should be
1227+
// avoided entirely for that form, once we stop needed a Def
1228+
// for `associated_path_def_to_ty`.
12231229
if segments.is_empty() {
12241230
opt_self_ty.expect("missing T in <T>::a::b::c")
12251231
} else {
@@ -1907,7 +1913,7 @@ pub fn partition_bounds<'a>(tcx: &ty::ctxt,
19071913
for ast_bound in ast_bounds {
19081914
match *ast_bound {
19091915
ast::TraitTyParamBound(ref b, ast::TraitBoundModifier::None) => {
1910-
match ::lookup_def_tcx(tcx, b.trait_ref.path.span, b.trait_ref.ref_id) {
1916+
match ::lookup_full_def(tcx, b.trait_ref.path.span, b.trait_ref.ref_id) {
19111917
def::DefTrait(trait_did) => {
19121918
match trait_def_ids.get(&trait_did) {
19131919
// Already seen this trait. We forbid

src/librustc_typeck/check/mod.rs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ use middle::ty::{MethodCall, MethodCallee, MethodMap, ObjectCastMap};
103103
use middle::ty_fold::{TypeFolder, TypeFoldable};
104104
use rscope::RegionScope;
105105
use session::Session;
106-
use {CrateCtxt, lookup_def_ccx, require_same_types};
106+
use {CrateCtxt, lookup_full_def, require_same_types};
107107
use TypeAndSubsts;
108108
use lint;
109109
use util::common::{block_query, indenter, loop_query};
@@ -3397,7 +3397,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
33973397
ast::ExprPath(None, ref path) => {
33983398
// FIXME(pcwalton): For now we hardcode the two permissible
33993399
// places: the exchange heap and the managed heap.
3400-
let definition = lookup_def(fcx, path.span, place.id);
3400+
let definition = lookup_full_def(tcx, path.span, place.id);
34013401
let def_id = definition.def_id();
34023402
let referent_ty = fcx.expr_ty(&**subexpr);
34033403
if tcx.lang_items.exchange_heap() == Some(def_id) {
@@ -3880,14 +3880,14 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
38803880
}
38813881
ast::ExprStruct(ref path, ref fields, ref base_expr) => {
38823882
// Resolve the path.
3883-
let def = tcx.def_map.borrow().get(&id).map(|d| d.full_def());
3883+
let def = lookup_full_def(tcx, path.span, id);
38843884
let struct_id = match def {
3885-
Some(def::DefVariant(enum_id, variant_id, true)) => {
3885+
def::DefVariant(enum_id, variant_id, true) => {
38863886
check_struct_enum_variant(fcx, id, expr.span, enum_id,
38873887
variant_id, &fields[..]);
38883888
enum_id
38893889
}
3890-
Some(def::DefTrait(def_id)) => {
3890+
def::DefTrait(def_id) => {
38913891
span_err!(tcx.sess, path.span, E0159,
38923892
"use of trait `{}` as a struct constructor",
38933893
pprust::path_to_string(path));
@@ -3897,7 +3897,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
38973897
base_expr);
38983898
def_id
38993899
},
3900-
Some(def) => {
3900+
def => {
39013901
// Verify that this was actually a struct.
39023902
let typ = ty::lookup_item_type(fcx.ccx.tcx, def.def_id());
39033903
match typ.ty.sty {
@@ -3922,10 +3922,6 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
39223922

39233923
def.def_id()
39243924
}
3925-
_ => {
3926-
tcx.sess.span_bug(path.span,
3927-
"structure constructor wasn't resolved")
3928-
}
39293925
};
39303926

39313927
// Turn the path into a type and verify that that type unifies with
@@ -4639,10 +4635,6 @@ pub fn check_enum_variants<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
46394635
check_instantiable(ccx.tcx, sp, id);
46404636
}
46414637

4642-
pub fn lookup_def(fcx: &FnCtxt, sp: Span, id: ast::NodeId) -> def::Def {
4643-
lookup_def_ccx(fcx.ccx, sp, id)
4644-
}
4645-
46464638
// Returns the type parameter count and the type for the given definition.
46474639
fn type_scheme_and_predicates_for_def<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
46484640
sp: Span,
@@ -5167,18 +5159,15 @@ pub fn may_break(cx: &ty::ctxt, id: ast::NodeId, b: &ast::Block) -> bool {
51675159
_ => false
51685160
}
51695161
})) ||
5170-
// Second: is there a labeled break with label
5171-
// <id> nested anywhere inside the loop?
5162+
// Second: is there a labeled break with label
5163+
// <id> nested anywhere inside the loop?
51725164
(block_query(b, |e| {
5173-
match e.node {
5174-
ast::ExprBreak(Some(_)) => {
5175-
match cx.def_map.borrow().get(&e.id).map(|d| d.full_def()) {
5176-
Some(def::DefLabel(loop_id)) if id == loop_id => true,
5177-
_ => false,
5178-
}
5179-
}
5180-
_ => false
5181-
}}))
5165+
if let ast::ExprBreak(Some(_)) = e.node {
5166+
lookup_full_def(cx, e.span, e.id) == def::DefLabel(id)
5167+
} else {
5168+
false
5169+
}
5170+
}))
51825171
}
51835172

51845173
pub fn check_bounds_are_used<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,

src/librustc_typeck/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,16 @@ fn write_substs_to_tcx<'tcx>(tcx: &ty::ctxt<'tcx>,
163163
tcx.item_substs.borrow_mut().insert(node_id, item_substs);
164164
}
165165
}
166-
fn lookup_def_tcx(tcx:&ty::ctxt, sp: Span, id: ast::NodeId) -> def::Def {
166+
167+
fn lookup_full_def(tcx: &ty::ctxt, sp: Span, id: ast::NodeId) -> def::Def {
167168
match tcx.def_map.borrow().get(&id) {
168169
Some(x) => x.full_def(),
169-
_ => {
170+
None => {
170171
span_fatal!(tcx.sess, sp, E0242, "internal error looking up a definition")
171172
}
172173
}
173174
}
174175

175-
fn lookup_def_ccx(ccx: &CrateCtxt, sp: Span, id: ast::NodeId)
176-
-> def::Def {
177-
lookup_def_tcx(ccx.tcx, sp, id)
178-
}
179-
180176
fn require_same_types<'a, 'tcx, M>(tcx: &ty::ctxt<'tcx>,
181177
maybe_infcx: Option<&infer::InferCtxt<'a, 'tcx>>,
182178
t1_is_expected: bool,

0 commit comments

Comments
 (0)