Hi Jason,
On 20 Sep 2024, at 18:01, Jason Merrill wrote:
> On 9/20/24 5:21 PM, Simon Martin wrote:
>> The following code triggers an ICE
>>
>> === cut here ===
>> class base {};
>> class derived : virtual public base {
>> public:
>> template<typename Arg> constexpr derived(Arg) {}
>> };
>> int main() {
>> derived obj(1.);
>> }
>> === cut here ===
>>
>> The problem is that cxx_bind_parameters_in_call ends up attempting to
>> convert a REAL_CST (the first non artificial parameter) to
>> INTEGER_TYPE
>> (the type of the __in_chrg parameter), which ICEs.
>>
>> This patch teaches cxx_bind_parameters_in_call to handle the
>> __in_chrg
>> and __vtt_parm parameters that {con,de}structors might have.
>>
>> Note that in the test case, the constructor is not
>> constexpr-suitable,
>> however it's OK since it's a template according to my read of
>> paragraph
>> (3) of [dcl.constexpr].
>
> Agreed.
>
> It looks like your patch doesn't correct the mismatching of arguments
> to parameters that you describe, but at least for now it should be
> enough to set *non_constant_p and return if we see a VTT or in-charge
> parameter.
>
Thanks, it’s true that my initial patch was wrong in that we’d leave
cxx_bind_parameters_in_call thinking the expression was actually a
constant expression :-/
The attached revised patch follows your suggestion (thanks!).
Successfully tested on x86_64-pc-linux-gnu. OK for trunk?
Thanks,
Simon
From 12d818220d4addc76f0d8e1fdf8feba336fb9b04 Mon Sep 17 00:00:00 2001
From: Simon Martin <si...@nasilyan.com>
Date: Wed, 18 Sep 2024 12:35:27 +0200
Subject: [PATCH] c++: Don't ICE due to artificial constructor parameters
[PR116722]
The following code triggers an ICE
=== cut here ===
class base {};
class derived : virtual public base {
public:
template<typename Arg> constexpr derived(Arg) {}
};
int main() {
derived obj(1.);
}
=== cut here ===
The problem is that cxx_bind_parameters_in_call ends up attempting to
convert a REAL_CST (the first non artificial parameter) to INTEGER_TYPE
(the type of the __in_chrg parameter), which ICEs.
This patch changes cxx_bind_parameters_in_call to return early if it's
called with a *structor that has an __in_chrg or __vtt_parm parameter
since the expression won't be a constant expression.
Note that in the test case, the constructor is not constexpr-suitable,
however it's OK since it's a template according to my read of paragraph
(3) of [dcl.constexpr].
Successfully tested on x86_64-pc-linux-gnu.
PR c++/116722
gcc/cp/ChangeLog:
* constexpr.cc (cxx_bind_parameters_in_call): Leave early for
{con,de}structors of classes with virtual bases.
gcc/testsuite/ChangeLog:
* g++.dg/cpp0x/constexpr-ctor22.C: New test.
---
gcc/cp/constexpr.cc | 11 ++++++++++-
gcc/testsuite/g++.dg/cpp0x/constexpr-ctor22.C | 15 +++++++++++++++
2 files changed, 25 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-ctor22.C
diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index f6fd059be46..5c6696740fc 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -1862,6 +1862,15 @@ cxx_bind_parameters_in_call (const constexpr_ctx *ctx,
tree t, tree fun,
int nparms = list_length (parms);
int nbinds = nargs < nparms ? nargs : nparms;
tree binds = make_tree_vec (nbinds);
+
+ /* The call is not a constant expression if it involves the cdtor for a type
+ with virtual bases. */
+ if (DECL_HAS_IN_CHARGE_PARM_P (fun) || DECL_HAS_VTT_PARM_P (fun))
+ {
+ *non_constant_p = true;
+ return binds;
+ }
+
for (i = 0; i < nargs; ++i)
{
tree x, arg;
@@ -1871,7 +1880,7 @@ cxx_bind_parameters_in_call (const constexpr_ctx *ctx,
tree t, tree fun,
x = get_nth_callarg (t, i);
/* For member function, the first argument is a pointer to the implied
object. For a constructor, it might still be a dummy object, in
- which case we get the real argument from ctx. */
+ which case we get the real argument from ctx. */
if (i == 0 && DECL_CONSTRUCTOR_P (fun)
&& is_dummy_object (x))
{
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-ctor22.C
b/gcc/testsuite/g++.dg/cpp0x/constexpr-ctor22.C
new file mode 100644
index 00000000000..279f6ec4454
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-ctor22.C
@@ -0,0 +1,15 @@
+// PR c++/116722
+// We're now accepting this in spite of the virtual base class. This is OK
+// according to [dcl.constexpr] 3: "Except for instantiated constexpr functions
+// non-templated constexpr functions shall be constexpr-suitable".
+// { dg-do compile { target c++11 } }
+
+class base {};
+class derived : virtual public base {
+public:
+ template<typename Arg>
+ constexpr derived(Arg) {}
+};
+int main() {
+ derived obj(1.);
+}
--
2.44.0