Skip to content

Commit f27560f

Browse files
committed
hdl.ir: bring new naming rules to instances
1 parent d041ff3 commit f27560f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

amaranth/hdl/_ir.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import enum
44
import warnings
55

6-
from .._utils import flatten
6+
from .._utils import flatten, validate_name
77
from .. import tracer, _unused
88
from . import _ast, _cd, _ir, _nir
99

@@ -309,11 +309,13 @@ class Instance(Fragment):
309309
def __init__(self, type, *args, src_loc=None, src_loc_at=0, **kwargs):
310310
super().__init__(src_loc=src_loc or tracer.get_src_loc(src_loc_at))
311311

312+
validate_name(type, "Instance type")
312313
self.type = type
313314
self.parameters = OrderedDict()
314315
self.named_ports = OrderedDict()
315316

316317
for (kind, name, value) in args:
318+
validate_name(name, "Instance argument name")
317319
if kind == "a":
318320
self.attrs[name] = value
319321
elif kind == "p":
@@ -331,6 +333,7 @@ def __init__(self, type, *args, src_loc=None, src_loc_at=0, **kwargs):
331333
.format((kind, name, value)))
332334

333335
for kw, arg in kwargs.items():
336+
validate_name(kw, "Instance keyword argument name")
334337
if kw.startswith("a_"):
335338
self.attrs[kw[2:]] = arg
336339
elif kw.startswith("p_"):

tests/test_hdl_ir.py

+11
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,17 @@ def test_construct(self):
734734
("io6", (io6, "io")),
735735
]))
736736

737+
def test_wrong_name(self):
738+
with self.assertRaisesRegex(TypeError,
739+
r"^Instance type must be a string, not 1$"):
740+
Instance(1)
741+
with self.assertRaisesRegex(TypeError,
742+
r"^Instance argument name must be a string, not 1$"):
743+
Instance("foo", ("a", 1, 2))
744+
with self.assertRaisesRegex(NameError,
745+
r"^Instance keyword argument name ' ' contains whitespace/control character ' '$"):
746+
Instance("foo", **{" ": 2})
747+
737748
def test_cast_ports(self):
738749
inst = Instance("foo",
739750
("i", "s1", 1),

0 commit comments

Comments
 (0)