From 1708d14ac69e5dd1abf47e58e2b204417c13e26d Mon Sep 17 00:00:00 2001 From: Jack Koenig Date: Tue, 26 Sep 2023 12:56:02 -0700 Subject: [PATCH] Fix DataMirror.internal.chiselTypeClone to preserve Scala type --- 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 8efcbe5927f..4681de8549e 100644 --- a/core/src/main/scala/chisel3/reflect/DataMirror.scala +++ b/core/src/main/scala/chisel3/reflect/DataMirror.scala @@ -191,8 +191,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 0a6046f3ee6..2c1208a7ede 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) + } + }