nac3-spec/toy-impl/examples/a.py

66 lines
1.1 KiB
Python
Raw Normal View History

2021-01-07 11:57:28 +08:00
T = TypeVar('T')
2020-12-23 10:34:56 +08:00
2021-01-07 11:57:28 +08:00
class Foo:
a: list[int32]
b: list[int32]
2020-12-23 10:34:56 +08:00
2021-01-07 11:57:28 +08:00
def choose(t: bool, a: T, b: T) -> T:
if t:
return a
else:
return b
2020-12-23 10:34:56 +08:00
2021-01-07 11:57:28 +08:00
def set_list(ls: list[T], a: T):
# this should fail
l2 = ls
l2[-1] = a
2021-01-07 11:57:28 +08:00
def get_foo(a: Foo) -> list[int32]:
return a.a
2020-12-23 10:34:56 +08:00
2021-01-07 11:57:28 +08:00
def set_foo(a: Foo, b: Foo):
a.a[0] = b.a[0]
if True:
c = b
# this should fail
c.a = a.a
def set_foo2(a: Foo, b: Foo):
a.a[0] = b.a[0]
if True:
c = [Foo()]
c[0] = a
# this should fail
c[0].a = b.a
def set_foo3(a: Foo, b: Foo):
a.a[0] = b.a[0]
if True:
c = [Foo()]
c[0] = a
# this should fail
c[0].a = get_foo(b)
def set_foo4(a: Foo, b: Foo):
a.a[0] = b.a[0]
if True:
c = [Foo()]
d = c
d[0] = a
# this should fail
c[0].a = get_foo(b)
def set_foo5(a: Foo, b: Foo):
a.a[0] = b.a[0]
if True:
c = [Foo()]
d = c
e = d
f = e
f[0] = a
# this should fail
c[0].a = get_foo(b)
2020-12-23 16:05:45 +08:00