Repository: amaranth-lang/amaranth
Pull request #933 (2023-10-10 09:46:31)

lib.data: return `View` from `.const()`
`Layout.const()` is updated as per amaranth-lang/rfcs#29.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/933?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) Report
> Merging [#933](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/933?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) (60ec135) into [main](https://app.codecov.io/gh/amaranth-lang/amaranth/commit/7582ec301c2ef8cb233f6c862b2aff600b4ba603?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) (7582ec3) will **decrease** coverage by `0.40%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##             main     #933      +/-   ##
==========================================
- Coverage   83.73%   83.34%   -0.40%     
==========================================
  Files          54       54              
  Lines        7681     7672       -9     
  Branches     1868     1557     -311     
==========================================
- Hits         6432     6394      -38     
- Misses       1048     1080      +32     
+ Partials      201      198       -3     
```


| [Files](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/933?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) | Coverage Δ | |
|---|---|---|
| [amaranth/lib/data.py](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/933?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang#diff-YW1hcmFudGgvbGliL2RhdGEucHk=) | `99.32% <100.00%> (ø)` | |

... and [11 files with indirect coverage changes](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/933/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang)

:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang)


Patch:
diff --git a/amaranth/lib/data.py b/amaranth/lib/data.py
index fb231ef..480bbec 100644
--- a/amaranth/lib/data.py
+++ b/amaranth/lib/data.py
@@ -229,7 +229,7 @@ class Layout(ShapeCastable, metaclass=ABCMeta):
             mask = ((1 << cast_field_shape.width) - 1) << field.offset
             int_value &= ~mask
             int_value |= (key_value.value << field.offset) & mask
-        return Const(int_value, self.as_shape())
+        return View(self, Const(int_value, self.as_shape()))
 
 
 class StructLayout(Layout):


Test patch:
diff --git a/tests/test_lib_data.py b/tests/test_lib_data.py
index 7c62177..2aad149 100644
--- a/tests/test_lib_data.py
+++ b/tests/test_lib_data.py
@@ -382,22 +382,22 @@ class LayoutTestCase(FHDLTestCase):
             "a": unsigned(1),
             "b": unsigned(2)
         })
-        self.assertRepr(sl.const(None), "(const 3'd0)")
-        self.assertRepr(sl.const({"a": 0b1, "b": 0b10}), "(const 3'd5)")
+        self.assertRepr(sl.const(None).as_value(), "(const 3'd0)")
+        self.assertRepr(sl.const({"a": 0b1, "b": 0b10}).as_value(), "(const 3'd5)")
 
         fl = FlexibleLayout(2, {
             "a": Field(unsigned(1), 0),
             "b": Field(unsigned(2), 0)
         })
-        self.assertRepr(fl.const({"a": 0b11}), "(const 2'd1)")
-        self.assertRepr(fl.const({"b": 0b10}), "(const 2'd2)")
-        self.assertRepr(fl.const({"a": 0b1, "b": 0b10}), "(const 2'd2)")
+        self.assertRepr(fl.const({"a": 0b11}).as_value(), "(const 2'd1)")
+        self.assertRepr(fl.const({"b": 0b10}).as_value(), "(const 2'd2)")
+        self.assertRepr(fl.const({"a": 0b1, "b": 0b10}).as_value(), "(const 2'd2)")
 
         sls = StructLayout({
             "a": signed(4),
             "b": signed(4)
         })
-        self.assertRepr(sls.const({"b": 0, "a": -1}), "(const 8'd15)")
+        self.assertRepr(sls.const({"b": 0, "a": -1}).as_value(), "(const 8'd15)")
 
     def test_const_wrong(self):
         sl = StructLayout({"f": unsigned(1)})
@@ -418,7 +418,7 @@ class LayoutTestCase(FHDLTestCase):
                 return int(init, 16)
 
         sl = StructLayout({"f": CastableFromHex()})
-        self.assertRepr(sl.const({"f": "aa"}), "(const 8'd170)")
+        self.assertRepr(sl.const({"f": "aa"}).as_value(), "(const 8'd170)")
 
         with self.assertRaisesRegex(ValueError,
                 r"^Constant returned by <.+?CastableFromHex.+?>\.const\(\) must have the shape "
@@ -427,7 +427,7 @@ class LayoutTestCase(FHDLTestCase):
 
     def test_const_field_const(self):
         sl = StructLayout({"f": unsigned(1)})
-        self.assertRepr(sl.const({"f": Const(1)}), "(const 1'd1)")
+        self.assertRepr(sl.const({"f": Const(1)}).as_value(), "(const 1'd1)")
 
     def test_signal_reset(self):
         sl = StructLayout({