From a4a29e29c3f1eed18f851dcf10bdc845571dfcb6 Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Fri, 12 Apr 2019 17:51:04 -0700 Subject: 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. --- chiselFrontend/src/main/scala/chisel3/internal/Builder.scala | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'chiselFrontend/src/main/scala/chisel3/internal') 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 + } +} -- cgit v1.2.3