public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/106827] New: operator++ doesn't work for enum  -O2 -mcpu=cortex-m0plus
@ 2022-09-04 18:39 bugzilla@little-bat.de
  2022-09-04 18:43 ` [Bug target/106827] " bugzilla@little-bat.de
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: bugzilla@little-bat.de @ 2022-09-04 18:39 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106827
           Summary: operator++ doesn't work for enum  -O2
                    -mcpu=cortex-m0plus
           Product: gcc
           Version: 11.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bugzilla@little-bat.de
  Target Milestone: ---

Created attachment 53536
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53536&action=edit
the example on godbolt.org

gcc "optimizes" away prefix and postfix operator++ for enums.

see https://godbolt.org/z/1E65Ej7aE

--> three loops, only version 3 works (presumably).

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

* [Bug target/106827] operator++ doesn't work for enum  -O2 -mcpu=cortex-m0plus
  2022-09-04 18:39 [Bug target/106827] New: operator++ doesn't work for enum -O2 -mcpu=cortex-m0plus bugzilla@little-bat.de
@ 2022-09-04 18:43 ` bugzilla@little-bat.de
  2022-09-05  9:59 ` rearnsha at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: bugzilla@little-bat.de @ 2022-09-04 18:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Kio <bugzilla@little-bat.de> ---
updated link at godbolt:

https://godbolt.org/z/5sf7cr9c3

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

* [Bug target/106827] operator++ doesn't work for enum  -O2 -mcpu=cortex-m0plus
  2022-09-04 18:39 [Bug target/106827] New: operator++ doesn't work for enum -O2 -mcpu=cortex-m0plus bugzilla@little-bat.de
  2022-09-04 18:43 ` [Bug target/106827] " bugzilla@little-bat.de
@ 2022-09-05  9:59 ` rearnsha at gcc dot gnu.org
  2022-09-05 10:26 ` marxin at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: rearnsha at gcc dot gnu.org @ 2022-09-05  9:59 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Earnshaw <rearnsha at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-09-05
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |WAITING

--- Comment #2 from Richard Earnshaw <rearnsha at gcc dot gnu.org> ---
Please put testcases in the bug report, godbolt links are not stable.

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

* [Bug target/106827] operator++ doesn't work for enum  -O2 -mcpu=cortex-m0plus
  2022-09-04 18:39 [Bug target/106827] New: operator++ doesn't work for enum -O2 -mcpu=cortex-m0plus bugzilla@little-bat.de
  2022-09-04 18:43 ` [Bug target/106827] " bugzilla@little-bat.de
  2022-09-05  9:59 ` rearnsha at gcc dot gnu.org
@ 2022-09-05 10:26 ` marxin at gcc dot gnu.org
  2022-09-05 11:48 ` bugzilla@little-bat.de
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: marxin at gcc dot gnu.org @ 2022-09-05 10:26 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|WAITING                     |NEW
                 CC|                            |marxin at gcc dot gnu.org

--- Comment #3 from Martin Liška <marxin at gcc dot gnu.org> ---
Copied test-case:

enum Foo{a,b,c,d,e,f};

Foo operator+(Foo n,int m) { return Foo(int(n)+m); }
Foo operator++(Foo n,int) { return Foo(int(n)+1); }
Foo operator++(Foo n) { return Foo(int(n)+1); }

void test1()
{
    for (Foo i=a; i<=f; i++) 
    {
        volatile Foo z=i;
    }
}

void test2()
{
    for (Foo i=a; i<=f; ++i) 
    {
        volatile Foo z=i;
    }
}

void test3()
{
    for (Foo i=a; i<=f; i=i+1) 
    {
        volatile Foo z=i;
    }
}

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

* [Bug target/106827] operator++ doesn't work for enum  -O2 -mcpu=cortex-m0plus
  2022-09-04 18:39 [Bug target/106827] New: operator++ doesn't work for enum -O2 -mcpu=cortex-m0plus bugzilla@little-bat.de
                   ` (2 preceding siblings ...)
  2022-09-05 10:26 ` marxin at gcc dot gnu.org
@ 2022-09-05 11:48 ` bugzilla@little-bat.de
  2022-09-05 12:12 ` schwab@linux-m68k.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: bugzilla@little-bat.de @ 2022-09-05 11:48 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Kio <bugzilla@little-bat.de> ---
