public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/96498] New: Wrong location for function first declared as a friend in a template
@ 2020-08-06 13:37 redi at gcc dot gnu.org
  2020-08-06 13:42 ` [Bug c++/96498] " redi at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: redi at gcc dot gnu.org @ 2020-08-06 13:37 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 96498
           Summary: Wrong location for function first declared as a friend
                    in a template
           Product: gcc
           Version: 10.1.1
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

struct S {
  friend void foo(void*);
};

template<typename>
struct T {
  friend void foo(void*);
};

__attribute__((nonnull(1))) void foo(void*);

void bar()
{
  S s;
  foo(0);
  T<int> t;
  foo(0);
}


attr.C: In function 'void bar()':
attr.C:15:8: warning: argument 1 null where non-null expected [-Wnonnull]
   15 |   foo(0);
      |        ^
attr.C:10:34: note: in a call to function 'void foo(void*)' declared 'nonnull'
   10 | __attribute__((nonnull(1))) void foo(void*);
      |                                  ^~~
attr.C:17:8: warning: argument 1 null where non-null expected [-Wnonnull]
   17 |   foo(0);
      |        ^
attr.C:7:15: note: in a call to function 'void foo(void*)' declared 'nonnull'
    7 |   friend void foo(void*);
      |               ^~~
attr.C:14:5: warning: unused variable 's' [-Wunused-variable]
   14 |   S s;
      |     ^
attr.C:16:10: warning: unused variable 't' [-Wunused-variable]
   16 |   T<int> t;
      |          ^


The first note is fine, it shows the function declaration on line 10, where the
attributes are declared. The friend declaration in struct S doesn't affect
anything.

The second note shows the location as line 7, which is the friend declaration
inside the class template T. Why is the location not line 10 again?

It seems that instantiating T injects the friend into the surrounding namespace
in a different way to the friend declaration in a non-template.

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

* [Bug c++/96498] Wrong location for function first declared as a friend in a template
  2020-08-06 13:37 [Bug c++/96498] New: Wrong location for function first declared as a friend in a template redi at gcc dot gnu.org
@ 2020-08-06 13:42 ` redi at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: redi at gcc dot gnu.org @ 2020-08-06 13:42 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2020-08-06

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This happens even if the function has already been declared at namespace scope
prior to the friend declaration in the class template:

__attribute__((nonnull(1))) void foo(void*);

template<typename>
struct T {
  friend void foo(void*);
};

void bar()
{
  foo(0);
  T<int> t;
  foo(0);
}

attr.C: In function 'void bar()':
attr.C:11:8: warning: argument 1 null where non-null expected [-Wnonnull]
   11 |   foo(0);
      |        ^
attr.C:1:34: note: in a call to function 'void foo(void*)' declared 'nonnull'
    1 | __attribute__((nonnull(1))) void foo(void*);
      |                                  ^~~
attr.C:13:8: warning: argument 1 null where non-null expected [-Wnonnull]
   13 |   foo(0);
      |        ^
attr.C:5:15: note: in a call to function 'void foo(void*)' declared 'nonnull'
    5 |   friend void foo(void*);
      |               ^~~
attr.C:12:10: warning: unused variable 't' [-Wunused-variable]
   12 |   T<int> t;
      |          ^

I think the friend in the template should not change anything here, since the
function is already visible.

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

end of thread, other threads:[~2020-08-06 13:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-06 13:37 [Bug c++/96498] New: Wrong location for function first declared as a friend in a template redi at gcc dot gnu.org
2020-08-06 13:42 ` [Bug c++/96498] " 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).