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 issue 20924 - std.numeric.gcd cannot be used with const BigInt #7531

Merged
merged 1 commit into from
Sep 18, 2020

Conversation

Biotronic
Copy link
Contributor

No description provided.

@Biotronic Biotronic requested a review from andralex as a code owner June 15, 2020 06:40
@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @Biotronic! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Auto-close Bugzilla Severity Description
20924 normal std.numeric.gcd cannot be used with const BigInt

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#7531"

@Biotronic Biotronic force-pushed the Issue20924 branch 2 times, most recently from 207e4ef to 516b4dc Compare June 15, 2020 06:54
std/numeric.d Outdated
if (!isIntegral!T &&
is(typeof(T.init % T.init)) &&
is(typeof(T.init == 0 || T.init > 0)))
{
import std.algorithm.mutation : swap;

Unqual!T a = _a;
Unqual!T b = _b;

enum canUseBinaryGcd = is(typeof(() {
T t, u;
Copy link
Contributor

Choose a reason for hiding this comment

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

This check will still fail for const/immutable T.

std/numeric.d Outdated
Comment on lines 2997 to 2999
Unqual!T a = _a;
Unqual!T b = _b;

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you static if this so you don't make copies if you don't have to? If T is not const, just alias a = _a?

Copy link
Contributor

Choose a reason for hiding this comment

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

Other option: use const T for the params and then always make a mutable copy. This would prevent all the 'redundant' const/immutable instantiations, another (common) problem of such public templates. Or just forward to the mutable version as integer gcd above:

phobos/std/numeric.d

Lines 2936 to 2939 in a394c13

static if (is(T == const) || is(T == immutable))
{
return gcd!(Unqual!T)(a, b);
}

Copy link
Contributor Author

@Biotronic Biotronic Jun 15, 2020

Choose a reason for hiding this comment

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

The main problem with the const T idea over the alias or forwarding is it pessimizes all code: the copy will always happen. There's also a theoretical issue of a type T such that const(T) can't be converted to T.

Copy link
Contributor

Choose a reason for hiding this comment

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

So you've changed your mind?

std/numeric.d Outdated
{
import std.algorithm.mutation : swap;
enum canUseBinaryGcd = is(typeof(() {
Unqual!T t, u;
Copy link
Member

Choose a reason for hiding this comment

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

In this branch you've already established is(T == Unqual!T).

@@ -2987,64 +2987,70 @@ if (isIntegral!T)
// This overload is for non-builtin numerical types like BigInt or
// user-defined types.
/// ditto
T gcd(T)(T a, T b)
auto gcd(T)(T a, T b)
Copy link
Contributor

Choose a reason for hiding this comment

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

I dislike auto as return type - it requires the user to look at the source to infer it; for the compiler, it means it always has to analyze the function to infer it too (but for templates it doesn't matter that much).

@@ -2987,64 +2987,70 @@ if (isIntegral!T)
// This overload is for non-builtin numerical types like BigInt or
// user-defined types.
/// ditto
T gcd(T)(T a, T b)
auto gcd(T)(T a, T b)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
auto gcd(T)(T a, T b)
Unqual!T gcd(T)(T a, T b)

import std.bigint : BigInt;
const a = BigInt("123143238472389492934020");
const b = BigInt("902380489324729338420924");
assert(__traits(compiles, gcd(a, b)));
Copy link
Contributor

Choose a reason for hiding this comment

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

Same test but no runtime checks & better error messages:

Suggested change
assert(__traits(compiles, gcd(a, b)));
if (false) gcd(a, b);

@n8sh n8sh added Merge:72h no objection -> merge The PR will be merged if there are no objections raised. Merge:auto-merge labels Sep 12, 2020
@dlang-bot dlang-bot merged commit 653e0e8 into dlang:master Sep 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Merge:auto-merge Merge:72h no objection -> merge The PR will be merged if there are no objections raised. Severity:Bug Fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants