add error for gt and lt comparison of invalid types

This commit is contained in:
Andrew Kelley 2016-04-06 14:15:20 -07:00
parent 7bb67b1fd0
commit 57688dea36
2 changed files with 18 additions and 5 deletions

View File

@ -2845,8 +2845,20 @@ static TypeTableEntry *analyze_bool_bin_op_expr(CodeGen *g, ImportTableEntry *im
TypeTableEntry *resolved_type = resolve_peer_type_compatibility(g, import, context, node,
op_nodes, op_types, 2);
bool type_can_gt_lt_cmp = (resolved_type->id == TypeTableEntryIdNumLitFloat ||
resolved_type->id == TypeTableEntryIdNumLitInt ||
resolved_type->id == TypeTableEntryIdFloat ||
resolved_type->id == TypeTableEntryIdInt);
if (resolved_type->id == TypeTableEntryIdInvalid) {
return g->builtin_types.entry_invalid;
} else if (bin_op_type != BinOpTypeCmpEq &&
bin_op_type != BinOpTypeCmpNotEq &&
!type_can_gt_lt_cmp)
{
add_node_error(g, node,
buf_sprintf("operator not allowed for type '%s'", buf_ptr(&resolved_type->name)));
return g->builtin_types.entry_invalid;
}
ConstExprValue *op1_val = &get_resolved_expr(op1)->const_val;
@ -2856,11 +2868,7 @@ static TypeTableEntry *analyze_bool_bin_op_expr(CodeGen *g, ImportTableEntry *im
}
bool answer;
if (resolved_type->id == TypeTableEntryIdNumLitFloat ||
resolved_type->id == TypeTableEntryIdNumLitInt ||
resolved_type->id == TypeTableEntryIdFloat ||
resolved_type->id == TypeTableEntryIdInt)
{
if (type_can_gt_lt_cmp) {
bool (*bignum_cmp)(BigNum *, BigNum *);
if (bin_op_type == BinOpTypeCmpEq) {
bignum_cmp = bignum_cmp_eq;

View File

@ -1776,6 +1776,11 @@ fn f() {
const foo = "a
b";
)SOURCE", 1, ".tmp_source.zig:2:13: error: use raw string for multiline string literal");
add_compile_fail_case("invalid comparison for function pointers", R"SOURCE(
fn foo() {}
const invalid = foo > foo;
)SOURCE", 1, ".tmp_source.zig:3:21: error: operator not allowed for type 'fn()'");
}
//////////////////////////////////////////////////////////////////////////////