Repository navigation
Translate _d_arrayappend{T,cTX} to templates #9982
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
Changes from all commits
c9be867
2bca07d
4683227
a4cf50f
9042771
a890f13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1286,7 +1286,8 @@ extern (C++) abstract class Expression : ASTNode | |
|
|
||
| // Lowered non-@nogc'd hooks will print their own error message inside of nogc.d (NOGCVisitor.visit(CallExp e)), | ||
| // so don't print anything to avoid double error messages. | ||
| if (!(f.ident == Id._d_HookTraceImpl || f.ident == Id._d_arraysetlengthT)) | ||
| if (!(f.ident == Id._d_HookTraceImpl || f.ident == Id._d_arraysetlengthT | ||
| || f.ident == Id._d_arrayappendT || f.ident == Id._d_arrayappendcTX)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this list grows much longer, we should probably use a switch statement instead? But I guess with 4 entries this is still ok. |
||
| error("`@nogc` %s `%s` cannot call non-@nogc %s `%s`", | ||
| sc.func.kind(), sc.func.toPrettyChars(), f.kind(), f.toPrettyChars()); | ||
| return true; | ||
|
|
@@ -5976,6 +5977,7 @@ extern (C++) final class UshrAssignExp : BinAssignExp | |
| */ | ||
| extern (C++) class CatAssignExp : BinAssignExp | ||
| { | ||
| bool isCTFEHookPart; | ||
| extern (D) this(const ref Loc loc, Expression e1, Expression e2) | ||
| { | ||
| super(loc, TOK.concatenateAssign, __traits(classInstanceSize, CatAssignExp), e1, e2); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,6 +81,17 @@ public: | |
| } | ||
| f.printGCUsage(e.loc, "setting `length` may cause a GC allocation"); | ||
| } | ||
| else if (fd.ident == Id._d_arrayappendT || fd.ident == Id._d_arrayappendcTX) | ||
| { | ||
| if (f.setGC()) | ||
| { | ||
| e.error("cannot use operator `~=` in `@nogc` %s `%s`", | ||
| f.kind(), f.toPrettyChars()); | ||
| err = true; | ||
| return; | ||
| } | ||
| f.printGCUsage(e.loc, "operator `~=` may cause a GC allocation"); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't quite understand the need for these error messages. If the runtime hook causes a GC allocation, then the error message will already be emitted there. However, the error message will be in terms of the lowered code, and not the user's code. If the purpose of this code is to make the error message more relevant to user code, then that is the case that I mentioned earlier on slack. What we could do instead is pass the file, line number, and even the user's code to the runtime hook and have the runtime hook emit a more meaningful error message, perhaps with a static assert. Regardless, I'm ok with keeping this as is to maintain the status quo.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, long-term moving error generation into the hooks would be nicer*. For now I think this is fine to keep compatibility for the error messages. EDIT: Actually, thinking about this some more, the problem below should be unrelated. I'll keep it here though, as I think it's an interesting, although unrelated, thought. string aCTFEOnlyHelper()(string a)
{
return "Hello" ~ a;
}
void nogcAppend(ref string r, string val) @nogc
{
}
void main1() @nogc
{
string result;
__ctfe ? result ~= "foo" : nogcAppend(result, "foo");
}
void main2() @nogc
{
enum val = aCTFEOnlyHelper("foo");
}With the changes suggested here, For main2, you have the problem that as soon as you instantiate aCTFEOnlyHelper, even if it's in a CTFE-only context, the code for aCTFEOnlyHelper will also be emitted to the object file, so you need the GC there. This is probably harder to fix than the (Actually, we writing the issue I had epiphany: What we want there is actually already largely available in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code was moved from |
||
| } | ||
|
|
||
| override void visit(ArrayLiteralExp e) | ||
|
|
@@ -206,14 +217,15 @@ public: | |
|
|
||
| override void visit(CatAssignExp e) | ||
| { | ||
| /* CatAssignExp will exist in `__traits(compiles, ...)` and in the `.e1` branch of a `__ctfe ? :` CondExp. | ||
| * The other branch will be `_d_arrayappendcTX(e1, 1), e1[$-1]=e2` which will generate the warning about | ||
| * GC usage. See visit(CallExp). | ||
| */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, could we change And then add a Test + Changelog entry for this. Some people might be quite happy about this change: https://run.dlang.io/is/G6fv55 ;-)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sadly I cannot change so it doesn't visit this function because of two reasons.
So this visit function will now just silently error. |
||
| if (f.setGC()) | ||
| { | ||
| e.error("cannot use operator `~=` in `@nogc` %s `%s`", | ||
| f.kind(), f.toPrettyChars()); | ||
| err = true; | ||
| return; | ||
| } | ||
| f.printGCUsage(e.loc, "operator `~=` may cause a GC allocation"); | ||
| } | ||
|
|
||
| override void visit(CatExp e) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
Seems like a good idea to me. Needs to be in its own PR though and I guess there's already a bug report for this somewhere.
Ideally, this should not hard-detect the ctfe variable though. You code will fail on constructs such as these:
What should be done instead is this: Set
__ctfe(somehow) to false, then constant fold / interpret the condition. If the result is a constantfalse, the if part of the condition is a ctfe only part. If it is constant true, theelsebranch is a ctfe only branch. If it is not const, both branches may be used at runtime.Maybe that's out of scope for GSoC though, but it would be very useful for betterC.
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 wonder how all that stuff integrates with SCOPEctfe though, maybe what should really happen is that the if block content should be set to SCOPEctfe instead and the toElem / nogc visitors should skip these scopes.
OTOH there's no scope introduced when using CondExp, so for CondExp your code here seems to be corret.
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.
ping @JinShil as IIRC your working on SCOPEctfe issues, so you may be interested in this.
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.
What is this code doing and why?
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.
It detects an
__ctfe ? e1 : e2expression, then ignorese1and only sendse2to the code generation stage. I guess without this, droppinge1code occurs later in the backend. @Vild can you elaborate why exactly this is necessary?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.
Without this change the codegen would generate code for both
e1ande2, and while generating code fore1it would trigger thisassert(0)https://github.com/dlang/dmd/pull/9982/files#diff-7a340f667e49df4f91bcab621efa8521R3294It will also only trigger on code that looks like
__ctfe ? e1 : e2, as this is the only form I'm outputting insideexpressionsem.d. But I probably need to extend this to atleast supportifstatements as the inline can rewriteCondExptoIfStatement.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.
OK. But somehow the backend currently also drops the
e1branch, the code is not in the generated object file. Does this happen only as dead-code-elimination after the initial codegen stage?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 have been unable to find where in the backend were it gets removed, all I know is that it is removed.
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, this is basically a workaround because we don't have
version(CTFE)?Why wasn't
e1lowered in the semantic phase?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.