Skip to content

Commit

Permalink
fix Issue 9891 - Ability to modify immutable using default value of r…
Browse files Browse the repository at this point in the history
…ef/out parameter
  • Loading branch information
9rnsr committed Sep 8, 2015
1 parent f630e99 commit 6b091dc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/mtype.d
Expand Up @@ -5915,9 +5915,17 @@ public:
if (fparam.defaultArg)
{
Expression e = fparam.defaultArg;
Initializer _init = new ExpInitializer(e.loc, e);
_init = _init.semantic(argsc, fparam.type, INITnointerpret);
e = _init.toExpression();
if (fparam.storageClass & (STCref | STCout))
{
e = e.semantic(argsc);
e = resolveProperties(argsc, e);
}
else
{
Initializer iz = new ExpInitializer(e.loc, e);
iz = iz.semantic(argsc, fparam.type, INITnointerpret);
e = iz.toExpression();
}
if (e.op == TOKfunction) // see Bugzilla 4820
{
FuncExp fe = cast(FuncExp)e;
Expand Down
26 changes: 26 additions & 0 deletions test/fail_compilation/fail9891.d
@@ -0,0 +1,26 @@
/*
TEST_OUTPUT:
---
fail_compilation/fail9891.d(13): Error: cast(int)i is not an lvalue
fail_compilation/fail9891.d(18): Error: cast(int)i is not an lvalue
fail_compilation/fail9891.d(23): Error: prop() is not an lvalue
---
*/

immutable int i;
int prop() { return 0; }

void f1(ref int n = i)
{
++n;
}

void f2(out int n = i)
{
++n;
}

void f3(ref int n = prop)
{
++n;
}

0 comments on commit 6b091dc

Please sign in to comment.