Typevars cause Option::unwrap() on a None value', nac3core/src/codegen/expr.rs:500:51 #229

Closed
opened 2022-03-20 22:39:36 +08:00 by sb10q · 4 comments
Owner

https://github.com/m-labs/artiq/issues/1207

from typing import TypeVar, Generic

from numpy import int32

from artiq.experiment import *
from artiq.coredevice.core import Core


@nac3
class A:
    @kernel
    def do(self) -> int32:
        return 0


@nac3
class B:
    @kernel
    def do(self) -> int32:
        return 1


T = TypeVar("T", A, B)


@nac3
class C(Generic[T]):
    sub: Kernel[T]

    def __init__(self, which):
        if which:
            self.sub = B()
        else:
            self.sub = A()

    @kernel
    def do(self) -> int32:
        return self.sub.do()


U = TypeVar("U", C[A], C[B])

@nac3
class Types(Generic[U], EnvExperiment):
    core: KernelInvariant[Core]
    c0: Kernel[U]
    c1: Kernel[U]
    
    def build(self):
        self.setattr_device("core")
        self.c0 = C(False)
        self.c1 = C(True)
    
    @kernel
    def run(self):
        print_rpc(self.c0.do() + self.c1.do())
thread '<unnamed>' panicked at 'called `Option::unwrap()` on a `None` value', nac3core/src/codegen/expr.rs:500:51
https://github.com/m-labs/artiq/issues/1207 ```python from typing import TypeVar, Generic from numpy import int32 from artiq.experiment import * from artiq.coredevice.core import Core @nac3 class A: @kernel def do(self) -> int32: return 0 @nac3 class B: @kernel def do(self) -> int32: return 1 T = TypeVar("T", A, B) @nac3 class C(Generic[T]): sub: Kernel[T] def __init__(self, which): if which: self.sub = B() else: self.sub = A() @kernel def do(self) -> int32: return self.sub.do() U = TypeVar("U", C[A], C[B]) @nac3 class Types(Generic[U], EnvExperiment): core: KernelInvariant[Core] c0: Kernel[U] c1: Kernel[U] def build(self): self.setattr_device("core") self.c0 = C(False) self.c1 = C(True) @kernel def run(self): print_rpc(self.c0.do() + self.c1.do()) ``` ``` thread '<unnamed>' panicked at 'called `Option::unwrap()` on a `None` value', nac3core/src/codegen/expr.rs:500:51 ```
Author
Owner

(This is probably not the right way to use Typevar/Generic, regardless it should not panic)

(This is probably not the right way to use Typevar/Generic, regardless it should not panic)
Contributor

btw the code is wrong: you should use

U = TypeVar("U", C[A], C[B])
U2 = TypeVar("U", C[A], C[B])

@nac3
class Types(Generic[U2], EnvExperiment):
    core: KernelInvariant[Core]
    c0: Kernel[U]
    c1: Kernel[U2]

Otherwise the type of c0 and c1 will be the same.

btw the code is wrong: you should use ```python U = TypeVar("U", C[A], C[B]) U2 = TypeVar("U", C[A], C[B]) @nac3 class Types(Generic[U2], EnvExperiment): core: KernelInvariant[Core] c0: Kernel[U] c1: Kernel[U2] ``` Otherwise the type of c0 and c1 will be the same.
Author
Owner
U = TypeVar("U", C[A], C[B])
V = TypeVar("V", C[A], C[B])

@nac3
class Types(Generic[U, V], EnvExperiment):
    core: KernelInvariant[Core]
    c0: Kernel[U]
    c1: Kernel[V]

still causes the Rust panic at the same spot.

```python U = TypeVar("U", C[A], C[B]) V = TypeVar("V", C[A], C[B]) @nac3 class Types(Generic[U, V], EnvExperiment): core: KernelInvariant[Core] c0: Kernel[U] c1: Kernel[V] ``` still causes the Rust panic at the same spot.
Contributor

Yes, I'm debugging the issue.

Yes, I'm debugging the issue.
sb10q closed this issue 2022-03-24 22:23:30 +08:00
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: M-Labs/nac3#229
No description provided.