Skip to content

Commit

Permalink
Fix getWidth on empty Vecs; add test
Browse files Browse the repository at this point in the history
Use fold(0) instead of reduce to handle the corner case.
  • Loading branch information
aswaterman committed Mar 24, 2017
1 parent 347bd2f commit e769e5a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion chiselFrontend/src/main/scala/chisel3/core/Aggregate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ sealed abstract class Aggregate extends Data {
*/
def getElements: Seq[Data]

private[core] def width: Width = getElements.map(_.width).reduce(_ + _)
private[core] def width: Width = getElements.map(_.width).foldLeft(0.W)(_ + _)
private[core] def legacyConnect(that: Data)(implicit sourceInfo: SourceInfo): Unit =
pushCommand(BulkConnect(sourceInfo, this.lref, that.lref))

Expand Down
9 changes: 9 additions & 0 deletions src/test/scala/chiselTests/Vec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ class OneBitUnitRegVecTester extends BasicTester {
stop()
}

class ZeroEntryVecTester extends BasicTester {
require(Vec(0, Bool()).getWidth == 0)
stop()
}

class VecSpec extends ChiselPropSpec {
// Disable shrinking on error.
implicit val noShrinkListVal = Shrink[List[Int]](_ => Stream.empty)
Expand Down Expand Up @@ -187,4 +192,8 @@ class VecSpec extends ChiselPropSpec {
property("A Reg of a Vec of a single 1 bit element should compile and work") {
assertTesterPasses{ new OneBitUnitRegVecTester }
}

property("A Vec with zero entries should compile and have zero width") {
assertTesterPasses{ new ZeroEntryVecTester }
}
}

0 comments on commit e769e5a

Please sign in to comment.