Skip to content

Commit 7fb35ec

Browse files
committed
Serialize constraints in types (literal arguments still not supported)
This involved, in part, changing the ast::def type so that a def_fn has a "purity" field. This lets the typechecker determine whether functions defined in other crates are pure. It also required updating some error messages in tests. As a test for cross-crate constrained functions, I added a safe_slice function to std::str (slice(), with one of the asserts replaced with a function precondition) and some test cases (various versions of fn-constraint.rs) that call it. Also, I changed "fn" to "pred" for some of the boolean functions in std::uint.
1 parent 3b6d94d commit 7fb35ec

File tree

15 files changed

+147
-113
lines changed

15 files changed

+147
-113
lines changed

src/comp/front/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn local_def(node_id id) -> def_id {
2727
type ty_param = ident;
2828

2929
tag def {
30-
def_fn(def_id);
30+
def_fn(def_id, purity);
3131
def_obj(def_id);
3232
def_obj_field(def_id);
3333
def_mod(def_id);
@@ -54,7 +54,7 @@ fn variant_def_ids(&def d) -> tup(def_id, def_id) {
5454

5555
fn def_id_of_def(def d) -> def_id {
5656
alt (d) {
57-
case (def_fn(?id)) { ret id; }
57+
case (def_fn(?id,_)) { ret id; }
5858
case (def_obj(?id)) { ret id; }
5959
case (def_obj_field(?id)) { ret id; }
6060
case (def_mod(?id)) { ret id; }

src/comp/front/creader.rs

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,22 @@ fn next(@pstate st) -> u8 {
5656
}
5757

5858
fn parse_ident(@pstate st, str_def sd, char last) -> ast::ident {
59+
fn is_last(char b, char c) -> bool {
60+
ret c == b;
61+
}
62+
ret parse_ident_(st, sd, bind is_last(last, _));
63+
}
64+
65+
fn parse_ident_(@pstate st, str_def sd, fn(char) -> bool is_last)
66+
-> ast::ident {
5967
auto res = "";
60-
while (peek(st) as char != last) {
68+
while (! is_last(peek(st) as char)) {
6169
res += str::unsafe_from_byte(next(st));
6270
}
6371
ret res;
6472
}
6573

74+
6675
fn parse_ty_data(vec[u8] data, int crate_num, uint pos, uint len, str_def sd,
6776
ty::ctxt tcx) -> ty::t {
6877
auto st =
@@ -85,40 +94,59 @@ fn parse_constrs(@pstate st, str_def sd) -> vec[@ty::constr_def] {
8594
do {
8695
auto ignore = next(st);
8796
vec::push(res, parse_constr(st, sd));
88-
} while (peek(st) as char == ',')
97+
} while (peek(st) as char == ';')
8998
}
9099
case (_) { }
91100
}
92101
ret res;
93102
}
94103

104+
fn parse_path(@pstate st, str_def sd) -> ast::path {
105+
let vec[ast::ident] idents = [];
106+
fn is_last(char c) -> bool {
107+
ret (c == '(' || c == ':');
108+
}
109+
idents += [parse_ident_(st, sd, is_last)];
110+
while (true) {
111+
alt (peek(st) as char) {
112+
case (':') {
113+
auto ignore = next(st);
114+
ignore = next(st);
115+
}
116+
case (?c) {
117+
if (c == '(') {
118+
ret respan(rec(lo=0u, hi=0u),
119+
rec(idents=idents, types=[]));
120+
}
121+
else {
122+
idents += [parse_ident_(st, sd, is_last)];
123+
}
124+
}
125+
}
126+
}
127+
fail "parse_path: ill-formed path";
128+
}
129+
95130
fn parse_constr(@pstate st, str_def sd) -> @ty::constr_def {
96-
st.tcx.sess.unimpl("Reading constraints " + " isn't implemented");
97-
/*
98131
let vec[@ast::constr_arg] args = [];
99-
auto sp = rec(lo=0u,hi=0u); // FIXME
100-
let vec[ast::ident] ids = [];
101-
let vec[@ast::ty] tys = [];
102-
let ast::path pth = respan(sp,
103-
rec(idents=ids, types=tys)); // FIXME
104-
let ast::ident p1 = parse_ident(st, sd, '(');
105-
log_err("ignore=");
106-
log_err(p1);
132+
auto sp = rec(lo=0u,hi=0u); // FIXME: use a real span
133+
let ast::path pth = parse_path(st, sd);
107134
let char ignore = next(st) as char;
108135
assert(ignore as char == '(');
109136
auto def = parse_def(st, sd);
110137
do {
111138
alt (peek(st) as char) {
112139
case ('*') {
113-
auto ignore = next(st);
140+
st.pos += 1u;
114141
args += [@respan(sp, ast::carg_base)];
115142
}
116143
case (?c) {
117-
log_err("c =");
118-
log_err(str::from_bytes([c as u8]));
119-
if (may_begin_ident(c)) {
120-
auto id = parse_ident(st, sd, ',');
121-
args += [@respan(sp, ast::carg_ident(id))];
144+
/* how will we disambiguate between
145+
an arg index and a lit argument? */
146+
if (c >= '0' && c <= '9') {
147+
// FIXME
148+
args += [@respan(sp, ast::carg_ident((c as uint) - 48u))];
149+
ignore = next(st) as char;
122150
}
123151
else {
124152
log_err("Lit args are unimplemented");
@@ -132,10 +160,10 @@ fn parse_constr(@pstate st, str_def sd) -> @ty::constr_def {
132160
*/
133161
}
134162
}
135-
} while (next(st) as char == ',');
136-
ignore = next(st) as char;
137-
*/
138-
163+
ignore = next(st) as char;
164+
} while (ignore == ';');
165+
assert(ignore == ')');
166+
ret @respan(sp, rec(path=pth, args=args, id=def));
139167
}
140168

141169
fn parse_ty(@pstate st, str_def sd) -> ty::t {
@@ -333,7 +361,7 @@ fn parse_ty_fn(@pstate st, str_def sd) ->
333361
}
334362
inputs += [rec(mode=mode, ty=parse_ty(st, sd))];
335363
}
336-
st.pos = st.pos + 1u;
364+
st.pos += 1u; // eat the ']'
337365
auto cs = parse_constrs(st, sd);
338366
auto res = parse_ty_or_bang(st, sd);
339367
alt (res) {
@@ -641,6 +669,7 @@ fn kind_has_type_params(u8 kind_ch) -> bool {
641669
ret alt (kind_ch as char) {
642670
case ('c') { false }
643671
case ('f') { true }
672+
case ('p') { true }
644673
case ('F') { true }
645674
case ('y') { true }
646675
case ('o') { true }
@@ -669,7 +698,8 @@ fn lookup_def(int cnum, vec[u8] data, &ast::def_id did_) -> ast::def {
669698
auto def =
670699
alt (kind_ch as char) {
671700
case ('c') { ast::def_const(did) }
672-
case ('f') { ast::def_fn(did) }
701+
case ('f') { ast::def_fn(did, ast::impure_fn) }
702+
case ('p') { ast::def_fn(did, ast::pure_fn) }
673703
case ('F') { ast::def_native_fn(did) }
674704
case ('y') { ast::def_ty(did) }
675705
case ('o') { ast::def_obj(did) }
@@ -781,6 +811,7 @@ fn item_kind_to_str(u8 kind) -> str {
781811
alt (kind as char) {
782812
case ('c') { ret "const"; }
783813
case ('f') { ret "fn"; }
814+
case ('p') { ret "pred"; }
784815
case ('F') { ret "native fn"; }
785816
case ('y') { ret "type"; }
786817
case ('o') { ret "obj"; }

src/comp/middle/metadata.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -264,34 +264,37 @@ mod Encode {
264264
enc_ty(w, cx, arg.ty);
265265
}
266266
w.write_char(']');
267-
alt (cf) {
268-
case (noreturn) { w.write_char('!'); }
269-
case (_) { enc_ty(w, cx, out); }
270-
}
271267
auto colon = true;
272268
for (@ty::constr_def c in constrs) {
273269
if (colon) {
274270
w.write_char(':');
275271
colon = false;
276-
} else { w.write_char(','); }
272+
} else { w.write_char(';'); }
277273
enc_constr(w, cx, c);
278274
}
275+
alt (cf) {
276+
case (noreturn) { w.write_char('!'); }
277+
case (_) { enc_ty(w, cx, out); }
278+
}
279+
279280
}
280281
fn enc_constr(&io::writer w, &@ctxt cx, &@ty::constr_def c) {
281282
w.write_str(path_to_str(c.node.path));
282283
w.write_char('(');
283-
// FIXME
284-
// w.write_str(cx.ds(c.node.id));
285-
286-
auto comma = false;
284+
w.write_str(cx.ds(c.node.id));
285+
w.write_char('|');
286+
auto semi = false;
287287
for (@constr_arg a in c.node.args) {
288-
if (comma) { w.write_char(','); } else { comma = true; }
288+
if (semi) { w.write_char(';'); } else { semi = true; }
289289
alt (a.node) {
290290
case (carg_base) { w.write_char('*'); }
291-
case (carg_ident(?i)) { w.write_uint(i); }
291+
case (carg_ident(?i)) {
292+
w.write_uint(i);
293+
}
292294
case (carg_lit(?l)) { w.write_str(lit_to_str(l)); }
293295
}
294296
}
297+
w.write_char(')');
295298
}
296299
}
297300

@@ -506,10 +509,12 @@ fn encode_info_for_item(@trans::crate_ctxt cx, &ebml::writer ebml_w,
506509
encode_symbol(cx, ebml_w, item.id);
507510
ebml::end_tag(ebml_w);
508511
}
509-
case (item_fn(_, ?tps)) {
512+
case (item_fn(?fd, ?tps)) {
510513
ebml::start_tag(ebml_w, tag_items_data_item);
511514
encode_def_id(ebml_w, local_def(item.id));
512-
encode_kind(ebml_w, 'f' as u8);
515+
encode_kind(ebml_w, alt (fd.decl.purity) {
516+
case (pure_fn) { 'p' }
517+
case (impure_fn) { 'f' } } as u8);
513518
encode_type_param_count(ebml_w, tps);
514519
encode_type(cx, ebml_w, trans::node_id_type(cx, item.id));
515520
encode_symbol(cx, ebml_w, item.id);

src/comp/middle/resolve.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ fn resolve_constr(@env e, node_id id, &@ast::constr c, &scopes sc,
412412
lookup_path_strict(*e, sc, c.span, c.node.path.node.idents, ns_value);
413413
if (option::is_some(new_def)) {
414414
alt (option::get(new_def)) {
415-
case (ast::def_fn(?pred_id)) {
415+
case (ast::def_fn(?pred_id, _)) {
416416
let ty::constr_general[uint] c_ =
417417
rec(path=c.node.path, args=c.node.args, id=pred_id);
418418
let ty::constr_def new_constr = respan(c.span, c_);
@@ -799,9 +799,9 @@ fn found_def_item(&@ast::item i, namespace ns) -> option::t[def] {
799799
ret some(ast::def_const(local_def(i.id)));
800800
}
801801
}
802-
case (ast::item_fn(_, _)) {
802+
case (ast::item_fn(?f, _)) {
803803
if (ns == ns_value) {
804-
ret some(ast::def_fn(local_def(i.id)));
804+
ret some(ast::def_fn(local_def(i.id), f.decl.purity));
805805
}
806806
}
807807
case (ast::item_mod(_)) {
@@ -1122,7 +1122,7 @@ fn index_nmod(&ast::native_mod md) -> mod_index {
11221122
// External lookups
11231123
fn ns_for_def(def d) -> namespace {
11241124
ret alt (d) {
1125-
case (ast::def_fn(?id)) { ns_value }
1125+
case (ast::def_fn(?id, _)) { ns_value }
11261126
case (ast::def_obj(?id)) { ns_value }
11271127
case (ast::def_obj_field(?id)) { ns_value }
11281128
case (ast::def_mod(?id)) { ns_module }

src/comp/middle/trans.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4681,7 +4681,7 @@ fn trans_path(&@block_ctxt cx, &ast::path p, ast::node_id id) -> lval_result {
46814681
assert (cx.fcx.llobjfields.contains_key(did._1));
46824682
ret lval_mem(cx, cx.fcx.llobjfields.get(did._1));
46834683
}
4684-
case (ast::def_fn(?did)) {
4684+
case (ast::def_fn(?did, _)) {
46854685
auto tyt = ty::lookup_item_type(cx.fcx.lcx.ccx.tcx, did);
46864686
ret lval_generic_fn(cx, tyt, did, id);
46874687
}

src/comp/middle/tstate/auxiliary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ fn node_id_for_constr(ty::ctxt tcx, node_id t) -> node_id {
537537
case (none) {
538538
tcx.sess.bug("node_id_for_constr: bad node_id " + istr(t));
539539
}
540-
case (some(def_fn(?i))) { ret i._1; }
540+
case (some(def_fn(?i,_))) { ret i._1; }
541541
case (_) {
542542
tcx.sess.bug("node_id_for_constr: pred is not a function");
543543
}

src/comp/middle/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2610,7 +2610,7 @@ fn substitute_type_params(&ctxt cx, vec[ty::t] substs, t typ) -> t {
26102610

26112611
fn def_has_ty_params(&ast::def def) -> bool {
26122612
alt (def) {
2613-
case (ast::def_fn(_)) { ret true; }
2613+
case (ast::def_fn(_,_)) { ret true; }
26142614
case (ast::def_obj(_)) { ret true; }
26152615
case (ast::def_obj_field(_)) { ret false; }
26162616
case (ast::def_mod(_)) { ret false; }

0 commit comments

Comments
 (0)