Skip to content

Commit 5e7229b

Browse files
committed
reduce self type to a special type parameter
1 parent d41af13 commit 5e7229b

File tree

15 files changed

+289
-324
lines changed

15 files changed

+289
-324
lines changed

src/rustc/metadata/tydecode.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,16 @@ fn parse_vstore(st: @pstate) -> ty::vstore {
199199
fn parse_substs(st: @pstate, conv: conv_did) -> ty::substs {
200200
let self_r = parse_opt(st) {|| parse_region(st) };
201201

202+
let self_ty = parse_opt(st) {|| parse_ty(st, conv) };
203+
202204
assert next(st) == '[';
203205
let mut params: [ty::t] = [];
204206
while peek(st) != ']' { params += [parse_ty(st, conv)]; }
205207
st.pos = st.pos + 1u;
206208

207-
ret {self_r: self_r, tps: params};
209+
ret {self_r: self_r,
210+
self_ty: self_ty,
211+
tps: params};
208212
}
209213

210214
fn parse_bound_region(st: @pstate) -> ty::bound_region {
@@ -298,10 +302,7 @@ fn parse_ty(st: @pstate, conv: conv_did) -> ty::t {
298302
ret ty::mk_param(st.tcx, parse_int(st) as uint, did);
299303
}
300304
's' {
301-
assert next(st) == '[';
302-
let substs = parse_substs(st, conv);
303-
assert next(st) == ']';
304-
ret ty::mk_self(st.tcx, substs);
305+
ret ty::mk_self(st.tcx);
305306
}
306307
'@' { ret ty::mk_box(st.tcx, parse_mt(st, conv)); }
307308
'~' { ret ty::mk_uniq(st.tcx, parse_mt(st, conv)); }

src/rustc/metadata/tyencode.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ fn enc_opt<T>(w: io::writer, t: option<T>, enc_f: fn(T)) {
115115

116116
fn enc_substs(w: io::writer, cx: @ctxt, substs: ty::substs) {
117117
enc_opt(w, substs.self_r) { |r| enc_region(w, cx, r) }
118+
enc_opt(w, substs.self_ty) { |t| enc_ty(w, cx, t) }
118119
w.write_char('[');
119120
for substs.tps.each { |t| enc_ty(w, cx, t); }
120121
w.write_char(']');
@@ -281,10 +282,8 @@ fn enc_sty(w: io::writer, cx: @ctxt, st: ty::sty) {
281282
w.write_char('|');
282283
w.write_str(uint::str(id));
283284
}
284-
ty::ty_self(substs) {
285-
w.write_str("s["/&);
286-
enc_substs(w, cx, substs);
287-
w.write_char(']');
285+
ty::ty_self {
286+
w.write_char('s');
288287
}
289288
ty::ty_type { w.write_char('Y'); }
290289
ty::ty_opaque_closure_ptr(ty::ck_block) { w.write_str("C&"/&); }

src/rustc/middle/infer.rs

+51-7
Original file line numberDiff line numberDiff line change
@@ -630,12 +630,15 @@ impl methods for resolve_state {
630630
fn resolve1(typ: ty::t) -> ty::t {
631631
#debug("Resolve1(%s)", typ.to_str(self.infcx));
632632
indent(fn&() -> ty::t {
633-
if !ty::get(typ).has_vars { ret typ; }
633+
if !ty::type_needs_infer(typ) { ret typ; }
634634

635-
let tb = ty::get(typ);
636-
alt tb.struct {
637-
ty::ty_var(vid) { self.resolve_ty_var(vid) }
638-
_ if !tb.has_regions && !self.deep { typ }
635+
alt ty::get(typ).struct {
636+
ty::ty_var(vid) {
637+
self.resolve_ty_var(vid)
638+
}
639+
_ if !ty::type_has_regions(typ) && !self.deep {
640+
typ
641+
}
639642
_ {
640643
ty::fold_regions_and_ty(
641644
self.infcx.tcx, typ,
@@ -935,6 +938,7 @@ iface combine {
935938
fn contratys(a: ty::t, b: ty::t) -> cres<ty::t>;
936939
fn tys(a: ty::t, b: ty::t) -> cres<ty::t>;
937940
fn tps(as: [ty::t], bs: [ty::t]) -> cres<[ty::t]>;
941+
fn self_tys(a: option<ty::t>, b: option<ty::t>) -> cres<option<ty::t>>;
938942
fn substs(as: ty::substs, bs: ty::substs) -> cres<ty::substs>;
939943
fn fns(a: ty::fn_ty, b: ty::fn_ty) -> cres<ty::fn_ty>;
940944
fn flds(a: ty::field, b: ty::field) -> cres<ty::field>;
@@ -982,8 +986,10 @@ fn super_substs<C:combine>(
982986
}
983987

984988
self.tps(a.tps, b.tps).chain { |tps|
985-
eq_opt_regions(self.infcx(), a.self_r, b.self_r).chain { |self_r|
986-
ok({self_r: self_r, tps: tps})
989+
self.self_tys(a.self_ty, b.self_ty).chain { |self_ty|
990+
eq_opt_regions(self.infcx(), a.self_r, b.self_r).chain { |self_r|
991+
ok({self_r: self_r, self_ty: self_ty, tps: tps})
992+
}
987993
}
988994
}
989995
}
@@ -995,6 +1001,7 @@ fn super_tps<C:combine>(
9951001
// (otherwise the type system would be unsound). In the
9961002
// future we could allow type parameters to declare a
9971003
// variance.
1004+
9981005
if check vec::same_length(as, bs) {
9991006
iter2(as, bs) {|a, b| self.infcx().eq_tys(a, b) }.then {||
10001007
ok(as)
@@ -1004,6 +1011,31 @@ fn super_tps<C:combine>(
10041011
}
10051012
}
10061013

1014+
fn super_self_tys<C:combine>(
1015+
self: C, a: option<ty::t>, b: option<ty::t>) -> cres<option<ty::t>> {
1016+
1017+
// Note: the self type parameter is (currently) always treated as
1018+
// *invariant* (otherwise the type system would be unsound).
1019+
1020+
alt (a, b) {
1021+
(none, none) {
1022+
ok(none)
1023+
}
1024+
(some(a), some(b)) {
1025+
self.infcx().eq_tys(a, b).then {||
1026+
ok(some(a))
1027+
}
1028+
}
1029+
(none, some(_)) |
1030+
(some(_), none) {
1031+
// I think it should never happen that we unify two substs and
1032+
// one of them has a self_ty and one doesn't...? I could be
1033+
// wrong about this.
1034+
err(ty::terr_self_substs)
1035+
}
1036+
}
1037+
}
1038+
10071039
fn super_flds<C:combine>(
10081040
self: C, a: ty::field, b: ty::field) -> cres<ty::field> {
10091041

@@ -1374,6 +1406,10 @@ impl of combine for sub {
13741406
fn tps(as: [ty::t], bs: [ty::t]) -> cres<[ty::t]> {
13751407
super_tps(self, as, bs)
13761408
}
1409+
1410+
fn self_tys(a: option<ty::t>, b: option<ty::t>) -> cres<option<ty::t>> {
1411+
super_self_tys(self, a, b)
1412+
}
13771413
}
13781414

13791415
impl of combine for lub {
@@ -1549,6 +1585,10 @@ impl of combine for lub {
15491585
fn tps(as: [ty::t], bs: [ty::t]) -> cres<[ty::t]> {
15501586
super_tps(self, as, bs)
15511587
}
1588+
1589+
fn self_tys(a: option<ty::t>, b: option<ty::t>) -> cres<option<ty::t>> {
1590+
super_self_tys(self, a, b)
1591+
}
15521592
}
15531593

15541594
impl of combine for glb {
@@ -1739,6 +1779,10 @@ impl of combine for glb {
17391779
fn tps(as: [ty::t], bs: [ty::t]) -> cres<[ty::t]> {
17401780
super_tps(self, as, bs)
17411781
}
1782+
1783+
fn self_tys(a: option<ty::t>, b: option<ty::t>) -> cres<option<ty::t>> {
1784+
super_self_tys(self, a, b)
1785+
}
17421786
}
17431787

17441788
// ______________________________________________________________________

src/rustc/middle/trans/common.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,9 @@ fn field_idx_strict(cx: ty::ctxt, sp: span, ident: ast::ident,
916916
}
917917

918918
fn dummy_substs(tps: [ty::t]) -> ty::substs {
919-
{self_r: some(ty::re_bound(ty::br_self)), tps: tps}
919+
{self_r: some(ty::re_bound(ty::br_self)),
920+
self_ty: none,
921+
tps: tps}
920922
}
921923

922924
//

src/rustc/middle/trans/impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ fn make_impl_vtable(ccx: @crate_ctxt, impl_id: ast::def_id, substs: [ty::t],
256256
let has_tps = (*ty::lookup_item_type(ccx.tcx, impl_id).bounds).len() > 0u;
257257
make_vtable(ccx, vec::map(*ty::iface_methods(tcx, ifce_id)) {|im|
258258
let fty = ty::subst_tps(tcx, substs, ty::mk_fn(tcx, im.fty));
259-
if (*im.tps).len() > 0u || ty::type_has_vars(fty) {
259+
if (*im.tps).len() > 0u || ty::type_has_self(fty) {
260260
C_null(T_ptr(T_nil()))
261261
} else {
262262
let m_id = method_with_name(ccx, impl_id, im.ident);

src/rustc/middle/trans/shape.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ fn shape_of(ccx: @crate_ctxt, t: ty::t, ty_param_map: [uint]) -> [u8] {
457457
ty::ty_fn({proto: ast::proto_bare, _}) { [shape_bare_fn] }
458458
ty::ty_opaque_closure_ptr(_) { [shape_opaque_closure_ptr] }
459459
ty::ty_constr(inner_t, _) { shape_of(ccx, inner_t, ty_param_map) }
460-
ty::ty_var(_) | ty::ty_self(_) {
460+
ty::ty_var(_) | ty::ty_self {
461461
ccx.sess.bug("shape_of: unexpected type struct found");
462462
}
463463
}

src/rustc/middle/trans/type_of.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn type_of_fn_from_ty(cx: @crate_ctxt, fty: ty::t) -> TypeRef {
4444
}
4545

4646
fn type_of_non_gc_box(cx: @crate_ctxt, t: ty::t) -> TypeRef {
47-
assert !ty::type_has_vars(t);
47+
assert !ty::type_needs_infer(t);
4848

4949
let t_norm = ty::normalize_ty(cx.tcx, t);
5050
if t != t_norm {
@@ -62,7 +62,7 @@ fn type_of_non_gc_box(cx: @crate_ctxt, t: ty::t) -> TypeRef {
6262
}
6363

6464
fn type_of(cx: @crate_ctxt, t: ty::t) -> TypeRef {
65-
assert !ty::type_has_vars(t);
65+
assert !ty::type_needs_infer(t);
6666

6767
#debug("type_of %?: %?", t, ty::get(t));
6868

@@ -149,8 +149,7 @@ fn type_of(cx: @crate_ctxt, t: ty::t) -> TypeRef {
149149
};
150150
T_struct(tys)
151151
}
152-
ty::ty_self(_) { cx.tcx.sess.unimpl("type_of: ty_self \
153-
not implemented"); }
152+
ty::ty_self { cx.tcx.sess.unimpl("type_of: ty_self"); }
154153
ty::ty_var(_) { cx.tcx.sess.bug("type_of shouldn't see a ty_var"); }
155154
}
156155
};

0 commit comments

Comments
 (0)