Skip to content

Commit c71306b

Browse files
committed
Explicitly store self_ids use for self locals in methods
This makes it possible to move them between crates without confusion, and to instantiate them at a point where the monomorphizing substitutions are known. Issue #1944
1 parent fd465f9 commit c71306b

File tree

14 files changed

+62
-42
lines changed

14 files changed

+62
-42
lines changed

src/rustc/metadata/astencode.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,13 @@ fn visit_ids(item: ast::inlined_item, vfn: fn@(ast::node_id)) {
191191

192192
alt fk {
193193
visit::fk_item_fn(_, tps) |
194-
visit::fk_method(_, tps) |
195194
visit::fk_res(_, tps) {
196195
vec::iter(tps) {|tp| vfn(tp.id)}
197196
}
197+
visit::fk_method(_, tps, m) {
198+
vfn(m.self_id);
199+
vec::iter(tps) {|tp| vfn(tp.id)}
200+
}
198201
visit::fk_anon(_) |
199202
visit::fk_fn_block {
200203
}

src/rustc/metadata/astencode_gen.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3938,7 +3938,7 @@ fn serialize_163<S: std::serialization::serializer>(s: S,
39383938
s.emit_rec(/*syntax::ast::ident*//*[syntax::ast::attribute]*/
39393939
/*[syntax::ast::ty_param]*//*syntax::ast::fn_decl*/
39403940
/*syntax::ast::blk*//*syntax::ast::node_id*/
3941-
/*syntax::codemap::span*/
3941+
/*syntax::codemap::span*//*syntax::ast::node_id*/
39423942
{||
39433943
{
39443944
s.emit_rec_field("ident", 0u,
@@ -3954,7 +3954,9 @@ fn serialize_163<S: std::serialization::serializer>(s: S,
39543954
s.emit_rec_field("id", 5u,
39553955
{|| serialize_27(s, v.id) });
39563956
s.emit_rec_field("span", 6u,
3957-
{|| serialize_19(s, v.span) })
3957+
{|| serialize_19(s, v.span) });
3958+
s.emit_rec_field("self_id", 7u,
3959+
{|| serialize_27(s, v.self_id) })
39583960
}
39593961
});
39603962
}
@@ -7845,6 +7847,8 @@ fn deserialize_163<S: std::serialization::deserializer>(s: S) ->
78457847

78467848
/*syntax::codemap::span*/
78477849

7850+
/*syntax::ast::node_id*/
7851+
78487852
{||
78497853
{ident:
78507854
s.read_rec_field("ident", 0u, {|| deserialize_1(s) }),
@@ -7858,8 +7862,10 @@ fn deserialize_163<S: std::serialization::deserializer>(s: S) ->
78587862
s.read_rec_field("body", 4u, {|| deserialize_82(s) }),
78597863
id: s.read_rec_field("id", 5u, {|| deserialize_27(s) }),
78607864
span:
7861-
s.read_rec_field("span", 6u,
7862-
{|| deserialize_19(s) }),}
7865+
s.read_rec_field("span", 6u, {|| deserialize_19(s) }),
7866+
self_id:
7867+
s.read_rec_field("self_id", 7u,
7868+
{|| deserialize_27(s) }),}
78637869
})
78647870

78657871
}
@@ -8165,7 +8171,7 @@ fn serialize_164<S: std::serialization::serializer>(s: S,
81658171

81668172
s.emit_enum("syntax::ast::def",
81678173
/*syntax::ast::def_id*//*syntax::ast::purity*/
8168-
/*syntax::ast::node_id*/
8174+
/*syntax::ast::def_id*/
81698175
/*syntax::ast::def_id*/
81708176
/*syntax::ast::def_id*/
81718177
/*syntax::ast::def_id*/
@@ -8528,7 +8534,7 @@ fn deserialize_164<S: std::serialization::deserializer>(s: S) ->
85288534
s.read_enum("syntax::ast::def",
85298535
/*syntax::ast::def_id*//*syntax::ast::purity*/
85308536

8531-
/*syntax::ast::node_id*/
8537+
/*syntax::ast::def_id*/
85328538

85338539
/*syntax::ast::def_id*/
85348540

src/rustc/metadata/encoder.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,11 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::writer, item: @item,
442442
ebml_w.end_tag();
443443
}
444444
alt ifce {
445-
some(_) {
445+
some(t) {
446446
encode_symbol(ecx, ebml_w, item.id);
447-
let i_ty = ty::lookup_item_type(tcx, local_def(item.id)).ty;
447+
let i_ty = alt check t.node {
448+
ty_path(_, id) { ty::node_id_to_type(tcx, id) }
449+
};
448450
ebml_w.start_tag(tag_impl_iface);
449451
write_type(ecx, ebml_w, i_ty);
450452
ebml_w.end_tag();

src/rustc/middle/ast_map.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ fn map_arm(arm: arm, cx: ctx, v: vt) {
132132
fn map_method(impl_did: def_id, impl_path: @path,
133133
m: @method, cx: ctx) {
134134
cx.map.insert(m.id, node_method(m, impl_did, impl_path));
135+
cx.map.insert(m.self_id, node_local(cx.local_id));
136+
cx.local_id += 1u;
135137
}
136138

137139
fn map_item(i: @item, cx: ctx, v: vt) {

src/rustc/middle/resolve.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,8 @@ fn visit_item_with_scope(e: @env, i: @ast::item, sc: scopes, v: vt<scopes>) {
530530
v.visit_ty(sty, sc, v);
531531
for m in methods {
532532
v.visit_ty_params(m.tps, sc, v);
533-
let msc = cons(scope_method(i.id, tps + m.tps), @sc);
534-
v.visit_fn(visit::fk_method(m.ident, []),
533+
let msc = cons(scope_method(m.self_id, tps + m.tps), @sc);
534+
v.visit_fn(visit::fk_method(m.ident, [], m),
535535
m.decl, m.body, m.span, m.id, msc, v);
536536
}
537537
}
@@ -590,7 +590,7 @@ fn visit_fn_with_scope(e: @env, fk: visit::fn_kind, decl: ast::fn_decl,
590590
for c: @ast::constr in decl.constraints { resolve_constr(e, c, sc, v); }
591591
let scope = alt fk {
592592
visit::fk_item_fn(_, tps) | visit::fk_res(_, tps) |
593-
visit::fk_method(_, tps) { scope_bare_fn(decl, id, tps) }
593+
visit::fk_method(_, tps, _) { scope_bare_fn(decl, id, tps) }
594594
visit::fk_anon(ast::proto_bare) { scope_bare_fn(decl, id, []) }
595595
visit::fk_anon(_) | visit::fk_fn_block { scope_fn_expr(decl, id, []) }
596596
};

src/rustc/middle/trans/base.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,7 +2136,7 @@ fn monomorphic_fn(ccx: crate_ctxt, fn_id: ast::def_id, substs: [ty::t],
21362136
(*tvs).len() == 1u, [], psubsts, lldecl);
21372137
}
21382138
ast_map::node_method(mth, impl_def_id, _) {
2139-
let selfty = ty::lookup_item_type(ccx.tcx, impl_def_id).ty;
2139+
let selfty = ty::node_id_to_type(ccx.tcx, mth.self_id);
21402140
let selfty = ty::substitute_type_params(ccx.tcx, substs, selfty);
21412141
trans_fn(ccx, pt, mth.decl, mth.body, lldecl,
21422142
impl_self(selfty), [], psubsts, fn_id.node, none);
@@ -2315,7 +2315,7 @@ fn trans_local_var(cx: block, def: ast::def) -> local_var_result {
23152315
assert (cx.fcx.lllocals.contains_key(nid));
23162316
ret take_local(cx.fcx.lllocals, nid);
23172317
}
2318-
ast::def_self(nid) {
2318+
ast::def_self(_) {
23192319
let slf = option::get(cx.fcx.llself);
23202320
let ptr = PointerCast(cx, slf.v,
23212321
T_ptr(type_of_or_i8(cx.ccx(), slf.t)));
@@ -4343,7 +4343,7 @@ fn trans_item(ccx: crate_ctxt, item: ast::item) {
43434343
}
43444344
}
43454345
ast::item_impl(tps, _, _, ms) {
4346-
impl::trans_impl(ccx, *path, item.ident, ms, item.id, tps);
4346+
impl::trans_impl(ccx, *path, item.ident, ms, tps);
43474347
}
43484348
ast::item_res(decl, tps, body, dtor_id, ctor_id) {
43494349
let llctor_decl = get_item_val(ccx, ctor_id);

src/rustc/middle/trans/impl.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,13 @@ import std::map::hashmap;
4444
// are referenced (ccx.method_map and ccx.dict_map).
4545

4646
fn trans_impl(ccx: crate_ctxt, path: path, name: ast::ident,
47-
methods: [@ast::method], id: ast::node_id,
48-
tps: [ast::ty_param]) {
47+
methods: [@ast::method], tps: [ast::ty_param]) {
4948
let sub_path = path + [path_name(name)];
5049
for m in methods {
5150
let llfn = get_item_val(ccx, m.id);
5251
let m_bounds = param_bounds(ccx, tps + m.tps);
5352
trans_fn(ccx, sub_path + [path_name(m.ident)], m.decl, m.body,
54-
llfn, impl_self(ty::node_id_to_type(ccx.tcx, id)),
53+
llfn, impl_self(ty::node_id_to_type(ccx.tcx, m.self_id)),
5554
m_bounds, none, m.id, none);
5655
}
5756
}

src/rustc/middle/ty.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,8 +2157,13 @@ fn iface_methods(cx: ctxt, id: ast::def_id) -> @[method] {
21572157

21582158
fn impl_iface(cx: ctxt, id: ast::def_id) -> option<t> {
21592159
if id.crate == ast::local_crate {
2160-
let t = cx.tcache.get(id).ty;
2161-
if get(t).struct == ty_nil { none } else { some(t) }
2160+
alt cx.items.get(id.node) {
2161+
ast_map::node_item(@{node: ast::item_impl(
2162+
_, some(@{node: ast::ty_path(_, id), _}), _, _), _}, _) {
2163+
some(node_id_to_type(cx, id))
2164+
}
2165+
_ { none }
2166+
}
21622167
} else {
21632168
csearch::get_impl_iface(cx, id)
21642169
}

src/rustc/middle/typeck.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn ty_param_bounds_and_ty_for_def(fcx: @fn_ctxt, sp: span, defn: ast::def) ->
106106
let typ = ty::mk_var(fcx.ccx.tcx, lookup_local(fcx, sp, nid));
107107
ret {bounds: @[], ty: typ};
108108
}
109-
ast::def_self(id) {
109+
ast::def_self(_) {
110110
alt get_self_info(fcx.ccx) {
111111
some(self_impl(impl_t)) {
112112
ret {bounds: @[], ty: impl_t};
@@ -362,8 +362,8 @@ fn ast_ty_to_ty(tcx: ty::ctxt, mode: mode, &&ast_ty: @ast::ty) -> ty::t {
362362
}
363363
ty::mk_param(tcx, n, id)
364364
}
365-
ast::def_self(iface_id) {
366-
alt check tcx.items.get(iface_id) {
365+
ast::def_self(self_id) {
366+
alt check tcx.items.get(self_id) {
367367
ast_map::node_item(@{node: ast::item_iface(tps, _), _}, _) {
368368
if vec::len(tps) != vec::len(path.node.types) {
369369
tcx.sess.span_err(ast_ty.span, "incorrect number of type \
@@ -850,7 +850,12 @@ mod collect {
850850
ast::item_impl(tps, ifce, selfty, ms) {
851851
let i_bounds = ty_param_bounds(tcx, m_collect, tps);
852852
let my_methods = [];
853+
let selfty = ast_ty_to_ty(tcx, m_collect, selfty);
854+
write_ty(tcx, it.id, selfty);
855+
tcx.tcache.insert(local_def(it.id), {bounds: i_bounds,
856+
ty: selfty});
853857
for m in ms {
858+
write_ty(tcx, m.self_id, selfty);
854859
let bounds = ty_param_bounds(tcx, m_collect, m.tps);
855860
let mty = ty_of_method(tcx, m_collect, m);
856861
my_methods += [{mty: mty, id: m.id, span: m.span}];
@@ -860,15 +865,15 @@ mod collect {
860865
ty: fty});
861866
write_ty(tcx, m.id, fty);
862867
}
863-
let selfty = ast_ty_to_ty(tcx, m_collect, selfty);
864-
write_ty(tcx, it.id, selfty);
865868
alt ifce {
866869
some(t) {
867870
let iface_ty = ast_ty_to_ty(tcx, m_collect, t);
868-
tcx.tcache.insert(local_def(it.id),
869-
{bounds: i_bounds, ty: iface_ty});
870871
alt ty::get(iface_ty).struct {
871872
ty::ty_iface(did, tys) {
873+
// Store the iface type in the type node
874+
alt check t.node {
875+
ast::ty_path(_, t_id) { write_ty(tcx, t_id, iface_ty); }
876+
}
872877
if did.crate == ast::local_crate {
873878
ensure_iface_methods(tcx, did.node);
874879
}
@@ -906,11 +911,7 @@ mod collect {
906911
}
907912
}
908913
}
909-
_ {
910-
// Store the bounds with a nil type.
911-
tcx.tcache.insert(local_def(it.id), {bounds: i_bounds,
912-
ty: ty::mk_nil(tcx)});
913-
}
914+
_ {}
914915
}
915916
}
916917
ast::item_res(decl, tps, _, dtor_id, ctor_id) {
@@ -1665,8 +1666,8 @@ fn impl_self_ty(tcx: ty::ctxt, did: ast::def_id) -> {n_tps: uint, ty: ty::t} {
16651666
}
16661667
}
16671668
} else {
1668-
let tpt = csearch::get_type(tcx, did);
1669-
{n_tps: vec::len(*tpt.bounds), ty: tpt.ty}
1669+
let ity = ty::lookup_item_type(tcx, did);
1670+
{n_tps: vec::len(*ity.bounds), ty: ity.ty}
16701671
}
16711672
}
16721673

src/rustc/syntax/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ enum ret_style {
424424

425425
type method = {ident: ident, attrs: [attribute],
426426
tps: [ty_param], decl: fn_decl, body: blk,
427-
id: node_id, span: span};
427+
id: node_id, span: span, self_id: node_id};
428428

429429
type _mod = {view_items: [@view_item], items: [@item]};
430430

src/rustc/syntax/ast_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn def_id_of_def(d: def) -> def_id {
4141
def_use(id) |
4242
def_class(id) | def_class_field(_, id) | def_class_method(_, id) { id }
4343

44-
def_self(id) | def_arg(id, _) | def_local(id, _) |
44+
def_arg(id, _) | def_local(id, _) | def_self(id) |
4545
def_upvar(id, _, _) | def_binding(id) {
4646
local_def(id)
4747
}

src/rustc/syntax/fold.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ fn noop_fold_method(&&m: @method, fld: ast_fold) -> @method {
301301
decl: fold_fn_decl(m.decl, fld),
302302
body: fld.fold_block(m.body),
303303
id: fld.new_id(m.id),
304-
span: fld.new_span(m.span)};
304+
span: fld.new_span(m.span),
305+
self_id: fld.new_id(m.self_id)};
305306
}
306307

307308

src/rustc/syntax/parse/parser.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1931,7 +1931,8 @@ fn parse_method(p: parser) -> @ast::method {
19311931
let (inner_attrs, body) = parse_inner_attrs_and_block(p, true);
19321932
let attrs = attrs + inner_attrs;
19331933
@{ident: ident, attrs: attrs, tps: tps, decl: decl, body: body,
1934-
id: p.get_id(), span: ast_util::mk_sp(lo, body.span.hi)}
1934+
id: p.get_id(), span: ast_util::mk_sp(lo, body.span.hi),
1935+
self_id: p.get_id()}
19351936
}
19361937

19371938
fn parse_item_iface(p: parser, attrs: [ast::attribute]) -> @ast::item {

src/rustc/syntax/visit.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ enum vt<E> { mk_vt(visitor<E>), }
1414

1515
enum fn_kind {
1616
fk_item_fn(ident, [ty_param]), //< an item declared with fn()
17-
fk_method(ident, [ty_param]),
17+
fk_method(ident, [ty_param], @method),
1818
fk_res(ident, [ty_param]),
1919
fk_anon(proto), //< an anonymous function like fn@(...)
2020
fk_fn_block, //< a block {||...}
2121
}
2222

2323
fn name_of_fn(fk: fn_kind) -> ident {
2424
alt fk {
25-
fk_item_fn(name, _) | fk_method(name, _) | fk_res(name, _) { name }
25+
fk_item_fn(name, _) | fk_method(name, _, _) | fk_res(name, _) { name }
2626
fk_anon(_) | fk_fn_block { "anon" }
2727
}
2828
}
2929

3030
fn tps_of_fn(fk: fn_kind) -> [ty_param] {
3131
alt fk {
32-
fk_item_fn(_, tps) | fk_method(_, tps) | fk_res(_, tps) { tps }
32+
fk_item_fn(_, tps) | fk_method(_, tps, _) | fk_res(_, tps) { tps }
3333
fk_anon(_) | fk_fn_block { [] }
3434
}
3535
}
@@ -256,7 +256,7 @@ fn visit_fn_decl<E>(fd: fn_decl, e: E, v: vt<E>) {
256256
// because it is not a default impl of any method, though I doubt that really
257257
// clarifies anything. - Niko
258258
fn visit_method_helper<E>(m: @method, e: E, v: vt<E>) {
259-
v.visit_fn(fk_method(m.ident, m.tps), m.decl, m.body, m.span,
259+
v.visit_fn(fk_method(m.ident, m.tps, m), m.decl, m.body, m.span,
260260
m.id, e, v);
261261
}
262262

0 commit comments

Comments
 (0)