public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/70248] constexpr initialization with unspecified equality expression accepted
       [not found] <bug-70248-4@http.gcc.gnu.org/bugzilla/>
@ 2020-10-15 11:19 ` redi at gcc dot gnu.org
  2020-10-15 11:24 ` redi at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2020-10-15 11:19 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2020-10-15
           Severity|minor                       |normal
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Another example with a relational operator:

constexpr const char* s = "";
constexpr const char* n = nullptr;
constexpr bool b = s > n;

The result of the comparison is unspecified because the pointers are not equal
and don't point to subobjects of the same object.

Clang, EDG and MSVC all reject it as expected.

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

* [Bug c++/70248] constexpr initialization with unspecified equality expression accepted
       [not found] <bug-70248-4@http.gcc.gnu.org/bugzilla/>
  2020-10-15 11:19 ` [Bug c++/70248] constexpr initialization with unspecified equality expression accepted redi at gcc dot gnu.org
@ 2020-10-15 11:24 ` redi at gcc dot gnu.org
  2020-10-15 15:03 ` mpolacek at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2020-10-15 11:24 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
In gcc/cp/typeck.c:cp_build_binary_op this warning should be an error during
constant evaluation for EQ_EXPR and NE_EXPR:

      if (complain & tf_warning)
        {
          tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
          tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
          if ((TREE_CODE (stripped_orig_op0) == STRING_CST
               && !integer_zerop (cp_fully_fold (op1)))
              || (TREE_CODE (stripped_orig_op1) == STRING_CST
                  && !integer_zerop (cp_fully_fold (op0))))
            warning_at (location, OPT_Waddress,
                        "comparison with string literal results in "
                        "unspecified behavior");
        }

And this one for rel ops and spaceships:

      if (TREE_CODE (orig_op0) == STRING_CST
          || TREE_CODE (orig_op1) == STRING_CST)
        {
          if (complain & tf_warning)
            warning_at (location, OPT_Waddress,
                        "comparison with string literal results "
                        "in unspecified behavior");
        }

That only handles the string literals cases though, we also need to reject
pointers to distinct objects at the same places.

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

* [Bug c++/70248] constexpr initialization with unspecified equality expression accepted
       [not found] <bug-70248-4@http.gcc.gnu.org/bugzilla/>
  2020-10-15 11:19 ` [Bug c++/70248] constexpr initialization with unspecified equality expression accepted redi at gcc dot gnu.org
  2020-10-15 11:24 ` redi at gcc dot gnu.org
@ 2020-10-15 15:03 ` mpolacek at gcc dot gnu.org
  2023-08-22 18:35 ` pkeir at outlook dot com
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-10-15 15:03 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
*** Bug 85474 has been marked as a duplicate of this bug. ***

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

* [Bug c++/70248] constexpr initialization with unspecified equality expression accepted
       [not found] <bug-70248-4@http.gcc.gnu.org/bugzilla/>
                   ` (2 preceding siblings ...)
  2020-10-15 15:03 ` mpolacek at gcc dot gnu.org
@ 2023-08-22 18:35 ` pkeir at outlook dot com
  2024-01-03 13:00 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 7+ messages in thread
From: pkeir at outlook dot com @ 2023-08-22 18:35 UTC (permalink / raw)
  To: gcc-bugs

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

Paul Keir <pkeir at outlook dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pkeir at outlook dot com

--- Comment #10 from Paul Keir <pkeir at outlook dot com> ---
Here is another example which GCC accepts, but Clang correctly rejects:

constexpr bool test()
{
  int arr[2]{};
  void *p1 = &arr[0];
  void *p2 = &arr[1];

  return p1 < p2;
}

constexpr bool b = test();

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

* [Bug c++/70248] constexpr initialization with unspecified equality expression accepted
       [not found] <bug-70248-4@http.gcc.gnu.org/bugzilla/>
                   ` (3 preceding siblings ...)
  2023-08-22 18:35 ` pkeir at outlook dot com
@ 2024-01-03 13:00 ` redi at gcc dot gnu.org
  2024-01-03 13:01 ` redi at gcc dot gnu.org
  2024-01-03 13:07 ` redi at gcc dot gnu.org
  6 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2024-01-03 13:00 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2020-10-15 00:00:00         |2024-1-3

--- Comment #11 from Jonathan Wakely <redi at gcc dot gnu.org> ---
And another:

struct S
{
    char data_[ 5 ];

    constexpr S( char const* p ) : data_()
    {
      if (p == data_)
        return;

      for (int i = 0; i < 5; ++i)
        data_[i] = p[i];
    }
};

constexpr S s( "test" );

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

* [Bug c++/70248] constexpr initialization with unspecified equality expression accepted
       [not found] <bug-70248-4@http.gcc.gnu.org/bugzilla/>
                   ` (4 preceding siblings ...)
  2024-01-03 13:00 ` redi at gcc dot gnu.org
@ 2024-01-03 13:01 ` redi at gcc dot gnu.org
  2024-01-03 13:07 ` redi at gcc dot gnu.org
  6 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2024-01-03 13:01 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #12 from Jonathan Wakely <redi at gcc dot gnu.org> ---
If the comparison happens in a template, GCC rejects it (see PR 113200).

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

* [Bug c++/70248] constexpr initialization with unspecified equality expression accepted
       [not found] <bug-70248-4@http.gcc.gnu.org/bugzilla/>
                   ` (5 preceding siblings ...)
  2024-01-03 13:01 ` redi at gcc dot gnu.org
@ 2024-01-03 13:07 ` redi at gcc dot gnu.org
  6 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2024-01-03 13:07 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #13 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Although this is accepted even though it's in a template, so it's more
complicated than just template vs non-template:

template<int N = 5>
consteval void f( char const* p )
{
  char buf[N]{};

  if (p == buf) // unspecified
    return;

  for (int i = 0; i < 5; ++i)
    buf[i] = p[i];
}

int main()
{
  f("test");
}

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

end of thread, other threads:[~2024-01-03 13:07 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-70248-4@http.gcc.gnu.org/bugzilla/>
2020-10-15 11:19 ` [Bug c++/70248] constexpr initialization with unspecified equality expression accepted redi at gcc dot gnu.org
2020-10-15 11:24 ` redi at gcc dot gnu.org
2020-10-15 15:03 ` mpolacek at gcc dot gnu.org
2023-08-22 18:35 ` pkeir at outlook dot com
2024-01-03 13:00 ` redi at gcc dot gnu.org
2024-01-03 13:01 ` redi at gcc dot gnu.org
2024-01-03 13:07 ` redi 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).