public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/105440] New: c++20: std::string's destructor not a constant expression when it should
@ 2022-04-30 13:28 janpmoeller at gmx dot de
  2022-05-04 16:16 ` [Bug libstdc++/105440] " redi at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: janpmoeller at gmx dot de @ 2022-04-30 13:28 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105440
           Summary: c++20: std::string's destructor not a constant
                    expression when it should
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: janpmoeller at gmx dot de
  Target Milestone: ---

The following valid c++20 program fails to compile on both gcc and clang when
compiled with libstdc++:

/************************************************************/
#include <string>
#include <array>

constexpr auto foo(char c){
    std::array a{std::string{"foo"} + c};
    return true;
}

static_assert(foo('a'));
/************************************************************/

The emitted error is:

/************************************************************/
<source>:9:18: error: non-constant condition for static assertion
    9 | static_assert(foo('a'));
      |               ~~~^~~~~
In file included from
/opt/compiler-explorer/gcc-trunk-20220430/include/c++/13.0.0/string:53,
                 from <source>:1:
<source>:9:18:   in 'constexpr' expansion of 'foo(97)'
<source>:7:1:   in 'constexpr' expansion of '(&
a)->std::array<std::__cxx11::basic_string<char>, 1>::~array()'
/opt/compiler-explorer/gcc-trunk-20220430/include/c++/13.0.0/array:99:12:   in
'constexpr' expansion of
'<anonymous>->std::__cxx11::basic_string<char>::~basic_string()'
/opt/compiler-explorer/gcc-trunk-20220430/include/c++/13.0.0/bits/basic_string.h:795:19:
  in 'constexpr' expansion of
'((std::__cxx11::basic_string<char>*)this)->std::__cxx11::basic_string<char>::_M_dispose()'
/opt/compiler-explorer/gcc-trunk-20220430/include/c++/13.0.0/bits/basic_string.h:275:26:
error: '((& a) == <anonymous>)' is not a constant expression
  275 |       { return _M_data() == _M_local_data(); }
      |                ~~~~~~~~~~^~~~~~~~~~~~~~~~~~
Compiler returned: 1
/************************************************************/

The same program compiles with libc++, and on msvc. Also see the following
godbolt link for comparison:
https://godbolt.org/z/zfn1Pr7P1

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

* [Bug libstdc++/105440] c++20: std::string's destructor not a constant expression when it should
  2022-04-30 13:28 [Bug libstdc++/105440] New: c++20: std::string's destructor not a constant expression when it should janpmoeller at gmx dot de
@ 2022-05-04 16:16 ` redi at gcc dot gnu.org
  2023-04-17 14:27 ` ppalka at gcc dot gnu.org
  2023-05-08 12:24 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: redi at gcc dot gnu.org @ 2022-05-04 16:16 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
     Ever confirmed|0                           |1
           Keywords|                            |rejects-valid
   Last reconfirmed|                            |2022-05-04
           Assignee|unassigned at gcc dot gnu.org      |redi at gcc dot gnu.org
   Target Milestone|---                         |12.2

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

* [Bug libstdc++/105440] c++20: std::string's destructor not a constant expression when it should
  2022-04-30 13:28 [Bug libstdc++/105440] New: c++20: std::string's destructor not a constant expression when it should janpmoeller at gmx dot de
  2022-05-04 16:16 ` [Bug libstdc++/105440] " redi at gcc dot gnu.org
@ 2023-04-17 14:27 ` ppalka at gcc dot gnu.org
  2023-05-08 12:24 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: ppalka at gcc dot gnu.org @ 2023-04-17 14:27 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #2 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Seems this is ultimately an FE bug which is fixed by:

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index d1097764b10..9dbbf6eec03 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -3213,7 +3213,12 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree
t,
                && CLASS_TYPE_P (TREE_TYPE (res))
                && !is_empty_class (TREE_TYPE (res)))
              if (replace_decl (&result, res, ctx->object))
-               cacheable = false;
+               {
+                 cacheable = false;
+                 result = cxx_eval_constant_expression (ctx, result, lval,
+                                                        non_constant_p,
+                                                        overflow_p);
+               }
        }
       else
        /* Couldn't get a function copy to evaluate.  */

In some cases we can simplify a call result further after replacing RESULT_DECL
with the current object under construction throughout the call result, which
allows fold_binary_loc etc to later successfully fold comparisons involving the
call result such as _M_data() == _M_local_data() in this case.

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

* [Bug libstdc++/105440] c++20: std::string's destructor not a constant expression when it should
  2022-04-30 13:28 [Bug libstdc++/105440] New: c++20: std::string's destructor not a constant expression when it should janpmoeller at gmx dot de
  2022-05-04 16:16 ` [Bug libstdc++/105440] " redi at gcc dot gnu.org
  2023-04-17 14:27 ` ppalka at gcc dot gnu.org
@ 2023-05-08 12:24 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-05-08 12:24 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|12.3                        |12.4

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 12.3 is being released, retargeting bugs to GCC 12.4.

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

end of thread, other threads:[~2023-05-08 12:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-30 13:28 [Bug libstdc++/105440] New: c++20: std::string's destructor not a constant expression when it should janpmoeller at gmx dot de
2022-05-04 16:16 ` [Bug libstdc++/105440] " redi at gcc dot gnu.org
2023-04-17 14:27 ` ppalka at gcc dot gnu.org
2023-05-08 12:24 ` 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).