public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "ppalka at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/105050] error: expression '<statement>' is not a constant expression
Date: Fri, 25 Mar 2022 13:22:03 +0000	[thread overview]
Message-ID: <bug-105050-4-xDVtCH0dMp@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-105050-4@http.gcc.gnu.org/bugzilla/>

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

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> ---
Created attachment 52687
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52687&action=edit
elaborate non-constexpr if stmt branches

Seems we issued this error because we determined that neither branch of the
first 'if' is a valid constexpr body (since each branch will end up calling a
non-constexpr function), and so the 'if' is a non-constant "expression"
regardless of control flow, which makes 'swap' non-constexpr overall.

The attached diff makes us elaborate why neither branch of an 'if' is
non-constant in this case, similar to how we diagnose disjunctions during
satisfaction.  For this testcase we'd now get:

105050.C:43:24: error: non-constant condition for static assertion
   43 | static_assert(test_swap());
      |               ~~~~~~~~~^~
105050.C:43:24:   in ‘constexpr’ expansion of ‘test_swap()’
105050.C:38:10: error: ‘constexpr void expected<_Tp>::swap(expected<_Tp>&)
[with _Tp = int]’ called in a constant expression
   38 |   e1.swap(e2);
      |   ~~~~~~~^~~~
105050.C:13:5: note: ‘constexpr void expected<_Tp>::swap(expected<_Tp>&) [with
_Tp = int]’ is not usable as a ‘constexpr’ function because:
   13 |     swap(expected& __x)
      |     ^~~~
105050.C:15:7: note: neither branch of ‘if’ is a valid ‘constexpr’ body
because:
   15 |       if (this->has_value())
      |       ^~
105050.C:17:9: note: neither branch of ‘if’ is a valid ‘constexpr’ body
because:
   17 |         if (__x.has_value())
      |         ^~
105050.C:18:28: error: call to non-‘constexpr’ function ‘void
expected<_Tp>::_M_swap_val(expected<_Tp>&) [with _Tp = int]’
   18 |           this->_M_swap_val(__x);
      |           ~~~~~~~~~~~~~~~~~^~~~~
105050.C:4:10: note: ‘void expected<_Tp>::_M_swap_val(expected<_Tp>&) [with _Tp
= int]’ declared here
    4 |     void _M_swap_val(expected&) { }
      |          ^~~~~~~~~~~
105050.C:20:33: error: call to non-‘constexpr’ function ‘void
expected<_Tp>::_M_swap_val_unex(expected<_Tp>&) [with _Tp = int]’
   20 |           this->_M_swap_val_unex(__x);
      |           ~~~~~~~~~~~~~~~~~~~~~~^~~~~
105050.C:8:10: note: ‘void expected<_Tp>::_M_swap_val_unex(expected<_Tp>&)
[with _Tp = int]’ declared here
    8 |     void _M_swap_val_unex(expected&) { }
      |          ^~~~~~~~~~~~~~~~
105050.C:24:9: note: neither branch of ‘if’ is a valid ‘constexpr’ body
because:
   24 |         if (__x.has_value())
      |         ^~
105050.C:25:31: error: call to non-‘constexpr’ function ‘void
expected<_Tp>::_M_swap_val_unex(expected<_Tp>&) [with _Tp = int]’
   25 |           __x._M_swap_val_unex(*this);
      |           ~~~~~~~~~~~~~~~~~~~~^~~~~~~
105050.C:8:10: note: ‘void expected<_Tp>::_M_swap_val_unex(expected<_Tp>&)
[with _Tp = int]’ declared here
    8 |     void _M_swap_val_unex(expected&) { }
      |          ^~~~~~~~~~~~~~~~
105050.C:27:29: error: call to non-‘constexpr’ function ‘void
expected<_Tp>::_M_swap_unex(expected<_Tp>&) [with _Tp = int]’
   27 |           this->_M_swap_unex(__x);
      |           ~~~~~~~~~~~~~~~~~~^~~~~
105050.C:6:10: note: ‘void expected<_Tp>::_M_swap_unex(expected<_Tp>&) [with
_Tp = int]’ declared here
    6 |     void _M_swap_unex(expected&) { }
      |          ^~~~~~~~~~~~

We definitely should clarify the initial error, but I'm not sure if we want the
recursive elaboration, might be too noisy..

  parent reply	other threads:[~2022-03-25 13:22 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-24 23:23 [Bug c++/105050] New: " redi at gcc dot gnu.org
2022-03-25 10:16 ` [Bug c++/105050] " redi at gcc dot gnu.org
2022-03-25 13:22 ` ppalka at gcc dot gnu.org [this message]
2022-03-25 13:31 ` redi at gcc dot gnu.org
2022-03-25 13:32 ` redi at gcc dot gnu.org
2022-03-26 14:20 ` cvs-commit at gcc dot gnu.org
2022-03-26 20:42 ` redi at gcc dot gnu.org
2022-12-07  1:18 ` pinskia at gcc dot gnu.org
2022-12-07  1:19 ` pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-105050-4-xDVtCH0dMp@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).