public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/103749] New: Misleading error message on template/non-template conflict
@ 2021-12-16 15:41 drepper.fsp+rhbz at gmail dot com
  2021-12-16 15:47 ` [Bug c++/103749] " mpolacek at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: drepper.fsp+rhbz at gmail dot com @ 2021-12-16 15:41 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103749
           Summary: Misleading error message on template/non-template
                    conflict
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: drepper.fsp+rhbz at gmail dot com
  Target Milestone: ---

This problem isn't new in the trunk version, it exists in all versions I
tested.

This is the code in question:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
struct foo {
  template<typename>
  friend struct bar;
};

struct bar {
  int baz;
};

bar var;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is obviously buggy, the actual definition of 'bar' is not a template
class.   This is exactly what clang tell me:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$ clang++ -c u.cc
u.cc:6:8: error: redefinition of 'bar' as different kind of symbol
struct bar {
       ^
u.cc:3:17: note: previous definition is here
  friend struct bar;
                ^
u.cc:10:1: error: unknown type name 'bar'
bar var;
^
2 errors generated.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

With g++ the error messages are misleading and it also generates a lot more
unnecessary text:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$ g++ -c u.cc
u.cc:6:8: error: template argument required for ‘struct bar’
    6 | struct bar {
      |        ^~~
u.cc:10:5: error: class template argument deduction failed:
   10 | bar var;
      |     ^~~
u.cc:10:5: error: no matching function for call to ‘bar()’
u.cc:3:17: note: candidate: ‘template<class> bar()-> bar<
<template-parameter-1-1> >’
    3 |   friend struct bar;
      |                 ^~~
u.cc:3:17: note:   template argument deduction/substitution failed:
u.cc:10:5: note:   couldn’t deduce template parameter
‘<template-parameter-1-1>’
   10 | bar var;
      |     ^~~
u.cc:3:17: note: candidate: ‘template<class> bar(bar< <template-parameter-1-1>
>)-> bar< <template-parameter-1-1> >’
    3 |   friend struct bar;
      |                 ^~~
u.cc:3:17: note:   template argument deduction/substitution failed:
u.cc:10:5: note:   candidate expects 1 argument, 0 provided
   10 | bar var;
      |     ^~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

* [Bug c++/103749] Misleading error message on template/non-template conflict
  2021-12-16 15:41 [Bug c++/103749] New: Misleading error message on template/non-template conflict drepper.fsp+rhbz at gmail dot com
@ 2021-12-16 15:47 ` mpolacek at gcc dot gnu.org
  2021-12-16 16:16 ` mpolacek at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-12-16 15:47 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-12-16
             Status|UNCONFIRMED                 |NEW
           Keywords|                            |diagnostic
                 CC|                            |mpolacek at gcc dot gnu.org

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

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

* [Bug c++/103749] Misleading error message on template/non-template conflict
  2021-12-16 15:41 [Bug c++/103749] New: Misleading error message on template/non-template conflict drepper.fsp+rhbz at gmail dot com
  2021-12-16 15:47 ` [Bug c++/103749] " mpolacek at gcc dot gnu.org
@ 2021-12-16 16:16 ` mpolacek at gcc dot gnu.org
  2021-12-16 19:46 ` mpolacek at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-12-16 16:16 UTC (permalink / raw)
  To: gcc-bugs

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

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

template<typename>
struct bar;

struct bar {
  int baz;
};

bar var;

Maybe xref_tag could detect this.

The other way round it's a bit better:

struct bar;

template<typename>
struct bar {};

bar var;


103749-2.C:11:8: error: ‘bar’ is not a template
   11 | struct bar {};
      |        ^~~
103749-2.C:8:8: note: previous declaration here
    8 | struct bar;
      |        ^~~

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

* [Bug c++/103749] Misleading error message on template/non-template conflict
  2021-12-16 15:41 [Bug c++/103749] New: Misleading error message on template/non-template conflict drepper.fsp+rhbz at gmail dot com
  2021-12-16 15:47 ` [Bug c++/103749] " mpolacek at gcc dot gnu.org
  2021-12-16 16:16 ` mpolacek at gcc dot gnu.org
@ 2021-12-16 19:46 ` mpolacek at gcc dot gnu.org
  2021-12-16 19:59 ` mpolacek at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-12-16 19:46 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
I have a patch with which we, for the original test, emit:

