Skip to content

Commit

Permalink
Suppress exception throwing in Data.toString (#4147) (#4153)
Browse files Browse the repository at this point in the history
(cherry picked from commit 139be37)

Co-authored-by: Jack Koenig <koenig@sifive.com>
  • Loading branch information
mergify[bot] and jackkoenig authored Jun 7, 2024
1 parent 3f53bed commit a7cb2ff
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 a7cb2ff

Please sign in to comment.