public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101811] New: Error not helpful for misplaced 'template'
@ 2021-08-07  9:26 tobi at gcc dot gnu.org
  2021-08-09  9:37 ` [Bug c++/101811] " redi at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: tobi at gcc dot gnu.org @ 2021-08-07  9:26 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101811
           Summary: Error not helpful for misplaced 'template'
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tobi at gcc dot gnu.org
  Target Milestone: ---

This is a bad error message that caught my eye while refactoring some code
(https://godbolt.org/z/558vM4Wb3):

struct X {
    void f();
};

template<int i>  // this line should not be here
void X::f()
{}

gives:


<source>:6:6: error: no declaration matches 'void X::f()'
    6 | void X::f()
      |      ^
<source>:2:10: note: candidate is: 'void X::f()'
    2 |     void f();
      |          ^
<source>:1:8: note: 'struct X' defined here
    1 | struct X {
      |        ^

Note that the error message doesn't actually include what's wrong and so it is
fairly confusing until you actually look at the code.

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

* [Bug c++/101811] Error not helpful for misplaced 'template'
  2021-08-07  9:26 [Bug c++/101811] New: Error not helpful for misplaced 'template' tobi at gcc dot gnu.org
@ 2021-08-09  9:37 ` redi at gcc dot gnu.org
  2021-08-10  4:57 ` tobi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2021-08-09  9:37 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-08-09
             Status|UNCONFIRMED                 |NEW
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=82334

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This seems like one specific case of PR 82334, which suggests that GCC should
tell you why each candidate doesn't match. So in this case it would tell you
that the candidate on line 2 is not a template. The error for line 6 should
also give some indication that it's referring to a function template, e.g.

<source>:6:6: error: no declaration matches 'template<int> void X::f()'

That would make it much more obvious that we're trying to match a template to a
non-template.

Clang is no better than GCC for this example, but EDG gets part of the way
there:

"t.C", line 6: error: "X" is not a class template
  void X::f()
       ^

It's true that X is not a class template, but that is only half the story. The
function template definition would match a member function template too:

struct X {
    template<int> void f();
};

template<int i>
void X::f()
{}

So it should really say X is not a class template and X::f is not a function
template.

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

* [Bug c++/101811] Error not helpful for misplaced 'template'
  2021-08-07  9:26 [Bug c++/101811] New: Error not helpful for misplaced 'template' tobi at gcc dot gnu.org
  2021-08-09  9:37 ` [Bug c++/101811] " redi at gcc dot gnu.org
@ 2021-08-10  4:57 ` tobi at gcc dot gnu.org
  2021-08-10 10:45 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: tobi at gcc dot gnu.org @ 2021-08-10  4:57 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Tobias Schlüter <tobi at gcc dot gnu.org> ---
Hi Jonathan,
actually I found clang's error message a lot more helpful, I just didn't bother
to write it explicitly, especially given that my compiler explorer link shows
it for everybody else to make the same observation.  Well, you came to a
different conclusion comparing the two and so I was wondering why I would find
clang's message more helpful and you didn't.

Here's clang's error message:
<source>:6:9: error: out-of-line definition of 'f' does not match any
declaration in 'X'
void X::f()
        ^
<source>:2:10: note: member declaration nearly matches
    void f();

When I read that I know 1) there are two 'f's.  2) the one on line 6 doesn't
match the one on line 2.  3) yet it ought to match (the) one in 'struct X'.  So
I as s user am left with wondering "how does it not match" which is sufficient
to resolve the problem.

Let me point to three things where gcc ends up making things harder to
understand in my opinion:
1) clang uses (almost) complete sentences.  One doesn't have to figure out how
the parts of the error message relate to then form a logical whole from the set
of "error" and "note"s.
2) "candidate" in gcc's error message appears to be a technical term or a
gcc-specific term, one has to make sense of what it means from the context
("nothing matches -> oh, so this was a candidate for a match and this is what
'candidate' means here" whereas clang's error implies directly that the 'f' on
line 2 was really close to the one on line 6.
3) gcc's second note, which points to the struct declaration isn't really
helpful, it just floats in the ether for the user to make sense of.  I would go
so far as to claim that it doesn't add any helpful information and thus does
more bad than good.

Anyway, I'm well aware that the C++ frontend unfortunately doesn't get much
developer attention, so I'm not writing these PRs to complain, but to give a
potential future FE developer a good starting point to get their hands dirty :)

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

* [Bug c++/101811] Error not helpful for misplaced 'template'
  2021-08-07  9:26 [Bug c++/101811] New: Error not helpful for misplaced 'template' tobi at gcc dot gnu.org
  2021-08-09  9:37 ` [Bug c++/101811] " redi at gcc dot gnu.org
  2021-08-10  4:57 ` tobi at gcc dot gnu.org
@ 2021-08-10 10:45 ` redi at gcc dot gnu.org
  2021-08-10 11:32 ` tobi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2021-08-10 10:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Tobias Schlüter from comment #2)
> Hi Jonathan,
> actually I found clang's error message a lot more helpful, I just didn't
> bother to write it explicitly, especially given that my compiler explorer
> link shows it for everybody else to make the same observation.  Well, you
> came to a different conclusion comparing the two and so I was wondering why
> I would find clang's message more helpful and you didn't.
> 
> Here's clang's error message:
> <source>:6:9: error: out-of-line definition of 'f' does not match any
> declaration in 'X'
> void X::f()
>         ^
> <source>:2:10: note: member declaration nearly matches
>     void f();
> 
> When I read that I know 1) there are two 'f's.

GCC tells you this too, there are two locations, one on line 2 and one on line
6.

> 2) the one on line 6 doesn't
> match the one on line 2.

That seems obvious from the fact there are two and GCC says "no declaration
matches" :-)


> 3) yet it ought to match (the) one in 'struct X'. 
> So I as s user am left with wondering "how does it not match" which is
> sufficient to resolve the problem.

I honestly don't see how GCC's error doesn't provide exactly the same
information. It could be clearer, but all the same information is there.


> Let me point to three things where gcc ends up making things harder to
> understand in my opinion:
> 1) clang uses (almost) complete sentences.  One doesn't have to figure out
> how the parts of the error message relate to then form a logical whole from
> the set of "error" and "note"s.

GCC's "error:" is a complete sentence too, it's just a lot more terse.

> 2) "candidate" in gcc's error message appears to be a technical term or a

It's the term used in the C++ standard for functions of the same name that are
participating in overload resolution.

The compiler isn't actually doing overload resolution here so maybe it's a poor
choice of terminology that only makes sense from the compiler writers'
perspective.

> gcc-specific term, one has to make sense of what it means from the context
> ("nothing matches -> oh, so this was a candidate for a match and this is
> what 'candidate' means here" whereas clang's error implies directly that the
> 'f' on line 2 was really close to the one on line 6.
> 3) gcc's second note, which points to the struct declaration isn't really
> helpful, it just floats in the ether for the user to make sense of.  I would
> go so far as to claim that it doesn't add any helpful information and thus
> does more bad than good.

I agree.

I think this would be better than GCC's current diagnostic, and better than
Clang's too:

<source>:6:6: error: no declaration matches 'template<int> void X::f()'
    6 | void X::f()
      |      ^
<source>:2:10: note: non-matching declaration 'void X::f()' is not a template
    2 |     void f();
      |          ^





> Anyway, I'm well aware that the C++ frontend unfortunately doesn't get much
> developer attention,

That's not true at all. Over the past year it has more commits than all other
GCC front ends combined, and more distinct contributors than any other front
end.

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

* [Bug c++/101811] Error not helpful for misplaced 'template'
  2021-08-07  9:26 [Bug c++/101811] New: Error not helpful for misplaced 'template' tobi at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2021-08-10 10:45 ` redi at gcc dot gnu.org
