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

            Bug ID: 62137
           Summary: Poor error recovery when parsing for-loops
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: manu at gcc dot gnu.org

This has to be a common mistake:

manuel@gcc10:~$ cat parseerr.cc 
void foo(void)
{
  for (int k, k < 20; k++);
}

Yet we give a not very good diagnostic:

parseerr.cc:3:17: error: expected initializer before ‘<’ token
   for (int k, k < 20; k++);
                 ^
parseerr.cc:3:17: error: expected ‘;’ before ‘<’ token
parseerr.cc:3:17: error: expected primary-expression before ‘<’ token

My guess is that "k <20" is parsed as a declaration of type "int", when that
fails something goes wrong, and instead of backtracking to ",", the parser
tries to finish the declaration starting from k but finds "<" instead of ";".
The last error, well, who knows!

After seeing the first error, there are perhaps two better options:

* If we know we are in a loop header, backtrack to after consuming, assume we
have seen ";" and continue.
* If not, simply skip to next ";"

Reply via email to