Skip to content

Commit 8678d5f

Browse files
committed
hdl.dsl: warn if a case is defined after a default case.
1 parent e9299cc commit 8678d5f

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

amaranth/hdl/dsl.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ def Case(self, *patterns):
308308
src_loc = tracer.get_src_loc(src_loc_at=1)
309309
switch_data = self._get_ctrl("Switch")
310310
new_patterns = ()
311+
if () in switch_data["cases"]:
312+
warnings.warn("A case defined after the default case will never be active",
313+
SyntaxWarning, stacklevel=3)
311314
# This code should accept exactly the same patterns as `v.matches(...)`.
312315
for pattern in patterns:
313316
if isinstance(pattern, str) and any(bit not in "01- \t" for bit in pattern):
@@ -357,6 +360,9 @@ def Default(self):
357360
self._check_context("Default", context="Switch")
358361
src_loc = tracer.get_src_loc(src_loc_at=1)
359362
switch_data = self._get_ctrl("Switch")
363+
if () in switch_data["cases"]:
364+
warnings.warn("A case defined after the default case will never be active",
365+
SyntaxWarning, stacklevel=3)
360366
try:
361367
_outer_case, self._statements = self._statements, []
362368
self._ctrl_context = None

tests/test_hdl_dsl.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,26 @@ def test_Case_outside_Switch_wrong(self):
497497
with m.Case():
498498
pass
499499

500+
def test_Case_after_Default_wrong(self):
501+
m = Module()
502+
with m.Switch(self.w1):
503+
with m.Default():
504+
pass
505+
with self.assertWarnsRegex(SyntaxWarning,
506+
r"^A case defined after the default case will never be active$"):
507+
with m.Case():
508+
pass
509+
510+
def test_Default_after_Default_wrong(self):
511+
m = Module()
512+
with m.Switch(self.w1):
513+
with m.Default():
514+
pass
515+
with self.assertWarnsRegex(SyntaxWarning,
516+
r"^A case defined after the default case will never be active$"):
517+
with m.Default():
518+
pass
519+
500520
def test_If_inside_Switch_wrong(self):
501521
m = Module()
502522
with m.Switch(self.s1):

0 commit comments

Comments
 (0)