https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119401

--- Comment #6 from Patrick Palka <ppalka at gcc dot gnu.org> ---
We could detect if there was an intervening redeclaration by comparing the
source location of the specialization vs that of the prevailing template
declaration:

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 1f5ab4e3f71..f3240ae40dc 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -27230,7 +27230,12 @@ regenerate_decl_from_template (tree decl, tree tmpl,
tree args)
        goto done;

       /* Use the source location of the definition.  */
-      DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (tmpl);
+      bool locs_differ = false;
+      if (DECL_SOURCE_LOCATION (decl) != DECL_SOURCE_LOCATION (tmpl))
+       {
+         locs_differ = true;
+         DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (tmpl);
+       }

       args_depth = TMPL_ARGS_DEPTH (args);
       parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
@@ -27251,9 +27256,11 @@ regenerate_decl_from_template (tree decl, tree tmpl,
tree args)
          }

       /* Merge parameter declarations.  */
-      if (tree pattern_parm
-         = skip_artificial_parms_for (code_pattern,
-                                      DECL_ARGUMENTS (code_pattern)))
+      if (!locs_differ)
+       /* The prevailing parameters are unchanged.  */;
+      else if (tree pattern_parm
+              = skip_artificial_parms_for (code_pattern,
+                                           DECL_ARGUMENTS (code_pattern)))
        {
          tree *p = &DECL_ARGUMENTS (decl);
          for (int skip = num_artificial_parms_for (decl); skip; --skip)

This fixes the testcase, and doesn't seem to introduce any testsuite
regressions.

Reply via email to