public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/116731] New: Incorrect behavior of -Wrange-loop-construct in GCC 14
@ 2024-09-16  3:44 sunil.dora1988 at gmail dot com
  2024-09-16 11:41 ` [Bug c++/116731] " redi at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: sunil.dora1988 at gmail dot com @ 2024-09-16  3:44 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 116731
           Summary: Incorrect behavior of -Wrange-loop-construct in GCC 14
           Product: gcc
           Version: 14.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sunil.dora1988 at gmail dot com
  Target Milestone: ---

It appears that the warning by -Wrange-loop-construct is triggered incorrectly
for certain loop constructs.

Consider the below example :

#include <array>
#include <cstdint>
#include <iostream>
#include <set>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

static std::string val{"vale"};

int main() {
    const std::vector arr{std::pair<std::string_view, int>{val, 1}};
    for (const auto [text, val] : arr) {
        std::cout << text << val;
    }
    return 0;
}

=============
g++ -pipe -W -Wall -Wextra -pedantic -g -O  -c -o test.o test.cpp
test.cpp: In function ‘int main()’:
test.cpp:14:21: warning: loop variable ‘<structured bindings>’ creates a copy
from type ‘const std::pair<std::basic_string_view<char>, int>’
[-Wrange-loop-construct]
   14 |     for (const auto [text, val] : arr) {
      |                     ^~~~~~~~~~~
test.cpp:14:21: note: use reference type to prevent copying
   14 |     for (const auto [text, val] : arr) {
      |                     ^~~~~~~~~~~
      |                     &
g++ -g -o test test.o
================

It seems that GCC/G++ might be checking for trivial copyability rather than
trivial copy-constructibility, which could be contributing to the issue.

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

* [Bug c++/116731] Incorrect behavior of -Wrange-loop-construct in GCC 14
  2024-09-16  3:44 [Bug c++/116731] New: Incorrect behavior of -Wrange-loop-construct in GCC 14 sunil.dora1988 at gmail dot com
@ 2024-09-16 11:41 ` redi at gcc dot gnu.org
  2024-09-16 17:36 ` mpolacek at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2024-09-16 11:41 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2024-09-16
           Keywords|                            |diagnostic
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This is the condition, so it is indeed using trivially copyable:

  /* Since small trivially copyable types are cheap to copy, we suppress the
     warning for them.  64B is a common size of a cache line.  */
  if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
      || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
      && trivially_copyable_p (type)))
    return;

I agree that the warning seems unhelpful in this case. The fact that
std::pair's copy assignment operator is not trivial is irrelevant here, we're
not doing any assignment.

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

* [Bug c++/116731] Incorrect behavior of -Wrange-loop-construct in GCC 14
  2024-09-16  3:44 [Bug c++/116731] New: Incorrect behavior of -Wrange-loop-construct in GCC 14 sunil.dora1988 at gmail dot com
  2024-09-16 11:41 ` [Bug c++/116731] " redi at gcc dot gnu.org
@ 2024-09-16 17:36 ` mpolacek at gcc dot gnu.org
  2024-09-17 20:52 ` mpolacek at gcc dot gnu.org
  2024-09-26 21:51 ` cvs-commit at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-09-16 17:36 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |mpolacek at gcc dot gnu.org
                 CC|                            |mpolacek at gcc dot gnu.org

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

* [Bug c++/116731] Incorrect behavior of -Wrange-loop-construct in GCC 14
  2024-09-16  3:44 [Bug c++/116731] New: Incorrect behavior of -Wrange-loop-construct in GCC 14 sunil.dora1988 at gmail dot com
  2024-09-16 11:41 ` [Bug c++/116731] " redi at gcc dot gnu.org
  2024-09-16 17:36 ` mpolacek at gcc dot gnu.org
@ 2024-09-17 20:52 ` mpolacek at gcc dot gnu.org
  2024-09-26 21:51 ` cvs-commit at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2024-09-17 20:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
LLVM fixed this in the same way:
https://github.com/llvm/llvm-project/issues/47355

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

* [Bug c++/116731] Incorrect behavior of -Wrange-loop-construct in GCC 14
  2024-09-16  3:44 [Bug c++/116731] New: Incorrect behavior of -Wrange-loop-construct in GCC 14 sunil.dora1988 at gmail dot com
                   ` (2 preceding siblings ...)
  2024-09-17 20:52 ` mpolacek at gcc dot gnu.org
@ 2024-09-26 21:51 ` cvs-commit at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-09-26 21:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:6ac4e2f4b2ca9980670e7d3815a9140730df1005

commit r15-3905-g6ac4e2f4b2ca9980670e7d3815a9140730df1005
Author: Marek Polacek <polacek@redhat.com>
Date:   Tue Sep 17 16:58:37 2024 -0400

    c++: tweak for -Wrange-loop-construct [PR116731]

    This PR reports that the warning would be better off using a check
    for trivially constructible rather than trivially copyable.

    LLVM accepted a similar fix:
    https://github.com/llvm/llvm-project/issues/47355

            PR c++/116731

    gcc/cp/ChangeLog:

            * parser.cc (warn_for_range_copy): Check if TYPE is trivially
            constructible, not copyable.

    gcc/testsuite/ChangeLog:

            * g++.dg/warn/Wrange-loop-construct3.C: New test.

    Reviewed-by: Jason Merrill <jason@redhat.com>

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

end of thread, other threads:[~2024-09-26 21:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-09-16  3:44 [Bug c++/116731] New: Incorrect behavior of -Wrange-loop-construct in GCC 14 sunil.dora1988 at gmail dot com
2024-09-16 11:41 ` [Bug c++/116731] " redi at gcc dot gnu.org
2024-09-16 17:36 ` mpolacek at gcc dot gnu.org
2024-09-17 20:52 ` mpolacek at gcc dot gnu.org
2024-09-26 21:51 ` cvs-commit 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).