@ 2021-08-10 11:32 ` tobi at gcc dot gnu.org
  2021-08-10 11:41 ` redi at gcc dot gnu.org
  2021-08-10 11:51 ` tobi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: tobi at gcc dot gnu.org @ 2021-08-10 11:32 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Tobias Schlüter <tobi at gcc dot gnu.org> ---
Hi Jonathan,

I know that we disagree about clang's error message and that's why I tried to
explain what makes clang's a  better error message for me.  My "parsing" of
clang's error message was not a comparison to gcc's, but an analysis how it
conveys all the information I need to find and fix the issue. 

Glad to see that we more or less agree on the issues with gcc's message,
though.  That's the important part anyway.  I also like your suggestion.  If we
are at a stage where grammatical niceties matter, I would make the symbol in
the first message the subject, so

<source>:6:6: error: 'template<int> void X::f()' matches no declaration
    6 | void X::f()
      |      ^
<source>:2:10: note: non-matching declaration 'void X::f()' is not a template
    2 |     void f();
      |          ^ 

Thanks for correcting my misunderstanding about the status of the C++ FE, and
I'm glad to hear that.  I really wonder where I'd picked up that piece of
misinformation.  Actually, while I wrote this I was wondering "how do they
manage to stay up to speed with the latest versions then?"  Now I'm less
impressed ;-)

Cheers!

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

* [Bug c++/101811] Error not helpful for misplaced 'template'
  2021-08-07  9:26 [Bug c++/101811] New: Error not helpful for misplaced 'template' tobi at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2021-08-10 11:32 ` tobi at gcc dot gnu.org
@ 2021-08-10 11:41 ` redi at gcc dot gnu.org
  2021-08-10 11:51 ` tobi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2021-08-10 11:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Tobias Schlüter from comment #4)
> we are at a stage where grammatical niceties matter, I would make the symbol
> in the first message the subject, so
> 
> <source>:6:6: error: 'template<int> void X::f()' matches no declaration
>     6 | void X::f()
>       |      ^

No, I disagree. The function declarator can be arbitrarily long, sometimes
wrapping over several lines on the screen. If the important "matches no
declaration" text is at an arbitrary place on the screen it is hard to find. If
it appears immediately after the "error:" string (which is typically
highlighted in red) then you don't have to scan the output to find it.

"X matches no declaration" also seems grammatically worse than "no declaration
matches X", but that's debatable.

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

* [Bug c++/101811] Error not helpful for misplaced 'template'
  2021-08-07  9:26 [Bug c++/101811] New: Error not helpful for misplaced 'template' tobi at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2021-08-10 11:41 ` redi at gcc dot gnu.org
@ 2021-08-10 11:51 ` tobi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: tobi at gcc dot gnu.org @ 2021-08-10 11:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Tobias Schlüter <tobi at gcc dot gnu.org> ---
Sure, that's a good argument (before I18N), and I guess it matches the general
style.

Anyway, I'll try to keep myself from bikeshedding this further, I'm sure the
person fixing this will have enough time to figure out a good way of conveying
this information.

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

end of thread, other threads:[~2021-08-10 11:51 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-07  9:26 [Bug c++/101811] New: Error not helpful for misplaced 'template' tobi at gcc dot gnu.org
2021-08-09  9:37 ` [Bug c++/101811] " redi at gcc dot gnu.org
2021-08-10  4:57 ` tobi at gcc dot gnu.org
2021-08-10 10:45 ` redi at gcc dot gnu.org
2021-08-10 11:32 ` tobi at gcc dot gnu.org
2021-08-10 11:41 ` redi at gcc dot gnu.org
2021-08-10 11:51 ` tobi 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).