Skip to content

Commit 199c34f

Browse files
committed
hdl.ir: improve error raised when returning None from elaborate.
Fixes #1569.
1 parent cf4a29d commit 199c34f

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

amaranth/hdl/_ir.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,24 @@ class DomainRequirementFailed(Exception):
4949
class Fragment:
5050
@staticmethod
5151
def get(obj, platform):
52-
code = None
5352
origins = []
53+
returned_by = ""
5454
while True:
5555
if isinstance(obj, Fragment):
5656
if hasattr(obj, "origins"):
5757
obj.origins = tuple(origins)
5858
return obj
5959
elif isinstance(obj, Elaboratable):
6060
code = obj.elaborate.__code__
61+
returned_by = f", returned by {code.co_filename}:{code.co_firstlineno}"
6162
UnusedElaboratable._MustUse__silence = False
6263
obj._MustUse__used = True
6364
new_obj = obj.elaborate(platform)
6465
else:
65-
raise TypeError(f"Object {obj!r} is not an 'Elaboratable' nor 'Fragment'")
66+
raise TypeError(
67+
f"Object {obj!r} is not an 'Elaboratable' nor 'Fragment'{returned_by}")
6668
if new_obj is obj:
67-
raise RecursionError(f"Object {obj!r} elaborates to itself")
68-
if new_obj is None and code is not None:
69-
warnings.warn_explicit(
70-
message=".elaborate() returned None; missing return statement?",
71-
category=UserWarning,
72-
filename=code.co_filename,
73-
lineno=code.co_firstlineno)
69+
raise RecursionError(f"Object {obj!r} elaborates to itself{returned_by}")
7470
origins.append(obj)
7571
obj = new_obj
7672

tests/test_hdl_ir.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,13 @@ def test_get_wrong_none(self):
3131
r"^Object None is not an 'Elaboratable' nor 'Fragment'$"):
3232
Fragment.get(None, platform=None)
3333

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

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

4543

0 commit comments

Comments
 (0)