Skip to content

Commit

Permalink
Improve tuple error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardt committed Aug 20, 2018
1 parent f11bac8 commit b1edef6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
5 changes: 3 additions & 2 deletions magma/tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .bit import BitOut, VCC, GND
from .debug import debug_wire, get_callee_frame_info
from .logging import error
from .port import report_wiring_error

__all__ = ['TupleType', 'TupleKind', 'Tuple']

Expand Down Expand Up @@ -77,11 +78,11 @@ def wire(i, o, debug_info):
# print('Tuple.wire(', o, ', ', i, ')')

if not isinstance(o, TupleType):
print('Wiring error: wiring', o, 'to', i, '(not an Tuple)')
report_wiring_error(f'Cannot wire {o.debug_name} (type={type(o)}) to {i.debug_name} (type={type(i)}) because {o.debug_name} is not a Tuple', debug_info) # noqa
return

if i.Ks != o.Ks:
print('Wiring error: Tuples must have the same keys', i.Ks, o.Ks)
report_wiring_error(f'Cannot wire {o.debug_name} (type={type(o)}, keys={i.Ks}) to {i.debug_name} (type={type(i)}, keys={o.Ks}) because the tuples do not have the same keys', debug_info) # noqa
return

#if i.Ts != o.Ts:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_type/test_type_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,29 @@ def test_bit_to_array(caplog):
\033[1mtests/test_type/test_type_errors.py:36: Cannot wire main.Buf_inst0.I (type=In(Bit)) to main.O (type=Array(7,In(Bit))) because main.Buf_inst0.I is not an Array
wire(buf.I, main.O)
"""


def test_tuple_to_array(caplog):
Buf = DeclareCircuit('Buf', "I", In(Tuple(a=Bit,b=Bit)), "O", Out(Array(8, Bit)))

main = DefineCircuit("main", "I", In(Bit), "O", Out(Bit))

buf = Buf()
wire(main.O, buf.I)
assert "\n".join(x.msg for x in caplog.records) == """\
\033[1mtests/test_type/test_type_errors.py:49: Cannot wire main.O (type=In(Bit)) to main.Buf_inst0.I (type=Tuple(a=In(Bit),b=In(Bit))) because main.O is not a Tuple
wire(main.O, buf.I)
"""


def test_bad_tuples(caplog):
Buf = DeclareCircuit('Buf', "I", In(Tuple(a=Bit,b=Bit)), "O", Out(Array(8, Bit)))

main = DefineCircuit("main", "I", In(Bit), "O", Out(Tuple(c=Bit,d=Bit)))

buf = Buf()
wire(main.O, buf.I)
assert "\n".join(x.msg for x in caplog.records) == """\
\033[1mtests/test_type/test_type_errors.py:62: Cannot wire main.O (type=Tuple(c=In(Bit),d=In(Bit)), keys=['a', 'b']) to main.Buf_inst0.I (type=Tuple(a=In(Bit),b=In(Bit)), keys=['c', 'd']) because the tuples do not have the same keys
wire(main.O, buf.I)
"""

0 comments on commit b1edef6

Please sign in to comment.