public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/116646] New: Compilation of code inside if constexpr with failed condition.
@ 2024-09-08 18:47 cas43 at cs dot stanford.edu
  2024-09-08 18:51 ` [Bug c++/116646] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: cas43 at cs dot stanford.edu @ 2024-09-08 18:47 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 116646
           Summary: Compilation of code inside if constexpr with failed
                    condition.
           Product: gcc
           Version: 12.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cas43 at cs dot stanford.edu
  Target Milestone: ---

When compiling the following code (g++ -std=c++20 file.cpp):

static const int y=0;
void f() {if constexpr(y<0){static_assert(y<0);}}

I expect no errors, but I get the following diagnostic with gcc version 12.3.0
(Ubuntu 12.3.0-1ubuntu1~22.04):

a.cpp: In function ‘void f()’:
a.cpp:2:44: error: static assertion failed
    2 | void f() {if constexpr(y<0){static_assert(y<0);}}
      |                                           ~^~
a.cpp:2:44: note: the comparison reduces to ‘(0 < 0)’

I get similar diagnostics with every compiler I have ready access to:

gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04) 
gcc version 10.5.0 (Ubuntu 10.5.0-1ubuntu1~22.04) 
Ubuntu clang version 15.0.7
Ubuntu clang version 14.0.0-1ubuntu1.1
Ubuntu clang version 13.0.1-2ubuntu2.2
Ubuntu clang version 12.0.1-19ubuntu3
Ubuntu clang version 11.1.0-6

This suggests a mistake in my own code, but I don't see it.  My understanding
is that the "if constexpr" should fail and prevent compilation within its body.
 Declaring y as "static constexpr" still fails.  The static_assert can be
replaced with other types of errors, such as a call to a function call that
fails to match.

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

* [Bug c++/116646] Compilation of code inside if constexpr with failed condition.
  2024-09-08 18:47 [Bug c++/116646] New: Compilation of code inside if constexpr with failed condition cas43 at cs dot stanford.edu
@ 2024-09-08 18:51 ` pinskia at gcc dot gnu.org
  2024-09-08 18:53 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-09-08 18:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
It works when y is dependent though:
```
template<int y>
void f() {
        if constexpr(y<0)
        {
                static_assert(y<0);
        }
}

auto t = &f<0>;
```

I think since it is not dependent, it gets resolved right away.

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

* [Bug c++/116646] Compilation of code inside if constexpr with failed condition.
  2024-09-08 18:47 [Bug c++/116646] New: Compilation of code inside if constexpr with failed condition cas43 at cs dot stanford.edu
  2024-09-08 18:51 ` [Bug c++/116646] " pinskia at gcc dot gnu.org
@ 2024-09-08 18:53 ` pinskia at gcc dot gnu.org
  2024-09-09  8:33 ` redi at gcc dot gnu.org
  2024-09-09 14:35 ` cas43 at cs dot stanford.edu
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-09-08 18:53 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
https://developercommunity.visualstudio.com/t/non-dependent-static-assertfalse-in-constexpr-if-s/1267343

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

* [Bug c++/116646] Compilation of code inside if constexpr with failed condition.
  2024-09-08 18:47 [Bug c++/116646] New: Compilation of code inside if constexpr with failed condition cas43 at cs dot stanford.edu
  2024-09-08 18:51 ` [Bug c++/116646] " pinskia at gcc dot gnu.org
  2024-09-08 18:53 ` pinskia at gcc dot gnu.org
@ 2024-09-09  8:33 ` redi at gcc dot gnu.org
  2024-09-09 14:35 ` cas43 at cs dot stanford.edu
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2024-09-09  8:33 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #1)
> I think since it is not dependent, it gets resolved right away.

Right. A discarded statement (i.e. an untaken if-constexpr branch) is not
instantiated in a template. But in a normal function, it's always compiled
normally.

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

* [Bug c++/116646] Compilation of code inside if constexpr with failed condition.
  2024-09-08 18:47 [Bug c++/116646] New: Compilation of code inside if constexpr with failed condition cas43 at cs dot stanford.edu
                   ` (2 preceding siblings ...)
  2024-09-09  8:33 ` redi at gcc dot gnu.org
@ 2024-09-09 14:35 ` cas43 at cs dot stanford.edu
  3 siblings, 0 replies; 5+ messages in thread
From: cas43 at cs dot stanford.edu @ 2024-09-09 14:35 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Craig Schroeder <cas43 at cs dot stanford.edu> ---
Thank you for taking the time to explain this.  I understand why the code
snippet does not work now.

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

end of thread, other threads:[~2024-09-09 14:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-09-08 18:47 [Bug c++/116646] New: Compilation of code inside if constexpr with failed condition cas43 at cs dot stanford.edu
2024-09-08 18:51 ` [Bug c++/116646] " pinskia at gcc dot gnu.org
2024-09-08 18:53 ` pinskia at gcc dot gnu.org
2024-09-09  8:33 ` redi at gcc dot gnu.org
2024-09-09 14:35 ` cas43 at cs dot stanford.edu

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