Make core.internal.convert usable in -betterC#2710
Make core.internal.convert usable in -betterC#2710lesderid wants to merge 1 commit intodlang:masterfrom
Conversation
|
Thanks for your pull request and interest in making D better, @lesderid! 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 referencesYour PR doesn't reference any Bugzilla issue. If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog. Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub fetch digger
dub run digger -- build "master + druntime#2710" |
JinShil
left a comment
There was a problem hiding this comment.
At a minimum, this needs a test. See other tests in https://github.com/dlang/druntime/tree/master/test/betterc
|
I'm not sure that this is the right solution. There are others who have made such changes and they have been merged, but I fear that if we stay on that trend, druntime is going to become a difficult to comprehend mess. I think there may be a way, in the DMD frontend, to not emit an error if compiling with -betterC and evaluating with CTFE. I also think such a change would implicitly solve a number of other betterC CTFE bugs. I'll investigate some if a test case can be provided to demonstrate the error being encountered in betterC CTFE. |
The diff is quite clean if viewing without whitespace changes. |
Using void f()
{
import core.internal.convert : toUbyte;
int[0] a;
toUbyte(a);
}fails with |
|
I feel like if we fix this in DMD, |
Anything in
betterC imports druntime, but doesn't link to it. That means all of druntime is available at compile-time but not runtime. Therefore, I'm beginning to think that DMD should not emit any betterC errors if evaluating CTFE. That can probably be accomplished by simply checking |
|
OK, as I look into this more, I'm beginning to understand. The test case you presented does indeed trigger the |
I assume because the cast isn't allowed in CTFE. The |
I think the cast is allowed in CTFE: void f()
{
enum a = [1];
import core.internal.convert : toUbyte;
enum x = toUbyte(a);
}
void main() {}https://run.dlang.io/is/UsBG6l
I thought that was the whole point of this fix: To enable the cast in betterC+CTFE. But from what you've said and demonstrated, why is the compiler even evaluating the Ah, as I write this, I think I understand. It's because we don't have the equivalent of |
|
After thinking about this some more, the primary limitation is the call to Line 911 in d835e18 There is currently a GSOC project underway to convert some of these array runtime hooks to templates that no longer depend on See also: cc @Vild |
I meant that the current code simply uses a reinterpret cast[1] for the non- (Possibly reiterating what you said:)
Once the [1] druntime/src/core/internal/convert.d Line 640 in 29eb98c [2] https://run.dlang.io/is/05aGYz |
But all that does is pass the buck on to someone else. Is converting the hooks to a template not within your ability? |
Sure, if @Vild hasn't already started on these, I don't mind taking a look at it. |
|
You could make this PR much smaller. You can just change the /// A @nogc function can allocate memory during CTFE.
@nogc nothrow pure @trusted
private ubyte[] ctfe_alloc()(size_t n)
{
version (D_BetterC)
assert(0, "Not available during CTFE when compiling with -betterC.");
else if (!__ctfe)
assert(0, "CTFE only");
else
{
static ubyte[] alloc(size_t x) nothrow pure
{
if (__ctfe) // Needed to prevent _d_newarray from appearing in compiled prorgam.
return new ubyte[x];
else
assert(0);
}
return (cast(ubyte[] function(size_t) @nogc nothrow pure) &alloc)(n);
}
}
|
True, but as @JinShil pointed out this is the wrong fix anyway, so closing. |
Currently
core.internal.convert.toUbyte(a dependency ofcore.internal.hash) doesn't compile with -betterC because the CTFE version usesnew. This fix disables the CTFE branch when theD_BetterCversion is defined.The diff is kind of ugly, but I'm not sure there's a better way.
Related: https://issues.dlang.org/show_bug.cgi?id=19268