public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/82125] Suboptimal error message for range-based for
Date: Thu, 06 Jan 2022 09:06:35 +0000	[thread overview]
Message-ID: <bug-82125-4-cVG3V4lwmx@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-82125-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
>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) { }
                 ^ ~

  parent reply	other threads:[~2022-01-06  9:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-82125-4@http.gcc.gnu.org/bugzilla/>
2021-07-23 21:26 ` pinskia at gcc dot gnu.org
2022-01-06  3:46 ` pinskia at gcc dot gnu.org
2022-01-06  9:06 ` redi at gcc dot gnu.org [this message]
2022-01-06  9:06 ` redi at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-82125-4-cVG3V4lwmx@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).