Skip to content

Commit f5dab52

Browse files
committed
Pack expansions in nested generic-lambda template default arguments [GH #506]
1 parent 63d8840 commit f5dab52

7 files changed

Lines changed: 113 additions & 2 deletions

File tree

‎src/pending_changes/gh-506.txt‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[GH #506]
2+
Pack expansions in nested generic-lambda template default arguments
3+
4+
A default template argument of a nested generic lambda could be rejected when a
5+
pack expansion involving an enclosing pack was incorrectly treated as empty
6+
before the lambda's template arguments were available. Such defaults now
7+
remain dependent until instantiation. For example, with --c++20 -tused:
8+
9+
template<int... Is> void f() {
10+
[](auto) {
11+
[]<int... Js, typename U = int[((Js * Is) * ... * 1) - 2]>() {};
12+
};
13+
}
14+
template void f<2>(); // Previously a spurious array bound error, now okay.

‎src/scope_stk.c‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13490,6 +13490,39 @@ template within a real instantiation of a class.
1349013490
} /* is_alias_prototype_in_real_instantiation */
1349113491

1349213492

13493+
static a_boolean has_unbound_template_pack(a_pack_expansion_descr_ptr pedp)
13494+
/*
13495+
Return TRUE if the expansion described by pedp references a template parameter
13496+
pack not found in the current or enclosing instantiations. A pack found with
13497+
no argument elements is bound and does not cause TRUE to be returned.
13498+
*/
13499+
{
13500+
a_boolean result = FALSE;
13501+
a_pack_reference_ptr prp;
13502+
a_template_param_ptr templ_param_list;
13503+
a_template_arg_ptr templ_arg_list;
13504+
13505+
get_curr_template_params_and_args(&templ_param_list, &templ_arg_list);
13506+
for (prp = pedp->packs_referenced; prp != NULL; prp = prp->next) {
13507+
if (prp->kind == prk_template_param) {
13508+
uint32_t elements;
13509+
a_template_param_ptr tpp;
13510+
a_boolean found;
13511+
13512+
(void)find_template_arg_for_pack(templ_param_list, templ_arg_list,
13513+
prp->symbol, &elements, &tpp, &found,
13514+
/*is_rescan=*/FALSE,
13515+
/*is_deduction=*/FALSE);
13516+
if (!found) {
13517+
result = TRUE;
13518+
break;
13519+
} /* if */
13520+
} /* if */
13521+
} /* for */
13522+
return result;
13523+
} /* has_unbound_template_pack */
13524+
13525+
1349313526
static a_boolean is_pack_instantiation_context(
1349413527
a_pack_expansion_descr_ptr *p_pedp)
1349513528
/*
@@ -13502,7 +13535,8 @@ Something is treated as an instantiation context if:
1350213535
only uses enclosing packs (e.g., the declaration of a member template
1350313536
in the instantiation of an enclosing class template).
1350413537
- We are in a generic lambda prototype instantiation inside some real
13505-
enclosing instantiation.
13538+
enclosing instantiation and have arguments for all referenced template
13539+
parameter packs.
1350613540
- We have real template arguments for all referenced packs.
1350713541

1350813542
In such contexts, return TRUE. If we found a pack expansion, set *p_pedp
@@ -13527,7 +13561,9 @@ that *p_pedp is set even when FALSE is returned.
1352713561
result = TRUE;
1352813562
} else if (pedp->uses_any_enclosing_packs &&
1352913563
in_generic_lambda_in_real_instantiation()) {
13530-
result = TRUE;
13564+
/* A nested lambda's template parameter defaults can reference packs
13565+
whose prototype arguments have not been created yet. */
13566+
result = !has_unbound_template_pack(pedp);
1353113567
} else if (pedp->uses_only_enclosing_packs) {
1353213568
a_pack_reference_ptr prp;
1353313569
a_template_nesting_depth max_pack_depth = NO_NESTING_DEPTH;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=1 -tused --c++20 Test_name.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=2 -tused --c++20 --gn 150200 Test_name.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=3 -tused --c++20 --clang_version 220100 Test_name.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=4 -tused --ms_c++20 --microsoft_version 1950 Test_name.c
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//type:fp
2+
//options:--c++20:--c++20 --gn 150200:--c++20 --clang_version 220100:--ms_c++20 --microsoft_version 1950
3+
//options_all:-tused
4+
5+
namespace nontype_template_parameter_default
6+
{
7+
template<int... Is> constexpr auto f() {
8+
return [](auto) {
9+
return []<int... Js, int N = ((Js * Is) * ... * 1)>() {
10+
return N;
11+
};
12+
};
13+
}
14+
15+
static_assert(f<>()(0).template operator()<>() == 1);
16+
static_assert(f<2, 3>()(0).template operator()<4, 5>() == 120);
17+
}
18+
19+
namespace uninstantiated_template_parameter_default
20+
{
21+
template<int... Is> void f() {
22+
[](auto) {
23+
[]<int... Js, typename U = int[((Js * Is) * ... * 1) - 2]>() {};
24+
};
25+
}
26+
template void f<2>();
27+
}
28+
29+
namespace inner_pack_first_in_template_parameter_default
30+
{
31+
template<int... Is> constexpr auto f() {
32+
return [](auto) {
33+
return []<int... Js,
34+
typename U = int[((Js * Is) * ... * 1) - 2]>() {
35+
return sizeof(U);
36+
};
37+
};
38+
}
39+
40+
static_assert(f<2>()(0).template operator()<3>() == 4 * sizeof(int));
41+
static_assert(f<2, 3>()(0).template operator()<4, 5>() == 118 * sizeof(int));
42+
}
43+
44+
namespace outer_pack_first_in_template_parameter_default
45+
{
46+
template<int... Is> constexpr auto f() {
47+
return [](auto) {
48+
return []<int... Js,
49+
typename U = int[((Is * Js) * ... * 1) - 2]>() {
50+
return sizeof(U);
51+
};
52+
};
53+
}
54+
55+
static_assert(f<2>()(0).template operator()<3>() == 4 * sizeof(int));
56+
static_assert(f<2, 3>()(0).template operator()<4, 5>() == 118 * sizeof(int));
57+
}

0 commit comments

Comments
 (0)