]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: bad pending_template recursion
authorJason Merrill <jason@redhat.com>
Fri, 18 Apr 2025 22:00:34 +0000 (18:00 -0400)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 25 Apr 2025 10:47:47 +0000 (11:47 +0100)
limit_bad_template_recursion currently avoids immediate instantiation of
templates from uses in an already ill-formed instantiation, but we still can
get unnecessary recursive instantiation in pending_templates if the
instantiation was queued before the error.

Initially this regressed several libstdc++ tests which seemed to rely on a
static_assert in a function called from another that is separately ill-formed.
For instance, in the 48101_neg.cc tests, we first got an error in find(), then
later instantiate _S_key() (called from find) and got the static_assert error
from there. r16-131-g876d1a22dfaf87 and r16-132-g901900bc37566c changed
the library code (and tests) to make the expected static_assert errors
happen earlier.

gcc/cp/ChangeLog:

* cp-tree.h (struct tinst_level): Add had_errors bit.
* pt.cc (push_tinst_level_loc): Clear it.
(pop_tinst_level): Set it.
(reopen_tinst_level): Check it.
(instantiate_pending_templates): Call limit_bad_template_recursion.

gcc/testsuite/ChangeLog:

* g++.dg/template/recurse5.C: New test.

gcc/cp/cp-tree.h
gcc/cp/pt.cc
gcc/testsuite/g++.dg/template/recurse5.C [new file with mode: 0644]

index 7798efba3dbad6daa4327655b07a0308ca18e12b..856202c65dd6e5fac56e867297a6ad8c6cc051a1 100644 (file)
@@ -6755,8 +6755,14 @@ struct GTY((chain_next ("%h.next"))) tinst_level {
   /* The location where the template is instantiated.  */
   location_t locus;
 
-  /* errorcount + sorrycount when we pushed this level.  */
-  unsigned short errors;
+  /* errorcount + sorrycount when we pushed this level.  If the value
+     overflows, it will always seem like we currently have more errors, so we
+     will limit template recursion even from non-erroneous templates.  In a TU
+     with over 32k errors, that's fine.  */
+  unsigned short errors : 15;
+
+  /* set in pop_tinst_level if there have been errors since we pushed.  */
+  bool had_errors : 1;
 
   /* Count references to this object.  If refcount reaches
      refcount_infinity value, we don't increment or decrement the
index a71705fd085b5f33479d4cac539c7ba1b32965d7..e8d342f99f6dbc43291a9c6217e964a906b254d9 100644 (file)
@@ -11418,6 +11418,7 @@ push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
   new_level->targs = targs;
   new_level->locus = loc;
   new_level->errors = errorcount + sorrycount;
+  new_level->had_errors = false;
   new_level->next = NULL;
   new_level->refcount = 0;
   new_level->path = new_level->visible = nullptr;
@@ -11468,6 +11469,9 @@ pop_tinst_level (void)
   /* Restore the filename and line number stashed away when we started
      this instantiation.  */
   input_location = current_tinst_level->locus;
+  if (unsigned errs = errorcount + sorrycount)
+    if (errs > current_tinst_level->errors)
+      current_tinst_level->had_errors = true;
   set_refcount_ptr (current_tinst_level, current_tinst_level->next);
   --tinst_depth;
 }
@@ -11487,7 +11491,7 @@ reopen_tinst_level (struct tinst_level *level)
 
   set_refcount_ptr (current_tinst_level, level);
   pop_tinst_level ();
-  if (current_tinst_level)
+  if (current_tinst_level && !current_tinst_level->had_errors)
     current_tinst_level->errors = errorcount+sorrycount;
 
   tree decl = level->maybe_get_node ();
@@ -28072,7 +28076,9 @@ instantiate_pending_templates (int retries)
          tree instantiation = reopen_tinst_level ((*t)->tinst);
          bool complete = false;
 
-         if (TYPE_P (instantiation))
+         if (limit_bad_template_recursion (instantiation))
+           /* Do nothing.  */;
+         else if (TYPE_P (instantiation))
            {
              if (!COMPLETE_TYPE_P (instantiation))
                {
diff --git a/gcc/testsuite/g++.dg/template/recurse5.C b/gcc/testsuite/g++.dg/template/recurse5.C
new file mode 100644 (file)
index 0000000..7bfe523
--- /dev/null
@@ -0,0 +1,17 @@
+// Test that we don't bother to instantiate add since there were errors in
+// checked_add.
+
+template <class T> T add (T t) { return t+1; } // { dg-bogus "no match" }
+
+template <class T> T checked_add (T t)
+{
+  add (t);
+  return t+1;                  // { dg-error "no match" }
+}
+
+struct A { };
+
+int main()
+{
+  checked_add (A());
+}