Skip to content

Commit 17ff2a0

Browse files
committed
Further support for predicate constraints
Changed function types to include a list of constraints. Added code for parsing and pretty-printing constraints. This necessitated splitting pprust into two files (pprust and ppaux) to break a circulate dependency, as ty_to_str now needs to print out constraints, which may include literals, but pprust depended on ty.
1 parent 46920e0 commit 17ff2a0

File tree

20 files changed

+984
-609
lines changed

20 files changed

+984
-609
lines changed

src/comp/back/link.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ fn mangle_internal_name_by_type_only(&@crate_ctxt ccx, &ty::t t,
431431
&str name) -> str {
432432
auto f = metadata::def_to_str;
433433
auto cx = @rec(ds=f, tcx=ccx.tcx, abbrevs=metadata::ac_no_abbrevs);
434-
auto s = ty::ty_to_short_str(ccx.tcx, t);
434+
auto s = pretty::ppaux::ty_to_short_str(ccx.tcx, t);
435435

436436
auto hash = get_symbol_hash(ccx, t);
437437
ret mangle([name, s, hash]);

src/comp/driver/rustc.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import middle::ty;
1111
import middle::typeck;
1212
import middle::tstate::ck;
1313
import pretty::pprust;
14+
import pretty::ppaux;
1415
import back::link;
1516
import lib::llvm;
1617
import util::common;
@@ -133,10 +134,10 @@ fn pretty_print_input(session::session sess, eval::env env, str input,
133134
auto def_map = resolve::resolve_crate(sess, crate);
134135
auto ty_cx = ty::mk_ctxt(sess, def_map);
135136
typeck::check_crate(ty_cx, crate);
136-
mode = pprust::mo_typed(ty_cx);
137+
mode = ppaux::mo_typed(ty_cx);
137138
}
138-
case (ppm_normal) { mode = pprust::mo_untyped; }
139-
case (ppm_identified) { mode = pprust::mo_identified; }
139+
case (ppm_normal) { mode = ppaux::mo_untyped; }
140+
case (ppm_identified) { mode = ppaux::mo_identified; }
140141
}
141142

