public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/111018] New: lexical interpretation of friendship rules depends on whether the friend function has a definition
@ 2023-08-14 20:23 eric.niebler at gmail dot com
  2023-08-14 21:05 ` [Bug c++/111018] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: eric.niebler at gmail dot com @ 2023-08-14 20:23 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111018
           Summary: lexical interpretation of friendship rules depends on
                    whether the friend function has a definition
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: eric.niebler at gmail dot com
  Target Milestone: ---

Starting in gcc 12, transitive friendship is extended to friend functions that
are defined lexically within the body of the friend class. E.g.:


struct S;

struct T {
  friend struct S;
private:
  template <class T>
  static void foo(T) {}
};

struct S {
  template <class Self>
  friend auto bar(Self s) -> decltype(::T::foo(s)) { // (1)
    return ::T::foo(s);
  }
};

Prior to gcc-12, the commented line would have been rejected, but now it is
accepted. Great, it brings gcc in line with clang and is arguably more
sensible.

HOWEVER, it does NOT work if the friend function is merely declared but not
defined. For instance, this is still an error:

struct S;

struct T {
  friend struct S;
private:
  template <class T>
  static void foo(T) {}
};

struct S {
  template <class Self>
  friend auto bar(Self s) -> decltype(::T::foo(s)); // NO FN DEFINITION
};

int main() {
  S s;
  using T = decltype(bar(s)); // ERROR: T::foo is private
}


This is very confusing and inconsistent behavior.

See: https://godbolt.org/z/WT9P37Wba

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

* [Bug c++/111018] lexical interpretation of friendship rules depends on whether the friend function has a definition
  2023-08-14 20:23 [Bug c++/111018] New: lexical interpretation of friendship rules depends on whether the friend function has a definition eric.niebler at gmail dot com
@ 2023-08-14 21:05 ` pinskia at gcc dot gnu.org
  2023-08-14 21:07 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-14 21:05 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So if we make a definition outside of the class:
```
  template <class Self>
  auto bar(Self s) -> decltype(::T::foo(s))
  {

  }
```
Then clang rejects the code too ...
So ....

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

* [Bug c++/111018] lexical interpretation of friendship rules depends on whether the friend function has a definition
  2023-08-14 20:23 [Bug c++/111018] New: lexical interpretation of friendship rules depends on whether the friend function has a definition eric.niebler at gmail dot com
  2023-08-14 21:05 ` [Bug c++/111018] " pinskia at gcc dot gnu.org
@ 2023-08-14 21:07 ` pinskia at gcc dot gnu.org
  2023-08-14 21:15 ` pinskia at gcc dot gnu.org
  2023-08-14 21:17 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-14 21:07 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|rejects-valid               |

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
MSVC rejects both cases ....

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

* [Bug c++/111018] lexical interpretation of friendship rules depends on whether the friend function has a definition
  2023-08-14 20:23 [Bug c++/111018] New: lexical interpretation of friendship rules depends on whether the friend function has a definition eric.niebler at gmail dot com
  2023-08-14 21:05 ` [Bug c++/111018] " pinskia at gcc dot gnu.org
  2023-08-14 21:07 ` pinskia at gcc dot gnu.org
@ 2023-08-14 21:15 ` pinskia at gcc dot gnu.org
  2023-08-14 21:17 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-14 21:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
https://wg21.link/cwg1699

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

* [Bug c++/111018] lexical interpretation of friendship rules depends on whether the friend function has a definition
  2023-08-14 20:23 [Bug c++/111018] New: lexical interpretation of friendship rules depends on whether the friend function has a definition eric.niebler at gmail dot com
                   ` (2 preceding siblings ...)
  2023-08-14 21:15 ` pinskia at gcc dot gnu.org
@ 2023-08-14 21:17 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-14 21:17 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=58993

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I am going to leave this one open only because of what is mentioned in the
commit that changed behavior here:
r13-465-g4df735e01e3199978

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

end of thread, other threads:[~2023-08-14 21:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-14 20:23 [Bug c++/111018] New: lexical interpretation of friendship rules depends on whether the friend function has a definition eric.niebler at gmail dot com
2023-08-14 21:05 ` [Bug c++/111018] " pinskia at gcc dot gnu.org
2023-08-14 21:07 ` pinskia at gcc dot gnu.org
2023-08-14 21:15 ` pinskia at gcc dot gnu.org
2023-08-14 21:17 ` pinskia 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).