* [PATCH] c++: ICE initing lifetime-extended constexpr var [PR107079]
@ 2023-02-08 21:01 Marek Polacek
2023-02-09 0:00 ` Jason Merrill
0 siblings, 1 reply; 4+ messages in thread
From: Marek Polacek @ 2023-02-08 21:01 UTC (permalink / raw)
To: Jason Merrill, GCC Patches, Patrick Palka
(This may not be a complete fix but I got stuck so I'm posting what
I have, which at least fixes the ICE.)
We ICE on the simple:
struct X { const X* x = this; };
constexpr const X& x = X{};
where store_init_value initializes 'x' with
&TARGET_EXPR <D.2768, {.x=(const struct X *) &<PLACEHOLDER_EXPR struct X>}>
but we must lifetime-extend via extend_ref_init_temps and we get
_ZGR1x_.x = (const struct X *) &<PLACEHOLDER_EXPR struct X> >>>;, (const struct X &) &_ZGR1x_;
Since 'x' was declared constexpr, we do cxx_constant_init and we hit
the preeval code added in r269003 while evaluating the INIT_EXPR:
_ZGR1x_.x = (const struct X *) &<PLACEHOLDER_EXPR struct X> >>>
but we have no ctx.ctor or ctx.object here so lookup_placeholder won't
find anything that could replace X and we ICE on
7861 /* A placeholder without a referent. We can get here when
7862 checking whether NSDMIs are noexcept, or in massage_init_elt;
7863 just say it's non-constant for now. */
7864 gcc_assert (ctx->quiet);
because cxx_constant_init means !ctx->quiet. It's not correct that
there isn't a referent. I think the following patch is a pretty
straightforward fix: pass the _ZGR var down to maybe_constant_init so
that it can replace the PLACEHOLDER_EXPR with _ZGR1x_.
What I wasn't able to make work is the commented assert in the test.
It doesn't pass because we complain that _ZGR1x_ isn't a constexpr
variable, but making it so would just result in "used in its own
initializer" (which is true). I notice that while clang++ compiles
the assert fine, MSVC++/icc reject it as non-constant. So maybe we
don't have to/shouldn't make it work.
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
PR c++/107079
gcc/cp/ChangeLog:
* call.cc (set_up_extended_ref_temp): Pass var to maybe_constant_init.
gcc/testsuite/ChangeLog:
* g++.dg/cpp0x/constexpr-nsdmi2.C: New test.
---
gcc/cp/call.cc | 2 +-
gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C | 9 +++++++++
2 files changed, 10 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C
diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index f7c5d9da94b..a0afab9b26a 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -13583,7 +13583,7 @@ set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
/* If the initializer is constant, put it in DECL_INITIAL so we get
static initialization and use in constant expressions. */
- init = maybe_constant_init (expr);
+ init = maybe_constant_init (expr, var);
/* As in store_init_value. */
init = cp_fully_fold (init);
if (TREE_CONSTANT (init))
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C
new file mode 100644
index 00000000000..6dbb7eb739a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C
@@ -0,0 +1,9 @@
+// PR c++/107079
+// { dg-do compile { target c++11 } }
+
+struct X {
+ const X* x = this;
+};
+constexpr const X& x = X{};
+// TODO: Should the assert pass?
+//static_assert(x.x == &x);
base-commit: 77bb54b1b07add45007c664724b726541d672ef3
--
2.39.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] c++: ICE initing lifetime-extended constexpr var [PR107079]
2023-02-08 21:01 [PATCH] c++: ICE initing lifetime-extended constexpr var [PR107079] Marek Polacek
@ 2023-02-09 0:00 ` Jason Merrill
2023-02-09 2:15 ` [PATCH v2] " Marek Polacek
0 siblings, 1 reply; 4+ messages in thread
From: Jason Merrill @ 2023-02-09 0:00 UTC (permalink / raw)
To: Marek Polacek, GCC Patches, Patrick Palka
On 2/8/23 13:01, Marek Polacek wrote:
> (This may not be a complete fix but I got stuck so I'm posting what
> I have, which at least fixes the ICE.)
>
> We ICE on the simple:
>
> struct X { const X* x = this; };
> constexpr const X& x = X{};
>
> where store_init_value initializes 'x' with
>
> &TARGET_EXPR <D.2768, {.x=(const struct X *) &<PLACEHOLDER_EXPR struct X>}>
>
> but we must lifetime-extend via extend_ref_init_temps and we get
>
> _ZGR1x_.x = (const struct X *) &<PLACEHOLDER_EXPR struct X> >>>;, (const struct X &) &_ZGR1x_;
>
> Since 'x' was declared constexpr, we do cxx_constant_init and we hit
> the preeval code added in r269003 while evaluating the INIT_EXPR:
>
> _ZGR1x_.x = (const struct X *) &<PLACEHOLDER_EXPR struct X> >>>
>
> but we have no ctx.ctor or ctx.object here so lookup_placeholder won't
> find anything that could replace X and we ICE on
> 7861 /* A placeholder without a referent. We can get here when
> 7862 checking whether NSDMIs are noexcept, or in massage_init_elt;
> 7863 just say it's non-constant for now. */
> 7864 gcc_assert (ctx->quiet);
> because cxx_constant_init means !ctx->quiet. It's not correct that
> there isn't a referent. I think the following patch is a pretty
> straightforward fix: pass the _ZGR var down to maybe_constant_init so
> that it can replace the PLACEHOLDER_EXPR with _ZGR1x_.
>
> What I wasn't able to make work is the commented assert in the test.
> It doesn't pass because we complain that _ZGR1x_ isn't a constexpr
> variable,
That sounds like we aren't (correctly) implementing
https://eel.is/c++draft/expr.const#4.7
> but making it so would just result in "used in its own
> initializer" (which is true).
True, but not in the sense it means; its initializer doesn't depend on
its (uninitialized) value.
> I notice that while clang++ compiles
> the assert fine, MSVC++/icc reject it as non-constant. So maybe we
> don't have to/shouldn't make it work.
>
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
>
> PR c++/107079
>
> gcc/cp/ChangeLog:
>
> * call.cc (set_up_extended_ref_temp): Pass var to maybe_constant_init.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/cpp0x/constexpr-nsdmi2.C: New test.
> ---
> gcc/cp/call.cc | 2 +-
> gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C | 9 +++++++++
> 2 files changed, 10 insertions(+), 1 deletion(-)
> create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C
>
> diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
> index f7c5d9da94b..a0afab9b26a 100644
> --- a/gcc/cp/call.cc
> +++ b/gcc/cp/call.cc
> @@ -13583,7 +13583,7 @@ set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
>
> /* If the initializer is constant, put it in DECL_INITIAL so we get
> static initialization and use in constant expressions. */
> - init = maybe_constant_init (expr);
> + init = maybe_constant_init (expr, var);
We should also pass true for manifestly_const_eval as in store_init_value.
> /* As in store_init_value. */
> init = cp_fully_fold (init);
> if (TREE_CONSTANT (init))
> diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C
> new file mode 100644
> index 00000000000..6dbb7eb739a
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C
> @@ -0,0 +1,9 @@
> +// PR c++/107079
> +// { dg-do compile { target c++11 } }
> +
> +struct X {
> + const X* x = this;
> +};
> +constexpr const X& x = X{};
> +// TODO: Should the assert pass?
> +//static_assert(x.x == &x);
>
> base-commit: 77bb54b1b07add45007c664724b726541d672ef3
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2] c++: ICE initing lifetime-extended constexpr var [PR107079]
2023-02-09 0:00 ` Jason Merrill
@ 2023-02-09 2:15 ` Marek Polacek
2023-02-09 23:41 ` Jason Merrill
0 siblings, 1 reply; 4+ messages in thread
From: Marek Polacek @ 2023-02-09 2:15 UTC (permalink / raw)
To: Jason Merrill; +Cc: GCC Patches, Patrick Palka
On Wed, Feb 08, 2023 at 04:00:25PM -0800, Jason Merrill wrote:
> On 2/8/23 13:01, Marek Polacek wrote:
> > (This may not be a complete fix but I got stuck so I'm posting what
> > I have, which at least fixes the ICE.)
> >
> > We ICE on the simple:
> >
> > struct X { const X* x = this; };
> > constexpr const X& x = X{};
> >
> > where store_init_value initializes 'x' with
> >
> > &TARGET_EXPR <D.2768, {.x=(const struct X *) &<PLACEHOLDER_EXPR struct X>}>
> >
> > but we must lifetime-extend via extend_ref_init_temps and we get
> >
> > _ZGR1x_.x = (const struct X *) &<PLACEHOLDER_EXPR struct X> >>>;, (const struct X &) &_ZGR1x_;
> >
> > Since 'x' was declared constexpr, we do cxx_constant_init and we hit
> > the preeval code added in r269003 while evaluating the INIT_EXPR:
> >
> > _ZGR1x_.x = (const struct X *) &<PLACEHOLDER_EXPR struct X> >>>
> >
> > but we have no ctx.ctor or ctx.object here so lookup_placeholder won't
> > find anything that could replace X and we ICE on
> > 7861 /* A placeholder without a referent. We can get here when
> > 7862 checking whether NSDMIs are noexcept, or in massage_init_elt;
> > 7863 just say it's non-constant for now. */
> > 7864 gcc_assert (ctx->quiet);
> > because cxx_constant_init means !ctx->quiet. It's not correct that
> > there isn't a referent. I think the following patch is a pretty
> > straightforward fix: pass the _ZGR var down to maybe_constant_init so
> > that it can replace the PLACEHOLDER_EXPR with _ZGR1x_.
> >
> > What I wasn't able to make work is the commented assert in the test.
> > It doesn't pass because we complain that _ZGR1x_ isn't a constexpr
> > variable,
>
> That sounds like we aren't (correctly) implementing
>
> https://eel.is/c++draft/expr.const#4.7
Ah yes, this is DR2126 = c++/101588. I wonder if the fix will include
checking startswith (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR")
to see if its lifetime is extended.
> > but making it so would just result in "used in its own
> > initializer" (which is true).
>
> True, but not in the sense it means; its initializer doesn't depend on its
> (uninitialized) value.
...because we're only interested in its address.
> > diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
> > index f7c5d9da94b..a0afab9b26a 100644
> > --- a/gcc/cp/call.cc
> > +++ b/gcc/cp/call.cc
> > @@ -13583,7 +13583,7 @@ set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
> > /* If the initializer is constant, put it in DECL_INITIAL so we get
> > static initialization and use in constant expressions. */
> > - init = maybe_constant_init (expr);
> > + init = maybe_constant_init (expr, var);
>
> We should also pass true for manifestly_const_eval as in store_init_value.
Done.
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
-- >8 --
We ICE on the simple:
struct X { const X* x = this; };
constexpr const X& x = X{};
where store_init_value initializes 'x' with
&TARGET_EXPR <D.2768, {.x=(const struct X *) &<PLACEHOLDER_EXPR struct X>}>
but we must lifetime-extend via extend_ref_init_temps and we get
_ZGR1x_.x = (const struct X *) &<PLACEHOLDER_EXPR struct X> >>>;, (const struct X &) &_ZGR1x_;
Since 'x' was declared constexpr, we do cxx_constant_init and we hit
the preeval code added in r269003 while evaluating the INIT_EXPR:
_ZGR1x_.x = (const struct X *) &<PLACEHOLDER_EXPR struct X> >>>
but we have no ctx.ctor or ctx.object here so lookup_placeholder won't
find anything that could replace X and we ICE on
7861 /* A placeholder without a referent. We can get here when
7862 checking whether NSDMIs are noexcept, or in massage_init_elt;
7863 just say it's non-constant for now. */
7864 gcc_assert (ctx->quiet);
because cxx_constant_init means !ctx->quiet. It's not correct that
there isn't a referent. I think the following patch is a pretty
straightforward fix: pass the _ZGR var down to maybe_constant_init so
that it can replace the PLACEHOLDER_EXPR with _ZGR1x_.
The commented assert in the test doesn't pass: we complain that _ZGR1x_
isn't a constexpr variable because we don't implement DR2126 (PR101588).
PR c++/107079
gcc/cp/ChangeLog:
* call.cc (set_up_extended_ref_temp): Pass var to maybe_constant_init.
gcc/testsuite/ChangeLog:
* g++.dg/cpp0x/constexpr-nsdmi2.C: New test.
---
gcc/cp/call.cc | 2 +-
gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C | 9 +++++++++
2 files changed, 10 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C
diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index f7c5d9da94b..a349d8e79db 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -13583,7 +13583,7 @@ set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
/* If the initializer is constant, put it in DECL_INITIAL so we get
static initialization and use in constant expressions. */
- init = maybe_constant_init (expr);
+ init = maybe_constant_init (expr, var, /*manifestly_const_eval=*/true);
/* As in store_init_value. */
init = cp_fully_fold (init);
if (TREE_CONSTANT (init))
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C
new file mode 100644
index 00000000000..d711b8051c0
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C
@@ -0,0 +1,9 @@
+// PR c++/107079
+// { dg-do compile { target c++11 } }
+
+struct X {
+ const X* x = this;
+};
+constexpr const X& x = X{};
+// TODO: The assert should pass once we implement DR2126 (c++/101588).
+//static_assert(x.x == &x);
base-commit: f6fc79d0c90ff3a924d272eff74b32656bdf5481
--
2.39.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2] c++: ICE initing lifetime-extended constexpr var [PR107079]
2023-02-09 2:15 ` [PATCH v2] " Marek Polacek
@ 2023-02-09 23:41 ` Jason Merrill
0 siblings, 0 replies; 4+ messages in thread
From: Jason Merrill @ 2023-02-09 23:41 UTC (permalink / raw)
To: Marek Polacek; +Cc: GCC Patches, Patrick Palka
On 2/8/23 18:15, Marek Polacek wrote:
> On Wed, Feb 08, 2023 at 04:00:25PM -0800, Jason Merrill wrote:
>> On 2/8/23 13:01, Marek Polacek wrote:
>>> (This may not be a complete fix but I got stuck so I'm posting what
>>> I have, which at least fixes the ICE.)
>>>
>>> We ICE on the simple:
>>>
>>> struct X { const X* x = this; };
>>> constexpr const X& x = X{};
>>>
>>> where store_init_value initializes 'x' with
>>>
>>> &TARGET_EXPR <D.2768, {.x=(const struct X *) &<PLACEHOLDER_EXPR struct X>}>
>>>
>>> but we must lifetime-extend via extend_ref_init_temps and we get
>>>
>>> _ZGR1x_.x = (const struct X *) &<PLACEHOLDER_EXPR struct X> >>>;, (const struct X &) &_ZGR1x_;
>>>
>>> Since 'x' was declared constexpr, we do cxx_constant_init and we hit
>>> the preeval code added in r269003 while evaluating the INIT_EXPR:
>>>
>>> _ZGR1x_.x = (const struct X *) &<PLACEHOLDER_EXPR struct X> >>>
>>>
>>> but we have no ctx.ctor or ctx.object here so lookup_placeholder won't
>>> find anything that could replace X and we ICE on
>>> 7861 /* A placeholder without a referent. We can get here when
>>> 7862 checking whether NSDMIs are noexcept, or in massage_init_elt;
>>> 7863 just say it's non-constant for now. */
>>> 7864 gcc_assert (ctx->quiet);
>>> because cxx_constant_init means !ctx->quiet. It's not correct that
>>> there isn't a referent. I think the following patch is a pretty
>>> straightforward fix: pass the _ZGR var down to maybe_constant_init so
>>> that it can replace the PLACEHOLDER_EXPR with _ZGR1x_.
>>>
>>> What I wasn't able to make work is the commented assert in the test.
>>> It doesn't pass because we complain that _ZGR1x_ isn't a constexpr
>>> variable,
>>
>> That sounds like we aren't (correctly) implementing
>>
>> https://eel.is/c++draft/expr.const#4.7
>
> Ah yes, this is DR2126 = c++/101588. I wonder if the fix will include
> checking startswith (IDENTIFIER_POINTER (DECL_NAME (variable)), "_ZGR")
> to see if its lifetime is extended.
>
>>> but making it so would just result in "used in its own
>>> initializer" (which is true).
>>
>> True, but not in the sense it means; its initializer doesn't depend on its
>> (uninitialized) value.
>
> ...because we're only interested in its address.
>
>>> diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
>>> index f7c5d9da94b..a0afab9b26a 100644
>>> --- a/gcc/cp/call.cc
>>> +++ b/gcc/cp/call.cc
>>> @@ -13583,7 +13583,7 @@ set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
>>> /* If the initializer is constant, put it in DECL_INITIAL so we get
>>> static initialization and use in constant expressions. */
>>> - init = maybe_constant_init (expr);
>>> + init = maybe_constant_init (expr, var);
>>
>> We should also pass true for manifestly_const_eval as in store_init_value.
>
> Done.
>
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
OK.
> -- >8 --
> We ICE on the simple:
>
> struct X { const X* x = this; };
> constexpr const X& x = X{};
>
> where store_init_value initializes 'x' with
>
> &TARGET_EXPR <D.2768, {.x=(const struct X *) &<PLACEHOLDER_EXPR struct X>}>
>
> but we must lifetime-extend via extend_ref_init_temps and we get
>
> _ZGR1x_.x = (const struct X *) &<PLACEHOLDER_EXPR struct X> >>>;, (const struct X &) &_ZGR1x_;
>
> Since 'x' was declared constexpr, we do cxx_constant_init and we hit
> the preeval code added in r269003 while evaluating the INIT_EXPR:
>
> _ZGR1x_.x = (const struct X *) &<PLACEHOLDER_EXPR struct X> >>>
>
> but we have no ctx.ctor or ctx.object here so lookup_placeholder won't
> find anything that could replace X and we ICE on
> 7861 /* A placeholder without a referent. We can get here when
> 7862 checking whether NSDMIs are noexcept, or in massage_init_elt;
> 7863 just say it's non-constant for now. */
> 7864 gcc_assert (ctx->quiet);
> because cxx_constant_init means !ctx->quiet. It's not correct that
> there isn't a referent. I think the following patch is a pretty
> straightforward fix: pass the _ZGR var down to maybe_constant_init so
> that it can replace the PLACEHOLDER_EXPR with _ZGR1x_.
>
> The commented assert in the test doesn't pass: we complain that _ZGR1x_
> isn't a constexpr variable because we don't implement DR2126 (PR101588).
>
> PR c++/107079
>
> gcc/cp/ChangeLog:
>
> * call.cc (set_up_extended_ref_temp): Pass var to maybe_constant_init.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/cpp0x/constexpr-nsdmi2.C: New test.
> ---
> gcc/cp/call.cc | 2 +-
> gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C | 9 +++++++++
> 2 files changed, 10 insertions(+), 1 deletion(-)
> create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C
>
> diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
> index f7c5d9da94b..a349d8e79db 100644
> --- a/gcc/cp/call.cc
> +++ b/gcc/cp/call.cc
> @@ -13583,7 +13583,7 @@ set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
>
> /* If the initializer is constant, put it in DECL_INITIAL so we get
> static initialization and use in constant expressions. */
> - init = maybe_constant_init (expr);
> + init = maybe_constant_init (expr, var, /*manifestly_const_eval=*/true);
> /* As in store_init_value. */
> init = cp_fully_fold (init);
> if (TREE_CONSTANT (init))
> diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C
> new file mode 100644
> index 00000000000..d711b8051c0
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-nsdmi2.C
> @@ -0,0 +1,9 @@
> +// PR c++/107079
> +// { dg-do compile { target c++11 } }
> +
> +struct X {
> + const X* x = this;
> +};
> +constexpr const X& x = X{};
> +// TODO: The assert should pass once we implement DR2126 (c++/101588).
> +//static_assert(x.x == &x);
>
> base-commit: f6fc79d0c90ff3a924d272eff74b32656bdf5481
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-02-09 23:41 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-08 21:01 [PATCH] c++: ICE initing lifetime-extended constexpr var [PR107079] Marek Polacek
2023-02-09 0:00 ` Jason Merrill
2023-02-09 2:15 ` [PATCH v2] " Marek Polacek
2023-02-09 23:41 ` Jason Merrill
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).