From 7e057ad1a5dfb86b3cc2d3ba7167c44c053c0253 Mon Sep 17 00:00:00 2001 From: Walter Bright Date: Wed, 29 Jun 2016 23:12:02 -0700 Subject: [PATCH] fix Issue 15191 - DIP25: Taking address of ref return is not type checked soundly --- src/expression.d | 13 +++++++++++++ test/fail_compilation/test15191.d | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 test/fail_compilation/test15191.d diff --git a/src/expression.d b/src/expression.d index a79d6051673b..2ac535c57391 100644 --- a/src/expression.d +++ b/src/expression.d @@ -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 diff --git a/test/fail_compilation/test15191.d b/test/fail_compilation/test15191.d new file mode 100644 index 000000000000..55b09b799f22 --- /dev/null +++ b/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); +}