Skip to content

Commit

Permalink
Improve the error message for mismatched types in Mux (#4331)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkoenig authored Aug 6, 2024
1 parent e6df4cf commit a633e78
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
6 changes: 4 additions & 2 deletions core/src/main/scala/chisel3/Data.scala
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,11 @@ private[chisel3] object cloneSupertype {
elt.getClass == filteredElts.head.getClass,
s"can't create $createdType with heterogeneous types ${filteredElts.head.getClass} and ${elt.getClass}"
)
val mismatch =
elt.findFirstTypeMismatch(filteredElts.head, strictTypes = true, strictWidths = true, strictProbeInfo = true)
require(
elt.typeEquivalent(filteredElts.head),
s"can't create $createdType with non-equivalent types ${filteredElts.head} and ${elt}"
mismatch.isEmpty,
s"can't create $createdType with non-equivalent types _${mismatch.get}"
)
}
filteredElts.head.cloneTypeFull
Expand Down
21 changes: 21 additions & 0 deletions src/test/scala/chiselTests/MuxSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,31 @@ class MuxTester extends BasicTester {
stop()
}

class MuxBetween[A <: Data, B <: Data](genA: A, genB: B) extends RawModule {
val in0 = IO(Input(genA))
val in1 = IO(Input(genB))
val sel = IO(Input(Bool()))
val result = Mux(sel, in0, in1)
}

class MuxSpec extends ChiselFlatSpec {
"Mux" should "pass basic checks" in {
assertTesterPasses { new MuxTester }
}

it should "give reasonable error messages for mismatched user-defined types" in {
class MyBundle(w: Int) extends Bundle {
val foo = UInt(w.W)
val bar = UInt(8.W)
}
val e = the[Exception] thrownBy {
ChiselStage.emitCHIRRTL(new MuxBetween(new MyBundle(8), new MyBundle(16)))
}
e.getMessage should include(
"can't create Mux with non-equivalent types _.foo: Left (MuxBetween.in1.foo: IO[UInt<16>]) and Right (MuxBetween.in0.foo: IO[UInt<8>]) have different widths."
)
}

}

class MuxLookupEnumTester extends BasicTester {
Expand Down

0 comments on commit a633e78

Please sign in to comment.