Skip to content

Commit 00f97b9

Browse files
committed
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. Step towards fixing `fn(&self)` (cc #4846) to have a distinct lifetime.
1 parent db4dc1f commit 00f97b9

File tree

12 files changed

+34
-38
lines changed

12 files changed

+34
-38
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

+3-9
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,6 @@ pub struct FnCtxt {
183183
// with any nested functions that capture the environment
184184
// (and with any functions whose environment is being captured).
185185

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>,
189186
ret_ty: ty::t,
190187
// Used by loop bodies that return from the outer function
191188
indirect_ret_ty: Option<ty::t>,
@@ -236,7 +233,6 @@ pub fn blank_fn_ctxt(ccx: @mut CrateCtxt,
236233
// It's kind of a kludge to manufacture a fake function context
237234
// and statement context, but we might as well do write the code only once
238235
@mut FnCtxt {
239-
self_info: None,
240236
ret_ty: rty,
241237
indirect_ret_ty: None,
242238
purity: ast::pure_fn,
@@ -332,7 +328,6 @@ pub fn check_fn(ccx: @mut CrateCtxt,
332328
};
333329

334330
@mut FnCtxt {
335-
self_info: self_info,
336331
ret_ty: ret_ty,
337332
indirect_ret_ty: indirect_ret_ty,
338333
purity: purity,
@@ -605,7 +600,8 @@ pub fn check_item(ccx: @mut CrateCtxt, it: @ast::item) {
605600
// bodies to check.
606601
}
607602
provided(m) => {
608-
check_method(ccx, m, ty::mk_self(ccx.tcx), local_def(it.id));
603+
let self_ty = ty::mk_self(ccx.tcx, local_def(it.id));
604+
check_method(ccx, m, self_ty, local_def(it.id));
609605
}
610606
}
611607
}
@@ -1699,9 +1695,7 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
16991695
ty::determine_inherited_purity(copy fcx.purity, purity,
17001696
fn_ty.sigil);
17011697

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,
1698+
check_fn(fcx.ccx, None, inherited_purity,
17051699
&fn_ty.sig, decl, body, fn_kind,
17061700
fcx.in_scope_regions, fcx.inh);
17071701
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn replace_bound_regions_in_fn_sig(
4848
_}, _}) => {
4949
let region = ty::re_bound(ty::br_self);
5050
let ty = ty::mk_rptr(tcx, region,
51-
ty::mt { ty: ty::mk_self(tcx), mutbl: m });
51+
ty::mt { ty: ty::mk_nil(tcx), mutbl: m });
5252
all_tys.push(ty);
5353
}
5454
_ => {}

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());

src/libsyntax/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ pub enum def {
185185
/* trait */ Option<def_id>,
186186
purity),
187187
def_self(node_id, bool /* is_implicit */),
188-
def_self_ty(node_id),
188+
def_self_ty(/* trait id */ node_id),
189189
def_mod(def_id),
190190
def_foreign_mod(def_id),
191191
def_const(def_id),

0 commit comments

Comments
 (0)