Skip to content

Commit 36b57e4

Browse files
committed
fix Issue 20150 - -dip1000 return attribute must be explicit in pure functions
1 parent 74190e5 commit 36b57e4

2 files changed

Lines changed: 34 additions & 6 deletions

File tree

src/dmd/escape.d

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,15 +1194,18 @@ private bool checkReturnEscapeImpl(Scope* sc, Expression e, bool refs, bool gag)
11941194

11951195
Dsymbol p = v.toParent2();
11961196

1197-
if ((v.isScope() || (v.storage_class & STC.maybescope)) &&
1198-
!(v.storage_class & STC.return_) &&
1197+
bool needsReturn = false;
1198+
if (!(v.storage_class & STC.return_) &&
11991199
v.isParameter() &&
1200-
!v.doNotInferReturn &&
1201-
sc.func.flags & FUNCFLAG.returnInprocess &&
12021200
p == sc.func)
12031201
{
1204-
inferReturn(sc.func, v); // infer addition of 'return'
1205-
continue;
1202+
if ((v.isScope() || (v.storage_class & STC.maybescope)) &&
1203+
sc.func.flags & FUNCFLAG.returnInprocess)
1204+
{
1205+
inferReturn(sc.func, v); // infer addition of 'return'
1206+
continue;
1207+
}
1208+
needsReturn = true; // needs 'return' annotation on parameter
12061209
}
12071210

12081211
if (v.isScope())
@@ -1249,6 +1252,17 @@ private bool checkReturnEscapeImpl(Scope* sc, Expression e, bool refs, bool gag)
12491252
result = false;
12501253
}
12511254
}
1255+
else if (needsReturn && sc.func.type.isTypeFunction().purity != PURE.impure && global.params.vsafe &&
1256+
sc._module && sc._module.isRoot())
1257+
{
1258+
/* Pure functions assume their parameters are `scope`, but they don't assume `return`.
1259+
* Hence, the annotation needs to be explicit.
1260+
*/
1261+
if (!gag)
1262+
error(e.loc, "`pure` function `%s` returns parameter `%s`, annotate with `return`",
1263+
sc.func.ident.toChars(), v.toChars());
1264+
result = true;
1265+
}
12521266
else
12531267
{
12541268
//printf("no infer for %s in %s, %d\n", v.toChars(), sc.func.ident.toChars(), __LINE__);

test/fail_compilation/retscope6.d

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,17 @@ T9 testfred()
171171
return fred(&i); // error
172172
}
173173

174+
/* TEST_OUTPUT:
175+
---
176+
fail_compilation/retscope6.d(10005): Error: `pure` function `escape` returns parameter `r`, annotate with `return`
177+
---
178+
*/
179+
180+
#line 10000
181+
182+
// https://issues.dlang.org/show_bug.cgi?id=20150
183+
184+
int* escape(int* r) @safe pure
185+
{
186+
return r;
187+
}

0 commit comments

Comments
 (0)