Skip to content

Commit cc16165

Browse files
committed
implement evec/estr subtyping/lub/glb/etc
1 parent eb935b8 commit cc16165

File tree

5 files changed

+126
-7
lines changed

5 files changed

+126
-7
lines changed

src/rustc/middle/infer.rs

+48-5
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,8 @@ iface combine {
896896
fn ret_styles(r1: ret_style, r2: ret_style) -> cres<ret_style>;
897897
fn contraregions(a: ty::region, b: ty::region) -> cres<ty::region>;
898898
fn regions(a: ty::region, b: ty::region) -> cres<ty::region>;
899+
fn vstores(vk: ty::terr_vstore_kind,
900+
a: ty::vstore, b: ty::vstore) -> cres<ty::vstore>;
899901
}
900902

901903
enum sub = infer_ctxt; // "subtype", "subregion" etc
@@ -951,6 +953,27 @@ fn super_args<C:combine>(
951953
}
952954
}
953955

956+
fn super_vstores<C:combine>(
957+
self: C, vk: ty::terr_vstore_kind,
958+
a: ty::vstore, b: ty::vstore) -> cres<ty::vstore> {
959+
960+
alt (a, b) {
961+
(ty::vstore_slice(a_r), ty::vstore_slice(b_r)) {
962+
self.contraregions(a_r, b_r).chain {|r|
963+
ok(ty::vstore_slice(r))
964+
}
965+
}
966+
967+
_ if a == b {
968+
ok(a)
969+
}
970+
971+
_ {
972+
err(ty::terr_vstores_differ(vk, b, a))
973+
}
974+
}
975+
}
976+
954977
fn super_fns<C:combine>(
955978
self: C, a_f: ty::fn_ty, b_f: ty::fn_ty) -> cres<ty::fn_ty> {
956979

@@ -1070,15 +1093,20 @@ fn super_tys<C:combine>(
10701093
}
10711094
}
10721095

