Skip to content

Commit c5dc46e

Browse files
committed
Empty enclosing packs in generic lambdas [GH #500]
1 parent 8fa21be commit c5dc46e

19 files changed

Lines changed: 139 additions & 37 deletions

File tree

‎src/expr.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,11 @@ changed below to be TRUE for certain kinds of expressions and/or contexts.
14541454
#endif /* MICROSOFT_EXTENSIONS_ALLOWED */
14551455
if (suppress_warning) {
14561456
/* The caller has requested that warnings be suppressed. */
1457+
} else if (in_generic_lambda_in_real_instantiation()) {
1458+
/* A hybrid expansion can collapse to an expression with no effect in
1459+
this prototype instantiation. Wait for the real instantiation rather
1460+
than diagnosing that transient form. */
1461+
suppress_warning = TRUE;
14571462
} else if (expr_stack->is_type_operator_arg_expression) {
14581463
/* decltype expressions are sometimes written to check SFINAE
14591464
conditions, so do not warn inside them. */

‎src/pending_changes/gh-500.txt‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[GH #500]
2+
Empty enclosing packs in generic lambdas
3+
4+
A pack expansion in a generic lambda nested in a template instantiation could
5+
produce an error node in the IL when it referenced a lambda parameter pack
6+
before an empty enclosing parameter pack. Empty enclosing packs are now
7+
handled correctly regardless of the order of the pack references. Spurious
8+
warnings from prototype instantiations in such cases are also suppressed. For
9+
example, with --c++20 -tused:
10+
11+
template<int... Is> void f() {
12+
[]<int... Js>() { ((Js * Is) * ... * 1); };
13+
}
14+
template void f<>(); // Previously produced an error node in the IL.

‎src/scope_stk.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12548,7 +12548,7 @@ have a pack expansion so that it can be expanded in a rescan context.
1254812548
} /* add_pack_expansion_descr_to_prototype_arg */
1254912549

1255012550

12551-
static a_boolean in_generic_lambda_in_real_instantiation(void)
12551+
a_boolean in_generic_lambda_in_real_instantiation(void)
1255212552
/*
1255312553
Return TRUE if we are in the definition of a generic lambda that is nested
1255412554
inside a real instantiation. In such contexts, a kind of "hybrid expansion"
@@ -12836,6 +12836,11 @@ lengths) *err is set to TRUE, FALSE otherwise.
1283612836
/* Got an expansion from an enclosing real instantiation and an
1283712837
unexpanded pack from a nested generic lambda. */
1283812838
has_hybrid_pack_expansion = TRUE;
12839+
if (elements_for_pack == 0) {
12840+
/* An empty enclosing pack makes the expansion empty even if an
12841+
unexpanded pack from the lambda was encountered first. */
12842+
elements = 0;
12843+
} /* if */
1283912844
} else if (!is_rescan) {
1284012845
pos_st2_error(ec_pack_length_mismatch, &prp->position,
1284112846
prp->symbol->header->identifier,

‎src/scope_stk.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,6 +2632,8 @@ extern void end_prescan_context(
26322632
a_boolean saved_in_disambiguation,
26332633
a_boolean saved_source_sequence_entries_disallowed);
26342634

2635+
extern a_boolean in_generic_lambda_in_real_instantiation(void);
2636+
26352637
extern a_boolean in_generic_lambda_in_prototype_instantiation(void);
26362638

26372639
extern a_routine_ptr enclosing_nonlambda_routine_for_lambda_class(

‎tests/tests/imported/clang/cpp/SemaCXX/.lambda-pack-expansion.rto/default.1.1.txt‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,4 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
5454
baz<TTPs>;
5555
^
5656

57-
"Test_name.c", line 130: error: template template parameter "<error>" is not compatible with template template parameter "<unnamed>"
58-
baz<TTPs>;
59-
^
60-
detected during instantiation of "void GH112352::S::foldExpr2<TTPs...>() [with TTPs=<>]" at line 139
61-
62-
"Test_name.c", line 131: error: too many arguments for template template parameter "TTPs"
63-
TTPs<int> D;
64-
^
65-
detected during instantiation of "void GH112352::S::foldExpr2<TTPs...>() [with TTPs=<>]" at line 139
66-
67-
5 errors detected in the compilation of "Test_name.c".
57+
3 errors detected in the compilation of "Test_name.c".

‎tests/tests/imported/clang/cpp/SemaCXX/.lambda-pack-expansion.rto/edg_x86_32_cp.1.1.txt‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,4 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
7878
instantiation of function "lambda []()->void [with Ts=<int, int>]" at line 55
7979
instantiation of "void GH63677::f<<unnamed>>() [with <unnamed>=int]" at line 58
8080

81-
"Test_name.c", line 130: error: template template parameter "<error>" is not compatible with template template parameter "<unnamed>"
82-
baz<TTPs>;
83-
^
84-
detected during instantiation of "void GH112352::S::foldExpr2<TTPs...>() [with TTPs=<>]" at line 139
85-
86-
"Test_name.c", line 131: error: too many arguments for template template parameter "TTPs"
87-
TTPs<int> D;
88-
^
89-
detected during instantiation of "void GH112352::S::foldExpr2<TTPs...>() [with TTPs=<>]" at line 139
90-
91-
9 errors detected in the compilation of "Test_name.c".
81+
7 errors detected in the compilation of "Test_name.c".

‎tests/tests/imported/clang/cpp/SemaCXX/.lambda-pack-expansion.rto/edg_x86_64_cp.1.1.txt‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,4 @@ fe_only -DTEST_NUMBER=1 --clang -tused -e 200 --no_wrap_diagnostics --clang_vers
7878
instantiation of function "lambda []()->void [with Ts=<int, int>]" at line 55
7979
instantiation of "void GH63677::f<<unnamed>>() [with <unnamed>=int]" at line 58
8080

81-
"Test_name.c", line 130: error: template template parameter "<error>" is not compatible with template template parameter "<unnamed>"
82-
baz<TTPs>;
83-
^
84-
detected during instantiation of "void GH112352::S::foldExpr2<TTPs...>() [with TTPs=<>]" at line 139
85-
86-
"Test_name.c", line 131: error: too many arguments for template template parameter "TTPs"
87-
TTPs<int> D;
88-
^
89-
detected during instantiation of "void GH112352::S::foldExpr2<TTPs...>() [with TTPs=<>]" at line 139
90-
91-
9 errors detected in the compilation of "Test_name.c".
81+
7 errors detected in the compilation of "Test_name.c".

‎tests/tests/imported/gnu/cpp/cpp1z/.constexpr-if-lambda5.rto/default.1.1.txt‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ fe_only -DTEST_NUMBER=1 --gnu_version=999999 -tused -e 200 --no_wrap --no_wrap_d
22
"Test_name.c", line 10: warning: expression has no effect
33
(..., ts);
44
^
5-
detected during instantiation of "void f(Ts...) [with Ts=<int, char>]" at line 16
5+
detected during:
6+
instantiation of function "lambda [](auto)->auto [with <auto-1>=int]" at line 12
7+
instantiation of "void f(Ts...) [with Ts=<int, char>]" at line 16
68

79
"Test_name.c", line 10: warning: expression has no effect
810
(..., ts);
911
^
10-
detected during instantiation of "void f(Ts...) [with Ts=<int, char>]" at line 16
12+
detected during:
13+
instantiation of function "lambda [](auto)->auto [with <auto-1>=int]" at line 12
14+
instantiation of "void f(Ts...) [with Ts=<int, char>]" at line 16
1115

‎tests/tests/imported/gnu/cpp/cpp1z/.fold-lambda4.rto/default.1.1.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ fe_only -DTEST_NUMBER=1 --gnu_version=999999 -tused -e 200 --no_wrap --no_wrap_d
33
ys;
44
^
55
detected during:
6-
instantiation of function "lambda [](auto &...)->auto [with <auto-1>=<int>]" at line 26
6+
instantiation of function "lambda [](auto)->auto [with <auto-1>=int]" at line 11
7+
instantiation of "void X::f(F) [with F=lambda [](auto)->auto]" at line 26
78
instantiation of "void bug(Xs...) [with Xs=<X>]" at line 31
89

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fe_only -DTEST_NUMBER=1 -tused --c++20 Test_name.c
2+
"Test_name.c", line 17: warning: expression has no effect
3+
auto l = []<int... S>() { ((S * V) * ... * 1); };
4+
^
5+
detected during:
6+
instantiation of function "lambda []()->auto [with S=<3>]" at line 18
7+
instantiation of "void real_instantiation::f<V...>() [with V=<2>]" at line 21
8+

0 commit comments

Comments
 (0)