Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CTFE error in biguintcore.d #10658

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion std/bigint.d
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,11 @@ public:
}
else static if (op=="*")
{
if (y == 0)
if (y == 0 || data.isZero())
{
sign = false;
data = 0UL;
return this;
}
else
{
Expand Down Expand Up @@ -361,6 +362,29 @@ public:
return this;
}

// https://issues.dlang.org/show_bug.cgi?id=10565
@safe unittest
Comment on lines +365 to +366
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the comment should be outdented to align with the with unittest.

{
// Test cases from the issue
BigInt a = BigInt("0");
BigInt b = BigInt("-0");
BigInt c = BigInt("0") * -1;
BigInt d = BigInt("0") * -42;
BigInt e = BigInt("0"); e *= -1;
BigInt f = BigInt(c);
BigInt g = BigInt("0") * cast(byte) -1;
BigInt h = BigInt("0"); h *= BigInt("-1");
BigInt i = BigInt("0"); i -= 2 * i;
BigInt j = BigInt("0"); j = -j;
// All of these should be zero and not negative
auto values = [a, b, c, d, e, f, g, h, i, j];
foreach (val; values)
{
assert(val == 0, "BigInt value should be equal to zero");
assert(!(val < 0), "BigInt zero should not be negative");
}
}

///
@safe unittest
{
Expand Down
9 changes: 8 additions & 1 deletion std/internal/math/biguintcore.d
Original file line number Diff line number Diff line change
Expand Up @@ -2785,6 +2785,10 @@ do
adjustRemainder(quotient[0 .. k], u[0 .. v.length], v, k,
scratch[0 .. 2 * k]);
}
() @trusted {
if (!__ctfe)
GC.free(scratchbuff.ptr);
} ();
}

// rem -= quot * v[0 .. k].
Expand Down Expand Up @@ -2842,7 +2846,10 @@ pure nothrow @safe
m -= v.length;
}
recursiveDivMod(quotient[0 .. m], u[0 .. m + v.length], v, scratch);
() @trusted { GC.free(scratch.ptr); } ();
() @trusted {
if (!__ctfe)
GC.free(scratch.ptr);
} ();
}

@system unittest
Expand Down
Loading