public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/102774] New: Stop showing "error: variable or field ‘f’ declared void" after an earlier error in a declarator
@ 2021-10-15 12:11 redi at gcc dot gnu.org
  2022-05-18 22:52 ` [Bug c++/102774] " barry.revzin at gmail dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2021-10-15 12:11 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 102774
           Summary: Stop showing "error: variable or field ‘f’ declared
                    void" after an earlier error in a declarator
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
            Blocks: 16663
  Target Milestone: ---

Consider the example in PR 102771 and its errors:

vart.C:1:18: error: expected nested-name-specifier before ‘.’ token
    1 | template<typename.. T> void f(T...) { }
      |                  ^
vart.C:1:18: error: expected ‘>’ before ‘.’ token
vart.C:1:29: error: variable or field ‘f’ declared void
    1 | template<typename.. T> void f(T...) { }
      |                             ^
vart.C:1:31: error: ‘T’ was not declared in this scope
    1 | template<typename.. T> void f(T...) { }
      |                               ^


G++ often prints some variation of "variable or field ‘f’ declared void" and
it's rarely helpful in practice.

I think that error is intended for cases like:

void f;

But this is probably rare, and in practice the diagnostic gets printed **all
the time** for code that isn't doing this at all. In the example above we have
an almost valid function declaration, except for the typo in the template-head.
Once we've hit that typo (and shown an error for it), it's reasonable for the
rest of the declarator to fail to parse, but the "declared void" is just
nonsense. The only problem is the bad '...' token, but if we can't recover from
that then

If we've already hit at least one error in a given declarator, maybe we should
suppress the "declared void" diagnostic. It's almost certainly due to poor
error recovery after the first error.

Or at least change the wording, so that it says something like "could not parse
invalid declrator".

"variable of field 'f' declared void" for a function declaration is just bad.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=16663
[Bug 16663] Poor parse error recovery with mispelled type in function
declaration

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

* [Bug c++/102774] Stop showing "error: variable or field ‘f’ declared void" after an earlier error in a declarator
  2021-10-15 12:11 [Bug c++/102774] New: Stop showing "error: variable or field ‘f’ declared void" after an earlier error in a declarator redi at gcc dot gnu.org
@ 2022-05-18 22:52 ` barry.revzin at gmail dot com
  2022-05-18 23:12 ` mpolacek at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: barry.revzin at gmail dot com @ 2022-05-18 22:52 UTC (permalink / raw)
  To: gcc-bugs

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

Barry Revzin <barry.revzin at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |barry.revzin at gmail dot com

--- Comment #1 from Barry Revzin <barry.revzin at gmail dot com> ---
Was just filing a bug for the same problem. Here's a short repro:

namespace A::B {
    struct X { void use(); };
}

void f(B::X x, int j) {
    x.use();
}

The error I get is:

<source>:5:6: error: variable or field 'f' declared void
    5 | void f(B::X x, int j) {
      |      ^
<source>:5:8: error: 'B' has not been declared
    5 | void f(B::X x, int j) {
      |        ^
<source>:5:16: error: expected primary-expression before 'int'
    5 | void f(B::X x, int j) {
      |                ^~~

The middle error is the real problem, the first and third are... quite
unhelpful! In some cases, the third error looks more like this:

<source>:7:18: error: expected primary-expression before 'j'
    7 | void f(B::X x, Y j) {
      |                  ^

Which is equivalently unhelpful.

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

* [Bug c++/102774] Stop showing "error: variable or field ‘f’ declared void" after an earlier error in a declarator
  2021-10-15 12:11 [Bug c++/102774] New: Stop showing "error: variable or field ‘f’ declared void" after an earlier error in a declarator redi at gcc dot gnu.org
  2022-05-18 22:52 ` [Bug c++/102774] " barry.revzin at gmail dot com
