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.

Reply via email to