From 6c5013b2d73ef99fb14acae17d6a154df663098b Mon Sep 17 00:00:00 2001 From: Jack Koenig Date: Tue, 26 Sep 2023 13:20:48 -0700 Subject: [PATCH] Fix DataMirror.internal.chiselTypeClone to preserve Scala type (#3553) (cherry picked from commit e6c07b3760aad1b7ad1c11c9feecc2549ed406df) --- core/src/main/scala/chisel3/reflect/DataMirror.scala | 4 ++-- .../scala/chiselTests/reflect/DataMirrorSpec.scala | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/chisel3/reflect/DataMirror.scala b/core/src/main/scala/chisel3/reflect/DataMirror.scala index a0709907503..d6f91f88fc1 100644 --- a/core/src/main/scala/chisel3/reflect/DataMirror.scala +++ b/core/src/main/scala/chisel3/reflect/DataMirror.scala @@ -174,8 +174,8 @@ object DataMirror { object internal { def isSynthesizable(target: Data): Boolean = target.isSynthesizable // For those odd cases where you need to care about object reference and uniqueness - def chiselTypeClone[T <: Data](target: Data): T = { - target.cloneTypeFull.asInstanceOf[T] + def chiselTypeClone[T <: Data](target: T): T = { + target.cloneTypeFull } } diff --git a/src/test/scala/chiselTests/reflect/DataMirrorSpec.scala b/src/test/scala/chiselTests/reflect/DataMirrorSpec.scala index 2ff44f066f2..be5a89b5609 100644 --- a/src/test/scala/chiselTests/reflect/DataMirrorSpec.scala +++ b/src/test/scala/chiselTests/reflect/DataMirrorSpec.scala @@ -111,4 +111,15 @@ class DataMirrorSpec extends ChiselFlatSpec { it should "not support name guesses for non-hardware" in { an[ExpectedHardwareException] should be thrownBy DataMirror.queryNameGuess(UInt(8.W)) } + + "chiselTypeClone" should "preserve Scala type information" in { + class MyModule extends Module { + val in = IO(Input(UInt(8.W))) + val out = IO(Output(DataMirror.internal.chiselTypeClone(in))) + // The connection checks the types + out :#= in + } + ChiselStage.emitCHIRRTL(new MyModule) + } + }