escape.d: Fix handling of isTypesafeVariadicArray (#16709)

This commit is contained in:
Dennis 2024-07-16 13:29:57 +02:00 committed by GitHub
parent 6b49d066a2
commit f158c3553e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 10 additions and 19 deletions

View file

@ -1215,7 +1215,13 @@ private bool checkReturnEscapeImpl(ref Scope sc, Expression e, bool refs, bool g
return;
}
if (v.isScope())
if (v.isTypesafeVariadicArray && p == sc.func)
{
if (!gag)
sc.eSink.error(e.loc, "returning `%s` escapes a reference to variadic parameter `%s`", e.toChars(), v.toChars());
result = false;
}
else if (v.isScope())
{
/* If `return scope` applies to v.
*/
@ -1270,12 +1276,6 @@ private bool checkReturnEscapeImpl(ref Scope sc, Expression e, bool refs, bool g
}
}
}
else if (v.isTypesafeVariadicArray && p == sc.func)
{
if (!gag)
sc.eSink.error(e.loc, "returning `%s` escapes a reference to variadic parameter `%s`", e.toChars(), v.toChars());
result = false;
}
else
{
//printf("no infer for %s in %s, %d\n", v.toChars(), sc.func.ident.toChars(), __LINE__);
@ -2023,15 +2023,6 @@ void escapeByRef(Expression e, ref scope EscapeByResults er, bool retRefTransiti
void visitIndex(IndexExp e)
{
Type tb = e.e1.type.toBasetype();
if (auto ve = e.e1.isVarExp())
{
VarDeclaration v = ve.var.isVarDeclaration();
if (v && v.isTypesafeVariadicArray)
{
er.byRef(v, retRefTransition);
return;
}
}
if (tb.ty == Tsarray)
{
escapeByRef(e.e1, er, retRefTransition);

View file

@ -323,9 +323,9 @@ int[] testSlice2() { int[3] sa; int n; return sa[n..2][1..2]; }
/*
TEST_OUTPUT:
---
fail_compilation/fail13902.d(324): Error: returning `vda[0]` escapes a reference to parameter `vda`
fail_compilation/fail13902.d(324): Error: returning `vda[0]` escapes a reference to variadic parameter `vda`
fail_compilation/fail13902.d(325): Error: returning `vda[]` escapes a reference to variadic parameter `vda`
---
*/
ref int testDynamicArrayVariadic1(int[] vda...) { return vda[0]; }
@safe int[] testDynamicArrayVariadic2(int[] vda...) { return vda[]; }

View file

@ -2,7 +2,7 @@
REQUIRED_ARGS: -preview=dip1000
TEST_OUTPUT:
---
fail_compilation/test23022.d(14): Error: scope parameter `p` may not be returned
fail_compilation/test23022.d(14): Error: returning `p` escapes a reference to variadic parameter `p`
---
*/