]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++/contracts: ICE with contract assert on non-empty statement [PR 117579]
authorNina Ranns <dinka.ranns@googlemail.com>
Tue, 3 Dec 2024 14:58:21 +0000 (14:58 +0000)
committerJason Merrill <jason@redhat.com>
Tue, 3 Dec 2024 19:29:00 +0000 (14:29 -0500)
Contract assert is an attribute on an empty statement. Currently we assert
that the statement is empty before emitting the assertion. This has been
changed to a conditional check that the statement is empty before the
assertion is emitted.

PR c++/117579

gcc/cp/ChangeLog:

* parser.cc (cp_parser_statement): Replace assertion with a
conditional check that the statement containing a contract assert
is empty.

gcc/testsuite/ChangeLog:

* g++.dg/contracts/pr117579.C: New test.

Signed-off-by: Nina Ranns <dinka.ranns@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/parser.cc
gcc/testsuite/g++.dg/contracts/pr117579.C [new file with mode: 0644]

index e583649bc93747004a749b06034782867c1419fe..2995a11c60fe8924e577ccabf7970c8059b68fb0 100644 (file)
@@ -13164,8 +13164,10 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr,
       if (cp_contract_assertion_p (std_attrs))
        {
          /* Add the assertion as a statement in the current block.  */
-         gcc_assert (!statement || statement == error_mark_node);
-         emit_assertion (std_attrs);
+         if (!statement)
+           emit_assertion (std_attrs);
+         /* We already checked that the contract assertion is followed by
+          a semicolon.  */
          std_attrs = NULL_TREE;
        }
     }
diff --git a/gcc/testsuite/g++.dg/contracts/pr117579.C b/gcc/testsuite/g++.dg/contracts/pr117579.C
new file mode 100644 (file)
index 0000000..8878de8
--- /dev/null
@@ -0,0 +1,9 @@
+// Check that contract assertion on a non-empty statement doesn't cause an
+// ICE.
+// { dg-options "-std=c++2a -fcontracts " }
+
+void f();
+int main ()
+{
+  [[assert: true]] f(); // { dg-error "assertions must be followed by" }
+}