Skip to content

Commit

Permalink
fix Issue 15490 - Error variable __nrvoretval cannot be modified at c…
Browse files Browse the repository at this point in the history
…ompile time when using -inline

CTFE interpretr can recognize a form `CommaExp(DeclaraationExp(v), VarExp(v))` as
a CTFEable variable declaration when `v._init` is `null`. In other cases, for
examle a sole `DeclarationExp` should have v._init for CTFE-ability.

Supply `VoidInitializer` to allow inlining the temporary for NRVO.
  • Loading branch information
9rnsr committed Feb 6, 2016
1 parent e56cd3b commit b56fa1d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/dinterpret.d
Expand Up @@ -4916,9 +4916,13 @@ public:
istate = &istateComma;
}
result = CTFEExp.cantexp;

// If the comma returns a temporary variable, it needs to be an lvalue
// (this is particularly important for struct constructors)
if (e.e1.op == TOKdeclaration && e.e2.op == TOKvar && (cast(DeclarationExp)e.e1).declaration == (cast(VarExp)e.e2).var && (cast(VarExp)e.e2).var.storage_class & STCctfe) // same as Expression::isTemp
if (e.e1.op == TOKdeclaration &&
e.e2.op == TOKvar &&
(cast(DeclarationExp)e.e1).declaration == (cast(VarExp)e.e2).var &&
(cast(VarExp)e.e2).var.storage_class & STCctfe)
{
VarExp ve = cast(VarExp)e.e2;
VarDeclaration v = ve.var.isVarDeclaration();
Expand Down
2 changes: 1 addition & 1 deletion src/inline.d
Expand Up @@ -2116,7 +2116,7 @@ void expandInline(FuncDeclaration fd, FuncDeclaration parent, Expression eret,
else
{
Identifier tmp = Identifier.generateId("__nrvoretval");
auto vd = new VarDeclaration(fd.loc, fd.nrvo_var.type, tmp, null);
auto vd = new VarDeclaration(fd.loc, fd.nrvo_var.type, tmp, new VoidInitializer(fd.loc));
assert(!tf.isref);
vd.storage_class = STCtemp | STCrvalue;
vd.linkage = tf.linkage;
Expand Down
8 changes: 8 additions & 0 deletions test/compilable/imports/imp15490a.d
@@ -0,0 +1,8 @@
module imports.imp15490a;

import imports.imp15490b;

void listenTCP()
{
enum r = regex();
}
12 changes: 12 additions & 0 deletions test/compilable/imports/imp15490b.d
@@ -0,0 +1,12 @@
module imports.imp15490b;

int regex()
{
return regexImpl();
}

auto regexImpl()
{
int r = 0;
return r;
}
12 changes: 12 additions & 0 deletions test/compilable/test15490.d
@@ -0,0 +1,12 @@
// REQUIRED_ARGS: -o- -inline
// PERMUTE_ARGS:
module test15490;

import imports.imp15490a;
import imports.imp15490b;

void main()
{
regex();
listenTCP();
}

0 comments on commit b56fa1d

Please sign in to comment.