Skip to content

Commit 9f5f156

Browse files
committed
py/emitnative: Raise ViperTypeError for unsupported unary ops.
1 parent 7e12a60 commit 9f5f156

3 files changed

Lines changed: 20 additions & 7 deletions

File tree

py/emitnative.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,14 +1974,19 @@ STATIC void emit_native_pop_except(emit_t *emit) {
19741974
STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
19751975
vtype_kind_t vtype;
19761976
emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1977-
assert(vtype == VTYPE_PYOBJ);
1978-
if (op == MP_UNARY_OP_NOT) {
1979-
// we need to synthesise this operation by converting to bool first
1980-
emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
1981-
ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
1977+
if (vtype == VTYPE_PYOBJ) {
1978+
if (op == MP_UNARY_OP_NOT) {
1979+
// we need to synthesise this operation by converting to bool first
1980+
emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
1981+
ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
1982+
}
1983+
emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
1984+
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1985+
} else {
1986+
adjust_stack(emit, 1);
1987+
EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1988+
"unary op %q not implemented", mp_unary_op_method_name[op]);
19821989
}
1983-
emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
1984-
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
19851990
}
19861991

19871992
STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {

tests/micropython/viper_error.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,8 @@ def f():
5252

5353
# must raise an object
5454
test("@micropython.viper\ndef f(): raise 1")
55+
56+
# unary ops not implemented
57+
test("@micropython.viper\ndef f(x:int): +x")
58+
test("@micropython.viper\ndef f(x:int): -x")
59+
test("@micropython.viper\ndef f(x:int): ~x")

tests/micropython/viper_error.py.exp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ ViperTypeError("can't load from 'int'",)
1010
ViperTypeError("can't store to 'int'",)
1111
ViperTypeError("can't store to 'int'",)
1212
ViperTypeError('must raise an object',)
13+
ViperTypeError('unary op __pos__ not implemented',)
14+
ViperTypeError('unary op __neg__ not implemented',)
15+
ViperTypeError('unary op __invert__ not implemented',)

0 commit comments

Comments
 (0)