@ 2022-05-18 23:12 ` mpolacek at gcc dot gnu.org
  2022-05-19  7:29 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-05-18 23:12 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mpolacek at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2022-05-18

--- Comment #2 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Confirmed.

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

* [Bug c++/102774] Stop showing "error: variable or field ‘f’ declared void" after an earlier error in a declarator
  2021-10-15 12:11 [Bug c++/102774] New: Stop showing "error: variable or field ‘f’ declared void" after an earlier error in a declarator redi at gcc dot gnu.org
  2022-05-18 22:52 ` [Bug c++/102774] " barry.revzin at gmail dot com
  2022-05-18 23:12 ` mpolacek at gcc dot gnu.org
@ 2022-05-19  7:29 ` redi at gcc dot gnu.org
  2022-05-19  7:30 ` redi at gcc dot gnu.org
  2024-05-21 14:11 ` redi at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2022-05-19  7:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
For the first case:

template<typename.. T> void f(T...) { }

GCC prints:

f.C:1:18: error: expected nested-name-specifier before ‘.’ token
    1 | template<typename.. T> void f(T...) { }
      |                  ^
f.C:1:18: error: expected ‘>’ before ‘.’ token
f.C:1:29: error: variable or field ‘f’ declared void
    1 | template<typename.. T> void f(T...) { }
      |                             ^
f.C:1:31: error: ‘T’ was not declared in this scope
    1 | template<typename.. T> void f(T...) { }
      |     

Clang says:

f.C:1:18: error: expected a qualified name after 'typename'
template<typename.. T> void f(T...) { }
                 ^
f.C:1:18: error: expected ',' or '>' in template-parameter-list
f.C:1:31: error: unknown type name 'T'
template<typename.. T> void f(T...) { }
                              ^
3 errors generated.

This has no "declared void" noise.

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

* [Bug c++/102774] Stop showing "error: variable or field ‘f’ declared void" after an earlier error in a declarator
  2021-10-15 12:11 [Bug c++/102774] New: Stop showing "error: variable or field ‘f’ declared void" after an earlier error in a declarator redi at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2022-05-19  7:29 ` redi at gcc dot gnu.org
@ 2022-05-19  7:30 ` redi at gcc dot gnu.org
  2024-05-21 14:11 ` redi at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2022-05-19  7:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
For Barry's example clang prints:

g.C:5:8: error: use of undeclared identifier 'B'; did you mean 'A::B'?
void f(B::X x, int j) {
       ^
       A::B
g.C:1:14: note: 'A::B' declared here
namespace A::B {
             ^
1 error generated.


This is much better. Ideal even.

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

* [Bug c++/102774] Stop showing "error: variable or field ‘f’ declared void" after an earlier error in a declarator
  2021-10-15 12:11 [Bug c++/102774] New: Stop showing "error: variable or field ‘f’ declared void" after an earlier error in a declarator redi at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2022-05-19  7:30 ` redi at gcc dot gnu.org
@ 2024-05-21 14:11 ` redi at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2024-05-21 14:11 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2022-05-18 00:00:00         |2024-5-21

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

template<class /* oops T not declared */> void func(T) { }


f.cc:1:48: error: variable or field ‘func’ declared void
    1 | template<class /* oops T not declared */> void func(T) { }
      |                                                ^~~~
f.cc:1:53: error: ‘T’ was not declared in this scope
    1 | template<class /* oops T not declared */> void func(T) { }
      |                                                     ^

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

end of thread, other threads:[~2024-05-21 14:11 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-15 12:11 [Bug c++/102774] New: Stop showing "error: variable or field ‘f’ declared void" after an earlier error in a declarator redi at gcc dot gnu.org
2022-05-18 22:52 ` [Bug c++/102774] " barry.revzin at gmail dot com
2022-05-18 23:12 ` mpolacek at gcc dot gnu.org
2022-05-19  7:29 ` redi at gcc dot gnu.org
2022-05-19  7:30 ` redi at gcc dot gnu.org
2024-05-21 14:11 ` 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).