ability to cast explicitly from int to enum

This commit also fixes a bug where pure functions are marked with
the read-only attribute in debug mode. This resulted in incorrect
codegen because calls to read-only functions with unused values
were not generated.

For example, a call to assert() would not be generated if assert
is marked with read-only. Which it *is* marked with in release
mode.
This commit is contained in:
Andrew Kelley
2016-07-09 15:21:50 -07:00
parent a5251a1c10
commit 49a4b1b930
5 changed files with 42 additions and 1 deletions

View File

@@ -657,6 +657,16 @@ void eval_const_expr_implicit_cast(CastOp cast_op,
bignum_init_unsigned(&const_val->data.x_bignum, other_val->data.x_bool ? 1 : 0);
const_val->ok = true;
break;
case CastOpIntToEnum:
{
uint64_t value = other_val->data.x_bignum.data.x_uint;
assert(new_type->id == TypeTableEntryIdEnum);
assert(value < new_type->data.enumeration.field_count);
const_val->data.x_enum.tag = value;
const_val->data.x_enum.payload = NULL;
const_val->ok = true;
break;
}
}
}