diff --git a/amaranth/hdl/_ir.py b/amaranth/hdl/_ir.py index f3e9fc9ae..e5af19dd8 100644 --- a/amaranth/hdl/_ir.py +++ b/amaranth/hdl/_ir.py @@ -3,7 +3,7 @@ import enum import warnings -from .._utils import flatten +from .._utils import flatten, validate_name from .. import tracer, _unused from . import _ast, _cd, _ir, _nir @@ -309,11 +309,13 @@ class Instance(Fragment): def __init__(self, type, *args, src_loc=None, src_loc_at=0, **kwargs): super().__init__(src_loc=src_loc or tracer.get_src_loc(src_loc_at)) + validate_name(type, "Instance type") self.type = type self.parameters = OrderedDict() self.named_ports = OrderedDict() for (kind, name, value) in args: + validate_name(name, "Instance argument name") if kind == "a": self.attrs[name] = value elif kind == "p": @@ -331,6 +333,7 @@ def __init__(self, type, *args, src_loc=None, src_loc_at=0, **kwargs): .format((kind, name, value))) for kw, arg in kwargs.items(): + validate_name(kw, "Instance keyword argument name") if kw.startswith("a_"): self.attrs[kw[2:]] = arg elif kw.startswith("p_"): diff --git a/tests/test_hdl_ir.py b/tests/test_hdl_ir.py index 133c32fd4..03cb9a03e 100644 --- a/tests/test_hdl_ir.py +++ b/tests/test_hdl_ir.py @@ -734,6 +734,17 @@ def test_construct(self): ("io6", (io6, "io")), ])) + def test_wrong_name(self): + with self.assertRaisesRegex(TypeError, + r"^Instance type must be a string, not 1$"): + Instance(1) + with self.assertRaisesRegex(TypeError, + r"^Instance argument name must be a string, not 1$"): + Instance("foo", ("a", 1, 2)) + with self.assertRaisesRegex(NameError, + r"^Instance keyword argument name ' ' contains whitespace/control character ' '$"): + Instance("foo", **{" ": 2}) + def test_cast_ports(self): inst = Instance("foo", ("i", "s1", 1),