[Bug c++/82125] Suboptimal error message for range-based for

2022-01-06 Thread redi at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82125

Jonathan Wakely  changed:

   What|Removed |Added

 CC||shlomo at fastmail dot com

--- Comment #4 from Jonathan Wakely  ---
*** Bug 103583 has been marked as a duplicate of this bug. ***

[Bug c++/82125] Suboptimal error message for range-based for

2022-01-06 Thread redi at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82125

--- Comment #3 from Jonathan Wakely  ---
>From PR 103583:

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.

* Comment 1 Jonathan Wakely 2021-12-06 14:30:35 UTC

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.

* Comment 2 Jonathan Wakely 2021-12-06 14:31:38 UTC

See [stmt.ranged] p1.

* Comment 3 Jonathan Wakely 2021-12-06 14:35:55 UTC

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) { }
 ^ ~

[Bug c++/82125] Suboptimal error message for range-based for

2022-01-05 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82125

Andrew Pinski  changed:

   What|Removed |Added

   Severity|normal  |enhancement

[Bug c++/82125] Suboptimal error message for range-based for

2021-07-23 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82125

Andrew Pinski  changed:

   What|Removed |Added

   Last reconfirmed|2017-09-07 00:00:00 |2021-7-23

--- Comment #2 from Andrew Pinski  ---
clang gives a reasonable error message:
:4:20: error: invalid range expression of type 'int *const'; no viable
'begin' function available
for(auto e : r) {}
   ^ ~
1 error generated.
ASM generation compiler returned: 1

[Bug c++/82125] Suboptimal error message for range-based for

2017-09-07 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82125

Jonathan Wakely  changed:

   What|Removed |Added

 Status|UNCONFIRMED |NEW
   Last reconfirmed||2017-09-07
 Ever confirmed|0   |1

--- Comment #1 from Jonathan Wakely  ---
Yes, we should have a custom diagnostic for range-based 'for' that fails to
compile.