Skip to content

Commit f02c714

Browse files
committed
fix Issue 20150 - -dip1000 defeated by pure
1 parent 388455b commit f02c714

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

src/dmd/mtype.d

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4533,15 +4533,15 @@ extern (C++) final class TypeFunction : TypeNext
45334533
}
45344534
}
45354535

4536-
stc |= STC.scope_;
4537-
45384536
/* Inferring STC.return_ here has false positives
45394537
* for pure functions, producing spurious error messages
45404538
* about escaping references.
45414539
* Give up on it for now.
45424540
*/
45434541
version (none)
45444542
{
4543+
stc |= STC.scope_;
4544+
45454545
Type tret = nextOf().toBasetype();
45464546
if (isref || tret.hasPointers())
45474547
{
@@ -4551,6 +4551,17 @@ extern (C++) final class TypeFunction : TypeNext
45514551
stc |= STC.return_;
45524552
}
45534553
}
4554+
else
4555+
{
4556+
// Check escaping through return value
4557+
Type tret = nextOf().toBasetype();
4558+
if (isref || tret.hasPointers())
4559+
{
4560+
return stc;
4561+
}
4562+
4563+
stc |= STC.scope_;
4564+
}
45544565

45554566
return stc;
45564567
}

test/fail_compilation/retscope6.d

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,36 @@ void hmac(scope ubyte[] secret)
198198
ubyte[10] buffer;
199199
secret = buffer[];
200200
}
201+
202+
/* TEST_OUTPUT:
203+
---
204+
fail_compilation/retscope6.d(12011): Error: reference to local variable `x` assigned to non-scope parameter `r` calling retscope6.escape_m_20150
205+
fail_compilation/retscope6.d(12022): Error: reference to local variable `x` assigned to non-scope parameter `r` calling retscope6.escape_c_20150
206+
---
207+
*/
208+
209+
#line 12000
210+
211+
// https://issues.dlang.org/show_bug.cgi?id=20150
212+
213+
int* escape_m_20150(int* r) @safe pure
214+
{
215+
return r;
216+
}
217+
218+
int* f_m_20150() @safe
219+
{
220+
int x = 42;
221+
return escape_m_20150(&x);
222+
}
223+
224+
const(int)* escape_c_20150(const int* r) @safe pure
225+
{
226+
return r;
227+
}
228+
229+
const(int)* f_c_20150() @safe
230+
{
231+
int x = 42;
232+
return escape_c_20150(&x);
233+
}

0 commit comments

Comments
 (0)