Repository: amaranth-lang/amaranth
Pull request #1017 (2024-01-05 13:59:50)

sim: fix ValueCastable not being recognized as a coroutine command

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1017?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`4014aef`)](https://app.codecov.io/gh/amaranth-lang/amaranth/commit/4014aef0330ea459d35a9995bb8d7f49ae0cba11?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) 86.54% compared to head [(`401018f`)](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1017?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) 86.54%.

> :exclamation: Current head 401018f differs from pull request most recent head 3633f7b. Consider uploading reports for the commit 3633f7b to get more accurate results


<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main    #1017   +/-   ##
=======================================
  Coverage   86.54%   86.54%           
=======================================
  Files          41       41           
  Lines        7051     7053    +2     
  Branches     1678     1679    +1     
=======================================
+ Hits         6102     6104    +2     
  Misses        788      788           
  Partials      161      161           
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1017?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang).


Patch:
diff --git a/amaranth/sim/_pycoro.py b/amaranth/sim/_pycoro.py
index 10ce1ca..e2714a2 100644
--- a/amaranth/sim/_pycoro.py
+++ b/amaranth/sim/_pycoro.py
@@ -1,7 +1,7 @@
 import inspect
 
 from ..hdl import *
-from ..hdl.ast import Statement, SignalSet
+from ..hdl.ast import Statement, SignalSet, ValueCastable
 from .core import Tick, Settle, Delay, Passive, Active
 from ._base import BaseProcess
 from ._pyrtl import _ValueCompiler, _RHSValueCompiler, _StatementCompiler
@@ -66,6 +66,8 @@ class PyCoroProcess(BaseProcess):
                     command = self.default_cmd
                 response = None
 
+                if isinstance(command, ValueCastable):
+                    command = Value.cast(command)
                 if isinstance(command, Value):
                     exec(_RHSValueCompiler.compile(self.state, command, mode="curr"),
                         self.exec_locals)


Test patch:
diff --git a/tests/test_sim.py b/tests/test_sim.py
index 19e493b..6f561a4 100644
--- a/tests/test_sim.py
+++ b/tests/test_sim.py
@@ -690,6 +690,27 @@ class SimulatorIntegrationTestCase(FHDLTestCase):
             sim.add_process(process)
         self.assertTrue(survived)
 
+    def test_value_castable(self):
+        class MyValue(ValueCastable):
+            @ValueCastable.lowermethod
+            def as_value(self):
+                return Signal()
+            
+            def shape():
+                return unsigned(1)
+        
+        a = Array([1,2,3])
+        a[MyValue()]
+
+        survived = False
+        with self.assertSimulation(Module()) as sim:
+            def process():
+                nonlocal survived
+                yield MyValue()
+                survived = True
+            sim.add_process(process)
+        self.assertTrue(survived)
+
     def setUp_memory(self, rd_synchronous=True, rd_transparent=True, wr_granularity=None):
         self.m = Module()
         self.memory = Memory(width=8, depth=4, init=[0xaa, 0x55])