Skip to content

Commit

Permalink
Suppress exception throwing in Data.toString
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkoenig committed Jun 6, 2024
1 parent 8327056 commit d6417fe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
4 changes: 3 additions & 1 deletion core/src/main/scala/chisel3/Data.scala
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,9 @@ abstract class Data extends HasId with NamedComponent with SourceInfoDoc {

private[chisel3] def stringAccessor(chiselType: String): String = {
// Trace views to give better error messages
val thiz = reifySingleData(this).getOrElse(this)
// Reifying involves checking against ViewParent which requires being in a Builder context
// Since we're just printing a String, suppress such errors and use this object
val thiz = Try(reifySingleData(this)).toOption.flatten.getOrElse(this)
thiz.topBindingOpt match {
case None => chiselType
// Handle DontCares specially as they are "literal-like" but not actually literals
Expand Down
32 changes: 14 additions & 18 deletions src/test/scala/chiselTests/DataPrint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,19 @@ class DataPrintSpec extends ChiselFlatSpec with Matchers {
}

"Literals" should "have a meaningful string representation" in {
ChiselStage.emitCHIRRTL {
new RawModule {
3.U.toString should be("UInt<2>(3)")
3.U(5.W).toString should be("UInt<5>(3)")
-1.S.toString should be("SInt<1>(-1)")
false.B.toString should be("Bool(false)")
true.B.toString should be("Bool(true)")
Vec(3, UInt(4.W)).toString should be("UInt<4>[3]")
EnumTest.sNone.toString should be("EnumTest(0=sNone)")
EnumTest.sTwo.toString should be("EnumTest(2=sTwo)")
EnumTest(1.U).toString should be("EnumTest(1=sOne)")
(new BundleTest).Lit(_.a -> 2.U, _.b -> false.B).toString should be("BundleTest(a=UInt<8>(2), b=Bool(false))")
(new PartialBundleTest).Lit().toString should be(
"PartialBundleTest(a=UInt<8>(DontCare), b=Bool(DontCare), c=SInt<8>(DontCare), f=EnumTest(DontCare))"
)
DontCare.toString should be("DontCare()")
}
}
3.U.toString should be("UInt<2>(3)")
3.U(5.W).toString should be("UInt<5>(3)")
-1.S.toString should be("SInt<1>(-1)")
false.B.toString should be("Bool(false)")
true.B.toString should be("Bool(true)")
Vec(3, UInt(4.W)).toString should be("UInt<4>[3]")
EnumTest.sNone.toString should be("EnumTest(0=sNone)")
EnumTest.sTwo.toString should be("EnumTest(2=sTwo)")
EnumTest(1.U).toString should be("EnumTest(1=sOne)")
(new BundleTest).Lit(_.a -> 2.U, _.b -> false.B).toString should be("BundleTest(a=UInt<8>(2), b=Bool(false))")
(new PartialBundleTest).Lit().toString should be(
"PartialBundleTest(a=UInt<8>(DontCare), b=Bool(DontCare), c=SInt<8>(DontCare), f=EnumTest(DontCare))"
)
DontCare.toString should be("DontCare()")
}
}

0 comments on commit d6417fe

Please sign in to comment.