Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the error message for mismatched types in Mux #4331

Merged
merged 1 commit into from
Aug 6, 2024
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
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