diff --git a/src/escape.d b/src/escape.d index 4c336dfc045d..6eac1b9884c6 100644 --- a/src/escape.d +++ b/src/escape.d @@ -414,6 +414,7 @@ bool checkEscapeRef(Scope* sc, Expression e, bool gag) private bool checkEscapeImpl(Scope* sc, Expression e, bool refs, bool gag) { + //printf("[%s] checkEscapeImpl, e = %s\n", e.loc.toChars(), e.toChars()); EscapeByResults er; if (refs) @@ -433,19 +434,21 @@ private bool checkEscapeImpl(Scope* sc, Expression e, bool refs, bool gag) Dsymbol p = v.toParent2(); + if ((v.isScope() || (v.storage_class & STCmaybescope)) && + !(v.storage_class & STCreturn) && + v.isParameter() && + sc.func.flags & FUNCFLAGreturnInprocess && + p == sc.func) + { + inferReturn(sc.func, v); // infer addition of 'return' + continue; + } + if (v.isScope()) { if (v.storage_class & STCreturn) continue; - if (v.isParameter()) - { - if (sc.func.flags & FUNCFLAGreturnInprocess && p == sc.func) - { - inferReturn(sc.func, v); // infer addition of 'return' - continue; - } - } if (sc._module && sc._module.isRoot() && /* This case comes up when the ReturnStatement of a __foreachbody is * checked for escapes by the caller of __foreachbody. Skip it. diff --git a/test/fail_compilation/retscope.d b/test/fail_compilation/retscope.d index fef2f027f31b..6ca7be36aae2 100644 --- a/test/fail_compilation/retscope.d +++ b/test/fail_compilation/retscope.d @@ -267,3 +267,23 @@ void escape5() @safe pp = &p; // error } +/***********************************************/ + +/* +TEST_OUTPUT: +--- +fail_compilation/retscope.d(287): Error: escaping reference to local variable b +--- +*/ + +@safe int* foo6()(int* arg) +{ + return arg; +} + +int* escape6() @safe +{ + int b; + return foo6(&b); +} +