$ xg++ -c 103749-2.C
103749-2.C:6:8: error: ‘bar’ was declared as template but no template header
provided
    6 | struct bar {
      |        ^~~
103749-2.C:3:17: note: previous declaration here
    3 |   friend struct bar;
      |                 ^~~
103749-2.C:10:5: error: ‘bar<...auto...> var’ has incomplete type
   10 | bar var;
      |     ^~~

Hopefully that's a bit better.

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

* [Bug c++/103749] Misleading error message on template/non-template conflict
  2021-12-16 15:41 [Bug c++/103749] New: Misleading error message on template/non-template conflict drepper.fsp+rhbz at gmail dot com
                   ` (2 preceding siblings ...)
  2021-12-16 19:46 ` mpolacek at gcc dot gnu.org
@ 2021-12-16 19:59 ` mpolacek at gcc dot gnu.org
  2021-12-16 20:16 ` drepper.fsp+rhbz at gmail dot com
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-12-16 19:59 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |mpolacek at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

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

* [Bug c++/103749] Misleading error message on template/non-template conflict
  2021-12-16 15:41 [Bug c++/103749] New: Misleading error message on template/non-template conflict drepper.fsp+rhbz at gmail dot com
                   ` (3 preceding siblings ...)
  2021-12-16 19:59 ` mpolacek at gcc dot gnu.org
@ 2021-12-16 20:16 ` drepper.fsp+rhbz at gmail dot com
  2021-12-17 18:08 ` cvs-commit at gcc dot gnu.org
  2021-12-17 18:17 ` mpolacek at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: drepper.fsp+rhbz at gmail dot com @ 2021-12-16 20:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Ulrich Drepper <drepper.fsp+rhbz at gmail dot com> ---
(In reply to Marek Polacek from comment #3)
> Hopefully that's a bit better.

This indeed looks as good as one can hope for.  Thanks.

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

* [Bug c++/103749] Misleading error message on template/non-template conflict
  2021-12-16 15:41 [Bug c++/103749] New: Misleading error message on template/non-template conflict drepper.fsp+rhbz at gmail dot com
                   ` (4 preceding siblings ...)
  2021-12-16 20:16 ` drepper.fsp+rhbz at gmail dot com
@ 2021-12-17 18:08 ` cvs-commit at gcc dot gnu.org
  2021-12-17 18:17 ` mpolacek at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-12-17 18:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:fae016862631da70e6482fe3173a111248f8b9bc

commit r12-6049-gfae016862631da70e6482fe3173a111248f8b9bc
Author: Marek Polacek <polacek@redhat.com>
Date:   Thu Dec 16 14:57:07 2021 -0500

    c++: Improve diagnostic for class tmpl/class redecl [PR103749]

    For code like

      template<typename>
      struct bar;

      struct bar {
        int baz;
      };

      bar var;

    we emit a fairly misleading and unwieldy diagnostic:

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    $ g++ -c u.cc
    u.cc:6:8: error: template argument required for 'struct bar'
        6 | struct bar {
          |        ^~~
    u.cc:10:5: error: class template argument deduction failed:
       10 | bar var;
          |     ^~~
    u.cc:10:5: error: no matching function for call to 'bar()'
    u.cc:3:17: note: candidate: 'template<class> bar()-> bar<
<template-parameter-1-1> >'
        3 |   friend struct bar;
          |                 ^~~
    u.cc:3:17: note:   template argument deduction/substitution failed:
    u.cc:10:5: note:   couldn't deduce template parameter
'<template-parameter-1-1>'
       10 | bar var;
          |     ^~~
    u.cc:3:17: note: candidate: 'template<class> bar(bar<
<template-parameter-1-1> >)-> bar< <template-parameter-1-1> >'
        3 |   friend struct bar;
          |                 ^~~
    u.cc:3:17: note:   template argument deduction/substitution failed:
    u.cc:10:5: note:   candidate expects 1 argument, 0 provided
       10 | bar var;
          |     ^~~
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    but with this patch we get:

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    z.C:4:10: error: class template 'bar' redeclared as non-template
        4 |   struct bar {
          |          ^~~
    z.C:2:10: note: previous declaration here
        2 |   struct bar;
          |          ^~~
    z.C:8:7: error: 'bar<...auto...> var' has incomplete type
        8 |   bar var;
          |       ^~~
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    which is clearer about what the problem is.

    I thought it'd be nice to avoid printing the messages about failed CTAD,
    too.  To that end, I'm using CLASSTYPE_ERRONEOUS to suppress CTAD.  Not
    sure if that's entirely kosher.

    The other direction (first a non-template class declaration followed by
    a class template definition) we handle quite well:

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    z.C:11:8: error: 'bar' is not a template
       11 | struct bar {};
          |        ^~~
    z.C:8:8: note: previous declaration here
        8 | struct bar;
          |        ^~~
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            PR c++/103749

    gcc/cp/ChangeLog:

            * decl.c (lookup_and_check_tag): Give an error when a class was
            declared as template but no template header has been provided.
            * pt.c (do_class_deduction): Don't deduce CLASSTYPE_ERRONEOUS
            types.

    gcc/testsuite/ChangeLog:

            * g++.dg/template/redecl4.C: Adjust dg-error.
            * g++.dg/diagnostic/redeclaration-2.C: New test.

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

* [Bug c++/103749] Misleading error message on template/non-template conflict
  2021-12-16 15:41 [Bug c++/103749] New: Misleading error message on template/non-template conflict drepper.fsp+rhbz at gmail dot com
                   ` (5 preceding siblings ...)
  2021-12-17 18:08 ` cvs-commit at gcc dot gnu.org
@ 2021-12-17 18:17 ` mpolacek at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-12-17 18:17 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #6 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Fixed.

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

end of thread, other threads:[~2021-12-17 18:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-16 15:41 [Bug c++/103749] New: Misleading error message on template/non-template conflict drepper.fsp+rhbz at gmail dot com
2021-12-16 15:47 ` [Bug c++/103749] " mpolacek at gcc dot gnu.org
2021-12-16 16:16 ` mpolacek at gcc dot gnu.org
2021-12-16 19:46 ` mpolacek at gcc dot gnu.org
2021-12-16 19:59 ` mpolacek at gcc dot gnu.org
2021-12-16 20:16 ` drepper.fsp+rhbz at gmail dot com
2021-12-17 18:08 ` cvs-commit at gcc dot gnu.org
2021-12-17 18:17 ` mpolacek 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).