[PATCH] c++, contracts: Preserve template arguments when rebuilding postconditions [PR125011]

Wang Jinghao <[email protected]>
Newsgroups gmane.comp.gcc.patches
Message-ID <[email protected]>
Bootstrapped/regtested on x86_64-pc-linux-gnu.

-- >8 --

`rebuild_postconditions' uses `tsubst_expr' to replace the
placeholder result variable with one of the known return type.
Passing an empty template argument vector causes direct references
to template parameters in the condition to ICE in tsubst.

Use generic arguments for template declarations so that template
parameters map to themselves while the result variable is rebuilt.

	PR c++/125011

gcc/cp/ChangeLog:

	* contracts.cc (rebuild_postconditions): Use generic
	arguments when rebuilding a template declaration.

gcc/testsuite/ChangeLog:

	* g++.dg/contracts/cpp26/pr125011.C: New test.

Signed-off-by: Wang Jinghao <[email protected]>
---
 gcc/cp/contracts.cc                             |  6 +++++-
 gcc/testsuite/g++.dg/contracts/cpp26/pr125011.C | 12 ++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/contracts/cpp26/pr125011.C

diff --git a/gcc/cp/contracts.cc b/gcc/cp/contracts.cc
index 62974bc7e12..bbb88f149e0 100644
--- a/gcc/cp/contracts.cc
+++ b/gcc/cp/contracts.cc
@@ -2066,7 +2066,11 @@ rebuild_postconditions (tree fndecl)
       bool old_pc = processing_postcondition;
       processing_postcondition = true;
 
-      condition = tsubst_expr (condition, make_tree_vec (0),
+      /* Use generic arguments when rebuilding a template declaration.  */
+      tree args = (processing_template_decl && current_template_parms
+		   ? template_parms_to_args (current_template_parms)
+		   : make_tree_vec (0));
+      condition = tsubst_expr (condition, args,
 			       tf_warning_or_error, fndecl);
 
       /* Update the contract condition and result.  */
diff --git a/gcc/testsuite/g++.dg/contracts/cpp26/pr125011.C b/gcc/testsuite/g++.dg/contracts/cpp26/pr125011.C
new file mode 100644
index 00000000000..49f3073b74b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/contracts/cpp26/pr125011.C
@@ -0,0 +1,12 @@
+// PR c++/125011
+// { dg-do compile { target c++26 } }
+// { dg-additional-options "-fcontracts" }
+
+template<bool x>
+  bool f()
+    post(r: r == x)
+  {
+    return x;
+  }
+
+template bool f<true> ();
-- 
2.52.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.