Skip to content

Commit e4c5ea9

Browse files
committed
Spurious error for pack expansions in nested generic lambdas [GH #491]
1 parent e54ec7a commit e4c5ea9

30 files changed

Lines changed: 134 additions & 451 deletions

‎src/expr.c‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14570,10 +14570,14 @@ indication in *rcblock).
1457014570
pesep != NULL && pesep->instantiation_descr == NULL) {
1457114571
/* Check that the identifier is a parameter pack name. */
1457214572
if (!symbol_is_pack(sym)) {
14573-
expr_pos_error(ec_sizeof_operand_not_parameter_pack,
14574-
&pos_curr_token);
14575-
suppress_expansion_with_no_packs_diagnostic(pesep);
14576-
err = TRUE;
14573+
/* An earlier scan may have recorded a pack whose name now
14574+
refers to a substituted symbol. */
14575+
if (!pesep->recorded_pack_expansion) {
14576+
expr_pos_error(ec_sizeof_operand_not_parameter_pack,
14577+
&pos_curr_token);
14578+
suppress_expansion_with_no_packs_diagnostic(pesep);
14579+
err = TRUE;
14580+
} /* if */
1457714581
} else {
1457814582
record_potential_pack_reference(sym, &pos_curr_token);
1457914583
} /* if */

‎src/pending_changes/gh-491.txt‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[GH #491]
2+
Spurious error for pack expansions in nested generic lambdas
3+
4+
Previously, valid pack expansions (including sizeof... and fold expressions) in
5+
nested generic lambdas (for example, in default arguments or exception
6+
specifications) could be rejected during instantiation with spurious missing
7+
parameter pack errors. For example, with --c++14 -tused:
8+
9+
int f() {
10+
auto g = [](auto... x) {
11+
return [](auto, int n = sizeof...(x)) { return n; }(0);
12+
};
13+
return g(1, 2); // Previously a spurious error, now okay.
14+
}

‎src/scope_stk.c‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11678,6 +11678,7 @@ to it.
1167811678
pesep->enclosing_packs_reset = FALSE;
1167911679
pesep->preserve_deduced_packs = FALSE;
1168011680
pesep->contains_pack_reference = FALSE;
11681+
pesep->recorded_pack_expansion = FALSE;
1168111682
return pesep;
1168211683
} /* alloc_pack_expansion_stack_entry */
1168311684

@@ -13754,6 +13755,12 @@ pack-index scanner.
1375413755
values for both T and U. */
1375513756
reset_enclosing_pack_values();
1375613757
pesep->enclosing_packs_reset = TRUE;
13758+
if (claim_pack_index || !pedp->is_pack_index) {
13759+
/* An earlier scan recorded a pack here, even if lookup now yields
13760+
substituted symbols. Pack-index descriptors belong to their
13761+
operand scanner, not an enclosing list-level context. */
13762+
pesep->recorded_pack_expansion = TRUE;
13763+
} /* if */
1375713764
} /* if */
1375813765
/* Allocate an expansion descriptor for this stack entry. */
1375913766
pesep->expansion_descr = alloc_pack_expansion_descr();
@@ -14202,11 +14209,11 @@ effect and returns NULL.
1420214209
result_pedp = pedp;
1420314210
} /* if */
1420414211
} else {
14205-
/* There were no packs referenced. This is not a pack expansion.
14206-
If a pack expansion ("...") has been seen, issue an error that
14207-
no packs were encountered. */
14212+
/* No packs were referenced. Diagnose an ellipsis unless an
14213+
earlier scan recorded a pack expansion for this context. */
1420814214
if (pedp->ellipsis_seen &&
14209-
!pesep->expansion_with_no_packs_diagnostic_issued) {
14215+
!pesep->expansion_with_no_packs_diagnostic_issued &&
14216+
!pesep->recorded_pack_expansion) {
1421014217
pos_error(ec_expansion_contains_no_packs,
1421114218
&pedp->ellipsis_position);
1421214219
} /* if */
@@ -14532,7 +14539,8 @@ a_boolean any_packs_referenced(void)
1453214539
Return TRUE if the current pack expansion context contains any pack
1453314540
references (including those from an enclosing pack context) and we are in
1453414541
a template definition context. See any_packs_referenced_in_curr_context
14535-
for more information.
14542+
for more information. recorded_pack_expansion is not considered here
14543+
because it describes only the context on the top of the stack.
1453614544
*/
1453714545
{
1453814546
a_boolean result = FALSE;
@@ -14558,20 +14566,25 @@ references and we are in a template definition context. This differs
1455814566
from any_packs_referenced in that this will only be TRUE if the
1455914567
context on the top of the pack expansion stack contains a pack reference
1456014568
while any_packs_referenced will be TRUE if the reference was in an
14561-
enclosing context.
14569+
enclosing context. This is also TRUE when an earlier scan recorded a
14570+
pack expansion for the context even though the current scan records no
14571+
pack references (see recorded_pack_expansion).
1456214572
*/
1456314573
{
1456414574
a_boolean result = FALSE;
1456514575

1456614576
/* In a prototype instantiation context expansion_descr->packs_referenced
1456714577
will be NULL but contains_pack_reference could be TRUE. In a real
1456814578
instantiation expansion_descr->packs_referenced might be TRUE but
14569-
contains_pack_reference will be FALSE. */
14579+
contains_pack_reference will be FALSE. recorded_pack_expansion is
14580+
TRUE when an earlier scan recorded a pack expansion but the current
14581+
scan records no pack references. */
1457014582
if (pack_expansion_stack != NULL &&
1457114583
!pack_expansion_stack->is_suppression) {
1457214584
a_pack_expansion_stack_entry_ptr pesep = pack_expansion_stack;
1457314585
result = pesep->expansion_descr->packs_referenced != NULL ||
14574-
pesep->contains_pack_reference;
14586+
pesep->contains_pack_reference ||
14587+
pesep->recorded_pack_expansion;
1457514588
} /* if */
1457614589
return result;
1457714590
} /* any_packs_referenced_in_curr_context */

‎src/scope_stk.h‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -606,55 +606,53 @@ typedef struct a_pack_expansion_stack_entry {
606606
/* In rescan contexts, a copy of the supplied
607607
template argument list is made. This points to
608608
that copy of the list. NULL otherwise. */
609-
a_byte_boolean
610-
is_rescan;
609+
a_bit_field is_rescan:1;
611610
/* TRUE when the expansion is being done in an
612611
expression rescan context. In such contexts the
613612
current values of the packs are maintained, but
614613
no token manipulation or checking is done. */
615-
a_byte_boolean
616-
is_deduction;
614+
a_bit_field is_deduction:1;
617615
/* TRUE when the expansion is being done in an
618616
template argument deduction context. In such
619617
contexts a new template argument is created for
620618
each pack element produced by the deduction
621619
process, but no token manipulation or checking
622620
is done. */
623-
a_byte_boolean
624-
is_suppression;
621+
a_bit_field is_suppression:1;
625622
/* TRUE when the processing of expansion contexts
626623
is being suppressed. This only suppresses
627624
expansions from tokens, not rescans. */
628-
a_byte_boolean
629-
expansion_with_no_packs_diagnostic_issued;
625+
a_bit_field expansion_with_no_packs_diagnostic_issued:1;
630626
/* TRUE if suppress_expansion_with_no_packs_diagnostic
631627
was called to indicate that the caller already
632628
issued a diagnostic for an expansion with no
633629
packs. */
634-
a_byte_boolean
635-
is_lookahead;
630+
a_bit_field is_lookahead:1;
636631
/* TRUE if this context is being pushed to
637632
distinguish between two contexts. When this
638633
is TRUE, it is assumed that another begin...
639634
call will be done for the same starting position,
640635
and that context will be responsible for the
641636
end... and advance... calls. */
642-
a_byte_boolean
643-
enclosing_packs_reset;
637+
a_bit_field enclosing_packs_reset:1;
644638
/* TRUE for a pack expansion of a template declaration
645639
in a real instantiation (see call of
646640
reset_enclosing_pack_values in
647641
begin_potential_pack_expansion_context_full for
648642
more information). */
649-
a_boolean
650-
preserve_deduced_packs;
643+
a_bit_field preserve_deduced_packs:1;
651644
/* TRUE if is_rescan is TRUE and deduced parameter
652645
packs should be retained in the substituted type. */
653-
a_byte_boolean
654-
contains_pack_reference;
646+
a_bit_field contains_pack_reference:1;
655647
/* TRUE during prototype instantiations if any
656648
pack references were recorded while this entry
657649
was at the top of the pack expansion stack. */
650+
a_bit_field recorded_pack_expansion:1;
651+
/* TRUE if an earlier scan recorded a pack expansion
652+
for this context, which is being scanned without
653+
pack instantiation. The descriptor is evidence
654+
of a pack even if lookup yields substituted
655+
symbols and no pack references are recorded. */
658656
} a_pack_expansion_stack_entry;
659657

660658

‎tests/expectations/edg_x86_64/imported.log‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,6 @@ clang/cpp/SemaTemplate/fun-template-def.sft.cpp:fn:-DTEST_NUMBER=1 --c++03:FAIL:
15061506
clang/cpp/SemaTemplate/fun-template-def.sft.cpp:fn:-DTEST_NUMBER=2 --c++11:FAIL:
15071507
clang/cpp/SemaTemplate/fun-template-def.sft.cpp:fn:-DTEST_NUMBER=3 --c++17:FAIL:
15081508
clang/cpp/SemaTemplate/fun-template-def.sft.cpp:fn:-DTEST_NUMBER=4 --c++20:FAIL:
1509-
clang/cpp/SemaTemplate/generic-lambda.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL:
15101509
clang/cpp/SemaTemplate/instantiate-after-fatal-cxx17.sft.cpp:fp:-DTEST_NUMBER=1 --c++17:CATASTROPHE:
15111510
clang/cpp/SemaTemplate/instantiate-explicitly-after-fatal.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL:
15121511
clang/cpp/SemaTemplate/instantiate-incomplete-typo-suggested-error-limit.sft.cpp:fp:-DTEST_NUMBER=1:FAIL:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=1 --c++14 -tused Test_name.c

‎tests/tests/changes/gh-491.sft.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//type:fp
2+
//options:--c++14 -tused
3+
//remark:Spurious error for pack expansions in nested generic lambdas
4+
5+
int f() {
6+
auto g = [](auto... x) {
7+
return [](auto, int n = sizeof...(x)) { return n; }(0);
8+
};
9+
return g(1, 2); // Previously a spurious error, now okay.
10+
}

‎tests/tests/imported/clang/cpp/SemaCXX/.cxx1y-generic-lambdas.rto/default.1.1.txt‎

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
6666
auto L = [](auto a) {
6767
^
6868

69-
"Test_name.c", line 448: error: pack expansion does not make use of any argument packs
70-
print("d = ", d ..., "\n");
71-
^
72-
detected during instantiation of function "lambda [](auto)->auto [with <auto-1>=char]" at line 461
73-
7469
"Test_name.c", line 468: error: no suitable conversion function from "lambda [](const char *, int, const char *, double, const char *, int)->char" to "int (*)(const char *, int, const char *, double, const char *, int)" exists
7570
int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
7671
^
@@ -79,34 +74,14 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
7974
} // end test()
8075
^
8176

82-
"Test_name.c", line 527: error: the operand of sizeof... must be a parameter pack name
83-
x = sizeof...(b);
84-
^
85-
detected during instantiation of "auto nested_non_capturing_lambda_tests::wrapped_within_templates::implicit_return_deduction::fooV(Ts...) [with Ts=<double, const char *, char, int>]" at line 542
86-
87-
"Test_name.c", line 563: error: pack expansion does not make use of any argument packs
88-
print("d = ", d ..., "\n");
89-
^
90-
detected during instantiation of function "lambda [](auto)->auto [with <auto-1>=char]" at line 574
91-
9277
"Test_name.c", line 581: error: no suitable conversion function from "lambda [](const char *, int, const char *, double, const char *, int)->char" to "int (*)(const char *, int, const char *, double, const char *, int)" exists
9378
int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
9479
^
9580

96-
"Test_name.c", line 687: error: pack expansion does not make use of any argument packs
97-
print("d = ", d ..., "\n");
98-
^
99-
detected during instantiation of function "lambda [](auto)->auto [with <auto-1>=char]" at line 700
100-
10181
"Test_name.c", line 707: error: no suitable conversion function from "lambda [](const char *, int, const char *, double, const char *, int)->char" to "int (*)(const char *, int, const char *, double, const char *, int)" exists
10282
int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
10383
^
10484

105-
"Test_name.c", line 728: error: pack expansion does not make use of any argument packs
106-
print("d = ", d ..., "\n");
107-
^
108-
detected during instantiation of function "lambda [](auto)->auto [with <auto-1>=char]" at line 742
109-
11085
"Test_name.c", line 749: error: no suitable conversion function from "lambda [](const char *, int, const char *, double, const char *, int)->char" to "int (*)(const char *, int, const char *, double, const char *, int)" exists
11186
int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
11287
^
@@ -156,7 +131,7 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
156131
auto y = [](auto (*p)() -> auto) { return p(); };
157132
^
158133

159-
30 errors and 1 catastrophic error detected in the compilation of "Test_name.c".
134+
25 errors and 1 catastrophic error detected in the compilation of "Test_name.c".
160135
Compilation aborted.
161136
Aborted
162137
eccp: front end returned an exit status of 134

‎tests/tests/imported/clang/cpp/SemaCXX/.cxx1y-generic-lambdas.rto/default.2.1.txt‎

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ fe_only -DTEST_NUMBER=2 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
6666
auto L = [](auto a) {
6767
^
6868

69-
"Test_name.c", line 448: error: pack expansion does not make use of any argument packs
70-
print("d = ", d ..., "\n");
71-
^
72-
detected during instantiation of function "lambda [](auto)->auto [with <auto-1>=char]" at line 461
73-
7469
"Test_name.c", line 468: error: no suitable conversion function from "lambda [](const char *, int, const char *, double, const char *, int)->char" to "int (*)(const char *, int, const char *, double, const char *, int)" exists
7570
int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
7671
^
@@ -79,34 +74,14 @@ fe_only -DTEST_NUMBER=2 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
7974
} // end test()
8075
^
8176

82-
"Test_name.c", line 527: error: the operand of sizeof... must be a parameter pack name
83-
x = sizeof...(b);
84-
^
85-
detected during instantiation of "auto nested_non_capturing_lambda_tests::wrapped_within_templates::implicit_return_deduction::fooV(Ts...) [with Ts=<double, const char *, char, int>]" at line 542
86-
87-
"Test_name.c", line 563: error: pack expansion does not make use of any argument packs
88-
print("d = ", d ..., "\n");
89-
^
90-
detected during instantiation of function "lambda [](auto)->auto [with <auto-1>=char]" at line 574
91-
9277
"Test_name.c", line 581: error: no suitable conversion function from "lambda [](const char *, int, const char *, double, const char *, int)->char" to "int (*)(const char *, int, const char *, double, const char *, int)" exists
9378
int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
9479
^
9580

96-
"Test_name.c", line 687: error: pack expansion does not make use of any argument packs
97-
print("d = ", d ..., "\n");
98-
^
99-
detected during instantiation of function "lambda [](auto)->auto [with <auto-1>=char]" at line 700
100-
10181
"Test_name.c", line 707: error: no suitable conversion function from "lambda [](const char *, int, const char *, double, const char *, int)->char" to "int (*)(const char *, int, const char *, double, const char *, int)" exists
10282
int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
10383
^
10484

105-
"Test_name.c", line 728: error: pack expansion does not make use of any argument packs
106-
print("d = ", d ..., "\n");
107-
^
108-
detected during instantiation of function "lambda [](auto)->auto [with <auto-1>=char]" at line 742
109-
11085
"Test_name.c", line 749: error: no suitable conversion function from "lambda [](const char *, int, const char *, double, const char *, int)->char" to "int (*)(const char *, int, const char *, double, const char *, int)" exists
11186
int (*np2)(const char*, int, const char*, double, const char*, int) = O; // expected-error{{no viable conversion}}
11287
^
@@ -156,7 +131,7 @@ fe_only -DTEST_NUMBER=2 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
156131
auto y = [](auto (*p)() -> auto) { return p(); };
157132
^
158133

159-
30 errors and 1 catastrophic error detected in the compilation of "Test_name.c".
134+
25 errors and 1 catastrophic error detected in the compilation of "Test_name.c".
160135
Compilation aborted.
161136
Aborted
162137
eccp: front end returned an exit status of 134

0 commit comments

Comments
 (0)