public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/107378] New: `get_if` implementation "is not a constant expression"
@ 2022-10-24 15:36 johelegp at gmail dot com
  2022-10-24 15:59 ` [Bug c++/107378] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: johelegp at gmail dot com @ 2022-10-24 15:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107378

            Bug ID: 107378
           Summary: `get_if` implementation "is not a constant expression"
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Keywords: rejects-valid
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: johelegp at gmail dot com
                CC: johelegp at gmail dot com
  Target Milestone: ---

See https://godbolt.org/z/cf1T7q6Tj.

```C++
#include <variant>
static_assert([v = std::variant<int*>{}] { return get_if<0>(&v); }());
```

```
<source>:2:67: error: non-constant condition for static assertion
    2 | static_assert([v = std::variant<int*>{}] { return get_if<0>(&v); }());
      |               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
In file included from <source>:1:
<source>:2:67:   in 'constexpr' expansion of '<lambda closure
object><lambda()>{std::variant<int*>{std::__detail::__variant::_Variant_base<int*>{std::__detail::__variant::_Move_assign_base<true,
int*>{std::__detail::__variant::_Copy_assign_base<true,
int*>{std::__detail::__variant::_Move_ctor_base<true,
int*>{std::__detail::__variant::_Copy_ctor_base<true,
int*>{std::__detail::__variant::_Variant_storage<true,
int*>{std::__detail::__variant::_Variadic_union<int*>{std::__detail::__variant::_Uninitialized<int*,
true>{0}}, 0}}}}}}}}.<lambda()>()'
<source>:2:60:   in 'constexpr' expansion of 'std::get_if<0, int*>((& v))'
/opt/compiler-explorer/gcc-trunk-20221024/include/c++/13.0.0/variant:1183:11:
error: '((&<anonymous>.<lambda()>::__v) != 0)' is not a constant expression
 1183 |       if (__ptr && __ptr->index() == _Np)
      |           ^~~~~
Compiler returned: 1
```

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/107378] `get_if` implementation "is not a constant expression"
  2022-10-24 15:36 [Bug libstdc++/107378] New: `get_if` implementation "is not a constant expression" johelegp at gmail dot com
@ 2022-10-24 15:59 ` pinskia at gcc dot gnu.org
  2022-10-24 16:20 ` mpolacek at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-10-24 15:59 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107378

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|libstdc++                   |c++

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I suspect this is a front-end issue and not a library issue.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/107378] `get_if` implementation "is not a constant expression"
  2022-10-24 15:36 [Bug libstdc++/107378] New: `get_if` implementation "is not a constant expression" johelegp at gmail dot com
  2022-10-24 15:59 ` [Bug c++/107378] " pinskia at gcc dot gnu.org
@ 2022-10-24 16:20 ` mpolacek at gcc dot gnu.org
  2022-10-24 16:55 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-10-24 16:20 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107378

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
                 CC|                            |mpolacek at gcc dot gnu.org
   Last reconfirmed|                            |2022-10-24
             Status|UNCONFIRMED                 |NEW

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/107378] `get_if` implementation "is not a constant expression"
  2022-10-24 15:36 [Bug libstdc++/107378] New: `get_if` implementation "is not a constant expression" johelegp at gmail dot com
  2022-10-24 15:59 ` [Bug c++/107378] " pinskia at gcc dot gnu.org
  2022-10-24 16:20 ` mpolacek at gcc dot gnu.org
@ 2022-10-24 16:55 ` pinskia at gcc dot gnu.org
  2022-10-24 18:59 ` johelegp at gmail dot com
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-10-24 16:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107378

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Reduced testcase without any headers:
```
struct g
{
  char a;
};
constexpr bool f(const char *a) { return a != nullptr; }
static_assert([v = g{}] { return f(&v.a); }());
```

This works with clang.
But MSVC rejects it ...

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/107378] `get_if` implementation "is not a constant expression"
  2022-10-24 15:36 [Bug libstdc++/107378] New: `get_if` implementation "is not a constant expression" johelegp at gmail dot com
                   ` (2 preceding siblings ...)
  2022-10-24 16:55 ` pinskia at gcc dot gnu.org
@ 2022-10-24 18:59 ` johelegp at gmail dot com
  2022-10-24 20:43 ` ppalka at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: johelegp at gmail dot com @ 2022-10-24 18:59 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107378

--- Comment #3 from Johel Ernesto Guerrero Peña <johelegp at gmail dot com> ---
MSVC accepts https://godbolt.org/z/nheYz9aez.

```C++
static_assert([i = 0] { return &i != nullptr; }());
```

```
<source>: In lambda function:
<source>:1:35: warning: the address of 'i' will never be NULL [-Waddress]
    1 | static_assert([i = 0] { return &i != nullptr; }());
      |                                ~~~^~~~~~~~~~
