Skip to content

Commit a586df8

Browse files
kivikakkwhitequark
andcommitted
lib.wiring.connect: diagnostic when no connection made.
If a connect() call results in no connections being made, and it's because there were no outputs specified at all, issue an error. Tests enumerate cases per #1153 (comment). Co-authored-by: Catherine <whitequark@whitequark.org>
1 parent 09029cd commit a586df8

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

amaranth/lib/wiring.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,7 @@ def connect(m, *args, **kwargs):
13711371
* For a given path, if any of the interface objects has an input port member corresponding
13721372
to a constant value, then the rest of the interface objects must have output port members
13731373
corresponding to the same constant value.
1374+
* When connecting multiple interface objects, at least one connection must be made.
13741375
13751376
For example, if :py:`obj1` is being connected to :py:`obj2` and :py:`obj3`, and :py:`obj1.a.b`
13761377
is an output, then :py:`obj2.a.b` and :py:`obj2.a.b` must exist and be inputs. If :py:`obj2.c`
@@ -1420,10 +1421,15 @@ def connect(m, *args, **kwargs):
14201421
reasons_as_string)
14211422
signatures[handle] = obj.signature
14221423

1423-
# Collate signatures and build connections.
1424+
# Connecting zero or one signatures is OK.
1425+
if len(signatures) <= 1:
1426+
return
1427+
1428+
# Collate signatures, build connections, track whether we see any input or output.
14241429
flattens = {handle: iter(sorted(signature.members.flatten()))
14251430
for handle, signature in signatures.items()}
14261431
connections = []
1432+
any_in, any_out = False, False
14271433
# Each iteration of the outer loop is intended to connect several (usually a pair) members
14281434
# to each other, e.g. an out member `[0].a` to an in member `[1].a`. However, because we
14291435
# do not just check signatures for equality (in order to improve diagnostics), it is possible
@@ -1437,6 +1443,7 @@ def connect(m, *args, **kwargs):
14371443
# implied in the flow of each port member, so the signature members are only classified
14381444
# here to ensure they are not connected to port members.
14391445
is_first = True
1446+
first_path = None
14401447
sig_kind, out_kind, in_kind = [], [], []
14411448
for handle, flattened_members in flattens.items():
14421449
path_for_handle, member = next(flattened_members, (None, None))
@@ -1499,6 +1506,8 @@ def connect(m, *args, **kwargs):
14991506
# There are no port members at this point; we're done with this path.
15001507
continue
15011508
# There are only port members after this point.
1509+
any_in = any_in or bool(in_kind)
1510+
any_out = any_out or bool(out_kind)
15021511
is_first = True
15031512
for (path, member) in in_kind + out_kind:
15041513
member_shape = member.shape
@@ -1574,6 +1583,14 @@ def connect_dimensions(dimensions, *, out_path, in_path):
15741583
out_path=(*out_path, index), in_path=(*in_path, index))
15751584
assert out_member.dimensions == in_member.dimensions
15761585
connect_dimensions(out_member.dimensions, out_path=out_path, in_path=in_path)
1586+
1587+
# If no connections were made, and there were inputs but no outputs in the
1588+
# signatures, issue a diagnostic as this is most likely in error.
1589+
if len(connections) == 0 and any_in and not any_out:
1590+
raise ConnectionError(f"Only input to input connections have been made between several "
1591+
f"interfaces; should one of them have been flipped?")
1592+
1593+
15771594
# Now that we know all of the connections are legal, add them to the module. This is done
15781595
# instead of returning them because adding them to a non-comb domain would subtly violate
15791596
# assumptions that `connect()` is intended to provide.

tests/test_lib_wiring.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,65 @@ def test_dimension_multi(self):
980980
'(eq (sig q__a__0__0) (sig p__a__0__0))',
981981
])
982982

983+
def test_connect_none(self):
984+
# Connecting zero or more empty signatures is permitted as (a) it's not
985+
# something you can write mistakenly out by hand, and (b) it permits
986+
# generic code to expand to nothing without errors around edges.
987+
m = Module()
988+
connect(m)
989+
990+
def test_connect_empty(self):
991+
m = Module()
992+
connect(m, p=NS(signature=Signature({})))
993+
994+
def test_connect_empty_multi(self):
995+
m = Module()
996+
connect(m, p=NS(signature=Signature({})),
997+
q=NS(signature=Signature({})))
998+
999+
def test_connect_one(self):
1000+
# Connecting just one signature should be allowed for the same reasons
1001+
# as above. (It's possible to forget an argument, but that stands out.)
1002+
m = Module()
1003+
connect(m, p=NS(signature=Signature({"a": Out(1), "b": In(1)}),
1004+
a=Signal(),
1005+
b=Signal()))
1006+
1007+
def test_connect_one_in_only(self):
1008+
# As above, even if there's only inputs.
1009+
m = Module()
1010+
connect(m, p=NS(signature=Signature({"a": In(1)}),
1011+
a=Signal()))
1012+
1013+
def test_connect_multi_in_only_fails(self):
1014+
# If we're only attempting to connect multiple inputs, we're not
1015+
# actually doing anything and it's most likely a mistake.
1016+
m = Module()
1017+
with self.assertRaisesRegex(ConnectionError,
1018+
r"^Only input to input connections have been made between several interfaces; "
1019+
r"should one of them have been flipped\?$"):
1020+
connect(m,
1021+
p=NS(signature=Signature({"a": In(1), "b": In(8)}),
1022+
a=Signal(),
1023+
b=Signal(8)),
1024+
q=NS(signature=Signature({"a": In(1), "b": In(8)}),
1025+
a=Signal(),
1026+
b=Signal(8)))
1027+
1028+
def test_connect_multi_some_in_pairs(self):
1029+
# Connecting matching inputs is an allowed no-op if there are also
1030+
# actual input-output connections to be made. See
1031+
# https://github.com/amaranth-lang/amaranth/pull/1153#issuecomment-1962810678
1032+
# for more discussion.
1033+
m = Module()
1034+
connect(m,
1035+
p=NS(signature=Signature({"a": In(1), "b": In(1)}),
1036+
a=Signal(),
1037+
b=Signal()),
1038+
q=NS(signature=Signature({"a": Out(1), "b": In(1)}),
1039+
a=Signal(),
1040+
b=Signal()))
1041+
9831042

9841043
class ComponentTestCase(unittest.TestCase):
9851044
def test_basic(self):

0 commit comments

Comments
 (0)