Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions amaranth/hdl/_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,24 @@ class DomainRequirementFailed(Exception):
class Fragment:
@staticmethod
def get(obj, platform):
code = None
origins = []
returned_by = ""
while True:
if isinstance(obj, Fragment):
if hasattr(obj, "origins"):
obj.origins = tuple(origins)
return obj
elif isinstance(obj, Elaboratable):
code = obj.elaborate.__code__
returned_by = f", returned by {code.co_filename}:{code.co_firstlineno}"
UnusedElaboratable._MustUse__silence = False
obj._MustUse__used = True
new_obj = obj.elaborate(platform)
else:
raise TypeError(f"Object {obj!r} is not an 'Elaboratable' nor 'Fragment'")
raise TypeError(
f"Object {obj!r} is not an 'Elaboratable' nor 'Fragment'{returned_by}")
if new_obj is obj:
raise RecursionError(f"Object {obj!r} elaborates to itself")
if new_obj is None and code is not None:
warnings.warn_explicit(
message=".elaborate() returned None; missing return statement?",
category=UserWarning,
filename=code.co_filename,
lineno=code.co_firstlineno)
raise RecursionError(f"Object {obj!r} elaborates to itself{returned_by}")
origins.append(obj)
obj = new_obj

Expand Down
10 changes: 4 additions & 6 deletions tests/test_hdl_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@ def test_get_wrong_none(self):
r"^Object None is not an 'Elaboratable' nor 'Fragment'$"):
Fragment.get(None, platform=None)

with self.assertWarnsRegex(UserWarning,
r"^\.elaborate\(\) returned None; missing return statement\?$"):
with self.assertRaisesRegex(TypeError,
r"^Object None is not an 'Elaboratable' nor 'Fragment'$"):
Fragment.get(ElaboratesToNone(), platform=None)
with self.assertRaisesRegex(TypeError,
r"^Object None is not an 'Elaboratable' nor 'Fragment', returned by .+?:19$"):
Fragment.get(ElaboratesToNone(), platform=None)

def test_get_wrong_self(self):
with self.assertRaisesRegex(RecursionError,
r"^Object <.+?ElaboratesToSelf.+?> elaborates to itself$"):
r"^Object <.+?ElaboratesToSelf.+?> elaborates to itself, returned by .+?:24$"):
Fragment.get(ElaboratesToSelf(), platform=None)


Expand Down
Loading