Skip to content

Commit

Permalink
fix Issue 15191 - DIP25: Taking address of ref return is not type che…
Browse files Browse the repository at this point in the history
…cked soundly
  • Loading branch information
WalterBright committed Jun 30, 2016
1 parent 1116591 commit 7e057ad
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/expression.d
Expand Up @@ -10662,6 +10662,19 @@ extern (C++) final class AddrExp : UnaExp
}
}
}
else if (e1.op == TOKcall)
{
CallExp ce = cast(CallExp)e1;
if (ce.e1.type.ty == Tfunction)
{
TypeFunction tf = cast(TypeFunction)ce.e1.type;
if (tf.isref && sc.func && !sc.intypeof && sc.func.setUnsafe())
{
error("cannot take address of ref return of %s() in @safe function %s",
ce.e1.toChars(), sc.func.toChars());
}
}
}
else if (wasCond)
{
/* a ? b : c was transformed to *(a ? &b : &c), but we still
Expand Down
18 changes: 18 additions & 0 deletions test/fail_compilation/test15191.d
@@ -0,0 +1,18 @@
/* TEST_OUTPUT:
---
fail_compilation/test15191.d(17): Error: cannot take address of ref return of foo() in @safe function bar
---
*/


// https://issues.dlang.org/show_bug.cgi?id=15191

ref int foo(return ref int s)@safe
{
return s;
}

int* bar(return ref int s) @safe
{
return &foo(s);
}

0 comments on commit 7e057ad

Please sign in to comment.