Skip to content

Commit c2a612f

Browse files
Warn on <literal>.asUInt|.asSInt(_: Int) (#4764) (#4766)
The user probably forgot .W. Apply the same technique as used for .U|.S. (cherry picked from commit 811bb46) Co-authored-by: Jack Koenig <[email protected]>
1 parent 9d28ac6 commit c2a612f

File tree

2 files changed

+2
-6
lines changed

2 files changed

+2
-6
lines changed

core/src/main/scala/chisel3/package.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ package object chisel3 {
3030
* after this call using apply, ie. 0.asUInt(1)(0) due to potential for
3131
* confusion (the 1 is a bit length and the 0 is a bit extraction position).
3232
* Prefer storing the result and then extracting from it.
33-
*
34-
* Implementation note: the empty parameter list (like `U()`) is necessary to prevent
35-
* interpreting calls that have a non-Width parameter as a chained apply, otherwise things like
36-
* `0.asUInt(16)` (instead of `16.W`) compile without error and produce undesired results.
3733
*/
3834
implicit class fromBigIntToLiteral(bigint: BigInt) {
3935

macros/src/main/scala/chisel3/internal/sourceinfo/SourceInfoTransform.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,13 @@ class IntLiteralApplyTransform(val c: Context) extends AutoSourceTransform {
375375
c.macroApplication match {
376376
case q"$_.$clazz($lit).$func.apply($arg)" =>
377377
if (
378-
Set("U", "S").contains(func.toString) &&
378+
Set("U", "S", "asUInt", "asSInt").contains(func.toString) &&
379379
Set("fromStringToLiteral", "fromIntToLiteral", "fromLongToIteral", "fromBigIntToLiteral").contains(
380380
clazz.toString
381381
)
382382
) {
383383
val msg =
384-
s"""Passing an Int to .$func is usually a mistake: It does *not* set the width but does a bit extract.
384+
s"""Passing an Int to .$func is usually a mistake: It does *not* set the width; it does a bit extraction.
385385
|Did you mean .$func($arg.W)?
386386
|If you do want bit extraction, use .$func.extract($arg) instead.
387387
|""".stripMargin

0 commit comments

Comments
 (0)