Skip to content

Commit e54ec7a

Browse files
jakub-labenskidaveedvdv-nvidia
authored andcommitted
Lookup in default template arguments during partial specialization rescans [GH #484]
1 parent ab072d3 commit e54ec7a

35 files changed

Lines changed: 495 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 or variable template
7+
named in that list was substituted in the context of the partial
8+
specialization rather than that of the class or variable template. Names
9+
declared after the partial specialization but before the class or variable
10+
template were therefore not found, and the substitution was wrongly
11+
considered to fail. (This only occurred when dependent name processing was
12+
done, so Microsoft permissive mode was not affected unless --dep_name was
13+
specified.) That is now fixed. 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: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16139,13 +16139,40 @@ 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 (depth_innermost_instantiation_scope != NO_SCOPE_DEPTH &&
16145+
scope_stack[depth_innermost_instantiation_scope].is_rescan &&
16146+
scope_stack[depth_innermost_instantiation_scope].template_sym !=
16147+
template_sym) {
16148+
/* The default argument belongs to template_sym, so names in it
16149+
must be looked up in the context of template_sym, not in the
16150+
context of an enclosing rescan (e.g., of a partial
16151+
specialization declared before entities that the default
16152+
argument refers to). */
16153+
push_instantiation_scope_for_rescan(template_sym);
16154+
pushed_rescan_scope = TRUE;
16155+
if (scope_is(&scope_stack_top(), sck_function_access)) {
16156+
/* A default template argument is not part of the template, so
16157+
the friends of the template do not have access to it. */
16158+
scope_stack_top().template_sym = NULL;
16159+
} /* if */
16160+
/* Pushing an instantiation scope clears the expression stack.
16161+
Also, a deduced pack would already have been added to the new
16162+
template argument list. */
16163+
def_arg_options &= ~(CTWS_INSIDE_EXPR_RESCAN |
16164+
CTWS_PRESERVE_DEDUCED_PACKS);
16165+
} /* if */
1614216166
substitute_template_argument(tap, tpp, new_list,
1614316167
param_list_for_copy,
1614416168
new_list, param_list_for_copy,
1614516169
source_pos,
16146-
options,
16170+
def_arg_options,
1614716171
is_generic,
1614816172
copy_error, ctws_state);
16173+
if (pushed_rescan_scope) {
16174+
pop_instantiation_scope_for_rescan();
16175+
} /* if */
1614916176
if (*copy_error) goto done;
1615016177
} /* if */
1615116178
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)