public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/100405] New: missing return assignment
@ 2021-05-03 20:27 redi at gcc dot gnu.org
  2021-05-03 20:28 ` [Bug c++/100405] " redi at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2021-05-03 20:27 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 100405
           Summary: missing return assignment
           Product: gcc
           Version: 10.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

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

* [Bug c++/100405] missing return assignment
  2021-05-03 20:27 [Bug c++/100405] New: missing return assignment redi at gcc dot gnu.org
@ 2021-05-03 20:28 ` redi at gcc dot gnu.org
  2021-05-03 20:40 ` [Bug c++/100405] Add implicit 'return *this;' in assignment operators redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2021-05-03 20:28 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Oops, I hit enter in the Summary field and submitted this too soon. Please
ignore (for now).

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

* [Bug c++/100405] Add implicit 'return *this;' in assignment operators
  2021-05-03 20:27 [Bug c++/100405] New: missing return assignment redi at gcc dot gnu.org
  2021-05-03 20:28 ` [Bug c++/100405] " redi at gcc dot gnu.org
@ 2021-05-03 20:40 ` redi at gcc dot gnu.org
  2021-05-03 22:22 ` redi at gcc dot gnu.org
  2021-05-03 22:38 ` jakub at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2021-05-03 20:40 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|missing return assignment   |Add implicit 'return
                   |                            |*this;' in assignment
                   |                            |operators
         Resolution|INVALID                     |---
           Severity|normal                      |enhancement
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=85523
   Last reconfirmed|                            |2021-05-03
     Ever confirmed|0                           |1
             Status|RESOLVED                    |NEW

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
It would be nice if G++ would Do The Right Thing here:

struct T
{
  T& operator=(const T& t)
  {
    i = t.i;
    /* forgot to write return *this; */
  }

  int i = 0;
};

If the user forgets to write 'return *this;' in an assignment operator the
results can be quite unpleasant (after optimization).

Similar to the implicit 'return 0;' in main it would be nice to add an implicit
return *this to assignment operators. It could be done in cases where
-Wreturn-type warns (e.g. there isn't a throw or a call to a noreturn function
before the end of the function) and if the return type is decltype(*this).

G++ does give a -Wreturn-type warning for the example above (and since gcc 9 it
adds a fix-it hint, thanks to PR 85523) but for a template it doesn't warn
until it's instantiated:

template<typename>
struct T
{
  T& operator=(const T& t)
  {
    i = t.i;
    /* forgot to write return *this; */
  }

  int i = 0;
};

This compiles without a warning (which is PR 48586).

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

* [Bug c++/100405] Add implicit 'return *this;' in assignment operators
  2021-05-03 20:27 [Bug c++/100405] New: missing return assignment redi at gcc dot gnu.org
  2021-05-03 20:28 ` [Bug c++/100405] " redi at gcc dot gnu.org
  2021-05-03 20:40 ` [Bug c++/100405] Add implicit 'return *this;' in assignment operators redi at gcc dot gnu.org
@ 2021-05-03 22:22 ` redi at gcc dot gnu.org
  2021-05-03 22:38 ` jakub at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2021-05-03 22:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Another option would be to add an implicit __builtin_trap() there. Failing hard
is probably preferable to the kind of surprising behaviour you get from the
optimizers today.

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

* [Bug c++/100405] Add implicit 'return *this;' in assignment operators
  2021-05-03 20:27 [Bug c++/100405] New: missing return assignment redi at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2021-05-03 22:22 ` redi at gcc dot gnu.org
@ 2021-05-03 22:38 ` jakub at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-05-03 22:38 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
That is already supported with -fsanitize-undefined-trap-on-error
-fsanitize=unreachable or -fsanitize=unreachable.
It isn't always the best choice though, e.g. for functions with switches or ifs
where the compiler can't prove it doesn't fallthru into end of function but the
user is certain, having __builtin_trap or __ubsan_handle_builtin_unreachable is
certainly more expensive than __builtin_unreachable.

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

end of thread, other threads:[~2021-05-03 22:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-03 20:27 [Bug c++/100405] New: missing return assignment redi at gcc dot gnu.org
2021-05-03 20:28 ` [Bug c++/100405] " redi at gcc dot gnu.org
2021-05-03 20:40 ` [Bug c++/100405] Add implicit 'return *this;' in assignment operators redi at gcc dot gnu.org
2021-05-03 22:22 ` redi at gcc dot gnu.org
2021-05-03 22:38 ` jakub 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).