Inner class <name>SumProcedure in abstractLazyPrimitiveIterable.stg should widen sum type for int, short, etc.
donraab opened this issue · 2 comments
donraab commented
Today, the generated code for lazy int returns the following, which will overflow silently for large sums.
private static final class IntSumProcedure implements IntProcedure
{
private int sum = 0;
@Override
public void value(int each)
{
this.sum += each;
}
public int getValue()
{
return this.sum;
}
}
This was a Project Loom code example in a test that I wrote that failed (using Java 20):
@Test
public void usingLoomWithEclipseCollectionsLazyAsParallel()
{
long sum = Interval.oneTo(2_000_000)
.toList()
.asParallel(Executors.newVirtualThreadPerTaskExecutor(), 2)
.sumOfInt(each -> each * 2);
long expected = IntInterval.oneTo(2_000_000).asLazy().collectInt(each -> each * 2).sum();
Assertions.assertEquals(expected, sum);
}
Expected :1387447424
Actual :4000002000000
A similar test could be written in Java 8 without using asParallel
.
emilie-robichaud commented
Hi, can you assign this issue to me?
donraab commented
Thanks for volunteering @emilie-robichaud ! I have assigned the issue to you.