Skip to content

Commit 662e949

Browse files
committed
rustc: Alias fix part 1 -- Separate out AST modes from typechecker modes, and introduce an "either value or alias" mode
1 parent bc879a4 commit 662e949

File tree

6 files changed

+63
-71
lines changed

6 files changed

+63
-71
lines changed

src/comp/front/creader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ fn parse_ty_fn(@pstate st, str_def sd) -> tup(vec[ty.arg], ty.t) {
249249
assert (next(st) as char == '[');
250250
let vec[ty.arg] inputs = vec();
251251
while (peek(st) as char != ']') {
252-
auto mode = ast.val;
252+
auto mode = ty.mo_val;
253253
if (peek(st) as char == '&') {
254-
mode = ast.alias;
254+
mode = ty.mo_alias;
255255
st.pos = st.pos + 1u;
256256
}
257257
inputs += vec(rec(mode=mode, ty=parse_ty(st, sd)));

src/comp/middle/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ mod Encode {
241241
fn enc_ty_fn(IO.writer w, @ctxt cx, vec[ty.arg] args, ty.t out) {
242242
w.write_char('[');
243243
for (ty.arg arg in args) {
244-
if (arg.mode == ast.alias) { w.write_char('&'); }
244+
if (arg.mode == ty.mo_alias) { w.write_char('&'); }
245245
enc_ty(w, cx, arg.ty);
246246
}
247247
w.write_char(']');

src/comp/middle/trans.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -605,12 +605,12 @@ fn type_of_explicit_args(@crate_ctxt cx, vec[ty.arg] inputs) -> vec[TypeRef] {
605605
let vec[TypeRef] atys = vec();
606606
for (ty.arg arg in inputs) {
607607
if (ty.type_has_dynamic_size(cx.tcx, arg.ty)) {
608-
assert (arg.mode == ast.alias);
608+
assert (arg.mode == ty.mo_alias);
609609
atys += vec(T_typaram_ptr(cx.tn));
610610
} else {
611611
let TypeRef t;
612612
alt (arg.mode) {
613-
case (ast.alias) {
613+
case (ty.mo_alias) {
614614
t = T_ptr(type_of_inner(cx, arg.ty));
615615
}
616616
case (_) {
@@ -675,7 +675,8 @@ fn type_of_fn_full(@crate_ctxt cx,
675675
atys +=
676676
vec(T_fn_pair(cx.tn,
677677
type_of_fn_full(cx, ast.proto_fn, none[TypeRef],
678-
vec(rec(mode=ast.alias, ty=output)),
678+
vec(rec(mode=ty.mo_alias,
679+
ty=output)),
679680
ty.mk_nil(cx.tcx), 0u)));
680681
}
681682

@@ -829,7 +830,7 @@ fn type_of_inner(@crate_ctxt cx, ty.t t) -> TypeRef {
829830
fn type_of_arg(@local_ctxt cx, &ty.arg arg) -> TypeRef {
830831
alt (ty.struct(cx.ccx.tcx, arg.ty)) {
831832
case (ty.ty_param(_)) {
832-
if (arg.mode == ast.alias) {
833+
if (arg.mode == ty.mo_alias) {
833834
ret T_typaram_ptr(cx.ccx.tn);
834835
}
835836
}
@@ -839,7 +840,7 @@ fn type_of_arg(@local_ctxt cx, &ty.arg arg) -> TypeRef {
839840
}
840841

841842
auto typ;
842-
if (arg.mode == ast.alias) {
843+
if (arg.mode == ty.mo_alias) {
843844
typ = T_ptr(type_of_inner(cx.ccx, arg.ty));
844845
} else {
845846
typ = type_of_inner(cx.ccx, arg.ty);
@@ -3712,7 +3713,7 @@ fn trans_for_each(@block_ctxt cx,
37123713
auto iter_body_llty =
37133714
type_of_fn_full(lcx.ccx, ast.proto_fn,
37143715
none[TypeRef],
3715-
vec(rec(mode=ast.alias, ty=decl_ty)),
3716+
vec(rec(mode=ty.mo_alias, ty=decl_ty)),
37163717
ty.mk_nil(lcx.ccx.tcx), 0u);
37173718

37183719
let ValueRef lliterbody = decl_internal_fastcall_fn(lcx.ccx.llmod,
@@ -4482,7 +4483,7 @@ fn trans_bind_thunk(@local_ctxt cx,
44824483
bcx = bound_arg.bcx;
44834484
auto val = bound_arg.val;
44844485

4485-
if (out_arg.mode == ast.val) {
4486+
if (out_arg.mode == ty.mo_val) {
44864487
if (type_is_immediate(cx.ccx, e_ty)) {
44874488
val = bcx.build.Load(val);
44884489
bcx = take_ty(bcx, val, e_ty).bcx;
@@ -4492,7 +4493,7 @@ fn trans_bind_thunk(@local_ctxt cx,
44924493
}
44934494
} else if (ty.type_contains_params(cx.ccx.tcx,
44944495
out_arg.ty)) {
4495-
assert (out_arg.mode == ast.alias);
4496+
assert (out_arg.mode == ty.mo_alias);
44964497
val = bcx.build.PointerCast(val, llout_arg_ty);
44974498
}
44984499

@@ -4505,7 +4506,7 @@ fn trans_bind_thunk(@local_ctxt cx,
45054506
let ValueRef passed_arg = llvm.LLVMGetParam(llthunk, a);
45064507

45074508
if (ty.type_contains_params(cx.ccx.tcx, out_arg.ty)) {
4508-
assert (out_arg.mode == ast.alias);
4509+
assert (out_arg.mode == ty.mo_alias);
45094510
passed_arg = bcx.build.PointerCast(passed_arg,
45104511
llout_arg_ty);
45114512
}
@@ -4745,7 +4746,7 @@ fn trans_arg_expr(@block_ctxt cx,
47454746
auto re = trans_expr(bcx, e);
47464747
val = re.val;
47474748
bcx = re.bcx;
4748-
} else if (arg.mode == ast.alias) {
4749+
} else if (arg.mode == ty.mo_alias) {
47494750
let lval_result lv;
47504751
if (ty.is_lval(e)) {
47514752
lv = trans_lval(bcx, e);
@@ -4772,13 +4773,13 @@ fn trans_arg_expr(@block_ctxt cx,
47724773
bcx = re.bcx;
47734774
}
47744775

4775-
if (arg.mode != ast.alias) {
4776+
if (arg.mode != ty.mo_alias) {
47764777
bcx = take_ty(bcx, val, e_ty).bcx;
47774778
}
47784779

47794780
if (ty.type_contains_params(cx.fcx.lcx.ccx.tcx, arg.ty)) {
47804781
auto lldestty = lldestty0;
4781-
if (arg.mode == ast.val) {
4782+
if (arg.mode == ty.mo_val) {
47824783
// FIXME: we'd prefer to use &&, but rustboot doesn't like it
47834784
if (ty.type_is_structural(cx.fcx.lcx.ccx.tcx, e_ty)) {
47844785
lldestty = T_ptr(lldestty);
@@ -4787,7 +4788,7 @@ fn trans_arg_expr(@block_ctxt cx,
47874788
val = bcx.build.PointerCast(val, lldestty);
47884789
}
47894790

4790-
if (arg.mode == ast.val) {
4791+
if (arg.mode == ty.mo_val) {
47914792
// FIXME: we'd prefer to use &&, but rustboot doesn't like it
47924793
if (ty.type_is_structural(cx.fcx.lcx.ccx.tcx, e_ty)) {
47934794
// Until here we've been treating structures by pointer;
@@ -5496,7 +5497,7 @@ fn trans_put(@block_ctxt cx, &Option.t[@ast.expr] e) -> result {
54965497
case (none[@ast.expr]) { }
54975498
case (some[@ast.expr](?x)) {
54985499
auto e_ty = ty.expr_ty(cx.fcx.lcx.ccx.tcx, x);
5499-
auto arg = rec(mode=ast.alias, ty=e_ty);
5500+
auto arg = rec(mode=ty.mo_alias, ty=e_ty);
55005501
auto arg_tys = type_of_explicit_args(cx.fcx.lcx.ccx, vec(arg));
55015502
auto r = trans_arg_expr(bcx, arg, arg_tys.(0), x);
55025503
bcx = r.bcx;
@@ -6362,10 +6363,7 @@ fn trans_obj(@local_ctxt cx, &ast._obj ob, ast.def_id oid,
63626363
// Translate obj ctor args to function arguments.
63636364
let vec[ast.arg] fn_args = vec();
63646365
for (ast.obj_field f in ob.fields) {
6365-
fn_args += vec(rec(mode=ast.alias,
6366-
ty=f.ty,
6367-
ident=f.ident,
6368-
id=f.id));
6366+
fn_args += vec(rec(mode=ast.alias, ty=f.ty, ident=f.ident, id=f.id));
63696367
}
63706368

63716369
auto fcx = new_fn_ctxt(cx, llctor_decl);
@@ -6814,8 +6812,8 @@ fn decl_native_fn_and_pair(@crate_ctxt ccx,
68146812
fn convert_arg_to_i32(@block_ctxt cx,
68156813
ValueRef v,
68166814
ty.t t,
6817-
ast.mode mode) -> ValueRef {
6818-
if (mode == ast.val) {
6815+
ty.mode mode) -> ValueRef {
6816+
if (mode == ty.mo_val) {
68196817
if (ty.type_is_integral(cx.fcx.lcx.ccx.tcx, t)) {
68206818
auto lldsttype = T_int();
68216819
auto llsrctype = type_of(cx.fcx.lcx.ccx, t);
@@ -6875,7 +6873,7 @@ fn decl_native_fn_and_pair(@crate_ctxt ccx,
68756873
call_args += vec(llarg);
68766874
}
68776875

6878-
if (arg.mode == ast.val) {
6876+
if (arg.mode == ty.mo_val) {
68796877
drop_args += vec(tup(llarg, arg.ty));
68806878
}
68816879

src/comp/middle/ty.rs

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ import util.typestate_ann.ts_ann;
3535

3636
// Data types
3737

38-
type arg = rec(ast.mode mode, t ty);
38+
tag mode {
39+
mo_val;
40+
mo_alias;
41+
mo_either;
42+
}
43+
44+
type arg = rec(mode mode, t ty);
3945
type field = rec(ast.ident ident, mt mt);
4046
type method = rec(ast.proto proto,
4147
ast.ident ident,
@@ -500,12 +506,12 @@ fn path_to_str(&ast.path pth) -> str {
500506

501507
fn ty_to_str(ctxt cx, &t typ) -> str {
502508

503-
fn fn_input_to_str(ctxt cx, &rec(ast.mode mode, t ty) input) -> str {
509+
fn fn_input_to_str(ctxt cx, &rec(mode mode, t ty) input) -> str {
504510
auto s;
505-
if (mode_is_alias(input.mode)) {
506-
s = "&";
507-
} else {
508-
s = "";
511+
alt (input.mode) {
512+
case (mo_val) { s = ""; }
513+
case (mo_alias) { s = "&"; }
514+
case (mo_either) { s = "?"; }
509515
}
510516

511517
ret s + ty_to_str(cx, input.ty);
@@ -827,15 +833,6 @@ fn copy_cname(ctxt cx, t struct_ty, t cname_ty) -> t {
827833
ret gen_ty_full(cx, struct(cx, struct_ty), cname_ty.cname);
828834
}
829835

830-
// FIXME: remove me when == works on these tags.
831-
fn mode_is_alias(ast.mode m) -> bool {
832-
alt (m) {
833-
case (ast.val) { ret false; }
834-
case (ast.alias) { ret true; }
835-
}
836-
fail;
837-
}
838-
839836
fn type_is_nil(ctxt cx, t ty) -> bool {
840837
alt (struct(cx, ty)) {
841838
case (ty_nil) { ret true; }
@@ -1272,23 +1269,6 @@ fn equal_type_structures(&sty a, &sty b) -> bool {
12721269
}
12731270
}
12741271

1275-
fn equal_mode(ast.mode a, ast.mode b) -> bool {
1276-
alt (a) {
1277-
case (ast.val) {
1278-
alt (b) {
1279-
case (ast.val) { ret true; }
1280-
case (_) { ret false; }
1281-
}
1282-
}
1283-
case (ast.alias) {
1284-
alt (b) {
1285-
case (ast.alias) { ret true; }
1286-
case (_) { ret false; }
1287-
}
1288-
}
1289-
}
1290-
}
1291-
12921272
fn equal_mt(&mt a, &mt b) -> bool {
12931273
ret equal_mut(a.mut, b.mut) && eq_ty(a.ty, b.ty);
12941274
}
@@ -1303,7 +1283,7 @@ fn equal_type_structures(&sty a, &sty b) -> bool {
13031283
auto i = 0u;
13041284
while (i < len) {
13051285
auto arg_a = args_a.(i); auto arg_b = args_b.(i);
1306-
if (!equal_mode(arg_a.mode, arg_b.mode)) { ret false; }
1286+
if (arg_a.mode != arg_b.mode) { ret false; }
13071287
if (!eq_ty(arg_a.ty, arg_b.ty)) { ret false; }
13081288
i += 1u;
13091289
}
@@ -2117,12 +2097,13 @@ mod Unify {
21172097
auto actual_input = actual_inputs.(i);
21182098

21192099
// This should be safe, I think?
2100+
// FIXME: It's not. At all.
21202101
auto result_mode;
2121-
if (mode_is_alias(expected_input.mode) ||
2122-
mode_is_alias(actual_input.mode)) {
2123-
result_mode = ast.alias;
2102+
if (expected_input.mode == mo_alias ||
2103+
actual_input.mode == mo_alias) {
2104+
result_mode = mo_alias;
21242105
} else {
2125-
result_mode = ast.val;
2106+
result_mode = mo_val;
21262107
}
21272108

21282109
auto result = unify_step(cx, actual_input.ty, expected_input.ty);

src/comp/middle/typeck.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import middle.ty.block_ty;
1919
import middle.ty.expr_ty;
2020
import middle.ty.field;
2121
import middle.ty.method;
22-
import middle.ty.mode_is_alias;
22+
import middle.ty.mo_val;
23+
import middle.ty.mo_alias;
24+
import middle.ty.mo_either;
2325
import middle.ty.pat_ty;
2426
import middle.ty.path_to_str;
2527
import middle.ty.struct;
@@ -210,15 +212,25 @@ fn instantiate_path(@fn_ctxt fcx, &ast.path pth, &ty_param_count_and_ty tpt,
210212
ret ast.ann_type(t, ty_substs_opt, none[@ts_ann]);
211213
}
212214

215+
fn ast_mode_to_mode(ast.mode mode) -> ty.mode {
216+
auto ty_mode;
217+
alt (mode) {
218+
case (ast.val) { ty_mode = mo_val; }
219+
case (ast.alias) { ty_mode = mo_alias; }
220+
}
221+
ret ty_mode;
222+
}
223+
213224
// Parses the programmer's textual representation of a type into our internal
214225
// notion of a type. `getter` is a function that returns the type
215226
// corresponding to a definition ID.
216227
fn ast_ty_to_ty(ty.ctxt tcx, ty_getter getter, &@ast.ty ast_ty) -> ty.t {
217228
fn ast_arg_to_arg(ty.ctxt tcx,
218229
ty_getter getter,
219230
&rec(ast.mode mode, @ast.ty ty) arg)
220-
-> rec(ast.mode mode, ty.t ty) {
221-
ret rec(mode=arg.mode, ty=ast_ty_to_ty(tcx, getter, arg.ty));
231+
-> rec(ty.mode mode, ty.t ty) {
232+
auto ty_mode = ast_mode_to_mode(arg.mode);
233+
ret rec(mode=ty_mode, ty=ast_ty_to_ty(tcx, getter, arg.ty));
222234
}
223235

224236
fn ast_mt_to_mt(ty.ctxt tcx,
@@ -430,8 +442,9 @@ mod Collect {
430442
}
431443

432444
fn ty_of_arg(@ctxt cx, &ast.arg a) -> arg {
445+
auto ty_mode = ast_mode_to_mode(a.mode);
433446
auto f = bind getter(cx, _);
434-
ret rec(mode=a.mode, ty=ast_ty_to_ty(cx.tcx, f, a.ty));
447+
ret rec(mode=ty_mode, ty=ast_ty_to_ty(cx.tcx, f, a.ty));
435448
}
436449

437450
fn ty_of_method(@ctxt cx, &@ast.method m) -> method {
@@ -468,7 +481,7 @@ mod Collect {
468481
for (ast.obj_field f in obj_info.fields) {
469482
auto g = bind getter(cx, _);
470483
auto t_field = ast_ty_to_ty(cx.tcx, g, f.ty);
471-
Vec.push[arg](t_inputs, rec(mode=ast.alias, ty=t_field));
484+
Vec.push[arg](t_inputs, rec(mode=ty.mo_alias, ty=t_field));
472485
}
473486

474487
cx.type_cache.insert(obj_ty_id, t_obj);
@@ -601,7 +614,7 @@ mod Collect {
601614
let vec[arg] args = vec();
602615
for (ast.variant_arg va in variant.node.args) {
603616
auto arg_ty = ast_ty_to_ty(cx.tcx, f, va.ty);
604-
args += vec(rec(mode=ast.alias, ty=arg_ty));
617+
args += vec(rec(mode=ty.mo_alias, ty=arg_ty));
605618
}
606619
auto tag_t = ty.mk_tag(cx.tcx, tag_id, ty_param_tys);
607620
result_ty = ty.mk_fn(cx.tcx, ast.proto_fn, args, tag_t);
@@ -1754,7 +1767,7 @@ fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr {
17541767
args_0 += vec(some[@ast.expr](a_0));
17551768

17561769
// FIXME: this breaks aliases. We need a ty_fn_arg.
1757-
auto arg_ty = rec(mode=ast.val,
1770+
auto arg_ty = rec(mode=mo_val,
17581771
ty=expr_ty(fcx.ccx.tcx, a_0));
17591772
Vec.push[arg](arg_tys_0, arg_ty);
17601773
}
@@ -1763,7 +1776,7 @@ fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr {
17631776

17641777
// FIXME: breaks aliases too?
17651778
auto typ = next_ty_var(fcx.ccx);
1766-
Vec.push[arg](arg_tys_0, rec(mode=ast.val, ty=typ));
1779+
Vec.push[arg](arg_tys_0, rec(mode=mo_val, ty=typ));
17671780
}
17681781
}
17691782
}
@@ -2869,7 +2882,7 @@ fn check_item_fn(&@crate_ctxt ccx, &span sp, ast.ident ident, &ast._fn f,
28692882
let vec[arg] inputs = vec();
28702883
for (ast.arg arg in f.decl.inputs) {
28712884
auto input_ty = ast_ty_to_ty_crate(ccx, arg.ty);
2872-
inputs += vec(rec(mode=arg.mode, ty=input_ty));
2885+
inputs += vec(rec(mode=ast_mode_to_mode(arg.mode), ty=input_ty));
28732886
}
28742887

28752888
auto output_ty = ast_ty_to_ty_crate(ccx, f.decl.output);

src/comp/pretty/pprust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ fn print_ty_fn(ps s, ast.proto proto, Option.t[str] id,
939939
}
940940
popen_h(s);
941941
fn print_arg(ps s, &ast.ty_arg input) {
942-
if (middle.ty.mode_is_alias(input.mode)) {wrd(s.s, "&");}
942+
if (input.mode == ast.alias) {wrd(s.s, "&");}
943943
print_type(s, input.ty);
944944
}
945945
auto f = print_arg;

0 commit comments

Comments
 (0)