]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Fix ICE on mangling invalid compound requirement [PR120618]
authorBen Wu <soggysocks206@gmail.com>
Tue, 19 Aug 2025 17:49:41 +0000 (13:49 -0400)
committerJason Merrill <jason@redhat.com>
Tue, 19 Aug 2025 17:49:50 +0000 (13:49 -0400)
This testcase caused an ICE when mangling the invalid type-constraint in
write_requirement since write_type_constraint expects a TEMPLATE_TYPE_PARM.

Setting the trailing return type to NULL_TREE when a
return-type-requirement is found in place of a type-constraint prevents the
failed assertion in write_requirement. It also allows the invalid
constraint to be satisfied in some contexts to prevent redundant errors,
e.g. in concepts-requires5.C.

Bootstrapped and tested on x86_64-linux-gnu.

PR c++/120618

gcc/cp/ChangeLog:

* parser.cc (cp_parser_compound_requirement): Set type to
NULL_TREE for invalid type-constraint.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/concepts-requires5.C: Don't require
redundant diagnostic in static assertion.
* g++.dg/concepts/pr120618.C: New test.

Suggested-by: Jason Merrill <jason@redhat.com>
gcc/cp/parser.cc
gcc/testsuite/g++.dg/concepts/pr120618.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/concepts-requires5.C

index d66b658b748f71a165e01e209817e432c1e11af0..40223bedcc1ca97756072f3f317d77f255f9778c 100644 (file)
@@ -33403,9 +33403,12 @@ cp_parser_compound_requirement (cp_parser *parser)
            }
        }
       else
-       /* P1452R2 removed the trailing-return-type option.  */
-       error_at (type_loc,
-                 "return-type-requirement is not a type-constraint");
+       {
+         /* P1452R2 removed the trailing-return-type option.  */
+         error_at (type_loc,
+                   "return-type-requirement is not a type-constraint");
+         type = NULL_TREE;
+       }
     }
 
   location_t loc = make_location (expr_token->location,
diff --git a/gcc/testsuite/g++.dg/concepts/pr120618.C b/gcc/testsuite/g++.dg/concepts/pr120618.C
new file mode 100644 (file)
index 0000000..85d2532
--- /dev/null
@@ -0,0 +1,13 @@
+// { dg-do compile { target c++17 } }
+// { dg-options "-fconcepts" }
+
+class B{};
+
+template <typename T>
+requires (!requires(T t) { { t } -> bool; }) // { dg-error "return-type-requirement is not a type-constraint" }
+void foo(T t) {}
+
+int main() {
+  B b;
+  foo(b); // { dg-prune-output "no matching function" }
+}
index 524eadbf5ddba93fe98a431e9df262f1368b81d7..3c5a9135c18149c920562097a4b5c1547dcc155f 100644 (file)
@@ -41,5 +41,5 @@ class D : /*private*/ B { };
 void driver_2()
 {
   static_assert(ConvertibleTo<D, B>()); // { dg-error "cannot call" }
-  static_assert(ConvertibleTo<D, B>); // { dg-error "static assertion failed" }
+  static_assert(ConvertibleTo<D, B>); // { dg-prune-output "static assertion failed" }
 }