Skip to content

Commit

Permalink
hdl._dsl: forbid empty string as submodule name.
Browse files Browse the repository at this point in the history
This is semantically ambiguous and breaks the RTLIL emitter.

Fixes amaranth-lang#1209.
  • Loading branch information
whitequark committed Jun 10, 2024
1 parent 7870eb3 commit 182330f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions amaranth/hdl/_dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,8 @@ def _add_submodule(self, submodule, name=None, src_loc=None):
if name == None:
self._anon_submodules.append((submodule, src_loc))
else:
if name == "":
raise NameError("Submodule name must not be empty")
if name in self._named_submodules:
raise NameError(f"Submodule named '{name}' already exists")
self._named_submodules[name] = (submodule, src_loc)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_hdl_dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,12 @@ def test_submodule_named_conflict(self):
with self.assertRaisesRegex(NameError, r"^Submodule named 'foo' already exists$"):
m1.submodules.foo = m2

def test_submodule_named_empty(self):
m1 = Module()
m2 = Module()
with self.assertRaisesRegex(NameError, r"^Submodule name must not be empty$"):
m1.submodules[""] = m2

def test_submodule_get(self):
m1 = Module()
m2 = Module()
Expand Down

0 comments on commit 182330f

Please sign in to comment.