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.
This commit is contained in:
Tim Chevalier 2011-06-20 17:29:54 -07:00
parent 3b6d94d489
commit 7fb35ecf84
15 changed files with 147 additions and 113 deletions

View file

@ -264,34 +264,37 @@ mod Encode {
enc_ty(w, cx, arg.ty);
}
w.write_char(']');
alt (cf) {
case (noreturn) { w.write_char('!'); }
case (_) { enc_ty(w, cx, out); }
}
auto colon = true;
for (@ty::constr_def c in constrs) {
if (colon) {
w.write_char(':');
colon = false;
} else { w.write_char(','); }
} else { w.write_char(';'); }
enc_constr(w, cx, c);
}
alt (cf) {
case (noreturn) { w.write_char('!'); }
case (_) { enc_ty(w, cx, out); }
}
}
fn enc_constr(&io::writer w, &@ctxt cx, &@ty::constr_def c) {
w.write_str(path_to_str(c.node.path));
w.write_char('(');
// FIXME
// w.write_str(cx.ds(c.node.id));
auto comma = false;
w.write_str(cx.ds(c.node.id));
w.write_char('|');
auto semi = false;
for (@constr_arg a in c.node.args) {
if (comma) { w.write_char(','); } else { comma = true; }
if (semi) { w.write_char(';'); } else { semi = true; }
alt (a.node) {
case (carg_base) { w.write_char('*'); }
case (carg_ident(?i)) { w.write_uint(i); }
case (carg_ident(?i)) {
w.write_uint(i);
}
case (carg_lit(?l)) { w.write_str(lit_to_str(l)); }
}
}
w.write_char(')');
}
}
@ -506,10 +509,12 @@ fn encode_info_for_item(@trans::crate_ctxt cx, &ebml::writer ebml_w,
encode_symbol(cx, ebml_w, item.id);
ebml::end_tag(ebml_w);
}
case (item_fn(_, ?tps)) {
case (item_fn(?fd, ?tps)) {
ebml::start_tag(ebml_w, tag_items_data_item);
encode_def_id(ebml_w, local_def(item.id));
encode_kind(ebml_w, 'f' as u8);
encode_kind(ebml_w, alt (fd.decl.purity) {
case (pure_fn) { 'p' }
case (impure_fn) { 'f' } } as u8);
encode_type_param_count(ebml_w, tps);
encode_type(cx, ebml_w, trans::node_id_type(cx, item.id));
encode_symbol(cx, ebml_w, item.id);

View file

@ -412,7 +412,7 @@ fn resolve_constr(@env e, node_id id, &@ast::constr c, &scopes sc,
lookup_path_strict(*e, sc, c.span, c.node.path.node.idents, ns_value);
if (option::is_some(new_def)) {
alt (option::get(new_def)) {
case (ast::def_fn(?pred_id)) {
case (ast::def_fn(?pred_id, _)) {
let ty::constr_general[uint] c_ =
rec(path=c.node.path, args=c.node.args, id=pred_id);
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] {
ret some(ast::def_const(local_def(i.id)));
}
}
case (ast::item_fn(_, _)) {
case (ast::item_fn(?f, _)) {
if (ns == ns_value) {
ret some(ast::def_fn(local_def(i.id)));
ret some(ast::def_fn(local_def(i.id), f.decl.purity));
}
}
case (ast::item_mod(_)) {
@ -1122,7 +1122,7 @@ fn index_nmod(&ast::native_mod md) -> mod_index {
// External lookups
fn ns_for_def(def d) -> namespace {
ret alt (d) {
case (ast::def_fn(?id)) { ns_value }
case (ast::def_fn(?id, _)) { ns_value }
case (ast::def_obj(?id)) { ns_value }
case (ast::def_obj_field(?id)) { ns_value }
case (ast::def_mod(?id)) { ns_module }

View file

@ -4681,7 +4681,7 @@ fn trans_path(&@block_ctxt cx, &ast::path p, ast::node_id id) -> lval_result {
assert (cx.fcx.llobjfields.contains_key(did._1));
ret lval_mem(cx, cx.fcx.llobjfields.get(did._1));
}
case (ast::def_fn(?did)) {
case (ast::def_fn(?did, _)) {
auto tyt = ty::lookup_item_type(cx.fcx.lcx.ccx.tcx, did);
ret lval_generic_fn(cx, tyt, did, id);
}

View file

@ -537,7 +537,7 @@ fn node_id_for_constr(ty::ctxt tcx, node_id t) -> node_id {
case (none) {
tcx.sess.bug("node_id_for_constr: bad node_id " + istr(t));
}
case (some(def_fn(?i))) { ret i._1; }
case (some(def_fn(?i,_))) { ret i._1; }
case (_) {
tcx.sess.bug("node_id_for_constr: pred is not a function");
}

View file

@ -2610,7 +2610,7 @@ fn substitute_type_params(&ctxt cx, vec[ty::t] substs, t typ) -> t {
fn def_has_ty_params(&ast::def def) -> bool {
alt (def) {
case (ast::def_fn(_)) { ret true; }
case (ast::def_fn(_,_)) { ret true; }
case (ast::def_obj(_)) { ret true; }
case (ast::def_obj_field(_)) { ret false; }
case (ast::def_mod(_)) { ret false; }

View file

@ -48,13 +48,10 @@ import middle::tstate::ann::ts_ann;
type ty_table = hashmap[ast::def_id, ty::t];
type fn_purity_table = hashmap[ast::def_id, ast::purity];
type obj_info = rec(vec[ast::obj_field] obj_fields, ast::node_id this_obj);
type crate_ctxt =
rec(mutable vec[obj_info] obj_infos,
@fn_purity_table fn_purity_table,
ty::ctxt tcx);
type fn_ctxt =
@ -91,7 +88,9 @@ fn ty_param_count_and_ty_for_def(&@fn_ctxt fcx, &span sp, &ast::def defn) ->
auto typ = ty::mk_var(fcx.ccx.tcx, fcx.locals.get(id._1));
ret tup(0u, typ);
}
case (ast::def_fn(?id)) { ret ty::lookup_item_type(fcx.ccx.tcx, id); }
case (ast::def_fn(?id, _)) {
ret ty::lookup_item_type(fcx.ccx.tcx, id);
}
case (ast::def_native_fn(?id)) {
ret ty::lookup_item_type(fcx.ccx.tcx, id);
}
@ -1271,51 +1270,24 @@ fn require_impure(&session::session sess, &ast::purity f_purity, &span sp) {
}
}
fn get_function_purity(@crate_ctxt ccx, &ast::def_id d_id) -> ast::purity {
let option::t[ast::purity] o = ccx.fn_purity_table.find(d_id);
ret from_maybe[ast::purity](ast::impure_fn, o);
}
fn require_pure_call(@crate_ctxt ccx, &ast::purity caller_purity,
&@ast::expr callee, &span sp) {
alt (caller_purity) {
case (ast::impure_fn) { ret; }
case (ast::pure_fn) {
alt (callee.node) {
case (ast::expr_path(_)) {
auto d_id;
alt (ccx.tcx.def_map.get(callee.id)) {
case (ast::def_fn(?_d_id)) { d_id = _d_id; }
}
alt (get_function_purity(ccx, d_id)) {
case (ast::pure_fn) { ret; }
case (_) {
ccx.tcx.sess.span_fatal(sp,
"Pure function calls \
impure function");
}
}
alt (ccx.tcx.def_map.get(callee.id)) {
case (ast::def_fn(_, ast::pure_fn)) {
ret;
}
case (_) {
ccx.tcx.sess.span_fatal(sp,
"Pure function calls \
unknown function");
"Pure function calls function not known to be pure");
}
}
}
}
}
fn require_pure_function(@crate_ctxt ccx, &ast::def_id d_id, &span sp) {
alt (get_function_purity(ccx, d_id)) {
case (ast::impure_fn) {
ccx.tcx.sess.span_fatal(sp,
"Found non-predicate in check expression");
}
case (_) { ret; }
}
}
fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
// fcx.ccx.tcx.sess.span_warn(expr.span, "typechecking expr " +
// pretty::pprust::expr_to_str(expr));
@ -1428,9 +1400,15 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
case (ast::expr_call(?operator, ?operands)) {
alt (operator.node) {
case (ast::expr_path(?oper_name)) {
auto d_id;
alt (fcx.ccx.tcx.def_map.get(operator.id)) {
case (ast::def_fn(?_d_id)) { d_id = _d_id; }
case (ast::def_fn(?_d_id, ast::pure_fn)) {
// do nothing
}
case (_) {
fcx.ccx.tcx.sess.span_fatal(operator.span,
"non-predicate as operator \
in constraint");
}
}
for (@ast::expr operand in operands) {
if (!ast::is_constraint_arg(operand)) {
@ -1439,7 +1417,6 @@ fn check_expr(&@fn_ctxt fcx, &@ast::expr expr) {
fcx.ccx.tcx.sess.span_fatal(e.span, s);
}
}
require_pure_function(fcx.ccx, d_id, e.span);
}
case (_) {
auto s = "In a constraint, expected the \
@ -2193,14 +2170,15 @@ fn get_obj_info(&@crate_ctxt ccx) -> option::t[obj_info] {
fn ast_constr_to_constr(ty::ctxt tcx, &@ast::constr c)
-> @ty::constr_def {
alt (tcx.def_map.find(c.node.id)) {
case (some(ast::def_fn(?pred_id))) {
case (some(ast::def_fn(?pred_id, ast::pure_fn))) {
ret @respan(c.span, rec(path=c.node.path, args=c.node.args,
id=pred_id));
}
case (_) {
tcx.sess.span_fatal(c.span, "Predicate "
+ path_to_str(c.node.path)
+ " is unbound or bound to a non-function");
+ " is unbound or bound to a non-function or an\
impure function");
}
}
}
@ -2372,30 +2350,12 @@ fn check_item(@crate_ctxt ccx, &@ast::item it) {
}
}
fn mk_fn_purity_table(&@ast::crate crate) -> @fn_purity_table {
auto res = @new_def_hash[ast::purity]();
fn do_one(@fn_purity_table t, &@ast::item i) {
alt (i.node) {
case (ast::item_fn(?f, _)) {
t.insert(local_def(i.id), f.decl.purity);
}
case (_) { }
}
}
auto do_one_fn = bind do_one(res, _);
auto v = walk::default_visitor();
auto add_fn_entry_visitor = rec(visit_item_post=do_one_fn with v);
walk::walk_crate(add_fn_entry_visitor, *crate);
ret res;
}
fn check_crate(&ty::ctxt tcx, &@ast::crate crate) {
collect::collect_item_types(tcx, crate);
let vec[obj_info] obj_infos = [];
auto fpt = mk_fn_purity_table(crate); // use a variation on collect
auto ccx =
@rec(mutable obj_infos=obj_infos, fn_purity_table=fpt, tcx=tcx);
@rec(mutable obj_infos=obj_infos, tcx=tcx);
auto visit =
rec(visit_item_pre=bind check_item(ccx, _)
with walk::default_visitor());