Skip to content

Commit a34b0a4

Browse files
committed
rustc: replace def::MethodProvenance with ty::ImplOrTraitItemContainer.
1 parent 1fe32ca commit a34b0a4

File tree

17 files changed

+96
-113
lines changed

17 files changed

+96
-113
lines changed

src/librustc/metadata/decoder.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -299,15 +299,7 @@ fn item_to_def_like(cdata: Cmd, item: rbml::Doc, did: ast::DefId) -> DefLike {
299299
Constant => {
300300
// Check whether we have an associated const item.
301301
if item_sort(item) == Some('C') {
302-
// Check whether the associated const is from a trait or impl.
303-
// See the comment for methods below.
304-
let provenance = if reader::maybe_get_doc(
305-
item, tag_item_trait_parent_sort).is_some() {
306-
def::FromTrait(item_require_parent_item(cdata, item))
307-
} else {
308-
def::FromImpl(item_require_parent_item(cdata, item))
309-
};
310-
DlDef(def::DefAssociatedConst(did, provenance))
302+
DlDef(def::DefAssociatedConst(did))
311303
} else {
312304
// Regular const item.
313305
DlDef(def::DefConst(did))
@@ -319,18 +311,7 @@ fn item_to_def_like(cdata: Cmd, item: rbml::Doc, did: ast::DefId) -> DefLike {
319311
Fn => DlDef(def::DefFn(did, false)),
320312
CtorFn => DlDef(def::DefFn(did, true)),
321313
Method | StaticMethod => {
322-
// def_static_method carries an optional field of its enclosing
323-
// trait or enclosing impl (if this is an inherent static method).
324-
// So we need to detect whether this is in a trait or not, which
325-
// we do through the mildly hacky way of checking whether there is
326-
// a trait_parent_sort.
327-
let provenance = if reader::maybe_get_doc(
328-
item, tag_item_trait_parent_sort).is_some() {
329-
def::FromTrait(item_require_parent_item(cdata, item))
330-
} else {
331-
def::FromImpl(item_require_parent_item(cdata, item))
332-
};
333-
DlDef(def::DefMethod(did, provenance))
314+
DlDef(def::DefMethod(did))
334315
}
335316
Type => {
336317
if item_sort(item) == Some('t') {

src/librustc/middle/astencode.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,7 @@ impl tr for def::Def {
444444
fn tr(&self, dcx: &DecodeContext) -> def::Def {
445445
match *self {
446446
def::DefFn(did, is_ctor) => def::DefFn(did.tr(dcx), is_ctor),
447-
def::DefMethod(did, p) => {
448-
def::DefMethod(did.tr(dcx), p.map(|did2| did2.tr(dcx)))
449-
}
447+
def::DefMethod(did) => def::DefMethod(did.tr(dcx)),
450448
def::DefSelfTy(opt_did, impl_ids) => { def::DefSelfTy(opt_did.map(|did| did.tr(dcx)),
451449
impl_ids.map(|(nid1, nid2)| {
452450
(dcx.tr_id(nid1),
@@ -456,9 +454,7 @@ impl tr for def::Def {
456454
def::DefForeignMod(did) => { def::DefForeignMod(did.tr(dcx)) }
457455
def::DefStatic(did, m) => { def::DefStatic(did.tr(dcx), m) }
458456
def::DefConst(did) => { def::DefConst(did.tr(dcx)) }
459-
def::DefAssociatedConst(did, p) => {
460-
def::DefAssociatedConst(did.tr(dcx), p.map(|did2| did2.tr(dcx)))
461-
}
457+
def::DefAssociatedConst(did) => def::DefAssociatedConst(did.tr(dcx)),
462458
def::DefLocal(nid) => { def::DefLocal(dcx.tr_id(nid)) }
463459
def::DefVariant(e_did, v_did, is_s) => {
464460
def::DefVariant(e_did.tr(dcx), v_did.tr(dcx), is_s)

src/librustc/middle/check_const.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
650650
}
651651
}
652652
Some(def::DefConst(did)) |
653-
Some(def::DefAssociatedConst(did, _)) => {
653+
Some(def::DefAssociatedConst(did)) => {
654654
if let Some(expr) = const_eval::lookup_const_by_id(v.tcx, did,
655655
Some(e.id)) {
656656
let inner = v.global_expr(Mode::Const, expr);
@@ -696,10 +696,17 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
696696
v.add_qualif(ConstQualif::NON_ZERO_SIZED);
697697
true
698698
}
699-
Some(def::DefMethod(did, def::FromImpl(_))) |
700699
Some(def::DefFn(did, _)) => {
701700
v.handle_const_fn_call(e, did, node_ty)
702701
}
702+
Some(def::DefMethod(did)) => {
703+
match v.tcx.impl_or_trait_item(did).container() {
704+
ty::ImplContainer(_) => {
705+
v.handle_const_fn_call(e, did, node_ty)
706+
}
707+
ty::TraitContainer(_) => false
708+
}
709+
}
703710
_ => false
704711
};
705712
if !is_const {

src/librustc/middle/check_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ impl<'a, 'tcx> Folder for StaticInliner<'a, 'tcx> {
442442
ast::PatIdent(..) | ast::PatEnum(..) | ast::PatQPath(..) => {
443443
let def = self.tcx.def_map.borrow().get(&pat.id).map(|d| d.full_def());
444444
match def {
445-
Some(DefAssociatedConst(did, _)) |
445+
Some(DefAssociatedConst(did)) |
446446
Some(DefConst(did)) => match lookup_const_by_id(self.tcx, did, Some(pat.id)) {
447447
Some(const_expr) => {
448448
const_expr_to_pat(self.tcx, const_expr, pat.span).map(|new_pat| {

src/librustc/middle/check_static_recursion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckItemRecursionVisitor<'a, 'ast> {
238238
ast::ExprPath(..) => {
239239
match self.def_map.borrow().get(&e.id).map(|d| d.base_def) {
240240
Some(DefStatic(def_id, _)) |
241-
Some(DefAssociatedConst(def_id, _)) |
241+
Some(DefAssociatedConst(def_id)) |
242242
Some(DefConst(def_id))
243243
if ast_util::is_local(def_id) => {
244244
match self.ast_map.get(def_id.node) {

src/librustc/middle/const_eval.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn lookup_const<'a>(tcx: &'a ty::ctxt, e: &Expr) -> Option<&'a Expr> {
4141
let opt_def = tcx.def_map.borrow().get(&e.id).map(|d| d.full_def());
4242
match opt_def {
4343
Some(def::DefConst(def_id)) |
44-
Some(def::DefAssociatedConst(def_id, _)) => {
44+
Some(def::DefAssociatedConst(def_id)) => {
4545
lookup_const_by_id(tcx, def_id, Some(e.id))
4646
}
4747
Some(def::DefVariant(enum_def, variant_def, _)) => {
@@ -929,10 +929,10 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
929929
(lookup_const_by_id(tcx, def_id, Some(e.id)), None)
930930
}
931931
}
932-
Some(def::DefAssociatedConst(def_id, provenance)) => {
932+
Some(def::DefAssociatedConst(def_id)) => {
933933
if ast_util::is_local(def_id) {
934-
match provenance {
935-
def::FromTrait(trait_id) => match tcx.map.find(def_id.node) {
934+
match tcx.impl_or_trait_item(def_id).container() {
935+
ty::TraitContainer(trait_id) => match tcx.map.find(def_id.node) {
936936
Some(ast_map::NodeTraitItem(ti)) => match ti.node {
937937
ast::ConstTraitItem(ref ty, _) => {
938938
if let ExprTypeChecked = ty_hint {
@@ -950,7 +950,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
950950
},
951951
_ => (None, None)
952952
},
953-
def::FromImpl(_) => match tcx.map.find(def_id.node) {
953+
ty::ImplContainer(_) => match tcx.map.find(def_id.node) {
954954
Some(ast_map::NodeImplItem(ii)) => match ii.node {
955955
ast::ConstImplItem(ref ty, ref expr) => {
956956
(Some(&**expr), Some(&**ty))

src/librustc/middle/def.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
pub use self::Def::*;
12-
pub use self::MethodProvenance::*;
1312

1413
use middle::privacy::LastPrivate;
1514
use middle::subst::ParamSpace;
@@ -28,7 +27,7 @@ pub enum Def {
2827
DefForeignMod(ast::DefId),
2928
DefStatic(ast::DefId, bool /* is_mutbl */),
3029
DefConst(ast::DefId),
31-
DefAssociatedConst(ast::DefId /* const */, MethodProvenance),
30+
DefAssociatedConst(ast::DefId),
3231
DefLocal(ast::NodeId),
3332
DefVariant(ast::DefId /* enum */, ast::DefId /* variant */, bool /* is_structure */),
3433
DefTy(ast::DefId, bool /* is_enum */),
@@ -51,7 +50,7 @@ pub enum Def {
5150
DefStruct(ast::DefId),
5251
DefRegion(ast::NodeId),
5352
DefLabel(ast::NodeId),
54-
DefMethod(ast::DefId /* method */, MethodProvenance),
53+
DefMethod(ast::DefId),
5554
}
5655

5756
/// The result of resolving a path.
@@ -112,23 +111,6 @@ pub struct Export {
112111
pub def_id: ast::DefId, // The definition of the target.
113112
}
114113

115-
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
116-
pub enum MethodProvenance {
117-
FromTrait(ast::DefId),
118-
FromImpl(ast::DefId),
119-
}
120-
121-
impl MethodProvenance {
122-
pub fn map<F>(self, f: F) -> MethodProvenance where
123-
F: FnOnce(ast::DefId) -> ast::DefId,
124-
{
125-
match self {
126-
FromTrait(did) => FromTrait(f(did)),
127-
FromImpl(did) => FromImpl(f(did))
128-
}
129-
}
130-
}
131-
132114
impl Def {
133115
pub fn local_node_id(&self) -> ast::NodeId {
134116
let def_id = self.def_id();
@@ -141,7 +123,7 @@ impl Def {
141123
DefFn(id, _) | DefMod(id) | DefForeignMod(id) | DefStatic(id, _) |
142124
DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(_, id) |
143125
DefTyParam(_, _, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
144-
DefMethod(id, _) | DefConst(id) | DefAssociatedConst(id, _) |
126+
DefMethod(id) | DefConst(id) | DefAssociatedConst(id) |
145127
DefSelfTy(Some(id), None)=> {
146128
id
147129
}

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -545,14 +545,12 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
545545

546546
match trait_item.node {
547547
ast::ConstTraitItem(..) => {
548-
let def = DefAssociatedConst(local_def(trait_item.id),
549-
FromTrait(local_def(item.id)));
548+
let def = DefAssociatedConst(local_def(trait_item.id));
550549
// NB: not DefModifiers::IMPORTABLE
551550
name_bindings.define_value(def, trait_item.span, DefModifiers::PUBLIC);
552551
}
553552
ast::MethodTraitItem(..) => {
554-
let def = DefMethod(local_def(trait_item.id),
555-
FromTrait(local_def(item.id)));
553+
let def = DefMethod(local_def(trait_item.id));
556554
// NB: not DefModifiers::IMPORTABLE
557555
name_bindings.define_value(def, trait_item.span, DefModifiers::PUBLIC);
558556
}

src/librustc_resolve/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3448,7 +3448,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
34483448
// Look for a method in the current self type's impl module.
34493449
if let Some(module) = get_module(self, path.span, &name_path) {
34503450
if let Some(binding) = module.children.borrow().get(&name) {
3451-
if let Some(DefMethod(did, _)) = binding.def_for_namespace(ValueNS) {
3451+
if let Some(DefMethod(did)) = binding.def_for_namespace(ValueNS) {
34523452
if is_static_method(self, did) {
34533453
return StaticMethod(path_names_to_string(&path, 0))
34543454
}

src/librustc_trans/save/dump_csv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
719719
let def_map = self.tcx.def_map.borrow();
720720
let def = def_map.get(&id).unwrap().full_def();
721721
match def {
722-
def::DefMethod(did, _) => {
722+
def::DefMethod(did) => {
723723
let ti = self.tcx.impl_or_trait_item(did);
724724
if let ty::MethodTraitItem(m) = ti {
725725
if m.explicit_self == ty::StaticExplicitSelfCategory {

src/librustc_trans/save/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,20 +551,20 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
551551
scope: self.enclosing_scope(id),
552552
}))
553553
}
554-
def::DefMethod(decl_id, provenence) => {
554+
def::DefMethod(decl_id) => {
555555
let sub_span = self.span_utils.sub_span_for_meth_name(path.span);
556556
let def_id = if decl_id.krate == ast::LOCAL_CRATE {
557557
let ti = self.tcx.impl_or_trait_item(decl_id);
558-
match provenence {
559-
def::FromTrait(def_id) => {
558+
match ti.container() {
559+
ty::TraitContainer(def_id) => {
560560
self.tcx.trait_items(def_id)
561561
.iter()
562562
.find(|mr| {
563563
mr.name() == ti.name() && self.trait_method_has_body(mr)
564564
})
565565
.map(|mr| mr.def_id())
566566
}
567-
def::FromImpl(def_id) => {
567+
ty::ImplContainer(def_id) => {
568568
let impl_items = self.tcx.impl_items.borrow();
569569
Some(impl_items.get(&def_id)
570570
.unwrap()

src/librustc_trans/trans/callee.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,27 @@ fn trans<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, expr: &ast::Expr)
159159
let def_id = inline::maybe_instantiate_inline(bcx.ccx(), did);
160160
Callee { bcx: bcx, data: Intrinsic(def_id.node, substs), ty: expr_ty }
161161
}
162-
def::DefFn(did, _) | def::DefMethod(did, def::FromImpl(_)) => {
162+
def::DefFn(did, _) => {
163163
fn_callee(bcx, trans_fn_ref(bcx.ccx(), did, ExprId(ref_expr.id),
164164
bcx.fcx.param_substs))
165165
}
166-
def::DefMethod(meth_did, def::FromTrait(trait_did)) => {
167-
fn_callee(bcx, meth::trans_static_method_callee(bcx.ccx(),
168-
meth_did,
169-
trait_did,
170-
ref_expr.id,
171-
bcx.fcx.param_substs))
166+
def::DefMethod(meth_did) => {
167+
let method_item = bcx.tcx().impl_or_trait_item(meth_did);
168+
let fn_datum = match method_item.container() {
169+
ty::ImplContainer(_) => {
170+
trans_fn_ref(bcx.ccx(), meth_did,
171+
ExprId(ref_expr.id),
172+
bcx.fcx.param_substs)
173+
}
174+
ty::TraitContainer(trait_did) => {
175+
meth::trans_static_method_callee(bcx.ccx(),
176+
meth_did,
177+
trait_did,
178+
ref_expr.id,
179+
bcx.fcx.param_substs)
180+
}
181+
};
182+
fn_callee(bcx, fn_datum)
172183
}
173184
def::DefVariant(tid, vid, _) => {
174185
let vinfo = bcx.tcx().enum_variant_with_id(tid, vid);

src/librustc_trans/trans/consts.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ pub fn get_const_expr_as_global<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
229229
ast::ExprPath(..) => {
230230
let def = ccx.tcx().def_map.borrow().get(&expr.id).unwrap().full_def();
231231
match def {
232-
def::DefConst(def_id) | def::DefAssociatedConst(def_id, _) => {
232+
def::DefConst(def_id) | def::DefAssociatedConst(def_id) => {
233233
if !ccx.tcx().tables.borrow().adjustments.contains_key(&expr.id) {
234234
debug!("get_const_expr_as_global ({:?}): found const {:?}",
235235
expr.id, def_id);
@@ -802,7 +802,7 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
802802
def::DefFn(..) | def::DefMethod(..) => {
803803
expr::trans_def_fn_unadjusted(cx, e, def, param_substs).val
804804
}
805-
def::DefConst(def_id) | def::DefAssociatedConst(def_id, _) => {
805+
def::DefConst(def_id) | def::DefAssociatedConst(def_id) => {
806806
const_deref_ptr(cx, get_const_val(cx, def_id, e))
807807
}
808808
def::DefVariant(enum_did, variant_did, _) => {
@@ -846,32 +846,32 @@ fn const_expr_unadjusted<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
846846
let def = cx.tcx().def_map.borrow()[&callee.id].full_def();
847847
let arg_vals = map_list(args);
848848
match def {
849-
def::DefFn(did, _) | def::DefMethod(did, _) => {
849+
def::DefFn(did, _) | def::DefMethod(did) => {
850850
const_fn_call(cx, ExprId(callee.id), did, &arg_vals, param_substs)
851-
},
851+
}
852852
def::DefStruct(_) => {
853853
if ety.is_simd(cx.tcx()) {
854854
C_vector(&arg_vals[..])
855855
} else {
856856
let repr = adt::represent_type(cx, ety);
857857
adt::trans_const(cx, &*repr, 0, &arg_vals[..])
858858
}
859-
},
859+
}
860860
def::DefVariant(enum_did, variant_did, _) => {
861861
let repr = adt::represent_type(cx, ety);
862862
let vinfo = cx.tcx().enum_variant_with_id(enum_did, variant_did);
863863
adt::trans_const(cx,
864864
&*repr,
865865
vinfo.disr_val,
866866
&arg_vals[..])
867-
},
867+
}
868868
_ => cx.sess().span_bug(e.span, "expected a struct, variant, or const fn def"),
869869
}
870870
},
871871
ast::ExprMethodCall(_, _, ref args) => {
872872
let arg_vals = map_list(args);
873873
let method_call = ty::MethodCall::expr(e.id);
874-
let method_did = cx.tcx().tables.borrow().method_map[&method_call].def_id;
874+
let method_did = cx.tcx().tables.borrow().method_map[&method_call].def_id;
875875
const_fn_call(cx, MethodCallKey(method_call),
876876
method_did, &arg_vals, param_substs)
877877
},

src/librustc_trans/trans/expr.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,14 +1293,22 @@ pub fn trans_def_fn_unadjusted<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
12931293

12941294
match def {
12951295
def::DefFn(did, _) |
1296-
def::DefStruct(did) | def::DefVariant(_, did, _) |
1297-
def::DefMethod(did, def::FromImpl(_)) => {
1296+
def::DefStruct(did) | def::DefVariant(_, did, _) => {
12981297
callee::trans_fn_ref(ccx, did, ExprId(ref_expr.id), param_substs)
12991298
}
1300-
def::DefMethod(impl_did, def::FromTrait(trait_did)) => {
1301-
meth::trans_static_method_callee(ccx, impl_did,
1302-
trait_did, ref_expr.id,
1303-
param_substs)
1299+
def::DefMethod(method_did) => {
1300+
match ccx.tcx().impl_or_trait_item(method_did).container() {
1301+
ty::ImplContainer(_) => {
1302+
callee::trans_fn_ref(ccx, method_did,
1303+
ExprId(ref_expr.id),
1304+
param_substs)
1305+
}
1306+
ty::TraitContainer(trait_did) => {
1307+
meth::trans_static_method_callee(ccx, method_did,
1308+
trait_did, ref_expr.id,
1309+
param_substs)
1310+
}
1311+
}
13041312
}
13051313
_ => {
13061314
ccx.tcx().sess.span_bug(ref_expr.span, &format!(

0 commit comments

Comments
 (0)