diff options
| author | Andrew Waterman | 2019-04-12 17:51:04 -0700 |
|---|---|---|
| committer | edwardcwang | 2019-04-15 13:07:11 -0700 |
| commit | a4a29e29c3f1eed18f851dcf10bdc845571dfcb6 (patch) | |
| tree | 876999a0301d4ef86e8bb0712e8bc3ec3d2f9ef7 /chiselFrontend/src/main/scala/chisel3/internal | |
| parent | 0028c64922a85087b2b6a6062fe202294e70855a (diff) | |
Avoid silently truncating BigInt to Int
- Introduce internal helper `castToInt`, which issues an error when the input
BigInt can't be represented as Int.
- Use `castToInt` wherever we were using `toInt` in a potentially unsafe way.
Diffstat (limited to 'chiselFrontend/src/main/scala/chisel3/internal')
| -rw-r--r-- | chiselFrontend/src/main/scala/chisel3/internal/Builder.scala | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/chiselFrontend/src/main/scala/chisel3/internal/Builder.scala b/chiselFrontend/src/main/scala/chisel3/internal/Builder.scala index 4105a699..1163171c 100644 --- a/chiselFrontend/src/main/scala/chisel3/internal/Builder.scala +++ b/chiselFrontend/src/main/scala/chisel3/internal/Builder.scala @@ -378,3 +378,12 @@ object DynamicNamingStack { prefixRef } } + +/** Casts BigInt to Int, issuing an error when the input isn't representable. */ +private[chisel3] object castToInt { + def apply(x: BigInt, msg: String): Int = { + val res = x.toInt + require(x == res, s"$msg $x is too large to be represented as Int") + res + } +} |
