Skip to content

Commit

Permalink
Mark return value of .asTypeOf as "deprecated read-only" (#4198) (#4199)
Browse files Browse the repository at this point in the history
Users will now get a deprecation warning if they connect to the result
of .asTypeOf.

(cherry picked from commit 00c7a62)

Co-authored-by: Jack Koenig <koenig@sifive.com>
  • Loading branch information
mergify[bot] and jackkoenig authored Jun 20, 2024
1 parent 805bf30 commit 0039ef0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 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 @@ -814,7 +814,9 @@ abstract class Data extends HasId with NamedComponent with SourceInfoDoc {
def do_asTypeOf[T <: Data](that: T)(implicit sourceInfo: SourceInfo): T = {
val thatCloned = Wire(that.cloneTypeFull)
thatCloned.connectFromBits(this.asUInt)
thatCloned
thatCloned.viewAsReadOnlyDeprecated(siteInfo =>
Warning(WarningID.AsTypeOfReadOnly, s"Return values of asTypeOf will soon be read-only")(siteInfo)
)
}

/** Assigns this node from Bits type. Internal implementation for asTypeOf.
Expand Down
1 change: 1 addition & 0 deletions core/src/main/scala/chisel3/internal/Warning.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ private[chisel3] object WarningID extends Enumeration {
val DynamicIndexTooNarrow = Value(5)
val ExtractFromVecSizeZero = Value(6)
val BundleLiteralValueTooWide = Value(7)
val AsTypeOfReadOnly = Value(8)
}
import WarningID.WarningID

Expand Down
27 changes: 26 additions & 1 deletion docs/src/explanations/warnings.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,29 @@ It can be fixed by removing the indexing operation for the size zero `Vec` (perh
This warning occurs when creating a [Bundle Literal](../appendix/experimental-features#bundle-literals) where the literal value for a
field is wider than the Bundle field's width.
It can be fixed by reducing the width of the literal (perhaps choosing a different value if it is impossible to encode the value in the
field's width).
field's width).

### [W008] Return values of asTypeOf will soon be read-only

This warning indicates that the result of a call to `.asTypeOf(_)` is being used as the destination for a connection.
It can be fixed by instantiating a wire.

For example, given the following:
```scala mdoc:compile-only
class MyBundle extends Bundle {
val foo = UInt(8.W)
val bar = UInt(8.W)
}
val x = 0.U.asTypeOf(new MyBundle)
x.bar := 123.U
```

The warning can be fixed by inserting a wire:
```scala mdoc:compile-only
class MyBundle extends Bundle {
val foo = UInt(8.W)
val bar = UInt(8.W)
}
val x = WireInit(0.U.asTypeOf(new MyBundle))
x.bar := 123.U
```
6 changes: 3 additions & 3 deletions src/test/scala/chiselTests/Vec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,9 @@ class VecSpec extends ChiselPropSpec with Utils {

})
chirrtl should include("output io : { foo : UInt<1>, bar : UInt<1>[0]}")
chirrtl should include("wire zero : { foo : UInt<1>, bar : UInt<1>[0]}")
chirrtl should include("connect zero.foo, UInt<1>(0h0)")
chirrtl should include("connect io, zero")
chirrtl should include("wire _zero_WIRE : { foo : UInt<1>, bar : UInt<1>[0]}")
chirrtl should include("connect _zero_WIRE.foo, UInt<1>(0h0)")
chirrtl should include("connect io, _zero_WIRE")
chirrtl should include("wire w : UInt<1>[0]")
chirrtl should include("connect w, m.io.bar")
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/scala/chiselTests/stage/WarningConfigurationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ object WarningConfigurationSpec {
val idx = IO(Input(UInt(8.W)))
in(idx)
}
class MyBundle extends Bundle {
val foo = UInt(8.W)
}
class AsTypeOfReadOnly extends RawModule {
val in = IO(Input(UInt(8.W)))
val bundle = in.asTypeOf(new MyBundle)
bundle.foo := 0.U
}
}

class WarningConfigurationSpec extends AnyFunSpec with Matchers with chiselTests.Utils {
Expand Down Expand Up @@ -371,5 +379,11 @@ class WarningConfigurationSpec extends AnyFunSpec with Matchers with chiselTests
val e = the[Exception] thrownBy ChiselStage.emitCHIRRTL(new ExtractFromVecSizeZero, args)
e.getMessage should include("[W006] Cannot extract from Vec of size 0")
}

it("should number AsTypeOfReadOnly as 8") {
val args = Array("--warn-conf", "id=8:e,any:s", "--throw-on-first-error")
val e = the[Exception] thrownBy ChiselStage.emitCHIRRTL(new AsTypeOfReadOnly, args)
e.getMessage should include("[W008] Return values of asTypeOf will soon be read-only")
}
}
}

0 comments on commit 0039ef0

Please sign in to comment.