Skip to content

Commit 0ad1cd5

Browse files
committed
---
yaml --- r: 3350 b: refs/heads/master c: cfaa0f4 h: refs/heads/master v: v3
1 parent d81c3c7 commit 0ad1cd5

File tree

13 files changed

+332
-14
lines changed

13 files changed

+332
-14
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 51d07830baddf3bbcee7352986050721abdca131
2+
refs/heads/master: cfaa0f4b91b27abfb3b4fba4d0bb31a5e3da7c89

trunk/src/comp/middle/ty.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export get_element_type;
5454
export hash_ty;
5555
export idx_nil;
5656
export is_lval;
57+
export is_binopable;
5758
export item_table;
5859
export lookup_item_type;
5960
export method;
@@ -2792,6 +2793,96 @@ fn strip_boxes(&ctxt cx, &ty::t t) -> ty::t {
27922793
}
27932794
fail;
27942795
}
2796+
2797+
fn is_binopable(&ctxt cx, t ty, ast::binop op) -> bool {
2798+
2799+
const int tycat_other = 0;
2800+
const int tycat_bool = 1;
2801+
const int tycat_int = 2;
2802+
const int tycat_float = 3;
2803+
const int tycat_str = 4;
2804+
const int tycat_vec = 5;
2805+
const int tycat_struct = 6;
2806+
2807+
const int opcat_add = 0;
2808+
const int opcat_sub = 1;
2809+
const int opcat_mult = 2;
2810+
const int opcat_shift = 3;
2811+
const int opcat_rel = 4;
2812+
const int opcat_eq = 5;
2813+
const int opcat_bit = 6;
2814+
const int opcat_logic = 7;
2815+
2816+
fn opcat(ast::binop op) -> int {
2817+
alt (op) {
2818+
case (ast::add) { opcat_add }
2819+
case (ast::sub) { opcat_sub }
2820+
case (ast::mul) { opcat_mult }
2821+
case (ast::div) { opcat_mult }
2822+
case (ast::rem) { opcat_mult }
2823+
case (ast::and) { opcat_logic }
2824+
case (ast::or) { opcat_logic }
2825+
case (ast::bitxor) { opcat_bit }
2826+
case (ast::bitand) { opcat_bit }
2827+
case (ast::bitor) { opcat_bit }
2828+
case (ast::lsl) { opcat_shift }
2829+
case (ast::lsr) { opcat_shift }
2830+
case (ast::asr) { opcat_shift }
2831+
case (ast::eq) { opcat_eq }
2832+
case (ast::ne) { opcat_eq }
2833+
case (ast::lt) { opcat_rel }
2834+
case (ast::le) { opcat_rel }
2835+
case (ast::ge) { opcat_rel }
2836+
case (ast::gt) { opcat_rel }
2837+
}
2838+
}
2839+
2840+
fn tycat(&ctxt cx, t ty) -> int {
2841+
alt (struct(cx, strip_boxes(cx, ty))) {
2842+
case (ty_bool) { tycat_bool }
2843+
case (ty_int) { tycat_int }
2844+
case (ty_uint) { tycat_int }
2845+
case (ty_machine(ty_i8)) { tycat_int }
2846+
case (ty_machine(ty_i16)) { tycat_int }
2847+
case (ty_machine(ty_i32)) { tycat_int }
2848+
case (ty_machine(ty_i64)) { tycat_int }
2849+
case (ty_machine(ty_u8)) { tycat_int }
2850+
case (ty_machine(ty_u16)) { tycat_int }
2851+
case (ty_machine(ty_u32)) { tycat_int }
2852+
case (ty_machine(ty_u64)) { tycat_int }
2853+
case (ty_float) { tycat_float }
2854+
case (ty_machine(ty_f32)) { tycat_float }
2855+
case (ty_machine(ty_f64)) { tycat_float }
2856+
case (ty_char) { tycat_int }
2857+
case (ty_ptr(_)) { tycat_int }
2858+
case (ty_str) { tycat_str }
2859+
case (ty_istr) { tycat_str }
2860+
case (ty_vec(_)) { tycat_vec }
2861+
case (ty_ivec(_)) { tycat_vec }
2862+
case (ty_tup(_)) { tycat_struct }
2863+
case (ty_rec(_)) { tycat_struct }
2864+
case (ty_tag(_, _)) { tycat_struct }
2865+
case (_) { tycat_other }
2866+
}
2867+
}
2868+
2869+
const bool t = true;
2870+
const bool f = false;
2871+
2872+
/*. add, shift, bit
2873+
. sub, rel, logic
2874+
. mult, eq, */
2875+
auto tbl = [[f, f, f, f, t, t, f, f], /*other*/
2876+
[f, f, f, f, t, t, t, t], /*bool*/
2877+
[t, t, t, t, t, t, t, f], /*int*/
2878+
[t, t, t, f, t, t, f, f], /*float*/
2879+
[t, f, f, f, t, t, f, f], /*str*/
2880+
[t, f, f, f, t, t, f, f], /*vec*/
2881+
[f, f, f, f, t, t, f, f]];/*struct*/
2882+
2883+
ret tbl.(tycat(cx, ty)).(opcat(op));
2884+
}
2885+
27952886
// Local Variables:
27962887
// mode: rust
27972888
// fill-column: 78;

