Skip to content

Commit d256eb1

Browse files
committed
rustc: remove MethodOrigin and use the container to distinguish inherent methods.
1 parent 536e71b commit d256eb1

File tree

17 files changed

+60
-121
lines changed

17 files changed

+60
-121
lines changed

src/librustc/middle/astencode.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -609,20 +609,17 @@ fn encode_method_callee<'a, 'tcx>(ecx: &e::EncodeContext<'a, 'tcx>,
609609
method: &ty::MethodCallee<'tcx>) {
610610
use serialize::Encoder;
611611

612-
rbml_w.emit_struct("MethodCallee", 5, |rbml_w| {
612+
rbml_w.emit_struct("MethodCallee", 4, |rbml_w| {
613613
rbml_w.emit_struct_field("autoderef", 0, |rbml_w| {
614614
autoderef.encode(rbml_w)
615615
});
616616
rbml_w.emit_struct_field("def_id", 1, |rbml_w| {
617617
Ok(rbml_w.emit_def_id(method.def_id))
618618
});
619-
rbml_w.emit_struct_field("origin", 2, |rbml_w| {
620-
method.origin.encode(rbml_w)
621-
});
622-
rbml_w.emit_struct_field("ty", 3, |rbml_w| {
619+
rbml_w.emit_struct_field("ty", 2, |rbml_w| {
623620
Ok(rbml_w.emit_ty(ecx, method.ty))
624621
});
625-
rbml_w.emit_struct_field("substs", 4, |rbml_w| {
622+
rbml_w.emit_struct_field("substs", 3, |rbml_w| {
626623
Ok(rbml_w.emit_substs(ecx, &method.substs))
627624
})
628625
}).unwrap();
@@ -632,19 +629,17 @@ impl<'a, 'tcx> read_method_callee_helper<'tcx> for reader::Decoder<'a> {
632629
fn read_method_callee<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
633630
-> (u32, ty::MethodCallee<'tcx>) {
634631

635-
self.read_struct("MethodCallee", 5, |this| {
632+
self.read_struct("MethodCallee", 4, |this| {
636633
let autoderef = this.read_struct_field("autoderef", 0,
637634
Decodable::decode).unwrap();
638635
Ok((autoderef, ty::MethodCallee {
639636
def_id: this.read_struct_field("def_id", 1, |this| {
640637
Ok(this.read_def_id(dcx))
641638
}).unwrap(),
642-
origin: this.read_struct_field("origin", 2,
643-
Decodable::decode).unwrap(),
644-
ty: this.read_struct_field("ty", 3, |this| {
639+
ty: this.read_struct_field("ty", 2, |this| {
645640
Ok(this.read_ty(dcx))
646641
}).unwrap(),
647-
substs: this.read_struct_field("substs", 4, |this| {
642+
substs: this.read_struct_field("substs", 3, |this| {
648643
Ok(dcx.tcx.mk_substs(this.read_substs(dcx)))
649644
}).unwrap()
650645
}))

src/librustc/middle/check_const.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -696,11 +696,10 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
696696
}
697697
}
698698
ast::ExprMethodCall(..) => {
699-
let is_const = match v.tcx.tables.borrow().method_map[&method_call] {
700-
ty::MethodCallee { def_id, origin: ty::MethodOrigin::Inherent, .. } => {
701-
v.handle_const_fn_call(e, def_id, node_ty)
702-
}
703-
_ => false
699+
let method = v.tcx.tables.borrow().method_map[&method_call];
700+
let is_const = match v.tcx.impl_or_trait_item(method.def_id).container() {
701+
ty::ImplContainer(_) => v.handle_const_fn_call(e, method.def_id, node_ty),
702+
ty::TraitContainer(_) => false
704703
};
705704
if !is_const {
706705
v.add_qualif(ConstQualif::NOT_CONST);

src/librustc/middle/dead.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,10 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
9393
});
9494
}
9595

96-
fn lookup_and_handle_method(&mut self, id: ast::NodeId,
97-
span: codemap::Span) {
96+
fn lookup_and_handle_method(&mut self, id: ast::NodeId) {
9897
let method_call = ty::MethodCall::expr(id);
99-
match self.tcx.tables.borrow().method_map.get(&method_call) {
100-
Some(method) => self.check_def_id(method.def_id),
101-
None => {
102-
self.tcx.sess.span_bug(span,
103-
"method call expression not \
104-
in method map?!")
105-
}
106-
}
98+
let method = self.tcx.tables.borrow().method_map[&method_call];
99+
self.check_def_id(method.def_id);
107100
}
108101

109102
fn handle_field_access(&mut self, lhs: &ast::Expr, name: ast::Name) {
@@ -239,7 +232,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
239232
fn visit_expr(&mut self, expr: &ast::Expr) {
240233
match expr.node {
241234
ast::ExprMethodCall(..) => {
242-
self.lookup_and_handle_method(expr.id, expr.span);
235+
self.lookup_and_handle_method(expr.id);
243236
}
244237
ast::ExprField(ref lhs, ref ident) => {
245238
self.handle_field_access(&**lhs, ident.node.name);

src/librustc/middle/effect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EffectCheckVisitor<'a, 'tcx> {
140140
match expr.node {
141141
ast::ExprMethodCall(_, _, _) => {
142142
let method_call = MethodCall::expr(expr.id);
143-
let base_type = self.tcx.tables.borrow().method_map.get(&method_call).unwrap().ty;
143+
let base_type = self.tcx.tables.borrow().method_map[&method_call].ty;
144144
debug!("effect: method call case, base type is {:?}",
145145
base_type);
146146
if type_is_unsafe_function(base_type) {

src/librustc/middle/liveness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
11481148

11491149
ast::ExprMethodCall(_, _, ref args) => {
11501150
let method_call = ty::MethodCall::expr(expr.id);
1151-
let method_ty = self.ir.tcx.tables.borrow().method_map.get(&method_call).unwrap().ty;
1151+
let method_ty = self.ir.tcx.tables.borrow().method_map[&method_call].ty;
11521152
let succ = if method_ty.fn_ret().diverges() {
11531153
self.s.exit_ln
11541154
} else {

src/librustc/middle/reachable.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,17 @@ impl<'a, 'tcx, 'v> Visitor<'v> for ReachableContext<'a, 'tcx> {
128128
}
129129
ast::ExprMethodCall(..) => {
130130
let method_call = ty::MethodCall::expr(expr.id);
131-
match self.tcx.tables.borrow().method_map[&method_call] {
132-
ty::MethodCallee { def_id, origin: ty::MethodOrigin::Inherent, .. } => {
131+
let def_id = self.tcx.tables.borrow().method_map[&method_call].def_id;
132+
match self.tcx.impl_or_trait_item(def_id).container() {
133+
ty::ImplContainer(_) => {
133134
if is_local(def_id) {
134135
if self.def_id_represents_local_inlined_item(def_id) {
135136
self.worklist.push(def_id.node)
136137
}
137138
self.reachable_symbols.insert(def_id.node);
138139
}
139140
}
140-
_ => {}
141+
ty::TraitContainer(_) => {}
141142
}
142143
}
143144
_ => {}

src/librustc/middle/stability.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,7 @@ pub fn check_expr(tcx: &ty::ctxt, e: &ast::Expr,
406406
ast::ExprMethodCall(i, _, _) => {
407407
span = i.span;
408408
let method_call = ty::MethodCall::expr(e.id);
409-
match tcx.tables.borrow().method_map.get(&method_call) {
410-
Some(method) => method.def_id,
411-
None => return
412-
}
409+
tcx.tables.borrow().method_map[&method_call].def_id
413410
}
414411
ast::ExprField(ref base_e, ref field) => {
415412
span = field.span;

src/librustc/middle/ty.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -625,20 +625,10 @@ pub enum CustomCoerceUnsized {
625625
Struct(usize)
626626
}
627627

628-
#[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug)]
629-
pub enum MethodOrigin {
630-
/// Inherent impl method call.
631-
Inherent,
632-
633-
/// Trait method call.
634-
Trait
635-
}
636-
637-
#[derive(Clone, Debug)]
628+
#[derive(Clone, Copy, Debug)]
638629
pub struct MethodCallee<'tcx> {
639630
/// Impl method ID, for inherent methods, or trait method ID, otherwise.
640631
pub def_id: ast::DefId,
641-
pub origin: MethodOrigin,
642632
pub ty: Ty<'tcx>,
643633
pub substs: &'tcx subst::Substs<'tcx>
644634
}

src/librustc_lint/builtin.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1995,9 +1995,9 @@ impl LintPass for UnconditionalRecursion {
19951995
fn expr_refers_to_this_method(tcx: &ty::ctxt,
19961996
method: &ty::Method,
19971997
id: ast::NodeId) -> bool {
1998-
let tables = tcx.tables.borrow();
1999-
let callee = match tables.method_map.get(&ty::MethodCall::expr(id)) {
2000-
Some(m) => m,
1998+
let method_call = ty::MethodCall::expr(id);
1999+
let callee = match tcx.tables.borrow().method_map.get(&method_call) {
2000+
Some(&m) => m,
20012001
None => return false
20022002
};
20032003
let callee_item = tcx.impl_or_trait_item(callee.def_id);

src/librustc_privacy/lib.rs

+9-18
Original file line numberDiff line numberDiff line change
@@ -844,17 +844,16 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
844844
}
845845

846846
// Checks that a method is in scope.
847-
fn check_method(&mut self, span: Span, callee: &ty::MethodCallee,
847+
fn check_method(&mut self, span: Span, method_def_id: ast::DefId,
848848
name: ast::Name) {
849-
match callee.origin {
850-
ty::MethodOrigin::Inherent => {
851-
self.check_static_method(span, callee.def_id, name)
849+
match self.tcx.impl_or_trait_item(method_def_id).container() {
850+
ty::ImplContainer(_) => {
851+
self.check_static_method(span, method_def_id, name)
852852
}
853853
// Trait methods are always all public. The only controlling factor
854854
// is whether the trait itself is accessible or not.
855-
ty::MethodOrigin::Trait => {
856-
let method = self.tcx.impl_or_trait_item(callee.def_id);
857-
self.report_error(self.ensure_public(span, method.container().id(),
855+
ty::TraitContainer(trait_def_id) => {
856+
self.report_error(self.ensure_public(span, trait_def_id,
858857
None, "source trait"));
859858
}
860859
}
@@ -899,17 +898,9 @@ impl<'a, 'tcx, 'v> Visitor<'v> for PrivacyVisitor<'a, 'tcx> {
899898
}
900899
ast::ExprMethodCall(ident, _, _) => {
901900
let method_call = ty::MethodCall::expr(expr.id);
902-
match self.tcx.tables.borrow().method_map.get(&method_call) {
903-
None => {
904-
self.tcx.sess.span_bug(expr.span,
905-
"method call not in \
906-
method map");
907-
}
908-
Some(method) => {
909-
debug!("(privacy checking) checking impl method");
910-
self.check_method(expr.span, method, ident.node.name);
911-
}
912-
}
901+
let method = self.tcx.tables.borrow().method_map[&method_call];
902+
debug!("(privacy checking) checking impl method");
903+
self.check_method(expr.span, method.def_id, ident.node.name);
913904
}
914905
ast::ExprStruct(_, ref fields, _) => {
915906
match self.tcx.expr_ty(expr).sty {

src/librustc_trans/save/dump_csv.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -886,15 +886,11 @@ impl <'l, 'tcx> DumpCsvVisitor<'l, 'tcx> {
886886
fn process_method_call(&mut self,
887887
ex: &ast::Expr,
888888
args: &Vec<P<ast::Expr>>) {
889-
let method_map = &self.tcx.tables.borrow().method_map;
890-
let method_callee = method_map.get(&ty::MethodCall::expr(ex.id)).unwrap();
891-
let (def_id, decl_id) = match method_callee.origin {
892-
ty::MethodOrigin::Inherent => {
893-
(Some(method_callee.def_id), None)
894-
}
895-
ty::MethodOrigin::Trait => {
896-
(None, Some(method_callee.def_id))
897-
}
889+
let method_call = ty::MethodCall::expr(ex.id);
890+
let method_id = self.tcx.tables.borrow().method_map[&method_call].def_id;
891+
let (def_id, decl_id) = match self.tcx.impl_or_trait_item(method_id).container() {
892+
ty::ImplContainer(_) => (Some(method_id), None),
893+
ty::TraitContainer(_) => (None, Some(method_id))
898894
};
899895
let sub_span = self.span.sub_span_for_meth_name(ex.span);
900896
self.fmt.meth_call_str(ex.span,

src/librustc_trans/trans/callee.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ pub fn trans_fn_ref_with_substs<'a, 'tcx>(
499499
let ref_ty = match node {
500500
ExprId(id) => tcx.node_id_to_type(id),
501501
MethodCallKey(method_call) => {
502-
tcx.tables.borrow().method_map.get(&method_call).unwrap().ty
502+
tcx.tables.borrow().method_map[&method_call].ty
503503
}
504504
};
505505
let ref_ty = monomorphize::apply_param_substs(tcx,

src/librustc_trans/trans/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ pub fn node_id_substs<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
10251025
tcx.node_id_item_substs(id).substs
10261026
}
10271027
MethodCallKey(method_call) => {
1028-
tcx.tables.borrow().method_map.get(&method_call).unwrap().substs.clone()
1028+
tcx.tables.borrow().method_map[&method_call].substs.clone()
10291029
}
10301030
};
10311031

src/librustc_trans/trans/meth.rs

+10-20
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,13 @@ pub fn trans_method_callee<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
106106
-> Callee<'blk, 'tcx> {
107107
let _icx = push_ctxt("meth::trans_method_callee");
108108

109-
let (method_id, origin, method_substs, method_ty) =
110-
bcx.tcx()
111-
.tables
112-
.borrow()
113-
.method_map
114-
.get(&method_call)
115-
.map(|method| (method.def_id, method.origin, method.substs, method.ty))
116-
.unwrap();
117-
118-
match origin {
119-
ty::MethodOrigin::Inherent => {
120-
debug!("trans_method_callee: static, {:?}", method_id);
109+
let method = bcx.tcx().tables.borrow().method_map[&method_call];
110+
111+
match bcx.tcx().impl_or_trait_item(method.def_id).container() {
112+
ty::ImplContainer(_) => {
113+
debug!("trans_method_callee: static, {:?}", method.def_id);
121114
let datum = callee::trans_fn_ref(bcx.ccx(),
122-
method_id,
115+
method.def_id,
123116
MethodCallKey(method_call),
124117
bcx.fcx.param_substs);
125118
Callee {
@@ -129,11 +122,8 @@ pub fn trans_method_callee<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
129122
}
130123
}
131124

132-
ty::MethodOrigin::Trait => {
133-
let method_item = bcx.tcx().impl_or_trait_item(method_id);
134-
let trait_def_id = method_item.container().id();
135-
136-
let trait_substs = method_substs.clone().method_to_trait();
125+
ty::TraitContainer(trait_def_id) => {
126+
let trait_substs = method.substs.clone().method_to_trait();
137127
let trait_substs = bcx.tcx().mk_substs(trait_substs);
138128
let trait_ref = ty::TraitRef::new(trait_def_id, trait_substs);
139129

@@ -152,8 +142,8 @@ pub fn trans_method_callee<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
152142
method_call,
153143
self_expr,
154144
trait_def_id,
155-
method_id,
156-
method_ty,
145+
method.def_id,
146+
method.ty,
157147
origin,
158148
arg_cleanup_scope)
159149
}

src/librustc_typeck/check/method/confirm.rs

+9-20
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
8484
self.enforce_illegal_method_limitations(&pick);
8585

8686
// Create substitutions for the method's type parameters.
87-
let (rcvr_substs, method_origin) =
88-
self.fresh_receiver_substs(self_ty, &pick);
87+
let rcvr_substs = self.fresh_receiver_substs(self_ty, &pick);
8988
let (method_types, method_regions) =
9089
self.instantiate_method_substs(&pick, supplied_method_types);
9190
let all_substs = rcvr_substs.with_method(method_types, method_regions);
@@ -112,7 +111,6 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
112111
}));
113112
let callee = ty::MethodCallee {
114113
def_id: pick.item.def_id(),
115-
origin: method_origin,
116114
ty: fty,
117115
substs: self.tcx().mk_substs(all_substs)
118116
};
@@ -193,16 +191,14 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
193191
fn fresh_receiver_substs(&mut self,
194192
self_ty: Ty<'tcx>,
195193
pick: &probe::Pick<'tcx>)
196-
-> (subst::Substs<'tcx>, ty::MethodOrigin)
194+
-> subst::Substs<'tcx>
197195
{
198196
match pick.kind {
199197
probe::InherentImplPick => {
200198
let impl_def_id = pick.item.container().id();
201199
assert!(self.tcx().impl_trait_ref(impl_def_id).is_none(),
202200
"impl {:?} is not an inherent impl", impl_def_id);
203-
let impl_polytype = check::impl_self_ty(self.fcx, self.span, impl_def_id);
204-
205-
(impl_polytype.substs, ty::MethodOrigin::Inherent)
201+
check::impl_self_ty(self.fcx, self.span, impl_def_id).substs
206202
}
207203

208204
probe::ObjectPick => {
@@ -228,9 +224,7 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
228224
original_poly_trait_ref,
229225
upcast_trait_ref,
230226
trait_def_id);
231-
let substs = upcast_trait_ref.substs.clone();
232-
233-
(substs, ty::MethodOrigin::Trait)
227+
upcast_trait_ref.substs.clone()
234228
})
235229
}
236230

@@ -250,8 +244,7 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
250244
self.span,
251245
&impl_polytype.substs,
252246
&self.tcx().impl_trait_ref(impl_def_id).unwrap());
253-
let substs = impl_trait_ref.substs.clone();
254-
(substs, ty::MethodOrigin::Trait)
247+
impl_trait_ref.substs.clone()
255248
}
256249

257250
probe::TraitPick => {
@@ -263,19 +256,15 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
263256
// the process we will unify the transformed-self-type
264257
// of the method with the actual type in order to
265258
// unify some of these variables.
266-
let substs = self.infcx().fresh_substs_for_trait(self.span,
267-
&trait_def.generics,
268-
self.infcx().next_ty_var());
269-
270-
(substs, ty::MethodOrigin::Trait)
259+
self.infcx().fresh_substs_for_trait(self.span,
260+
&trait_def.generics,
261+
self.infcx().next_ty_var())
271262
}
272263

273264
probe::WhereClausePick(ref poly_trait_ref) => {
274265
// Where clauses can have bound regions in them. We need to instantiate
275266
// those to convert from a poly-trait-ref to a trait-ref.
276-
let trait_ref = self.replace_late_bound_regions_with_fresh_var(&*poly_trait_ref);
277-
let substs = trait_ref.substs.clone();
278-
(substs, ty::MethodOrigin::Trait)
267+
self.replace_late_bound_regions_with_fresh_var(&*poly_trait_ref).substs.clone()
279268
}
280269
}
281270
}

0 commit comments

Comments
 (0)