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 17832 - std.random.choice cannot be used with other random generators #5741
Conversation
Thanks for your pull request, @dunkyp! We are looking forward to reviewing it, and you should be hearing from a maintainer soon. Some tips to help speed things up:
Bear in mind that large or tricky changes may require multiple rounds of review and revision. Please see CONTRIBUTING.md for more information. Bugzilla references
|
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 fix looks good, but there's something deeper going on here that needs to be investigated further. It looks like there may be a bug here with template type deduction. It's not clear to me how things should work in this situation.
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.
Thanks a lot for your PR!
I am not sure whether this is the right way to move forward as Phobos should be a prime example of D code and not a collection of workarounds against compiler bugs. Hence, I would prefer that this gets reduced and filled as compiler bug.
If there is a strong opinion to merge this now, we should at the very least mark the workaround and mention it on Bugzilla so that it can be removed in the future.
Found underlying cause: https://issues.dlang.org/show_bug.cgi?id=16467 Basically, the problem is that a template function like
The problem is that during IFTI of a call like One possible solution is to change the declaration to:
Since this is a lot of ugly boilerplate, I'm tempted to say the compiler should perform this lowering automatically. However, that unfortunately breaks the valid case where you do want the default parameter to apply across different types, for example:
In this case, the default argument could arguably(!) be construed to impose a constraint that So, unfortunately, it seems that the workaround in this PR is probably a necessary one. |
Thanks for the explanation. When you put it that way, this is definitely a fairly subtle bug in the code and needs to be fixed. It's basically the same as doing:
Obviously this is wrong and should be corrected. |
ping @wilzbach Does the above address your concerns? Should we merge this? It's been rotting here for far too long already. |
ping @wilzbach I'm in favor of merging this, because the bug is real, and needs to be addressed somehow. The fact that Phobos looks a little uglier to me seems secondary, and something that we can work on afterwards. I've thought of an idea that might work, but it will probably involve a DIP, so I'd much rather get this fix in now rather than later. What do you think? |
std/random.d
Outdated
@@ -2008,6 +2008,11 @@ if (isRandomAccessRange!Range && hasLength!Range && isUniformRNG!RandomGen) | |||
return range[uniform(size_t(0), $, urng)]; | |||
} | |||
|
|||
auto ref choice(Range)(auto ref Range range) |
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.
Should be /// ditto
- this tells the Ddoc engine to visually group the overloads.
Sorry getting too many GH lately. Feel free to ping me on Slack for something that requires immediate attention.
Here's a reduction: struct Foo {}
struct Bar {}
Foo foo;
void myFun(D)(D d = foo){}
void bar()
{
myFun(); // works
myFun(Bar()); // error
}
As you might have guessed, it's a known bug: There's also the pre-approved https://issues.dlang.org/show_bug.cgi?id=17186 which even had a PR (dlang/dmd#2270).
Without knowing your idea, I can say the following: "you might be luckily and it could be considered as a bug fix". Remember that 17186 is pre-approved.
Yeah, fair enough, but let's fix entire |
We won't be able to fix the compiler bug in the near future :/
I quickly fixed this myself. |
Random choice default argument was deciding the type of the template. I'm not sure if this is a deeper bug, but it can easily be fixed with this patch which simply overloads the function. Also added a unittest which would have failed under the old code.
Is the CircleCI failure relevant? All I can glean from the output is that some built-in timeout was exceeded while running tests. Doesn't seem related to this PR directly? |
It has to do with something else, but so far I haven't had time to dig deeper into it. |
Random choice default argument was deciding the type of the
template. I'm not sure if this is a deeper bug, but it can easily be
fixed with this patch which simply overloads the function.
Also added a unittest which would have failed under the old code.