1073-
(ty::ty_evec(a_mt, ty::vstore_slice(a_r)),
1074-
ty::ty_evec(b_mt, ty::vstore_slice(b_r))) {
1075-
self.contraregions(a_r, b_r).chain {|r|
1076-
self.mts(a_mt, b_mt).chain {|mt|
1077-
ok(ty::mk_evec(tcx, mt, ty::vstore_slice(r)))
1096+
(ty::ty_evec(a_mt, vs_a), ty::ty_evec(b_mt, vs_b)) {
1097+
self.mts(a_mt, b_mt).chain {|mt|
1098+
self.vstores(ty::terr_vec, vs_a, vs_b).chain {|vs|
1099+
ok(ty::mk_evec(tcx, mt, vs))
10781100
}
10791101
}
10801102
}
10811103

1104+
(ty::ty_estr(vs_a), ty::ty_estr(vs_b)) {
1105+
self.vstores(ty::terr_str, vs_a, vs_b).chain {|vs|
1106+
ok(ty::mk_estr(tcx,vs))
1107+
}
1108+
}
1109+
10821110
(ty::ty_res(a_id, a_t, a_tps), ty::ty_res(b_id, b_t, b_tps))
10831111
if a_id == b_id {
10841112
self.tys(a_t, b_t).chain {|t|
@@ -1234,6 +1262,11 @@ impl of combine for sub {
12341262
super_flds(self, a, b)
12351263
}
12361264

1265+
fn vstores(vk: ty::terr_vstore_kind,
1266+
a: ty::vstore, b: ty::vstore) -> cres<ty::vstore> {
1267+
super_vstores(self, vk, a, b)
1268+
}
1269+
12371270
fn modes(a: ast::mode, b: ast::mode) -> cres<ast::mode> {
12381271
super_modes(self, a, b)
12391272
}
@@ -1402,6 +1435,11 @@ impl of combine for lub {
14021435
super_flds(self, a, b)
14031436
}
14041437

1438+
fn vstores(vk: ty::terr_vstore_kind,
1439+
a: ty::vstore, b: ty::vstore) -> cres<ty::vstore> {
1440+
super_vstores(self, vk, a, b)
1441+
}
1442+
14051443
fn modes(a: ast::mode, b: ast::mode) -> cres<ast::mode> {
14061444
super_modes(self, a, b)
14071445
}
@@ -1587,6 +1625,11 @@ impl of combine for glb {
15871625
super_flds(self, a, b)
15881626
}
15891627

1628+
fn vstores(vk: ty::terr_vstore_kind,
1629+
a: ty::vstore, b: ty::vstore) -> cres<ty::vstore> {
1630+
super_vstores(self, vk, a, b)
1631+
}
1632+
15901633
fn modes(a: ast::mode, b: ast::mode) -> cres<ast::mode> {
15911634
super_modes(self, a, b)
15921635
}

src/rustc/middle/trans/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2616,7 +2616,7 @@ fn trans_arg_expr(cx: block, arg: ty::arg, lldestty: TypeRef, e: @ast::expr,
26162616
ret rslt(bcx, val);
26172617
}
26182618

2619-
fn adapt_borrowed_value(lv: lval_result, arg: ty::arg,
2619+
fn adapt_borrowed_value(lv: lval_result, _arg: ty::arg,
26202620
e: @ast::expr) -> lval_result {
26212621
let bcx = lv.bcx;
26222622
if !expr_is_borrowed(bcx, e) { ret lv; }

src/rustc/middle/ty.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import syntax::codemap::span;
1111
import metadata::csearch;
1212
import util::common::*;
1313
import util::ppaux::region_to_str;
14+
import util::ppaux::vstore_to_str;
1415
import util::ppaux::ty_to_str;
1516
import util::ppaux::ty_constr_to_str;
1617
import syntax::print::pprust::*;
@@ -105,7 +106,7 @@ export ty_fn_args;
105106
export type_constr;
106107
export kind, kind_sendable, kind_copyable, kind_noncopyable;
107108
export kind_can_be_copied, kind_can_be_sent, proto_kind, kind_lteq, type_kind;
108-
export type_err;
109+
export type_err, terr_vstore_kind;
109110
export type_err_to_str;
110111
export type_needs_drop;
111112
export type_allows_implicit_copy;
@@ -328,6 +329,10 @@ type constr_general<ARG> = spanned<constr_general_<ARG, def_id>>;
328329
type type_constr = constr_general<@path>;
329330
type constr = constr_general<uint>;
330331

332+
enum terr_vstore_kind {
333+
terr_vec, terr_str
334+
}
335+
331336
// Data structures used in type unification
332337
enum type_err {
333338
terr_mismatch,
@@ -348,6 +353,7 @@ enum type_err {
348353
terr_constr_len(uint, uint),
349354
terr_constr_mismatch(@type_constr, @type_constr),
350355
terr_regions_differ(region, region),
356+
terr_vstores_differ(terr_vstore_kind, vstore, vstore),
351357
terr_in_field(@type_err, str),
352358
terr_sorts(t, t)
353359
}
@@ -1976,6 +1982,10 @@ fn ty_sort_str(cx: ctxt, t: t) -> str {
19761982
}
19771983

19781984
fn type_err_to_str(cx: ctxt, err: type_err) -> str {
1985+
fn terr_vstore_kind_to_str(k: terr_vstore_kind) -> str {
1986+
alt k { terr_vec { "[]" } terr_str { "str" } }
1987+
}
1988+
19791989
alt err {
19801990
terr_mismatch { ret "types differ"; }
19811991
terr_ret_style_mismatch(expect, actual) {
@@ -2038,6 +2048,12 @@ fn type_err_to_str(cx: ctxt, err: type_err) -> str {
20382048
region_to_str(cx, subregion),
20392049
region_to_str(cx, superregion));
20402050
}
2051+
terr_vstores_differ(k, e_vs, a_vs) {
2052+
ret #fmt("%s storage differs: expected %s but found %s",
2053+
terr_vstore_kind_to_str(k),
2054+
vstore_to_str(cx, e_vs),
2055+
vstore_to_str(cx, a_vs));
2056+
}
20412057
terr_in_field(err, fname) {
20422058
ret #fmt("in field `%s`, %s", fname, type_err_to_str(cx, *err));
20432059
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
fn wants_box(x: str/@) { }
2+
fn wants_uniq(x: str/~) { }
3+
fn wants_three(x: str/3) { }
4+
5+
fn has_box(x: str/@) {
6+
wants_box(x);
7+
wants_uniq(x); //! ERROR str storage differs: expected ~ but found @
8+
wants_three(x); //! ERROR str storage differs: expected 3 but found @
9+
}
10+
11+
fn has_uniq(x: str/~) {
12+
wants_box(x); //! ERROR str storage differs: expected @ but found ~
13+
wants_uniq(x);
14+
wants_three(x); //! ERROR str storage differs: expected 3 but found ~
15+
}
16+
17+
fn has_three(x: str/3) {
18+
wants_box(x); //! ERROR str storage differs: expected @ but found 3
19+
wants_uniq(x); //! ERROR str storage differs: expected ~ but found 3
20+
wants_three(x);
21+
}
22+
23+
fn has_four(x: str/4) {
24+
wants_box(x); //! ERROR str storage differs: expected @ but found 4
25+
wants_uniq(x); //! ERROR str storage differs: expected ~ but found 4
26+
wants_three(x); //! ERROR str storage differs: expected 3 but found 4
27+
}
28+
29+
fn main() {
30+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
fn wants_box(x: [uint]/@) { }
2+
fn wants_uniq(x: [uint]/~) { }
3+
fn wants_three(x: [uint]/3) { }
4+
5+
fn has_box(x: [uint]/@) {
6+
wants_box(x);
7+
wants_uniq(x); //! ERROR [] storage differs: expected ~ but found @
8+
wants_three(x); //! ERROR [] storage differs: expected 3 but found @
9+
}
10+
11+
fn has_uniq(x: [uint]/~) {
12+
wants_box(x); //! ERROR [] storage differs: expected @ but found ~
13+
wants_uniq(x);
14+
wants_three(x); //! ERROR [] storage differs: expected 3 but found ~
15+
}
16+
17+
fn has_three(x: [uint]/3) {
18+
wants_box(x); //! ERROR [] storage differs: expected @ but found 3
19+
wants_uniq(x); //! ERROR [] storage differs: expected ~ but found 3
20+
wants_three(x);
21+
}
22+
23+
fn has_four(x: [uint]/4) {
24+
wants_box(x); //! ERROR [] storage differs: expected @ but found 4
25+
wants_uniq(x); //! ERROR [] storage differs: expected ~ but found 4
26+
wants_three(x); //! ERROR [] storage differs: expected 3 but found 4
27+
}
28+
29+
fn main() {
30+
}

0 commit comments

Comments
 (0)