142143
pprust::print_file(sess, crate.node.module, input, std::io::stdout(),

src/comp/front/ast.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ type mt = rec(@ty ty, mutability mut);
312312
type ty_field_ = rec(ident ident, mt mt);
313313
type ty_arg_ = rec(mode mode, @ty ty);
314314
type ty_method_ = rec(proto proto, ident ident,
315-
vec[ty_arg] inputs, @ty output,
316-
controlflow cf);
315+
vec[ty_arg] inputs, @ty output,
316+
controlflow cf, vec[@constr] constrs);
317317
type ty_field = spanned[ty_field_];
318318
type ty_arg = spanned[ty_arg_];
319319
type ty_method = spanned[ty_method_];
@@ -342,7 +342,7 @@ tag ty_ {
342342
ty_chan(@ty);
343343
ty_tup(vec[mt]);
344344
ty_rec(vec[ty_field]);
345-
ty_fn(proto, vec[ty_arg], @ty, controlflow);
345+
ty_fn(proto, vec[ty_arg], @ty, controlflow, vec[@constr]);
346346
ty_obj(vec[ty_method]);
347347
ty_path(path, ann);
348348
ty_type;
@@ -362,7 +362,8 @@ type arg = rec(mode mode, @ty ty, ident ident, def_id id);
362362
type fn_decl = rec(vec[arg] inputs,
363363
@ty output,
364364
purity purity,
365-
controlflow cf);
365+
controlflow cf,
366+
vec[@constr] constraints);
366367
tag purity {
367368
pure_fn; // declared with "pred"
368369
impure_fn; // declared with "fn"

src/comp/front/creader.rs

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import middle::ty;
1414
import back::x86;
1515
import util::common;
1616
import util::common::span;
17+
import util::common::respan;
1718
import util::common::a_bang;
1819
import util::common::a_ty;
20+
import util::common::may_begin_ident;
1921

2022
import std::str;
2123
import std::uint;
@@ -54,6 +56,14 @@ fn next(@pstate st) -> u8 {
5456
ret ch;
5557
}
5658

59+
fn parse_ident(@pstate st, str_def sd, char last) -> ast::ident {
60+
auto res = "";
61+
while (peek(st) as char != last) {
62+
res += str::unsafe_from_byte(next(st));
63+
}
64+
ret res;
65+
}
66+
5767
fn parse_ty_data(vec[u8] data, int crate_num, uint pos, uint len,
5868
str_def sd, ty::ctxt tcx) -> ty::t {
5969
auto st = @rec(data=data, crate=crate_num,
@@ -69,6 +79,62 @@ fn parse_ty_or_bang(@pstate st, str_def sd) -> ty_or_bang {
6979
}
7080
}
7181

82+
fn parse_constrs(@pstate st, str_def sd) -> vec[@ast::constr] {
83+
let vec[@ast::constr] res = [];
84+
alt (peek(st) as char) {
85+
case (':') {
86+
do {
87+
auto ignore = next(st);
88+
vec::push(res, parse_constr(st, sd));
89+
} while (peek(st) as char == ',')
90+
}
91+
case (_) {}
92+
}
93+
ret res;
94+
}
95+
96+
fn parse_constr(@pstate st, str_def sd) -> @ast::constr {
97+
let vec[@ast::constr_arg] args = [];
98+
auto sp = rec(lo=0u,hi=0u); // FIXME
99+
let vec[ast::ident] ids = [];
100+
let vec[@ast::ty] tys = [];
101+
let ast::path pth = respan(sp,
102+
rec(idents=ids, types=tys)); // FIXME
103+
let ast::ident p1 = parse_ident(st, sd, '(');
104+
log_err("ignore=");
105+
log_err(p1);
106+
let char ignore = next(st) as char;
107+
assert(ignore as char == '(');
108+
do {
109+
alt (peek(st) as char) {
110+
case ('*') {
111+
auto ignore = next(st);
112+
args += [@respan(sp, ast::carg_base)];
113+
}
114+
case (?c) {
115+
log_err("c =");
116+
log_err(str::from_bytes([c as u8]));
117+
if (may_begin_ident(c)) {
118+
auto id = parse_ident(st, sd, ',');
119+
args += [@respan(sp, ast::carg_ident(id))];
120+
}
121+
else {
122+
log_err("Lit args are unimplemented");
123+
fail; // FIXME
124+
}
125+
/*
126+
else {
127+
auto lit = parse_lit(st, sd, ',');
128+
args += [respan(st.span, ast::carg_lit(lit))];
129+
}
130+
*/
131+
}
132+
}
133+
} while (next(st) as char == ',');
134+
ignore = next(st) as char;
135+
ret @respan(sp, rec(path=pth, args=args));
136+
}
137+
72138
fn parse_ty(@pstate st, str_def sd) -> ty::t {
73139
alt (next(st) as char) {
74140
case ('n') { ret ty::mk_nil(st.tcx); }
@@ -135,11 +201,13 @@ fn parse_ty(@pstate st, str_def sd) -> ty::t {
135201
}
136202
case ('F') {
137203
auto func = parse_ty_fn(st, sd);
138-
ret ty::mk_fn(st.tcx, ast::proto_fn, func._0, func._1, func._2);
204+
ret ty::mk_fn(st.tcx, ast::proto_fn, func._0,
205+
func._1, func._2, func._3);
139206
}
140207
case ('W') {
141208
auto func = parse_ty_fn(st, sd);
142-
ret ty::mk_fn(st.tcx, ast::proto_iter, func._0, func._1, func._2);
209+
ret ty::mk_fn(st.tcx, ast::proto_iter, func._0,
210+
func._1, func._2, func._3);
143211
}
144212
case ('N') {
145213
auto abi;
@@ -170,7 +238,8 @@ fn parse_ty(@pstate st, str_def sd) -> ty::t {
170238
ident=name,
171239
inputs=func._0,
172240
output=func._1,
173-
cf=func._2)];
241+
cf=func._2,
242+
constrs=func._3)];
174243
}
175244
st.pos += 1u;
176245
ret ty::mk_obj(st.tcx, methods);
@@ -250,7 +319,8 @@ fn parse_hex(@pstate st) -> uint {
250319
}
251320

252321
fn parse_ty_fn(@pstate st, str_def sd) -> tup(vec[ty::arg], ty::t,
253-
ast::controlflow) {
322+
ast::controlflow,
323+
vec[@ast::constr]) {
254324
assert (next(st) as char == '[');
255325
let vec[ty::arg] inputs = [];
256326
while (peek(st) as char != ']') {
@@ -262,15 +332,17 @@ fn parse_ty_fn(@pstate st, str_def sd) -> tup(vec[ty::arg], ty::t,
262332
inputs += [rec(mode=mode, ty=parse_ty(st, sd))];
263333
}
264334
st.pos = st.pos + 1u;
335+
auto cs = parse_constrs(st, sd);
265336
auto res = parse_ty_or_bang(st, sd);
266337
alt (res) {
267338
case (a_bang) {
268-
ret tup(inputs, ty::mk_bot(st.tcx), ast::noreturn);
339+
ret tup(inputs, ty::mk_bot(st.tcx), ast::noreturn, cs);
269340
}
270341
case (a_ty(?t)) {
271-
ret tup(inputs, t, ast::return);
342+
ret tup(inputs, t, ast::return, cs);
272343
}
273344
}
345+
274346
}
275347

276348

@@ -579,7 +651,7 @@ fn get_tag_variants(ty::ctxt tcx, ast::def_id def)
579651
auto ctor_ty = item_type(item, external_crate_id, tcx);
580652
let vec[ty::t] arg_tys = [];
581653
alt (ty::struct(tcx, ctor_ty)) {
582-
case (ty::ty_fn(_, ?args, _, _)) {
654+
case (ty::ty_fn(_, ?args, _, _, _)) {
583655
for (ty::arg a in args) {
584656
arg_tys += [a.ty];
585657
}

src/comp/front/lexer.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import std::option::some;
99
import std::option::none;
1010
import driver::session::session;
1111
import util::common;
12-
import util::common::new_str_hash;
12+
import util::common::*;
1313
import util::data::interner;
1414

1515
state type reader = state obj {
@@ -111,32 +111,6 @@ fn new_reader(session sess, io::reader rdr,
111111
ret rd;
112112
}
113113

114-
fn in_range(char c, char lo, char hi) -> bool {
115-
ret lo <= c && c <= hi;
116-
}
117-
118-
fn is_alpha(char c) -> bool {
119-
ret in_range(c, 'a', 'z') ||
120-
in_range(c, 'A', 'Z');
121-
}
122-
123-
fn is_dec_digit(char c) -> bool {
124-
ret in_range(c, '0', '9');
125-
}
126-
127-
fn is_alnum(char c) -> bool {
128-
ret is_alpha(c) || is_dec_digit(c);
129-
}
130-
131-
fn is_hex_digit(char c) -> bool {
132-
ret in_range(c, '0', '9') ||
133-
in_range(c, 'a', 'f') ||
134-
in_range(c, 'A', 'F');
135-
}
136-
137-
fn is_bin_digit(char c) -> bool {
138-
ret c == '0' || c == '1';
139-
}
140114

141115
fn dec_digit_val(char c) -> int {
142116
ret (c as int) - ('0' as int);

src/comp/front/parser.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,7 @@ fn parse_ty_fn(ast::proto proto, &parser p, uint lo)
345345
auto inputs = parse_seq(token::LPAREN, token::RPAREN,
346346
some(token::COMMA), parse_fn_input_ty, p);
347347

348-
// FIXME: dropping constrs on the floor at the moment.
349-
// pick them up when they're used by typestate pass.
350-
parse_constrs(p);
348+
auto constrs = parse_constrs(p);
351349

352350
let @ast::ty output;
353351
auto cf = ast::return;
@@ -367,7 +365,7 @@ fn parse_ty_fn(ast::proto proto, &parser p, uint lo)
367365
output = @spanned(lo, inputs.span.hi, ast::ty_nil);
368366
}
369367

370-
ret ast::ty_fn(proto, inputs.node, output, cf);
368+
ret ast::ty_fn(proto, inputs.node, output, cf, constrs.node);
371369
}
372370

373371
fn parse_proto(&parser p) -> ast::proto {
@@ -386,10 +384,11 @@ fn parse_ty_obj(&parser p, &mutable uint hi) -> ast::ty_ {
386384
auto f = parse_ty_fn(proto, p, flo);
387385
expect(p, token::SEMI);
388386
alt (f) {
389-
case (ast::ty_fn(?proto, ?inputs, ?output, ?cf)) {
387+
case (ast::ty_fn(?proto, ?inputs, ?output, ?cf, ?constrs)) {
390388
ret spanned(flo, output.span.hi,
391389
rec(proto=proto, ident=ident,
392-
inputs=inputs, output=output, cf=cf));
390+
inputs=inputs, output=output, cf=cf,
391+
constrs=constrs));
393392
}
394393
}
395394
fail;
@@ -547,15 +546,15 @@ fn parse_ty(&parser p) -> @ast::ty {
547546
auto flo = p.get_last_lo_pos();
548547
t = parse_ty_fn(ast::proto_fn, p, flo);
549548
alt (t) {
550-
case (ast::ty_fn(_, _, ?out, _)) {
549+
case (ast::ty_fn(_, _, ?out, _, _)) {
551550
hi = out.span.hi;
552551
}
553552
}
554553
} else if (eat_word(p, "iter")) {
555554
auto flo = p.get_last_lo_pos();
556555
t = parse_ty_fn(ast::proto_iter, p, flo);
557556
alt (t) {
558-
case (ast::ty_fn(_, _, ?out, _)) {
557+
case (ast::ty_fn(_, _, ?out, _, _)) {
559558
hi = out.span.hi;
560559
}
561560
}
@@ -1756,9 +1755,7 @@ fn parse_fn_decl(&parser p, ast::purity purity) -> ast::fn_decl {
17561755

17571756
let ty_or_bang res;
17581757

1759-
// FIXME: dropping constrs on the floor at the moment.
1760-
// pick them up when they're used by typestate pass.
1761-
parse_constrs(p);
1758+
auto constrs = parse_constrs(p).node;
17621759

17631760
if (p.peek() == token::RARROW) {
17641761
p.bump();
@@ -1771,13 +1768,13 @@ fn parse_fn_decl(&parser p, ast::purity purity) -> ast::fn_decl {
17711768
alt (res) {
17721769
case (a_ty(?t)) {
17731770
ret rec(inputs=inputs.node, output=t,
1774-
purity=purity, cf=ast::return);
1771+
purity=purity, cf=ast::return, constraints=constrs);
17751772
}
17761773
case (a_bang) {
17771774
ret rec(inputs=inputs.node,
17781775
output=@spanned(p.get_lo_pos(),
17791776
p.get_hi_pos(), ast::ty_bot),
1780-
purity=purity, cf=ast::noreturn);
1777+
purity=purity, cf=ast::noreturn, constraints=constrs);
17811778
}
17821779
}
17831780
}
@@ -1833,7 +1830,9 @@ fn parse_dtor(&parser p) -> @ast::method {
18331830
let ast::fn_decl d = rec(inputs=inputs,
18341831
output=output,
18351832
purity=ast::impure_fn,
1836-
cf=ast::return);
1833+
cf=ast::return,
1834+
// I guess dtors can't have constraints?
1835+
constraints=[]);
18371836
let ast::_fn f = rec(decl = d,
18381837
proto = ast::proto_fn,
18391838
body = b);

src/comp/middle/alias.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn check_call(&ctx cx, &@ast::expr f, &vec[@ast::expr] args, &scope sc)
100100
-> rec(vec[def_num] root_vars, vec[ty::t] unsafe_ts) {
101101
auto fty = ty::expr_ty(*cx.tcx, f);
102102
auto arg_ts = alt (ty::struct(*cx.tcx, fty)) {
103-
case (ty::ty_fn(_, ?args, _, _)) { args }
103+
case (ty::ty_fn(_, ?args, _, _, _)) { args }
104104
case (ty::ty_native_fn(_, ?args, _)) { args }
105105
};
106106

@@ -490,7 +490,7 @@ fn ty_can_unsafely_include(&ctx cx, ty::t needle, ty::t haystack, bool mut)
490490
ret false;
491491
}
492492
// These may contain anything.
493-
case (ty::ty_fn(_, _, _, _)) { ret true; }
493+
case (ty::ty_fn(_, _, _, _, _)) { ret true; }
494494
case (ty::ty_obj(_)) { ret true; }
495495
// A type param may include everything, but can only be treated as
496496
// opaque downstream, and is thus safe unless we saw mutable

0 commit comments

Comments
 (0)