Skip to content

Commit aa25f22

Browse files
catamorphismgraydon
authored andcommitted
Use different syntax for checks that matter to typestate
This giant commit changes the syntax of Rust to use "assert" for "check" expressions that didn't mean anything to the typestate system, and continue using "check" for checks that are used as part of typestate checking. Most of the changes are just replacing "check" with "assert" in test cases and rustc.
1 parent 870435c commit aa25f22

File tree

182 files changed

+1256
-1239
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

182 files changed

+1256
-1239
lines changed

src/boot/fe/item.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,14 @@ and parse_stmts_including_none (ps:pstate) : Ast.stmt array =
199199
bump ps;
200200
expect ps SEMI;
201201
[| span ps apos (lexpos ps) Ast.STMT_cont |]
202+
| ASSERT ->
203+
bump ps;
204+
let (stmts, expr) =
205+
ctxt "stmts: check value" parse_expr ps
206+
in
207+
expect ps SEMI;
208+
spans ps stmts apos (Ast.STMT_check_expr expr)
209+
(* leaving check as it is; adding assert as a synonym for the "old" check *)
202210
| CHECK ->
203211
bump ps;
204212
begin

src/boot/fe/lexer.mll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292

9393
("type", TYPE);
9494
("check", CHECK);
95+
("assert", ASSERT);
9596
("claim", CLAIM);
9697
("prove", PROVE);
9798

src/boot/fe/token.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ type token =
7777
(* Type and type-state keywords *)
7878
| TYPE
7979
| CHECK
80+
| ASSERT
8081
| CLAIM
8182
| PROVE
8283

@@ -237,6 +238,7 @@ let rec string_of_tok t =
237238
(* Type and type-state keywords *)
238239
| TYPE -> "type"
239240
| CHECK -> "check"
241+
| ASSERT -> "assert"
240242
| CLAIM -> "claim"
241243
| PROVE -> "prove"
242244

src/comp/front/ast.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,10 @@ tag expr_ {
283283
expr_put(option.t[@expr], ann);
284284
expr_be(@expr, ann);
285285
expr_log(int, @expr, ann);
286-
expr_check_expr(@expr, ann);
286+
/* just an assert, no significance to typestate */
287+
expr_assert(@expr, ann);
288+
/* preds that typestate is aware of */
289+
expr_check(@expr, ann);
287290
expr_port(ann);
288291
expr_chan(@expr, ann);
289292
}