<source>:1:21: note: 'i' declared here
    1 | static_assert([i = 0] { return &i != nullptr; }());
      |                     ^
<source>: At global scope:
<source>:1:48: error: non-constant condition for static assertion
    1 | static_assert([i = 0] { return &i != nullptr; }());
      |               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
<source>:1:35: error: '((&<anonymous>.<lambda()>::__i) != 0)' is not a constant
expression
    1 | static_assert([i = 0] { return &i != nullptr; }());
      |                                ~~~^~~~~~~~~~
Compiler returned: 1
```

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/107378] `get_if` implementation "is not a constant expression"
  2022-10-24 15:36 [Bug libstdc++/107378] New: `get_if` implementation "is not a constant expression" johelegp at gmail dot com
                   ` (3 preceding siblings ...)
  2022-10-24 18:59 ` johelegp at gmail dot com
@ 2022-10-24 20:43 ` ppalka at gcc dot gnu.org
  2022-10-24 22:00 ` johelegp at gmail dot com
  2022-10-25  6:57 ` rguenth at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: ppalka at gcc dot gnu.org @ 2022-10-24 20:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107378

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ppalka at gcc dot gnu.org

--- Comment #4 from Patrick Palka <ppalka at gcc dot gnu.org> ---
dup of PR85944?

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/107378] `get_if` implementation "is not a constant expression"
  2022-10-24 15:36 [Bug libstdc++/107378] New: `get_if` implementation "is not a constant expression" johelegp at gmail dot com
                   ` (4 preceding siblings ...)
  2022-10-24 20:43 ` ppalka at gcc dot gnu.org
@ 2022-10-24 22:00 ` johelegp at gmail dot com
  2022-10-25  6:57 ` rguenth at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: johelegp at gmail dot com @ 2022-10-24 22:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107378

--- Comment #5 from Johel Ernesto Guerrero Peña <johelegp at gmail dot com> ---
I think so. My actual use case looks like `constexpr auto v = E;` at global
scope, where `E` creates a temporary with a subobject of `std::variant`.
Wrapping `E` in an IILE is a workaround for this Bug.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/107378] `get_if` implementation "is not a constant expression"
  2022-10-24 15:36 [Bug libstdc++/107378] New: `get_if` implementation "is not a constant expression" johelegp at gmail dot com
                   ` (5 preceding siblings ...)
  2022-10-24 22:00 ` johelegp at gmail dot com
@ 2022-10-25  6:57 ` rguenth at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-10-25  6:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107378

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> ---
dup then

*** This bug has been marked as a duplicate of bug 85944 ***

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-10-25  6:57 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-24 15:36 [Bug libstdc++/107378] New: `get_if` implementation "is not a constant expression" johelegp at gmail dot com
2022-10-24 15:59 ` [Bug c++/107378] " pinskia at gcc dot gnu.org
2022-10-24 16:20 ` mpolacek at gcc dot gnu.org
2022-10-24 16:55 ` pinskia at gcc dot gnu.org
2022-10-24 18:59 ` johelegp at gmail dot com
2022-10-24 20:43 ` ppalka at gcc dot gnu.org
2022-10-24 22:00 ` johelegp at gmail dot com
2022-10-25  6:57 ` rguenth at gcc dot gnu.org

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).