public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106520] New: 2+ index expressions in build_op_subscript are incorrectly interpreted as comma expression
@ 2022-08-03 22:29 mkretz at gcc dot gnu.org
  2022-08-05 20:31 ` [Bug c++/106520] " pinskia at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: mkretz at gcc dot gnu.org @ 2022-08-03 22:29 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106520
           Summary: 2+ index expressions in build_op_subscript are
                    incorrectly interpreted as comma expression
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mkretz at gcc dot gnu.org
  Target Milestone: ---

Commit b38c9cf6d570f6c4c1109e00c8b81d82d0f24df3 implemented Multidimensional
subscript operator [PR102611]. However, the backwards compatibility leads to
surprising results. E.g.:

struct A
{
  void operator[](unsigned);
  void operator[](unsigned, unsigned);
};

struct B
{
  explicit operator unsigned() const;
};

void f(A a, B b)
{
  a[1];
  a[b, 2];
}

Compiles to two calls to A::operator[](unsigned) with the following
diagnostics:

<source>: In function 'void f(A, B)':
<source>:15:4: warning: top-level comma expression in array subscript changed
meaning in C++23 [-Wcomma-subscript]
   15 |   a[b, 2];
      |    ^

[https://godbolt.org/z/f6vf3x5Gv]

The user probably intended to call the two-index subscript overload. But
there's no indication why the call failed. The warning is probably puzzling to
most users. It's probably not obvious to most users that the "wrong" function
gets called.

I'm not sure the compatibility issue is worth it. I think it would be better to
call build_op_subscript with unmodified complain and let code that turns on
-std=c++23 break if it relies on comma expressions in subscripts.

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

* [Bug c++/106520] 2+ index expressions in build_op_subscript are incorrectly interpreted as comma expression
  2022-08-03 22:29 [Bug c++/106520] New: 2+ index expressions in build_op_subscript are incorrectly interpreted as comma expression mkretz at gcc dot gnu.org
@ 2022-08-05 20:31 ` pinskia at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-08-05 20:31 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
           Keywords|                            |accepts-invalid
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2022-08-05

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
clang diagnostic is better:
<source>:15:4: error: no viable overloaded operator[] for type 'A'
  a[b, 2];
  ~^~~~~
<source>:4:8: note: candidate function not viable: no known conversion from 'B'
to 'unsigned int' for 1st argument
  void operator[](unsigned, unsigned);
       ^
<source>:3:8: note: candidate function not viable: requires 1 argument, but 2
were provided
  void operator[](unsigned);
       ^

Though it does not do the warning except for C++20.

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

end of thread, other threads:[~2022-08-05 20:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-03 22:29 [Bug c++/106520] New: 2+ index expressions in build_op_subscript are incorrectly interpreted as comma expression mkretz at gcc dot gnu.org
2022-08-05 20:31 ` [Bug c++/106520] " pinskia 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).