Skip to content

Commit 923616e

Browse files
committed
Fix @nikomatsakis' nits in typeck.
1 parent 09ad993 commit 923616e

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
@@ -622,7 +622,7 @@ pub fn instantiate_trait_ref<'tcx>(
622622
-> Rc<ty::TraitRef<'tcx>>
623623
{
624624
let path = &trait_ref.path;
625-
match ::lookup_def_tcx(this.tcx(), path.span, trait_ref.ref_id) {
625+
match ::lookup_full_def(this.tcx(), path.span, trait_ref.ref_id) {
626626
def::DefTrait(trait_def_id) => {
627627
let trait_ref = ast_path_to_trait_ref(this,
628628
rscope,
@@ -899,7 +899,10 @@ fn ast_ty_to_trait_ref<'tcx>(this: &AstConv<'tcx>,
899899

900900
match ty.node {
901901
ast::TyPath(None, ref path) => {
902-
let def = this.tcx().def_map.borrow().get(&ty.id).map(|d| d.full_def());
902+
let def = match this.tcx().def_map.borrow().get(&ty.id) {
903+
Some(&def::PathResolution { base_def, depth: 0, .. }) => Some(base_def),
904+
_ => None
905+
};
903906
match def {
904907
Some(def::DefTrait(trait_def_id)) => {
905908
let mut projection_bounds = Vec::new();
@@ -1202,7 +1205,10 @@ pub fn finish_resolving_def_to_ty<'tcx>(this: &AstConv<'tcx>,
12021205
segments.last().unwrap())
12031206
}
12041207
def::DefMod(id) => {
1205-
// Used as sentinel by callers to indicate the `<T>::a::b::c` form.
1208+
// Used as sentinel by callers to indicate the `<T>::A::B::C` form.
1209+
// FIXME(#22519) This part of the resolution logic should be
1210+
// avoided entirely for that form, once we stop needed a Def
1211+
// for `associated_path_def_to_ty`.
12061212
if segments.is_empty() {
12071213
opt_self_ty.expect("missing T in <T>::a::b::c")
12081214
} else {
@@ -1890,7 +1896,7 @@ pub fn partition_bounds<'a>(tcx: &ty::ctxt,
18901896
for ast_bound in ast_bounds {
18911897
match *ast_bound {
18921898
ast::TraitTyParamBound(ref b, ast::TraitBoundModifier::None) => {
1893-
match ::lookup_def_tcx(tcx, b.trait_ref.path.span, b.trait_ref.ref_id) {
1899+
match ::lookup_full_def(tcx, b.trait_ref.path.span, b.trait_ref.ref_id) {
18941900
def::DefTrait(trait_did) => {
18951901
match trait_def_ids.get(&trait_did) {
18961902
// 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};
@@ -3401,7 +3401,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
34013401
ast::ExprPath(None, ref path) => {
34023402
// FIXME(pcwalton): For now we hardcode the two permissible
34033403
// places: the exchange heap and the managed heap.
3404-
let definition = lookup_def(fcx, path.span, place.id);
3404+
let definition = lookup_full_def(tcx, path.span, place.id);
34053405
let def_id = definition.def_id();
34063406
let referent_ty = fcx.expr_ty(&**subexpr);
34073407
if tcx.lang_items.exchange_heap() == Some(def_id) {
@@ -3884,14 +3884,14 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
38843884
}
38853885
ast::ExprStruct(ref path, ref fields, ref base_expr) => {
38863886
// Resolve the path.
3887-
let def = tcx.def_map.borrow().get(&id).map(|d| d.full_def());
3887+
let def = lookup_full_def(tcx, path.span, id);
38883888
let struct_id = match def {
3889-
Some(def::DefVariant(enum_id, variant_id, true)) => {
3889+
def::DefVariant(enum_id, variant_id, true) => {
38903890
check_struct_enum_variant(fcx, id, expr.span, enum_id,
38913891
variant_id, &fields[..]);
38923892
enum_id
38933893
}
3894-
Some(def::DefTrait(def_id)) => {
3894+
def::DefTrait(def_id) => {
38953895
span_err!(tcx.sess, path.span, E0159,
38963896
"use of trait `{}` as a struct constructor",
38973897
pprust::path_to_string(path));
@@ -3901,7 +3901,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
39013901
base_expr);
39023902
def_id
39033903
},
3904-
Some(def) => {
3904+
def => {
39053905
// Verify that this was actually a struct.
39063906
let typ = ty::lookup_item_type(fcx.ccx.tcx, def.def_id());
39073907
match typ.ty.sty {
@@ -3926,10 +3926,6 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
39263926

39273927
def.def_id()
39283928
}
3929-
_ => {
3930-
tcx.sess.span_bug(path.span,
3931-
"structure constructor wasn't resolved")
3932-
}
39333929
};
39343930

39353931
// Turn the path into a type and verify that that type unifies with
@@ -4643,10 +4639,6 @@ pub fn check_enum_variants<'a,'tcx>(ccx: &CrateCtxt<'a,'tcx>,
46434639
check_instantiable(ccx.tcx, sp, id);
46444640
}
46454641

4646-
pub fn lookup_def(fcx: &FnCtxt, sp: Span, id: ast::NodeId) -> def::Def {
4647-
lookup_def_ccx(fcx.ccx, sp, id)
4648-
}
4649-
46504642
// Returns the type parameter count and the type for the given definition.
46514643
fn type_scheme_and_predicates_for_def<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
46524644
sp: Span,
@@ -5171,18 +5163,15 @@ pub fn may_break(cx: &ty::ctxt, id: ast::NodeId, b: &ast::Block) -> bool {
51715163
_ => false
51725164
}
51735165
})) ||
5174-
// Second: is there a labeled break with label
5175-
// <id> nested anywhere inside the loop?
5166+
// Second: is there a labeled break with label
5167+
// <id> nested anywhere inside the loop?
51765168
(block_query(b, |e| {
5177-
match e.node {
5178-
ast::ExprBreak(Some(_)) => {
5179-
match cx.def_map.borrow().get(&e.id).map(|d| d.full_def()) {
5180-
Some(def::DefLabel(loop_id)) if id == loop_id => true,
5181-
_ => false,
5182-
}
5183-
}
5184-
_ => false
5185-
}}))
5169+
if let ast::ExprBreak(Some(_)) = e.node {
5170+
lookup_full_def(cx, e.span, e.id) == def::DefLabel(id)
5171+
} else {
5172+
false
5173+
}
5174+
}))
51865175
}
51875176

51885177
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)