1# test syntax and type errors specific to viper code generation 2 3 4def test(code): 5 try: 6 exec(code) 7 except (SyntaxError, ViperTypeError, NotImplementedError) as e: 8 print(repr(e)) 9 10 11# viper: annotations must be identifiers 12test("@micropython.viper\ndef f(a:1): pass") 13test("@micropython.viper\ndef f() -> 1: pass") 14 15# unknown type 16test("@micropython.viper\ndef f(x:unknown_type): pass") 17 18# local used before type known 19test( 20 """ 21@micropython.viper 22def f(): 23 print(x) 24 x = 1 25""" 26) 27 28# type mismatch storing to local 29test( 30 """ 31@micropython.viper 32def f(): 33 x = 1 34 y = [] 35 x = y 36""" 37) 38 39# can't implicitly convert type to bool 40test( 41 """ 42@micropython.viper 43def f(): 44 x = ptr(0) 45 if x: 46 pass 47""" 48) 49 50# incorrect return type 51test("@micropython.viper\ndef f() -> int: return []") 52 53# can't do binary op between incompatible types 54test("@micropython.viper\ndef f(): 1 + []") 55test("@micropython.viper\ndef f(x:int, y:uint): x < y") 56 57# can't load 58test("@micropython.viper\ndef f(): 1[0]") 59test("@micropython.viper\ndef f(): 1[x]") 60 61# can't store 62test("@micropython.viper\ndef f(): 1[0] = 1") 63test("@micropython.viper\ndef f(): 1[x] = 1") 64test("@micropython.viper\ndef f(x:int): x[0] = x") 65test("@micropython.viper\ndef f(x:ptr32): x[0] = None") 66test("@micropython.viper\ndef f(x:ptr32): x[x] = None") 67 68# must raise an object 69test("@micropython.viper\ndef f(): raise 1") 70 71# unary ops not implemented 72test("@micropython.viper\ndef f(x:int): +x") 73test("@micropython.viper\ndef f(x:int): -x") 74test("@micropython.viper\ndef f(x:int): ~x") 75 76# binary op not implemented 77test("@micropython.viper\ndef f(x:uint, y:uint): res = x // y") 78test("@micropython.viper\ndef f(x:uint, y:uint): res = x % y") 79test("@micropython.viper\ndef f(x:int): res = x in x") 80 81# yield (from) not implemented 82test("@micropython.viper\ndef f(): yield") 83test("@micropython.viper\ndef f(): yield from f") 84 85# passing a ptr to a Python function not implemented 86test("@micropython.viper\ndef f(): print(ptr(1))") 87 88# cast of a casting identifier not implemented 89test("@micropython.viper\ndef f(): int(int)") 90