Skip to content

Commit 29ca6bb

Browse files
Lookup in default template arguments during partial specialization rescans [GH #484]
1 parent dd338bd commit 29ca6bb

20 files changed

Lines changed: 311 additions & 1 deletion
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[GH #484]
2+
Lookup in default template arguments during partial specialization rescans
3+
4+
When a template argument list was checked during the rescan of a partial
5+
specialization (e.g., when matching a partial specialization that uses
6+
void_t), a default template argument of a class template named in that
7+
list was substituted in the context of the partial specialization rather
8+
than that of the class template. Names declared after the partial
9+
specialization but before the class template were therefore not found,
10+
and the substitution was wrongly considered to fail. (This only occurred
11+
when dependent name processing was done, so Microsoft permissive mode was
12+
not affected unless --dep_name was specified.) That is now fixed.
13+
For example, with --c++17:
14+
15+
template<class...> using void_t = void;
16+
template<class, template<class...> class TT, class... P>
17+
constexpr bool ok_impl = false;
18+
template<template<class...> class TT, class... P>
19+
constexpr bool ok_impl<void_t<TT<P...>>, TT, P...> = true;
20+
namespace lib { template<class T> T&& declval() noexcept; }
21+
template<class T, class = decltype(&lib::declval<T&>())> struct A {};
22+
struct V {};
23+
static_assert(ok_impl<void, A, V>, ""); // Previously failed.

‎src/templates.c‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16139,13 +16139,36 @@ If there is an error in the copying, set *copy_error to TRUE.
1613916139
template <typename T = int, typename S = C<T> > class A {};
1614016140
*/
1614116141
if (new_list != NULL && tpp->def_arg_involves_template_param) {
16142+
a_boolean pushed_rescan_scope = FALSE;
16143+
a_ctws_options_set def_arg_options = options;
16144+
if (is_class_template_symbol(template_sym) &&
16145+
depth_innermost_instantiation_scope != NO_SCOPE_DEPTH &&
16146+
scope_stack[depth_innermost_instantiation_scope].is_rescan &&
16147+
scope_stack[depth_innermost_instantiation_scope].template_sym !=
16148+
template_sym) {
16149+
/* The default argument belongs to template_sym, so names in it
16150+
must be looked up in the context of template_sym, not in the
16151+
context of an enclosing rescan (e.g., of a partial
16152+
specialization declared before entities that the default
16153+
argument refers to). */
16154+
push_instantiation_scope_for_rescan(template_sym);
16155+
pushed_rescan_scope = TRUE;
16156+
/* Pushing an instantiation scope clears the expression stack.
16157+
Also, a deduced pack would already have been added to the new
16158+
template argument list. */
16159+
def_arg_options &= ~(CTWS_INSIDE_EXPR_RESCAN |
16160+
CTWS_PRESERVE_DEDUCED_PACKS);
16161+
} /* if */
1614216162
substitute_template_argument(tap, tpp, new_list,
1614316163
param_list_for_copy,
1614416164
new_list, param_list_for_copy,
1614516165
source_pos,
16146-
options,
16166+
def_arg_options,
1614716167
is_generic,
1614816168
copy_error, ctws_state);
16169+
if (pushed_rescan_scope) {
16170+
pop_instantiation_scope_for_rescan();
16171+
} /* if */
1614916172
if (*copy_error) goto done;
1615016173
} /* if */
1615116174
if (new_list == NULL) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fe_only -DTEST_NUMBER=1 --c++17 --g++ 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 --c++17 --clang 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 --ms_c++17 --microsoft --microsoft_version 1936 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 --ms_c++17 --microsoft --microsoft_version 1936 --dep_name 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=5 --c++17 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=1 --c++17 --g++ 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 --c++17 --clang 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 --ms_c++17 --microsoft --microsoft_version 1936 Test_name.c

0 commit comments

Comments
 (0)