Skip to content

Commit 585e283

Browse files
committed
Add provided method information to ty::Method. Get rid of ProvidedMethodSource.
1 parent 2ea6120 commit 585e283

File tree

12 files changed

+77
-95
lines changed

12 files changed

+77
-95
lines changed

src/librustc/metadata/decoder.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,6 @@ fn item_parent_item(d: ebml::Doc) -> Option<ast::def_id> {
174174
None
175175
}
176176

177-
fn translated_parent_item_opt(cnum: ast::crate_num, d: ebml::Doc) ->
178-
Option<ast::def_id> {
179-
let trait_did_opt = item_parent_item(d);
180-
do trait_did_opt.map |trait_did| {
181-
ast::def_id { crate: cnum, node: trait_did.node }
182-
}
183-
}
184-
185177
fn item_reqd_and_translated_parent_item(cnum: ast::crate_num,
186178
d: ebml::Doc) -> ast::def_id {
187179
let trait_did = item_parent_item(d).expect("item without parent");
@@ -323,13 +315,19 @@ fn item_to_def_like(item: ebml::Doc, did: ast::def_id, cnum: ast::crate_num)
323315
UnsafeFn => dl_def(ast::def_fn(did, ast::unsafe_fn)),
324316
Fn => dl_def(ast::def_fn(did, ast::impure_fn)),
325317
ForeignFn => dl_def(ast::def_fn(did, ast::extern_fn)),
326-
UnsafeStaticMethod => {
327-
let trait_did_opt = translated_parent_item_opt(cnum, item);
328-
dl_def(ast::def_static_method(did, trait_did_opt, ast::unsafe_fn))
329-
}
330-
StaticMethod => {
331-
let trait_did_opt = translated_parent_item_opt(cnum, item);
332-
dl_def(ast::def_static_method(did, trait_did_opt, ast::impure_fn))
318+
StaticMethod | UnsafeStaticMethod => {
319+
let purity = if fam == UnsafeStaticMethod { ast::unsafe_fn } else
320+
{ ast::impure_fn };
321+
// def_static_method carries an optional field of its enclosing
322+
// *trait*, but not an inclosing Impl (if this is an inherent
323+
// static method). So we need to detect whether this is in
324+
// a trait or not, which we do through the mildly hacky
325+
// way of checking whether there is a trait_method_sort.
326+
let trait_did_opt = if reader::maybe_get_doc(
327+
item, tag_item_trait_method_sort).is_some() {
328+
Some(item_reqd_and_translated_parent_item(cnum, item))
329+
} else { None };
330+
dl_def(ast::def_static_method(did, trait_did_opt, purity))
333331
}
334332
Type | ForeignType => dl_def(ast::def_ty(did)),
335333
Mod => dl_def(ast::def_mod(did)),
@@ -837,6 +835,8 @@ pub fn get_method(intr: @ident_interner, cdata: cmd, id: ast::node_id,
837835
{
838836
let method_doc = lookup_item(id, cdata.data);
839837
let def_id = item_def_id(method_doc, cdata);
838+
let container_id = item_reqd_and_translated_parent_item(cdata.cnum,
839+
method_doc);
840840
let name = item_name(intr, method_doc);
841841
let type_param_defs = item_ty_param_defs(method_doc, tcx, cdata,
842842
tag_item_method_tps);
@@ -855,7 +855,9 @@ pub fn get_method(intr: @ident_interner, cdata: cmd, id: ast::node_id,
855855
fty,
856856
explicit_self,
857857
vis,
858-
def_id
858+
def_id,
859+
container_id,
860+
None
859861
)
860862
}
861863

src/librustc/metadata/encoder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,7 @@ fn encode_info_for_method(ecx: &EncodeContext,
772772
let method_def_id = local_def(m.id);
773773
let method_ty = ty::method(ecx.tcx, method_def_id);
774774
encode_method_ty_fields(ecx, ebml_w, method_ty);
775+
encode_parent_item(ebml_w, local_def(parent_id));
775776

776777
match m.explicit_self.node {
777778
ast::sty_static => {

src/librustc/middle/privacy.rs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -106,29 +106,6 @@ pub fn check_crate<'mm>(tcx: ty::ctxt,
106106
}
107107
};
108108

109-
// Returns the ID of the container (impl or trait) that a crate-local
110-
// method belongs to.
111-
let local_method_container_id:
112-
@fn(span: span, method_id: node_id) -> def_id =
113-
|span, method_id| {
114-
match tcx.items.find(&method_id) {
115-
Some(&node_method(_, impl_id, _)) => impl_id,
116-
Some(&node_trait_method(_, trait_id, _)) => trait_id,
117-
Some(_) => {
118-
tcx.sess.span_bug(span,
119-
fmt!("method was a %s?!",
120-
ast_map::node_id_to_str(
121-
tcx.items,
122-
method_id,
123-
token::get_ident_interner())));
124-
}
125-
None => {
126-
tcx.sess.span_bug(span, "method not found in \
127-
AST map?!");
128-
}
129-
}
130-
};
131-
132109
// Returns true if a crate-local method is private and false otherwise.
133110
let method_is_private: @fn(span: span, method_id: node_id) -> bool =
134111
|span, method_id| {
@@ -248,15 +225,12 @@ pub fn check_crate<'mm>(tcx: ty::ctxt,
248225
// If the method is a default method, we need to use the def_id of
249226
// the default implementation.
250227
// Having to do this this is really unfortunate.
251-
let method_id = match tcx.provided_method_sources.find(&method_id) {
252-
None => method_id,
253-
Some(source) => source.method_id
254-
};
228+
let method_id = ty::method(tcx, method_id).provided_source
229+
.get_or_default(method_id);
255230

256231
if method_id.crate == local_crate {
257232
let is_private = method_is_private(span, method_id.node);
258-
let container_id = local_method_container_id(span,
259-
method_id.node);
233+
let container_id = ty::method(tcx, method_id).container_id;
260234
if is_private &&
261235
(container_id.crate != local_crate ||
262236
!privileged_items.iter().any(|x| x == &(container_id.node))) {

src/librustc/middle/trans/base.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,6 @@ pub fn get_res_dtor(ccx: @mut CrateContext,
524524
&tsubsts,
525525
None,
526526
None,
527-
None,
528527
None);
529528

530529
val

src/librustc/middle/trans/callee.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,10 @@ pub fn trans_fn_ref_with_vtables(
296296
// We need to do a bunch of special handling for default methods.
297297
// We need to modify the def_id and our substs in order to monomorphize
298298
// the function.
299-
let (def_id, opt_impl_did, substs, self_vtable, vtables) =
300-
match tcx.provided_method_sources.find(&def_id) {
301-
None => (def_id, None, substs, None, vtables),
302-
Some(source) => {
299+
let (is_default, def_id, substs, self_vtable, vtables) =
300+
match ty::provided_source(tcx, def_id) {
301+
None => (false, def_id, substs, None, vtables),
302+
Some(source_id) => {
303303
// There are two relevant substitutions when compiling
304304
// default methods. First, there is the substitution for
305305
// the type parameters of the impl we are using and the
@@ -313,10 +313,11 @@ pub fn trans_fn_ref_with_vtables(
313313
// So, what we need to do is find this substitution and
314314
// compose it with the one we already have.
315315

316-
let trait_ref = ty::impl_trait_ref(tcx, source.impl_id)
316+
let impl_id = ty::method(tcx, def_id).container_id;
317+
let method = ty::method(tcx, source_id);
318+
let trait_ref = ty::impl_trait_ref(tcx, impl_id)
317319
.expect("could not find trait_ref for impl with \
318320
default methods");
319-
let method = ty::method(tcx, source.method_id);
320321

321322
// Get all of the type params for the receiver
322323
let param_defs = method.generics.type_param_defs;
@@ -330,18 +331,18 @@ pub fn trans_fn_ref_with_vtables(
330331
};
331332

332333
let self_vtable =
333-
typeck::vtable_static(source.impl_id, receiver_substs,
334+
typeck::vtable_static(impl_id, receiver_substs,
334335
receiver_vtables);
335336
// Compute the first substitution
336337
let first_subst = make_substs_for_receiver_types(
337-
tcx, source.impl_id, trait_ref, method);
338+
tcx, impl_id, trait_ref, method);
338339

339340
// And compose them
340341
let new_substs = first_subst.subst(tcx, &substs);
341342

342343

343344
let vtables =
344-
resolve_default_method_vtables(bcx, source.impl_id,
345+
resolve_default_method_vtables(bcx, impl_id,
345346
method, &new_substs, vtables);
346347

347348
debug!("trans_fn_with_vtables - default method: \
@@ -352,7 +353,7 @@ pub fn trans_fn_ref_with_vtables(
352353
first_subst.repr(tcx), new_substs.repr(tcx),
353354
self_vtable.repr(tcx), vtables.repr(tcx));
354355

355-
(source.method_id, Some(source.impl_id),
356+
(true, source_id,
356357
new_substs, Some(self_vtable), Some(vtables))
357358
}
358359
};
@@ -372,7 +373,7 @@ pub fn trans_fn_ref_with_vtables(
372373
// intrinsic that is inlined from a different crate, we want to reemit the
373374
// intrinsic instead of trying to call it in the other crate.
374375
let must_monomorphise;
375-
if type_params.len() > 0 || opt_impl_did.is_some() {
376+
if type_params.len() > 0 || is_default {
376377
must_monomorphise = true;
377378
} else if def_id.crate == ast::local_crate {
378379
let map_node = session::expect(
@@ -400,7 +401,7 @@ pub fn trans_fn_ref_with_vtables(
400401
let (val, must_cast) =
401402
monomorphize::monomorphic_fn(ccx, def_id, &substs,
402403
vtables, self_vtable,
403-
opt_impl_did, Some(ref_id));
404+
Some(ref_id));
404405
let mut val = val;
405406
if must_cast && ref_id != 0 {
406407
// Monotype of the REFERENCE to the function (type params

src/librustc/middle/trans/common.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,8 +938,7 @@ pub fn mono_data_classify(t: ty::t) -> MonoDataClass {
938938
#[deriving(Eq,IterBytes)]
939939
pub struct mono_id_ {
940940
def: ast::def_id,
941-
params: ~[mono_param_id],
942-
impl_did_opt: Option<ast::def_id>
941+
params: ~[mono_param_id]
943942
}
944943

945944
pub type mono_id = @mono_id_;

src/librustc/middle/trans/meth.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,6 @@ pub fn vtable_id(ccx: @mut CrateContext,
617617
monomorphize::make_mono_id(
618618
ccx,
619619
impl_id,
620-
None,
621620
&psubsts,
622621
None)
623622
}

src/librustc/middle/trans/monomorphize.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ pub fn monomorphic_fn(ccx: @mut CrateContext,
4242
real_substs: &ty::substs,
4343
vtables: Option<typeck::vtable_res>,
4444
self_vtable: Option<typeck::vtable_origin>,
45-
impl_did_opt: Option<ast::def_id>,
4645
ref_id: Option<ast::node_id>)
4746
-> (ValueRef, bool)
4847
{
@@ -51,13 +50,11 @@ pub fn monomorphic_fn(ccx: @mut CrateContext,
5150
real_substs=%s, \
5251
vtables=%s, \
5352
self_vtable=%s, \
54-
impl_did_opt=%s, \
5553
ref_id=%?)",
5654
fn_id.repr(ccx.tcx),
5755
real_substs.repr(ccx.tcx),
5856
vtables.repr(ccx.tcx),
5957
self_vtable.repr(ccx.tcx),
60-
impl_did_opt.repr(ccx.tcx),
6158
ref_id);
6259

6360
assert!(real_substs.tps.iter().all(|t| !ty::type_needs_infer(*t)));
@@ -83,9 +80,7 @@ pub fn monomorphic_fn(ccx: @mut CrateContext,
8380
let param_uses = type_use::type_uses_for(ccx, fn_id, psubsts.tys.len());
8481

8582

86-
let hash_id = make_mono_id(ccx, fn_id, impl_did_opt,
87-
&*psubsts,
88-
Some(param_uses));
83+
let hash_id = make_mono_id(ccx, fn_id, &*psubsts, Some(param_uses));
8984
if hash_id.params.iter().any(
9085
|p| match *p { mono_precise(_, _) => false, _ => true }) {
9186
must_cast = true;
@@ -367,7 +362,6 @@ pub fn normalize_for_monomorphization(tcx: ty::ctxt,
367362

368363
pub fn make_mono_id(ccx: @mut CrateContext,
369364
item: ast::def_id,
370-
impl_did_opt: Option<ast::def_id>,
371365
substs: &param_substs,
372366
param_uses: Option<@~[type_use::type_uses]>) -> mono_id {
373367
// FIXME (possibly #5801): Need a lot of type hints to get
@@ -442,5 +436,5 @@ pub fn make_mono_id(ccx: @mut CrateContext,
442436
}).collect()
443437
}
444438
};
445-
@mono_id_ {def: item, params: param_ids, impl_did_opt: impl_did_opt}
439+
@mono_id_ {def: item, params: param_ids}
446440
}

src/librustc/middle/trans/type_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub fn type_uses_for(ccx: @mut CrateContext, fn_id: def_id, n_tps: uint)
9090
// used. This is imprecise, but simple. Getting it right is
9191
// tricky because the substs on the call and the substs on the
9292
// default method differ, because of substs on the trait/impl.
93-
let is_default = ccx.tcx.provided_method_sources.contains_key(&fn_id_loc);
93+
let is_default = ty::provided_source(ccx.tcx, fn_id_loc).is_some();
9494
// We also mark all of the params as used if it is an extern thing
9595
// that we haven't been able to inline yet.
9696
if is_default || fn_id_loc.crate != local_crate {

src/librustc/middle/ty.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ pub struct Method {
6464
fty: BareFnTy,
6565
explicit_self: ast::explicit_self_,
6666
vis: ast::visibility,
67-
def_id: ast::def_id
67+
def_id: ast::def_id,
68+
container_id: ast::def_id,
69+
70+
// If this method is provided, we need to know where it came from
71+
provided_source: Option<ast::def_id>
6872
}
6973

7074
impl Method {
@@ -74,7 +78,9 @@ impl Method {
7478
fty: BareFnTy,
7579
explicit_self: ast::explicit_self_,
7680
vis: ast::visibility,
77-
def_id: ast::def_id)
81+
def_id: ast::def_id,
82+
container_id: ast::def_id,
83+
provided_source: Option<ast::def_id>)
7884
-> Method {
7985
// Check the invariants.
8086
if explicit_self == ast::sty_static {
@@ -90,7 +96,9 @@ impl Method {
9096
fty: fty,
9197
explicit_self: explicit_self,
9298
vis: vis,
93-
def_id: def_id
99+
def_id: def_id,
100+
container_id: container_id,
101+
provided_source: provided_source
94102
}
95103
}
96104
}
@@ -219,11 +227,6 @@ pub enum AutoRef {
219227
AutoUnsafe(ast::mutability)
220228
}
221229

222-
pub struct ProvidedMethodSource {
223-
method_id: ast::def_id,
224-
impl_id: ast::def_id
225-
}
226-
227230
pub type ctxt = @ctxt_;
228231

229232
struct ctxt_ {
@@ -278,7 +281,7 @@ struct ctxt_ {
278281
normalized_cache: @mut HashMap<t, t>,
279282
lang_items: middle::lang_items::LanguageItems,
280283
// A mapping of fake provided method def_ids to the default implementation
281-
provided_method_sources: @mut HashMap<ast::def_id, ProvidedMethodSource>,
284+
provided_method_sources: @mut HashMap<ast::def_id, ast::def_id>,
282285
supertraits: @mut HashMap<ast::def_id, @~[@TraitRef]>,
283286

284287
// A mapping from the def ID of an enum or struct type to the def ID
@@ -3511,6 +3514,11 @@ pub fn def_has_ty_params(def: ast::def) -> bool {
35113514
}
35123515
}
35133516

3517+
pub fn provided_source(cx: ctxt, id: ast::def_id)
3518+
-> Option<ast::def_id> {
3519+
cx.provided_method_sources.find(&id).map(|x| **x)
3520+
}
3521+
35143522
pub fn provided_trait_methods(cx: ctxt, id: ast::def_id) -> ~[@Method] {
35153523
if is_local(id) {
35163524
match cx.items.find(&id.node) {

0 commit comments

Comments
 (0)