Skip to content

Commit c202430

Browse files
committed
auto merge of #5435 : nikomatsakis/rust/issue-4846-refactor-self_info, r=nikomatsakis
Refactor the self-info so that the def-id is carried in ty_self()and the fn_ctxt doesn't need any self_info field at all. Pull out explicit self transformation into `check_method`. Step towards fixing `fn(&self)` to have a distinct lifetime. (cc #4846) r? @catamorphism
2 parents db4dc1f + ad70c74 commit c202430

File tree

13 files changed

+53
-86
lines changed

13 files changed

+53
-86
lines changed

src/librustc/metadata/tydecode.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ fn parse_ty(st: @mut PState, conv: conv_did) -> ty::t {
289289
return ty::mk_param(st.tcx, parse_int(st) as uint, did);
290290
}
291291
's' => {
292-
return ty::mk_self(st.tcx);
292+
let did = parse_def(st, TypeParameter, conv);
293+
return ty::mk_self(st.tcx, did);
293294
}
294295
'@' => return ty::mk_box(st.tcx, parse_mt(st, conv)),
295296
'~' => return ty::mk_uniq(st.tcx, parse_mt(st, conv)),

src/librustc/metadata/tyencode.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,10 @@ fn enc_sty(w: @io::Writer, cx: @ctxt, +st: ty::sty) {
312312
w.write_char('|');
313313
w.write_str(uint::to_str(id));
314314
}
315-
ty::ty_self => {
315+
ty::ty_self(did) => {
316316
w.write_char('s');
317+
w.write_str((cx.ds)(did));
318+
w.write_char('|');
317319
}
318320
ty::ty_type => w.write_char('Y'),
319321
ty::ty_opaque_closure_ptr(p) => {

src/librustc/middle/trans/reflect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ pub impl Reflector {
297297
let extra = ~[self.c_uint(p.idx)];
298298
self.visit(~"param", extra)
299299
}
300-
ty::ty_self => self.leaf(~"self"),
300+
ty::ty_self(*) => self.leaf(~"self"),
301301
ty::ty_type => self.leaf(~"type"),
302302
ty::ty_opaque_box => self.leaf(~"opaque_box"),
303303
ty::ty_opaque_closure_ptr(ck) => {

src/librustc/middle/trans/type_of.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ pub fn sizing_type_of(cx: @CrateContext, t: ty::t) -> TypeRef {
144144
T_struct(adt::sizing_fields_of(cx, repr))
145145
}
146146

147-
ty::ty_self | ty::ty_infer(*) | ty::ty_param(*) | ty::ty_err(*) => {
147+
ty::ty_self(_) | ty::ty_infer(*) | ty::ty_param(*) | ty::ty_err(*) => {
148148
cx.tcx.sess.bug(
149149
fmt!("fictitious type %? in sizing_type_of()",
150150
ty::get(t).sty))
@@ -251,7 +251,7 @@ pub fn type_of(cx: @CrateContext, t: ty::t) -> TypeRef {
251251
did,
252252
/*bad*/ copy substs.tps))
253253
}
254-
ty::ty_self => cx.tcx.sess.unimpl(~"type_of: ty_self"),
254+
ty::ty_self(*) => cx.tcx.sess.unimpl(~"type_of: ty_self"),
255255
ty::ty_infer(*) => cx.tcx.sess.bug(~"type_of with ty_infer"),
256256
ty::ty_param(*) => cx.tcx.sess.bug(~"type_of with ty_param"),
257257
ty::ty_err(*) => cx.tcx.sess.bug(~"type_of with ty_err")

src/librustc/middle/ty.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,8 @@ pub enum sty {
522522
ty_tup(~[t]),
523523

524524
ty_param(param_ty), // type parameter
525-
ty_self, // special, implicit `self` type parameter
525+
ty_self(def_id), /* special, implicit `self` type parameter;
526+
* def_id is the id of the trait */
526527

527528
ty_infer(InferTy), // something used only during inference/typeck
528529
ty_err, // Also only used during inference/typeck, to represent
@@ -897,7 +898,7 @@ fn mk_t_with_id(cx: ctxt, +st: sty, o_def_id: Option<ast::def_id>) -> t {
897898
&ty_err => flags |= has_ty_err as uint,
898899
&ty_param(_) => flags |= has_params as uint,
899900
&ty_infer(_) => flags |= needs_infer as uint,
900-
&ty_self => flags |= has_self as uint,
901+
&ty_self(_) => flags |= has_self as uint,
901902
&ty_enum(_, ref substs) | &ty_struct(_, ref substs) |
902903
&ty_trait(_, ref substs, _) => {
903904
flags |= sflags(substs);
@@ -1082,7 +1083,7 @@ pub fn mk_float_var(cx: ctxt, v: FloatVid) -> t { mk_infer(cx, FloatVar(v)) }
10821083

10831084
pub fn mk_infer(cx: ctxt, +it: InferTy) -> t { mk_t(cx, ty_infer(it)) }
10841085

1085-
pub fn mk_self(cx: ctxt) -> t { mk_t(cx, ty_self) }
1086+
pub fn mk_self(cx: ctxt, did: ast::def_id) -> t { mk_t(cx, ty_self(did)) }
10861087

10871088
pub fn mk_param(cx: ctxt, n: uint, k: def_id) -> t {
10881089
mk_t(cx, ty_param(param_ty { idx: n, def_id: k }))
@@ -1163,7 +1164,7 @@ pub fn maybe_walk_ty(ty: t, f: &fn(t) -> bool) {
11631164
if !f(ty) { return; }
11641165
match get(ty).sty {
11651166
ty_nil | ty_bot | ty_bool | ty_int(_) | ty_uint(_) | ty_float(_) |
1166-
ty_estr(_) | ty_type | ty_opaque_box | ty_self |
1167+
ty_estr(_) | ty_type | ty_opaque_box | ty_self(_) |
11671168
ty_opaque_closure_ptr(_) | ty_infer(_) | ty_param(_) | ty_err => {
11681169
}
11691170
ty_box(ref tm) | ty_evec(ref tm, _) | ty_unboxed_vec(ref tm) |
@@ -1250,7 +1251,7 @@ fn fold_sty(sty: &sty, fldop: &fn(t) -> t) -> sty {
12501251
}
12511252
ty_nil | ty_bot | ty_bool | ty_int(_) | ty_uint(_) | ty_float(_) |
12521253
ty_estr(_) | ty_type | ty_opaque_closure_ptr(_) | ty_err |
1253-
ty_opaque_box | ty_infer(_) | ty_param(*) | ty_self => {
1254+
ty_opaque_box | ty_infer(_) | ty_param(*) | ty_self(_) => {
12541255
/*bad*/copy *sty
12551256
}
12561257
}
@@ -1362,7 +1363,7 @@ pub fn subst_tps(cx: ctxt, tps: &[t], self_ty_opt: Option<t>, typ: t) -> t {
13621363
if self_ty_opt.is_none() && !tbox_has_flag(tb, has_params) { return typ; }
13631364
match tb.sty {
13641365
ty_param(p) => tps[p.idx],
1365-
ty_self => {
1366+
ty_self(_) => {
13661367
match self_ty_opt {
13671368
None => cx.sess.bug(~"ty_self unexpected here"),
13681369
Some(self_ty) => {
@@ -1424,7 +1425,7 @@ pub fn subst(cx: ctxt,
14241425
if !tbox_has_flag(tb, needs_subst) { return typ; }
14251426
match tb.sty {
14261427
ty_param(p) => substs.tps[p.idx],
1427-
ty_self => substs.self_ty.get(),
1428+
ty_self(_) => substs.self_ty.get(),
14281429
_ => {
14291430
fold_regions_and_ty(
14301431
cx, typ,
@@ -2002,7 +2003,7 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
20022003
cx, cx.ty_param_bounds.get(&p.def_id.node))
20032004
}
20042005
2005-
ty_self => {
2006+
ty_self(_) => {
20062007
// Currently, self is not bounded, so we must assume the
20072008
// worst. But in the future we should examine the super
20082009
// traits.
@@ -2159,7 +2160,7 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
21592160
v.args.foldl(0, |s, a| *s + type_size(cx, *a))))
21602161
}
21612162
2162-
ty_param(_) | ty_self => {
2163+
ty_param(_) | ty_self(_) => {
21632164
1
21642165
}
21652166
@@ -2220,7 +2221,7 @@ pub fn is_instantiable(cx: ctxt, r_ty: t) -> bool {
22202221
ty_infer(_) |
22212222
ty_err |
22222223
ty_param(_) |
2223-
ty_self |
2224+
ty_self(_) |
22242225
ty_type |
22252226
ty_opaque_box |
22262227
ty_opaque_closure_ptr(_) |
@@ -2655,7 +2656,7 @@ impl to_bytes::IterBytes for sty {
26552656
ty_bare_fn(ref ft) =>
26562657
to_bytes::iter_bytes_2(&12u8, ft, lsb0, f),
26572658
2658-
ty_self => 13u8.iter_bytes(lsb0, f),
2659+
ty_self(ref did) => to_bytes::iter_bytes_2(&13u8, did, lsb0, f),
26592660
26602661
ty_infer(ref v) =>
26612662
to_bytes::iter_bytes_2(&14u8, v, lsb0, f),
@@ -3341,7 +3342,7 @@ pub fn ty_sort_str(cx: ctxt, t: t) -> ~str {
33413342
ty_infer(IntVar(_)) => ~"integral variable",
33423343
ty_infer(FloatVar(_)) => ~"floating-point variable",
33433344
ty_param(_) => ~"type parameter",
3344-
ty_self => ~"self",
3345+
ty_self(_) => ~"self",
33453346
ty_err => ~"type error"
33463347
}
33473348
}

src/librustc/middle/typeck/astconv.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use middle::typeck::{CrateCtxt, write_substs_to_tcx, write_ty_to_tcx};
6464

6565
use core::result;
6666
use core::vec;
67-
use syntax::ast;
67+
use syntax::{ast, ast_util};
6868
use syntax::codemap::span;
6969
use syntax::print::pprust::{lifetime_to_str, path_to_str};
7070
use syntax::parse::token::special_idents;
@@ -400,12 +400,13 @@ pub fn ast_ty_to_ty<AC:AstConv, RS:region_scope + Copy + Durable>(
400400
check_path_args(tcx, path, NO_TPS | NO_REGIONS);
401401
ty::mk_param(tcx, n, id)
402402
}
403-
ast::def_self_ty(_) => {
403+
ast::def_self_ty(id) => {
404404
// n.b.: resolve guarantees that the self type only appears in a
405405
// trait, which we rely upon in various places when creating
406406
// substs
407407
check_path_args(tcx, path, NO_TPS | NO_REGIONS);
408-
ty::mk_self(tcx)
408+
let did = ast_util::local_def(id);
409+
ty::mk_self(tcx, did)
409410
}
410411
_ => {
411412
tcx.sess.span_fatal(ast_ty.span,

src/librustc/middle/typeck/check/method.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,9 @@ pub impl LookupContext/&self {
307307
self_ty, did, substs, store);
308308
self.push_inherent_impl_candidates_for_type(did);
309309
}
310-
ty_self => {
310+
ty_self(self_did) => {
311311
// Call is of the form "self.foo()" and appears in one
312312
// of a trait's default method implementations.
313-
let self_did = self.fcx.self_info.expect(
314-
~"self_impl_def_id is undefined (`self` may not \
315-
be in scope here").def_id;
316313
let substs = substs {
317314
self_r: None,
318315
self_ty: None,
@@ -932,7 +929,7 @@ pub impl LookupContext/&self {
932929
ty_bare_fn(*) | ty_box(*) | ty_uniq(*) | ty_rptr(*) |
933930
ty_infer(IntVar(_)) |
934931
ty_infer(FloatVar(_)) |
935-
ty_self | ty_param(*) | ty_nil | ty_bot | ty_bool |
932+
ty_self(_) | ty_param(*) | ty_nil | ty_bot | ty_bool |
936933
ty_int(*) | ty_uint(*) |
937934
ty_float(*) | ty_enum(*) | ty_ptr(*) | ty_struct(*) | ty_tup(*) |
938935
ty_estr(*) | ty_evec(*) | ty_trait(*) | ty_closure(*) => {

src/librustc/middle/typeck/check/mod.rs

+19-44
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ pub mod method;
144144
pub struct SelfInfo {
145145
self_ty: ty::t,
146146
self_id: ast::node_id,
147-
def_id: ast::def_id,
148-
explicit_self: ast::self_ty
147+
span: span
149148
}
150149

151150
/// Fields that are part of a `FnCtxt` which are inherited by
@@ -183,9 +182,6 @@ pub struct FnCtxt {
183182
// with any nested functions that capture the environment
184183
// (and with any functions whose environment is being captured).
185184

186-
// Refers to whichever `self` is in scope, even this FnCtxt is
187-
// for a nested closure that captures `self`
188-
self_info: Option<SelfInfo>,
189185
ret_ty: ty::t,
190186
// Used by loop bodies that return from the outer function
191187
indirect_ret_ty: Option<ty::t>,
@@ -236,7 +232,6 @@ pub fn blank_fn_ctxt(ccx: @mut CrateCtxt,
236232
// It's kind of a kludge to manufacture a fake function context
237233
// and statement context, but we might as well do write the code only once
238234
@mut FnCtxt {
239-
self_info: None,
240235
ret_ty: rty,
241236
indirect_ret_ty: None,
242237
purity: ast::pure_fn,
@@ -332,7 +327,6 @@ pub fn check_fn(ccx: @mut CrateCtxt,
332327
};
333328

334329
@mut FnCtxt {
335-
self_info: self_info,
336330
ret_ty: ret_ty,
337331
indirect_ret_ty: indirect_ret_ty,
338332
purity: purity,
@@ -344,25 +338,6 @@ pub fn check_fn(ccx: @mut CrateCtxt,
344338
}
345339
};
346340

347-
// Update the SelfInfo to contain an accurate self type (taking
348-
// into account explicit self).
349-
let self_info = do self_info.chain_ref |self_info| {
350-
// If the self type is sty_static, we don't have a self ty.
351-
if self_info.explicit_self.node == ast::sty_static {
352-
None
353-
} else {
354-
let in_scope_regions = fcx.in_scope_regions;
355-
let self_region = in_scope_regions.find(ty::br_self);
356-
let ty = method::transform_self_type_for_method(
357-
fcx.tcx(),
358-
self_region,
359-
self_info.self_ty,
360-
self_info.explicit_self.node,
361-
TransformTypeNormally);
362-
Some(SelfInfo { self_ty: ty,.. *self_info })
363-
}
364-
};
365-
366341
gather_locals(fcx, decl, body, arg_tys, self_info);
367342
check_block(fcx, body);
368343

@@ -500,20 +475,25 @@ pub fn check_fn(ccx: @mut CrateCtxt,
500475

501476
pub fn check_method(ccx: @mut CrateCtxt,
502477
method: @ast::method,
503-
self_ty: ty::t,
504-
self_impl_def_id: ast::def_id) {
505-
let self_info = SelfInfo {
506-
self_ty: self_ty,
507-
self_id: method.self_id,
508-
def_id: self_impl_def_id,
509-
explicit_self: method.self_ty
478+
self_ty: ty::t)
479+
{
480+
let self_info = if method.self_ty.node == ast::sty_static {None} else {
481+
let ty = method::transform_self_type_for_method(
482+
ccx.tcx,
483+
Some(ty::re_bound(ty::br_self)),
484+
self_ty,
485+
method.self_ty.node,
486+
TransformTypeNormally);
487+
Some(SelfInfo {self_ty: ty, self_id: method.self_id,
488+
span: method.self_ty.span})
510489
};
490+
511491
check_bare_fn(
512492
ccx,
513493
&method.decl,
514494
&method.body,
515495
method.id,
516-
Some(self_info)
496+
self_info
517497
);
518498
}
519499

@@ -550,11 +530,7 @@ pub fn check_struct(ccx: @mut CrateCtxt,
550530
let class_t = SelfInfo {
551531
self_ty: self_ty,
552532
self_id: dtor.node.self_id,
553-
def_id: local_def(id),
554-
explicit_self: spanned {
555-
node: ast::sty_by_ref,
556-
span: codemap::dummy_sp()
557-
}
533+
span: dtor.span,
558534
};
559535
// typecheck the dtor
560536
let dtor_dec = ast_util::dtor_dec();
@@ -594,7 +570,7 @@ pub fn check_item(ccx: @mut CrateCtxt, it: @ast::item) {
594570
*ccx.tcx.sess.str_of(it.ident), it.id, rp);
595571
let self_ty = ccx.to_ty(&rscope::type_rscope(rp), ty);
596572
for ms.each |m| {
597-
check_method(ccx, *m, self_ty, local_def(it.id));
573+
check_method(ccx, *m, self_ty);
598574
}
599575
}
600576
ast::item_trait(_, _, ref trait_methods) => {
@@ -605,7 +581,8 @@ pub fn check_item(ccx: @mut CrateCtxt, it: @ast::item) {
605581
// bodies to check.
606582
}
607583
provided(m) => {
608-
check_method(ccx, m, ty::mk_self(ccx.tcx), local_def(it.id));
584+
let self_ty = ty::mk_self(ccx.tcx, local_def(it.id));
585+
check_method(ccx, m, self_ty);
609586
}
610587
}
611588
}
@@ -1699,9 +1676,7 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
16991676
ty::determine_inherited_purity(copy fcx.purity, purity,
17001677
fn_ty.sigil);
17011678

1702-
// We inherit the same self info as the enclosing scope,
1703-
// since the function we're checking might capture `self`
1704-
check_fn(fcx.ccx, fcx.self_info, inherited_purity,
1679+
check_fn(fcx.ccx, None, inherited_purity,
17051680
&fn_ty.sig, decl, body, fn_kind,
17061681
fcx.in_scope_regions, fcx.inh);
17071682
}

src/librustc/middle/typeck/check/regionmanip.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,8 @@ pub fn replace_bound_regions_in_fn_sig(
4040

4141
let mut all_tys = ty::tys_in_fn_sig(fn_sig);
4242

43-
match self_info {
44-
Some(SelfInfo {
45-
explicit_self: codemap::spanned {
46-
node: ast::sty_region(_, m),
47-
// FIXME(#4846) ------^ Use this lifetime instead of self
48-
_}, _}) => {
49-
let region = ty::re_bound(ty::br_self);
50-
let ty = ty::mk_rptr(tcx, region,
51-
ty::mt { ty: ty::mk_self(tcx), mutbl: m });
52-
all_tys.push(ty);
53-
}
54-
_ => {}
43+
for self_info.each |self_info| {
44+
all_tys.push(self_info.self_ty);
5545
}
5646

5747
for self_ty.each |t| { all_tys.push(*t) }

src/librustc/middle/typeck/check/writeback.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ pub fn resolve_type_vars_in_fn(fcx: @mut FnCtxt,
283283
let visit = mk_visitor();
284284
(visit.visit_block)(blk, wbcx, visit);
285285
for self_info.each |self_info| {
286-
if self_info.explicit_self.node == ast::sty_static { break; }
287-
resolve_type_vars_for_node(wbcx, self_info.explicit_self.span,
286+
resolve_type_vars_for_node(wbcx,
287+
self_info.span,
288288
self_info.self_id);
289289
}
290290
for decl.inputs.each |arg| {

src/librustc/middle/typeck/coherence.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub fn get_base_type(inference_context: @mut InferCtxt,
9494

9595
ty_nil | ty_bot | ty_bool | ty_int(*) | ty_uint(*) | ty_float(*) |
9696
ty_estr(*) | ty_evec(*) | ty_bare_fn(*) | ty_closure(*) | ty_tup(*) |
97-
ty_infer(*) | ty_param(*) | ty_self | ty_type | ty_opaque_box |
97+
ty_infer(*) | ty_param(*) | ty_self(*) | ty_type | ty_opaque_box |
9898
ty_opaque_closure_ptr(*) | ty_unboxed_vec(*) | ty_err | ty_box(_) |
9999
ty_uniq(_) | ty_ptr(_) | ty_rptr(_, _) => {
100100
debug!("(getting base type) no base type; found %?",

src/librustc/util/ppaux.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ pub fn ty_to_str(cx: ctxt, typ: t) -> ~str {
443443
str::from_bytes(~[('a' as u8) + (id as u8)]))
444444
}
445445
}
446-
ty_self => ~"self",
446+
ty_self(*) => ~"self",
447447
ty_enum(did, ref substs) | ty_struct(did, ref substs) => {
448448
let path = ty::item_path(cx, did);
449449
let base = ast_map::path_to_str(path, cx.sess.intr());

0 commit comments

Comments
 (0)