Check well-formedness of constraints

Check that the operand in a constraint is an explicit name,
and that the operands are all local variables or literals. Still need
to check that the name refers to a pure function.
This commit is contained in:
Tim Chevalier 2011-05-02 14:28:35 -07:00 committed by Graydon Hoare
parent bc5650a9d0
commit 3060eadcba
4 changed files with 63 additions and 6 deletions

View file

@ -563,6 +563,20 @@ fn is_call_expr(@expr e) -> bool {
}
}
fn is_constraint_arg(@expr e) -> bool {
alt (e.node) {
case (expr_lit(_,_)) {
ret true;
}
case (expr_path(_, option.some[def](def_local(_)), _)) {
ret true;
}
case (_) {
ret false;
}
}
}
//
// Local Variables:
// mode: rust

View file

@ -1964,12 +1964,29 @@ fn check_expr(&@fn_ctxt fcx, @ast.expr expr) -> @ast.expr {
literals or slots */
alt (e.node) {
case (ast.expr_call(?operator, ?operands, _)) {
/* operator must be a pure function */
/* FIXME: need more checking */
ret @fold.respan[ast.expr_]
(expr.span, ast.expr_check(expr_t,
plain_ann(fcx.ccx.tcx)));
alt (operator.node) {
case (ast.expr_path(?oper_name, ?d_id, _)) {
for (@ast.expr operand in operands) {
if (! ast.is_constraint_arg(operand)) {
fcx.ccx.sess.span_err(expr.span,
"Constraint args must be "
+ "slot variables or literals");
}
}
/* operator must be a pure function */
/* FIXME: need more checking */
ret @fold.respan[ast.expr_]
(expr.span, ast.expr_check(expr_t,
plain_ann(fcx.ccx.tcx)));
}
case (_) {
fcx.ccx.sess.span_err(expr.span,
"In a constraint, expected the constraint name "
+ "to be an explicit name");
}
}
}
case (_) {
fcx.ccx.sess.span_err(expr.span,

View file

@ -0,0 +1,15 @@
// -*- rust -*-
// xfail-boot
// error-pattern: expected the constraint name
obj f () {
fn g (int q) -> bool {
ret true;
}
}
fn main() {
auto z = f ();
check (z.g)(42); // should fail to typecheck, as z.g isn't an explicit name
}

View file

@ -0,0 +1,11 @@
// -*- rust -*-
// xfail-boot
// error-pattern: Constraint args must be
fn f(int q) -> bool { ret true; }
fn main() {
// should fail to typecheck, as pred args must be slot variables or literals
check f(42 * 17);
}