From b5563183fe03062a58bfc4b38c11eca8b1d71ae8 Mon Sep 17 00:00:00 2001 From: Jack Koenig Date: Fri, 7 Jun 2024 09:09:22 -0700 Subject: [PATCH] Suppress exception throwing in Data.toString (#4147) (cherry picked from commit 139be3717430ae4ce7d74eade49a84ff050cf759) --- core/src/main/scala/chisel3/Data.scala | 4 ++- src/test/scala/chiselTests/DataPrint.scala | 32 ++++++++++------------ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/core/src/main/scala/chisel3/Data.scala b/core/src/main/scala/chisel3/Data.scala index 624ae4f68db..b56dd5110cd 100644 --- a/core/src/main/scala/chisel3/Data.scala +++ b/core/src/main/scala/chisel3/Data.scala @@ -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 diff --git a/src/test/scala/chiselTests/DataPrint.scala b/src/test/scala/chiselTests/DataPrint.scala index 23eda39ccd5..784a2089d78 100644 --- a/src/test/scala/chiselTests/DataPrint.scala +++ b/src/test/scala/chiselTests/DataPrint.scala @@ -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()") } }