Skip to content

Commit

Permalink
Add lvalue check for ref/out params with default arguments.
Browse files Browse the repository at this point in the history
Move def arg check into semantic.

Move semantic check to FuncType.

Add more tests, do if check before assignment.

Separate fail tests.

toLvalue can take a scope. Add another test-case.
  • Loading branch information
AndrejMitrovic committed Oct 6, 2012
1 parent 77ad29c commit a4348cc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/mtype.c
Expand Up @@ -5584,6 +5584,11 @@ Type *TypeFunction::semantic(Loc loc, Scope *sc)
e = e->semantic(argsc);
}
e = e->implicitCastTo(argsc, fparam->type);

// default arg must be an lvalue
if (fparam->storageClass & (STCout | STCref))
e = e->toLvalue(argsc, e);

fparam->defaultArg = e;
}

Expand Down
1 change: 1 addition & 0 deletions test/fail_compilation/fail7603a.d
@@ -0,0 +1 @@
void test(ref bool val = true) { }
1 change: 1 addition & 0 deletions test/fail_compilation/fail7603b.d
@@ -0,0 +1 @@
void test(out bool val = true) { }
2 changes: 2 additions & 0 deletions test/fail_compilation/fail7603c.d
@@ -0,0 +1,2 @@
enum x = 3;
void test(ref int val = x) { }
30 changes: 30 additions & 0 deletions test/runnable/test7603.d
@@ -0,0 +1,30 @@
void test7603()
{
int g;
void foo(int n, ref int r = g) { r = n; }

int x;
foo(1, x);
assert(x == 1);

foo(2);
assert(g == 2);

int h = 100;
void bar(int n, out int r = h) { if (n != 0) r = n; }

bar(0);
assert(h == 0);

bar(10);
assert(h == 10);

bar(10, x);
assert(x == 10);

bar(0, x);
assert(x == 0);

}

void main() { test7603(); }

0 comments on commit a4348cc

Please sign in to comment.