Repository: amaranth-lang/amaranth
Pull request #1181 (2024-03-03 18:32:42)

hdl._xfrm: Simplify `EnableInserter` logic.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1181?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:
> Project coverage is 89.06%. Comparing base [(`7d295b0`)](https://app.codecov.io/gh/amaranth-lang/amaranth/commit/7d295b040a347e99383f4ba157382ab49b250529?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=amaranth-lang) to head [(`8aafbcd`)](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1181?src=pr&el=desc&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    #1181      +/-   ##
==========================================
- Coverage   89.09%   89.06%   -0.04%     
==========================================
  Files          43       43              
  Lines        8797     8791       -6     
  Branches     2089     2089              
==========================================
- Hits         7838     7830       -8     
- Misses        780      782       +2     
  Partials      179      179              
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/amaranth-lang/amaranth/pull/1181?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/_xfrm.py b/amaranth/hdl/_xfrm.py
index e60be23..7f61b9d 100644
--- a/amaranth/hdl/_xfrm.py
+++ b/amaranth/hdl/_xfrm.py
@@ -575,23 +575,14 @@ class ResetInserter(_ControlInserter):
         fragment.add_statements(domain, Switch(self.controls[domain], {1: stmts}, src_loc=self.src_loc))
 
 
-class _PropertyEnableInserter(StatementTransformer):
-    def __init__(self, en):
-        self.en = en
-
-    def on_Property(self, stmt):
-        return Switch(
-            self.en,
-            {1: [stmt]},
-            src_loc=stmt.src_loc,
-        )
-
-
 class EnableInserter(_ControlInserter):
     def _insert_control(self, fragment, domain, signals):
-        stmts = [s.eq(s) for s in signals]
-        fragment.add_statements(domain, Switch(self.controls[domain], {0: stmts}, src_loc=self.src_loc))
-        fragment.statements[domain] = _PropertyEnableInserter(self.controls[domain])(fragment.statements[domain])
+        if domain in fragment.statements:
+            fragment.statements[domain] = _StatementList([Switch(
+                self.controls[domain],
+                {1: fragment.statements[domain]},
+                src_loc=self.src_loc,
+            )])
 
     def on_fragment(self, fragment):
         new_fragment = super().on_fragment(fragment)


Test patch:
diff --git a/tests/test_hdl_xfrm.py b/tests/test_hdl_xfrm.py
index b13fad2..450939b 100644
--- a/tests/test_hdl_xfrm.py
+++ b/tests/test_hdl_xfrm.py
@@ -337,9 +337,8 @@ class EnableInserterTestCase(FHDLTestCase):
         f = EnableInserter(self.c1)(f)
         self.assertRepr(f.statements["sync"], """
         (
-            (eq (sig s1) (const 1'd1))
             (switch (sig c1)
-                (case 0 (eq (sig s1) (sig s1)))
+                (case 1 (eq (sig s1) (const 1'd1)))
             )
         )
         """)
@@ -359,9 +358,8 @@ class EnableInserterTestCase(FHDLTestCase):
         """)
         self.assertRepr(f.statements["pix"], """
         (
-            (eq (sig s2) (const 1'd0))
             (switch (sig c1)
-                (case 0 (eq (sig s2) (sig s2)))
+                (case 1 (eq (sig s2) (const 1'd0)))
             )
         )
         """)
@@ -380,17 +378,15 @@ class EnableInserterTestCase(FHDLTestCase):
         (f2, _, _), = f1.subfragments
         self.assertRepr(f1.statements["sync"], """
         (
-            (eq (sig s1) (const 1'd1))
             (switch (sig c1)
-                (case 0 (eq (sig s1) (sig s1)))
+                (case 1 (eq (sig s1) (const 1'd1)))
             )
         )
         """)
         self.assertRepr(f2.statements["sync"], """
         (
-            (eq (sig s2) (const 1'd1))
             (switch (sig c1)
-                (case 0 (eq (sig s2) (sig s2)))
+                (case 1 (eq (sig s2) (const 1'd1)))
             )
         )
         """)
@@ -451,9 +447,8 @@ class TransformedElaboratableTestCase(FHDLTestCase):
         f = Fragment.get(te2, None)
         self.assertRepr(f.statements["sync"], """
         (
-            (eq (sig s1) (const 1'd1))
             (switch (sig c1)
-                (case 0 (eq (sig s1) (sig s1)))
+                (case 1 (eq (sig s1) (const 1'd1)))
             )
             (switch (sig c2)
                 (case 1 (eq (sig s1) (const 1'd0)))