From f158c3553e512b2b90b93b5393fff2f31e17c972 Mon Sep 17 00:00:00 2001 From: Dennis Date: Tue, 16 Jul 2024 13:29:57 +0200 Subject: [PATCH] escape.d: Fix handling of isTypesafeVariadicArray (#16709) --- compiler/src/dmd/escape.d | 23 +++++++--------------- compiler/test/fail_compilation/fail13902.d | 4 ++-- compiler/test/fail_compilation/test23022.d | 2 +- 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/compiler/src/dmd/escape.d b/compiler/src/dmd/escape.d index 847780a80a..473031348b 100644 --- a/compiler/src/dmd/escape.d +++ b/compiler/src/dmd/escape.d @@ -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); diff --git a/compiler/test/fail_compilation/fail13902.d b/compiler/test/fail_compilation/fail13902.d index 47cb65cde2..3d3a4f41cc 100644 --- a/compiler/test/fail_compilation/fail13902.d +++ b/compiler/test/fail_compilation/fail13902.d @@ -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[]; } diff --git a/compiler/test/fail_compilation/test23022.d b/compiler/test/fail_compilation/test23022.d index 8c4eca9c56..db1f4a712c 100644 --- a/compiler/test/fail_compilation/test23022.d +++ b/compiler/test/fail_compilation/test23022.d @@ -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` --- */