trunk/src/comp/middle/typeck.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,19 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
14761476
write::ty_only_fixup(fcx, id, if_t);
14771477
}
14781478

1479+
// Checks the compatibility
1480+
fn check_binop_type_compat(&@fn_ctxt fcx, common::span span,
1481+
ty::t ty, ast::binop binop) {
1482+
auto resolved_t = resolve_type_vars_if_possible(fcx, ty);
1483+
if (!ty::is_binopable(fcx.ccx.tcx, resolved_t, binop)) {
1484+
auto binopstr = ast::binop_to_str(binop);
1485+
auto t_str = ty_to_str(fcx.ccx.tcx, resolved_t);
1486+
auto errmsg = "binary operation " + binopstr
1487+
+ " cannot be applied to type `" + t_str + "`";
1488+
fcx.ccx.tcx.sess.span_fatal(span, errmsg);
1489+
}
1490+
}
1491+
14791492
auto id = expr.id;
14801493
alt (expr.node) {
14811494
case (ast::expr_lit(?lit)) {
@@ -1490,19 +1503,17 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
14901503
auto rhs_t = expr_ty(fcx.ccx.tcx, rhs);
14911504

14921505
demand::autoderef(fcx, rhs.span, lhs_t, rhs_t, AUTODEREF_OK);
1493-
1494-
// FIXME: Binops have a bit more subtlety than this.
1495-
1496-
auto t = strip_boxes(fcx, expr.span, lhs_t);
1497-
alt (binop) {
1498-
case (ast::eq) { t = ty::mk_bool(fcx.ccx.tcx); }
1499-
case (ast::lt) { t = ty::mk_bool(fcx.ccx.tcx); }
1500-
case (ast::le) { t = ty::mk_bool(fcx.ccx.tcx); }
1501-
case (ast::ne) { t = ty::mk_bool(fcx.ccx.tcx); }
1502-
case (ast::ge) { t = ty::mk_bool(fcx.ccx.tcx); }
1503-
case (ast::gt) { t = ty::mk_bool(fcx.ccx.tcx); }
1504-
case (_) {/* fall through */ }
1505-
}
1506+
check_binop_type_compat(fcx, expr.span, lhs_t, binop);
1507+
1508+
auto t = alt (binop) {
1509+
case (ast::eq) { ty::mk_bool(fcx.ccx.tcx) }
1510+
case (ast::lt) { ty::mk_bool(fcx.ccx.tcx) }
1511+
case (ast::le) { ty::mk_bool(fcx.ccx.tcx) }
1512+
case (ast::ne) { ty::mk_bool(fcx.ccx.tcx) }
1513+
case (ast::ge) { ty::mk_bool(fcx.ccx.tcx) }
1514+
case (ast::gt) { ty::mk_bool(fcx.ccx.tcx) }
1515+
case (_) { strip_boxes(fcx, expr.span, lhs_t) }
1516+
};
15061517
write::ty_only_fixup(fcx, id, t);
15071518
}
15081519
case (ast::expr_unary(?unop, ?oper)) {
@@ -1646,6 +1657,8 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
16461657
case (ast::expr_assign_op(?op, ?lhs, ?rhs)) {
16471658
require_impure(fcx.ccx.tcx.sess, fcx.purity, expr.span);
16481659
check_assignment(fcx, expr.span, lhs, rhs, id);
1660+
check_binop_type_compat(fcx, expr.span,
1661+
expr_ty(fcx.ccx.tcx, lhs), op);
16491662
}
16501663
case (ast::expr_send(?lhs, ?rhs)) {
16511664
require_impure(fcx.ccx.tcx.sess, fcx.purity, expr.span);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// xfail-stage0
2+
// error-pattern:+ cannot be applied to type `tup(bool)`
3+
4+
fn main() {
5+
auto x = tup(true);
6+
x += tup(false);
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// xfail-stage0
2+
// error-pattern:+ cannot be applied to type `tup(bool)`
3+
4+
fn main() {
5+
auto x = tup(true) + tup(false);
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// xfail-stage0
2+
// error-pattern:\^ cannot be applied to type `str`
3+
4+
fn main() {
5+
auto x = "a" ^ "b";
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// xfail-stage0
2+
// error-pattern:|| cannot be applied to type `f32`
3+
4+
fn main() {
5+
auto x = 1.0_f32 || 2.0_f32;
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// xfail-stage0
2+
// error-pattern:&& cannot be applied to type `int`
3+
4+
fn main() {
5+
auto x = 1 && 2;
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// xfail-stage0
2+
// error-pattern:* cannot be applied to type `bool`
3+
4+
fn main() {
5+
auto x = true * false;
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// xfail-stage0
2+
// error-pattern:>> cannot be applied to type `port\[int\]`
3+
4+
fn main() {
5+
let port[int] p1 = port();
6+
let port[int] p2 = port();
7+
auto x = p1 >> p2;
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// xfail-stage0
2+
// error-pattern:\- cannot be applied to type `obj
3+
4+
fn main() {
5+
auto x = obj(){} - obj(){};
6+
}

trunk/src/test/run-pass/binops.rs

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
// Binop corner cases
2+
3+
fn test_nil() {
4+
assert () == ();
5+
assert !(() != ());
6+
// FIXME (#576): The current implementation of relational ops on nil
7+
// is nonsensical
8+
assert () < ();
9+
assert () <= ();
10+
assert !(() > ());
11+
assert !(() >= ());
12+
}
13+
14+
fn test_bool() {
15+
assert !(true < false);
16+
assert !(true <= false);
17+
assert (true > false);
18+
assert (true >= false);
19+
20+
assert (false < true);
21+
assert (false <= true);
22+
assert !(false > true);
23+
assert !(false >= true);
24+
25+
// Bools support bitwise binops
26+
assert (false & false == false);
27+
assert (true & false == false);
28+
assert (true & true == true);
29+
assert (false | false == false);
30+
assert (true | false == true);
31+
assert (true | true == true);
32+
assert (false ^ false == false);
33+
assert (true ^ false == true);
34+
assert (true ^ true == false);
35+
}
36+
37+
fn test_char() {
38+
auto ch10 = 10 as char;
39+
auto ch4 = 4 as char;
40+
auto ch2 = 2 as char;
41+
assert ch10 + ch4 == 14 as char;
42+
assert ch10 - ch4 == 6 as char;
43+
assert ch10 * ch4 == 40 as char;
44+
assert ch10 / ch4 == ch2;
45+
assert ch10 % ch4 == ch2;
46+
assert ch10 >> ch2 == ch2;
47+
assert ch10 >>> ch2 == ch2;
48+
assert ch10 << ch4 == 160 as char;
49+
assert ch10 | ch4 == 14 as char;
50+
assert ch10 & ch2 == ch2;
51+
assert ch10 ^ ch2 == 8 as char;
52+
}
53+
54+
fn test_box() {
55+
assert @10 == 10;
56+
assert 0xFF & @0xF0 == 0xF0;
57+
assert tup(1, 3) < @tup(1, 4);
58+
assert @rec(a = 'x') != @rec(a = 'y');
59+
}
60+
61+
fn test_port() {
62+
let port[int] p1 = port();
63+
let port[int] p2 = port();
64+
65+
// FIXME (#577) comparison of ports
66+
// assert (p1 != p2);
67+
// assert !(p1 < p2);
68+
// etc
69+
}
70+
71+
fn test_chan() {
72+
let port[int] p = port();
73+
auto ch1 = chan(p);
74+
auto ch2 = chan(p);
75+
76+
// FIXME (#577) comparison of channels
77+
// assert (ch1 != ch2);
78+
// etc
79+
}
80+
81+
fn test_ptr() {
82+
// FIXME: Don't know what binops apply to pointers. Don't know how
83+
// to make or use pointers
84+
}
85+
86+
fn test_task() {
87+
fn f() {}
88+
auto t1 = spawn f();
89+
auto t2 = spawn f();
90+
// FIXME (#577) comparison of tasks
91+
//assert t1 != t2;
92+
}
93+
94+
fn test_fn() {
95+
fn f() {}
96+
fn g() {}
97+
fn h(int i) {}
98+
auto f1 = f;
99+
auto f2 = f;
100+
auto g1 = g;
101+
auto h1 = h;
102+
auto h2 = h;
103+
assert (f1 == f2);
104+
assert (f1 == f);
105+
106+
assert (f1 == g1);
107+
assert (h1 == h2);
108+
assert !(f1 != f2);
109+
assert !(h1 < h2);
110+
assert (h1 <= h2);
111+
assert !(h1 > h2);
112+
assert (h1 >= h2);
113+
}
114+
115+
native "rust" mod native_mod {
116+
fn str_byte_len(str s) -> vec[u8];
117+
fn str_alloc(uint n_bytes) -> str;
118+
}
119+
120+
// FIXME: comparison of native fns
121+
fn test_native_fn() {
122+
/*assert (native_mod::str_byte_len == native_mod::str_byte_len);
123+
assert (native_mod::str_byte_len != native_mod::str_alloc);*/
124+
}
125+
126+
fn test_obj() {
127+
auto o1 = obj() { };
128+
auto o2 = obj() { };
129+
130+
assert (o1 == o2);
131+
assert !(o1 != o2);
132+
assert !(o1 < o2);
133+
assert (o1 <= o2);
134+
assert !(o1 > o2);
135+
assert (o1 >= o2);
136+
137+
obj constr1(int i) { };
138+
obj constr2(int i) { };
139+
140+
auto o5 = constr1(10);
141+
auto o6 = constr1(10);
142+
auto o7 = constr1(11);
143+
auto o8 = constr2(11);
144+
145+
assert (o5 == o6);
146+
assert (o6 == o7);
147+
assert (o7 == o8);
148+
}
149+
150+
fn main() {
151+
test_nil();
152+
test_bool();
153+
test_char();
154+
test_box();
155+
test_port();
156+
test_chan();
157+
test_ptr();
158+
test_task();
159+
test_fn();
160+
test_native_fn();
161+
test_obj();
162+
}

0 commit comments

Comments
 (0)