-
-
Notifications
You must be signed in to change notification settings - Fork 706
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
Conversation
|
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 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
Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + phobos#7531" |
207e4ef to
516b4dc
Compare
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; |
There was a problem hiding this comment.
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
| Unqual!T a = _a; | ||
| Unqual!T b = _b; | ||
|
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
Lines 2936 to 2939 in a394c13
| static if (is(T == const) || is(T == immutable)) | |
| { | |
| return gcd!(Unqual!T)(a, b); | |
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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) | |||
There was a problem hiding this comment.
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) | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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))); |
There was a problem hiding this comment.
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:
| assert(__traits(compiles, gcd(a, b))); | |
| if (false) gcd(a, b); |
No description provided.