(In reply to Richard Earnshaw from comment #2)
> Please put testcases in the bug report, godbolt links are not stable.

test case was also attached as a file with the bug report.

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

* [Bug target/106827] operator++ doesn't work for enum  -O2 -mcpu=cortex-m0plus
  2022-09-04 18:39 [Bug target/106827] New: operator++ doesn't work for enum -O2 -mcpu=cortex-m0plus bugzilla@little-bat.de
                   ` (3 preceding siblings ...)
  2022-09-05 11:48 ` bugzilla@little-bat.de
@ 2022-09-05 12:12 ` schwab@linux-m68k.org
  2022-09-05 12:51 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: schwab@linux-m68k.org @ 2022-09-05 12:12 UTC (permalink / raw)
  To: gcc-bugs

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

Andreas Schwab <schwab@linux-m68k.org> changed:

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

--- Comment #5 from Andreas Schwab <schwab@linux-m68k.org> ---
The operator++ function is expected to modify the argument in place, and needs
to take a reference to it.

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

* [Bug target/106827] operator++ doesn't work for enum  -O2 -mcpu=cortex-m0plus
  2022-09-04 18:39 [Bug target/106827] New: operator++ doesn't work for enum -O2 -mcpu=cortex-m0plus bugzilla@little-bat.de
                   ` (4 preceding siblings ...)
  2022-09-05 12:12 ` schwab@linux-m68k.org
@ 2022-09-05 12:51 ` jakub at gcc dot gnu.org
  2022-09-05 18:55 ` bugzilla@little-bat.de
  2022-09-06  9:55 ` rearnsha at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-09-05 12:51 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

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

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
I think C++ doesn't explicitly say that the argument has to be a reference:
https://eel.is/c++draft/over.oper#over.inc
but obviously if it isn't and it isn't a non-static method operator++,
then it can only compute the return value and can't actually modify in place.
For ++i just operator++(i) is called and for i++ operator++(i, 0).
So, with your overloads you'd actually need to do i = ++i
The other overload is just unusable because it does the wrong thing completely.

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

* [Bug target/106827] operator++ doesn't work for enum  -O2 -mcpu=cortex-m0plus
  2022-09-04 18:39 [Bug target/106827] New: operator++ doesn't work for enum -O2 -mcpu=cortex-m0plus bugzilla@little-bat.de
                   ` (5 preceding siblings ...)
  2022-09-05 12:51 ` jakub at gcc dot gnu.org
@ 2022-09-05 18:55 ` bugzilla@little-bat.de
  2022-09-06  9:55 ` rearnsha at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: bugzilla@little-bat.de @ 2022-09-05 18:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Kio <bugzilla@little-bat.de> ---
(In reply to Andreas Schwab from comment #5)
> The operator++ function is expected to modify the argument in place, and
> needs to take a reference to it.

Yes, that's my error! after modifying the operators to:

Foo operator++(Foo& n,int) { Foo v=n; n=Foo(int(n)+1); return v; }
Foo& operator++(Foo& n)    { return n=Foo(int(n)+1); }

then the compiled code is as expected.

Obviously arg#1 must be a reference, because the argument must be modified.
Actually i think this code should not compile or at least produce a warning (i
compile with almost all warnings on, but that went silent). When passing a
value the operator can't do what is expected.

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

* [Bug target/106827] operator++ doesn't work for enum  -O2 -mcpu=cortex-m0plus
  2022-09-04 18:39 [Bug target/106827] New: operator++ doesn't work for enum -O2 -mcpu=cortex-m0plus bugzilla@little-bat.de
                   ` (6 preceding siblings ...)
  2022-09-05 18:55 ` bugzilla@little-bat.de
@ 2022-09-06  9:55 ` rearnsha at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: rearnsha at gcc dot gnu.org @ 2022-09-06  9:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Richard Earnshaw <rearnsha at gcc dot gnu.org> ---
That's C++ for you, it permits you to overload operators in completely
meaningless ways.

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

end of thread, other threads:[~2022-09-06  9:56 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-04 18:39 [Bug target/106827] New: operator++ doesn't work for enum -O2 -mcpu=cortex-m0plus bugzilla@little-bat.de
2022-09-04 18:43 ` [Bug target/106827] " bugzilla@little-bat.de
2022-09-05  9:59 ` rearnsha at gcc dot gnu.org
2022-09-05 10:26 ` marxin at gcc dot gnu.org
2022-09-05 11:48 ` bugzilla@little-bat.de
2022-09-05 12:12 ` schwab@linux-m68k.org
2022-09-05 12:51 ` jakub at gcc dot gnu.org
2022-09-05 18:55 ` bugzilla@little-bat.de
2022-09-06  9:55 ` rearnsha 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).