public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/103583] New: Range loop: "error: 'begin' was not declared in this scope" when 'end' is missing
@ 2021-12-06 14:18 shlomo at fastmail dot com
  2021-12-06 14:30 ` [Bug c++/103583] " redi at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: shlomo at fastmail dot com @ 2021-12-06 14:18 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103583
           Summary: Range loop: "error: 'begin' was not declared in this
                    scope" when 'end' is missing
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: shlomo at fastmail dot com
  Target Milestone: ---

Compiling the following with gcc -c:

struct A {
    int *begin();
    // int *end();
};
void foo(A a) {
    for (auto it : a) { }
}

shows two error messages:

error: ‘begin’ was not declared in this scope
error: ‘end’ was not declared in this scope

The first error message is incorrect.
If only one of 'begin' and 'end' is missing, GCC shouldn't print an error about
the other one.

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

* [Bug c++/103583] Range loop: "error: 'begin' was not declared in this scope" when 'end' is missing
  2021-12-06 14:18 [Bug c++/103583] New: Range loop: "error: 'begin' was not declared in this scope" when 'end' is missing shlomo at fastmail dot com
@ 2021-12-06 14:30 ` redi at gcc dot gnu.org
  2021-12-06 14:31 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2021-12-06 14:30 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The error is technically correct.

If your class had both begin and end, then the range-based for loop would use
a.begin() and a.end(). But because it doesn't have both, the loop uses begin(a)
and end(a), neither of which is defined. And that's what the error says.

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

* [Bug c++/103583] Range loop: "error: 'begin' was not declared in this scope" when 'end' is missing
  2021-12-06 14:18 [Bug c++/103583] New: Range loop: "error: 'begin' was not declared in this scope" when 'end' is missing shlomo at fastmail dot com
  2021-12-06 14:30 ` [Bug c++/103583] " redi at gcc dot gnu.org
@ 2021-12-06 14:31 ` redi at gcc dot gnu.org
  2021-12-06 14:35 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2021-12-06 14:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
See [stmt.ranged] p1.

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

* [Bug c++/103583] Range loop: "error: 'begin' was not declared in this scope" when 'end' is missing
  2021-12-06 14:18 [Bug c++/103583] New: Range loop: "error: 'begin' was not declared in this scope" when 'end' is missing shlomo at fastmail dot com
  2021-12-06 14:30 ` [Bug c++/103583] " redi at gcc dot gnu.org
  2021-12-06 14:31 ` redi at gcc dot gnu.org
@ 2021-12-06 14:35 ` redi at gcc dot gnu.org
  2021-12-06 21:59 ` pinskia at gcc dot gnu.org
  2022-01-06  9:06 ` redi at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2021-12-06 14:35 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I think the implementation doesn't do anything special to handle this case. It
just looks for the members a.begin() and a.end() and then if they aren't found,
it goes ahead with rewriting the code to use begin(a) and end(a), and any
errors that result from that are shown straight to the user.

Another option would be to do lookup for begin(a) and end(a) with errors
suppressed, and if they aren't found, issue a custom diagnostic saying
something like "no suitable begin/end pair available for range-based 'for'
loop".


Clang seems to do something similar:

f.C:6:18: error: invalid range expression of type 'A'; no viable 'end' function
available
    for (auto it : a) { }
                 ^ ~

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

* [Bug c++/103583] Range loop: "error: 'begin' was not declared in this scope" when 'end' is missing
  2021-12-06 14:18 [Bug c++/103583] New: Range loop: "error: 'begin' was not declared in this scope" when 'end' is missing shlomo at fastmail dot com
                   ` (2 preceding siblings ...)
  2021-12-06 14:35 ` redi at gcc dot gnu.org
@ 2021-12-06 21:59 ` pinskia at gcc dot gnu.org
  2022-01-06  9:06 ` redi at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-06 21:59 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-12-06
           Severity|normal                      |enhancement

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #3)
> Another option would be to do lookup for begin(a) and end(a) with errors
> suppressed, and if they aren't found, issue a custom diagnostic saying
> something like "no suitable begin/end pair available for range-based 'for'
> loop".

I think this is a good option.

Confirmed.

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

* [Bug c++/103583] Range loop: "error: 'begin' was not declared in this scope" when 'end' is missing
  2021-12-06 14:18 [Bug c++/103583] New: Range loop: "error: 'begin' was not declared in this scope" when 'end' is missing shlomo at fastmail dot com
                   ` (3 preceding siblings ...)
  2021-12-06 21:59 ` pinskia at gcc dot gnu.org
@ 2022-01-06  9:06 ` redi at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2022-01-06  9:06 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
dup

*** This bug has been marked as a duplicate of bug 82125 ***

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

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

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-06 14:18 [Bug c++/103583] New: Range loop: "error: 'begin' was not declared in this scope" when 'end' is missing shlomo at fastmail dot com
2021-12-06 14:30 ` [Bug c++/103583] " redi at gcc dot gnu.org
2021-12-06 14:31 ` redi at gcc dot gnu.org
2021-12-06 14:35 ` redi at gcc dot gnu.org
2021-12-06 21:59 ` pinskia at gcc dot gnu.org
2022-01-06  9:06 ` 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).