Repository: amaranth-lang/amaranth
Pull request #1013 (2024-01-03 13:12:05)

hdl.ast: fix Array not being indexable by ValueCastable

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1013?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) Report
Attention: `2 lines` in your changes are missing coverage. Please review.
> Comparison is base [(`5d9ad62`)](https://app.codecov.io/gh/amaranth-lang/amaranth/commit/5d9ad62f368d28122cd0ddc6b7fb459da6d180d8?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) 86.61% compared to head [(`947e38d`)](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1013?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) 86.59%.

| [Files](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1013?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) | Patch % | Lines |
|---|---|---|
| [amaranth/hdl/ast.py](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1013?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang#diff-YW1hcmFudGgvaGRsL2FzdC5weQ==) | 0.00% | [1 Missing and 1 partial :warning: ](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1013?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) |

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


```diff
@@            Coverage Diff             @@
##             main    #1013      +/-   ##
==========================================
- Coverage   86.61%   86.59%   -0.03%     
==========================================
  Files          41       41              
  Lines        7039     7041       +2     
  Branches     1676     1677       +1     
==========================================
  Hits         6097     6097              
- Misses        781      782       +1     
- Partials      161      162       +1     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1013?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/hdl/ast.py b/amaranth/hdl/ast.py
index 70e4763..c7822aa 100644
--- a/amaranth/hdl/ast.py
+++ b/amaranth/hdl/ast.py
@@ -1382,6 +1382,8 @@ class Array(MutableSequence):
         self._mutable  = True
 
     def __getitem__(self, index):
+        if isinstance(index, ValueCastable):
+            index = Value.cast(index)
         if isinstance(index, Value):
             if self._mutable:
                 self._proxy_at = tracer.get_src_loc()


Test patch:
diff --git a/tests/test_hdl_ast.py b/tests/test_hdl_ast.py
index b97b2a1..2d9ef2a 100644
--- a/tests/test_hdl_ast.py
+++ b/tests/test_hdl_ast.py
@@ -1005,6 +1005,18 @@ class ArrayTestCase(FHDLTestCase):
                 r"^Array can no longer be mutated after it was indexed with a value at "):
             a.insert(1, 2)
 
+    def test_index_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()]
+
     def test_repr(self):
         a = Array([1,2,3])
         self.assertEqual(repr(a), "(array mutable [1, 2, 3])")