src/comp/front/creader.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fn parse_ty(@pstate st, str_def sd) -> ty.t {
9393
case ('c') { ret ty.mk_char(st.tcx); }
9494
case ('s') { ret ty.mk_str(st.tcx); }
9595
case ('t') {
96-
check(next(st) as char == '[');
96+
assert (next(st) as char == '[');
9797
auto def = parse_def(st, sd);
9898
let vec[ty.t] params = vec();
9999
while (peek(st) as char != ']') {
@@ -108,7 +108,7 @@ fn parse_ty(@pstate st, str_def sd) -> ty.t {
108108
case ('P') { ret ty.mk_port(st.tcx, parse_ty(st, sd)); }
109109
case ('C') { ret ty.mk_chan(st.tcx, parse_ty(st, sd)); }
110110
case ('T') {
111-
check(next(st) as char == '[');
111+
assert (next(st) as char == '[');
112112
let vec[ty.mt] params = vec();
113113
while (peek(st) as char != ']') {
114114
params += vec(parse_mt(st, sd));
@@ -117,7 +117,7 @@ fn parse_ty(@pstate st, str_def sd) -> ty.t {
117117
ret ty.mk_tup(st.tcx, params);
118118
}
119119
case ('R') {
120-
check(next(st) as char == '[');
120+
assert (next(st) as char == '[');
121121
let vec[ty.field] fields = vec();
122122
while (peek(st) as char != ']') {
123123
auto name = "";
@@ -149,7 +149,7 @@ fn parse_ty(@pstate st, str_def sd) -> ty.t {
149149
ret ty.mk_native_fn(st.tcx,abi,func._0,func._1);
150150
}
151151
case ('O') {
152-
check(next(st) as char == '[');
152+
assert (next(st) as char == '[');
153153
let vec[ty.method] methods = vec();
154154
while (peek(st) as char != ']') {
155155
auto proto;
@@ -175,9 +175,9 @@ fn parse_ty(@pstate st, str_def sd) -> ty.t {
175175
case ('Y') { ret ty.mk_type(st.tcx); }
176176
case ('#') {
177177
auto pos = parse_hex(st);
178-
check (next(st) as char == ':');
178+
assert (next(st) as char == ':');
179179
auto len = parse_hex(st);
180-
check (next(st) as char == '#');
180+
assert (next(st) as char == '#');
181181
alt (st.tcx.rcache.find(tup(st.crate,pos,len))) {
182182
case (some[ty.t](?tt)) { ret tt; }
183183
case (none[ty.t]) {
@@ -245,7 +245,7 @@ fn parse_hex(@pstate st) -> uint {
245245
}
246246

247247
fn parse_ty_fn(@pstate st, str_def sd) -> tup(vec[ty.arg], ty.t) {
248-
check(next(st) as char == '[');
248+
assert (next(st) as char == '[');
249249
let vec[ty.arg] inputs = vec();
250250
while (peek(st) as char != ']') {
251251
auto mode = ast.val;

src/comp/front/lexer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ fn keyword_table() -> std.map.hashmap[str, token.token] {
127127

128128
keywords.insert("type", token.TYPE);
129129
keywords.insert("check", token.CHECK);
130+
keywords.insert("assert", token.ASSERT);
130131
keywords.insert("claim", token.CLAIM);
131132
keywords.insert("prove", token.PROVE);
132133

@@ -528,7 +529,7 @@ fn scan_numeric_escape(reader rdr) -> char {
528529

529530
auto n_hex_digits = 0;
530531

531-
check (rdr.curr() == '\\');
532+
assert (rdr.curr() == '\\');
532533

533534
alt (rdr.next()) {
534535
case ('x') { n_hex_digits = 2; }

src/comp/front/parser.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ fn parse_path(parser p, greed g) -> ast.path {
621621
if (p.peek() == token.DOT) {
622622
if (g == GREEDY) {
623623
p.bump();
624-
check (is_ident(p.peek()));
624+
assert (is_ident(p.peek()));
625625
} else {
626626
more = false;
627627
}
@@ -816,20 +816,23 @@ fn parse_bottom_expr(parser p) -> @ast.expr {
816816
ex = ast.expr_log(0, e, ast.ann_none);
817817
}
818818

819-
case (token.CHECK) {
819+
case (token.ASSERT) {
820820
p.bump();
821-
alt (p.peek()) {
822-
case (token.LPAREN) {
823-
auto e = parse_expr(p);
824-
auto hi = e.span.hi;
825-
ex = ast.expr_check_expr(e, ast.ann_none);
826-
}
827-
case (_) {
828-
p.get_session().unimpl("constraint-check stmt");
829-
}
830-
}
821+
auto e = parse_expr(p);
822+
auto hi = e.span.hi;
823+
ex = ast.expr_assert(e, ast.ann_none);
831824
}
832825

826+
case (token.CHECK) {
827+
p.bump();
828+
/* Should be a predicate (pure boolean function) applied to
829+
arguments that are all either slot variables or literals.
830+
but the typechecker enforces that. */
831+
auto e = parse_expr(p);
832+
auto hi = e.span.hi;
833+
ex = ast.expr_check(e, ast.ann_none);
834+
}
835+
833836
case (token.RET) {
834837
p.bump();
835838
alt (p.peek()) {
@@ -937,7 +940,7 @@ fn expand_syntax_ext(parser p, ast.span sp,
937940
&ast.path path, vec[@ast.expr] args,
938941
option.t[str] body) -> ast.expr_ {
939942

940-
check (_vec.len[ast.ident](path.node.idents) > 0u);
943+
assert (_vec.len[ast.ident](path.node.idents) > 0u);
941944
auto extname = path.node.idents.(0);
942945
if (_str.eq(extname, "fmt")) {
943946
auto expanded = extfmt.expand_syntax_ext(args, body);
@@ -1673,7 +1676,8 @@ fn stmt_ends_with_semi(@ast.stmt stmt) -> bool {
16731676
case (ast.expr_put(_,_)) { ret true; }
16741677
case (ast.expr_be(_,_)) { ret true; }
16751678
case (ast.expr_log(_,_,_)) { ret true; }
1676-
case (ast.expr_check_expr(_,_)) { ret true; }
1679+
case (ast.expr_check(_,_)) { ret true; }
1680+
case (ast.expr_assert(_,_)) { ret true; }
16771681
}
16781682
}
16791683
// We should not be calling this on a cdir.
@@ -2157,24 +2161,24 @@ fn parse_item(parser p) -> @ast.item {
21572161

21582162
alt (p.peek()) {
21592163
case (token.CONST) {
2160-
check (lyr == ast.layer_value);
2164+
assert (lyr == ast.layer_value);
21612165
ret parse_item_const(p);
21622166
}
21632167

21642168
case (token.FN) {
2165-
check (lyr == ast.layer_value);
2169+
assert (lyr == ast.layer_value);
21662170
ret parse_item_fn_or_iter(p);
21672171
}
21682172
case (token.ITER) {
2169-
check (lyr == ast.layer_value);
2173+
assert (lyr == ast.layer_value);
21702174
ret parse_item_fn_or_iter(p);
21712175
}
21722176
case (token.MOD) {
2173-
check (lyr == ast.layer_value);
2177+
assert (lyr == ast.layer_value);
21742178
ret parse_item_mod(p);
21752179
}
21762180
case (token.NATIVE) {
2177-
check (lyr == ast.layer_value);
2181+
assert (lyr == ast.layer_value);
21782182
ret parse_item_native_mod(p);
21792183
}
21802184
case (token.TYPE) {

src/comp/front/token.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ tag token {
8989

9090
/* Type and type-state keywords */
9191
TYPE;
92+
ASSERT;
9293
CHECK;
9394
CLAIM;
9495
PROVE;
@@ -258,6 +259,7 @@ fn to_str(token t) -> str {
258259

259260
/* Type and type-state keywords */
260261
case (TYPE) { ret "type"; }
262+
case (ASSERT) { ret "assert"; }
261263
case (CHECK) { ret "check"; }
262264
case (CLAIM) { ret "claim"; }
263265
case (PROVE) { ret "prove"; }

0 commit comments

Comments
 (0)