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) + } + }