Skip to content

Commit 811bb46

Browse files
authored
Warn on <literal>.asUInt|.asSInt(_: Int) (#4764)
The user probably forgot .W. Apply the same technique as used for .U|.S.
1 parent c1b5135 commit 811bb46

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
@@ -361,13 +361,13 @@ class IntLiteralApplyTransform(val c: Context) extends AutoSourceTransform {
361361
c.macroApplication match {
362362
case q"$_.$clazz($lit).$func.apply($arg)" =>
363363
if (
364-
Set("U", "S").contains(func.toString) &&
364+
Set("U", "S", "asUInt", "asSInt").contains(func.toString) &&
365365
Set("fromStringToLiteral", "fromIntToLiteral", "fromLongToIteral", "fromBigIntToLiteral").contains(
366366
clazz.toString
367367
)
368368
) {
369369
val msg =
370-
s"""Passing an Int to .$func is usually a mistake: It does *not* set the width but does a bit extract.
370+
s"""Passing an Int to .$func is usually a mistake: It does *not* set the width; it does a bit extraction.
371371
|Did you mean .$func($arg.W)?
372372
|If you do want bit extraction, use .$func.extract($arg) instead.
373373
|""".stripMargin

0 commit comments

Comments
 (0)