Bug 59766

Summary: c++1y: declaring friend function with 'auto' return type deduction is rejected with bogus reason
Product: gcc Reporter: Matheus Izvekov <mizvekov>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: daniel.kruegler, jason, potswa
Priority: P3 Keywords: rejects-valid
Version: 4.8.2   
Target Milestone: 5.2   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2014-12-05 00:00:00
Attachments: ChangeLog entry
Fix: disable the error for friend declarators.

Description Matheus Izvekov 2014-01-10 23:59:26 UTC
Simple example showing the problem:

struct foo {
	friend auto test(const foo &) { return 0; }
};

g++ rejects this with:

test.cc:2:30: error: non-static data member declared ‘auto’
  friend auto test(const foo &) { return 0; }
Comment 1 lucdanton 2014-01-19 10:51:21 UTC
Happens too with GCC 4.9 (20140105), as well as when using decltype(auto).
Comment 2 Paolo Carlini 2014-01-24 11:28:20 UTC
Maybe Adam can have a look.
Comment 3 Paolo Carlini 2014-01-24 11:49:34 UTC
Sorry.
Comment 4 Felix Fontein 2014-06-28 22:35:18 UTC
I can confirm this bug for GCC 4.9.0 20140604 (prerelease).
Comment 5 David Krauss 2015-01-23 09:31:41 UTC
Created attachment 34538 [details]
ChangeLog entry
Comment 6 David Krauss 2015-01-23 09:43:55 UTC
Created attachment 34541 [details]
Fix: disable the error for friend declarators.

The fix is simple. Tested and submitted for approval. (I am a registered contributor.)

I don't suppose this warrants a testcase file. If you want to exercise the compiler, here is a more interesting case:


template< typename k >
struct t {
    friend auto leak( t );

    template< typename v >
    struct m {
        friend auto leak( t ) { return v{}; }
    };
};

int main() {
    t< int >::m< char > a;
    decltype( leak( t< int >{} ) ) b;
    static_assert ( sizeof b == 1, "" );
    
    t< int >::m< short > c; // error: redefinition of ‘auto leak(t<int>)’
}
Comment 7 Louis Dionne 2015-04-09 02:20:21 UTC
The following still fails on GCC trunk:
    
    struct T {
        friend auto f() { }
    };


Sample command line:

› ~/code/gcc5/bin/g++ --version
g++ (GCC) 5.0.0 20150326 (experimental)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

› ~/code/gcc5/bin/g++ ~/code/hana/test/worksheet.cpp -std=c++1y
[...]: error: non-static data member declared ‘auto’
     friend auto f() { }
                   ^
Comment 8 Marc Glisse 2015-04-09 05:44:30 UTC
(In reply to David Krauss from comment #6)
> The fix is simple. Tested and submitted for approval.

Thank you. Did you send it to gcc-patches, with jason AT redhat in Cc: ? I can't find any email from you around that date.

https://gcc.gnu.org/contribute.html

> I don't suppose this warrants a testcase file.

Why not? It is the only way to make sure a later refactoring of the code won't regress on this.
Comment 9 David Krauss 2015-04-09 19:04:17 UTC
I didn't send it to gcc-patches. It's never been necessary for me before. I suppose I can collect the examples here into a testcase file. In the meantime, anyone else is free to take charge and submit it on their own. The change is just a matter of "don't do that."
Comment 10 Jason Merrill 2015-04-10 19:29:24 UTC
(In reply to David Krauss from comment #9)
> I didn't send it to gcc-patches. It's never been necessary for me before.

I appreciate getting direct mail about C++ patches so that I see them faster; I didn't notice this one until today.  Also feel free to ping me if I haven't responded after a week.

I'll check this in after GCC 5 branches.
Comment 11 Jason Merrill 2015-04-14 15:29:59 UTC
Author: jason
Date: Tue Apr 14 15:29:27 2015
New Revision: 222095

URL: https://gcc.gnu.org/viewcvs?rev=222095&root=gcc&view=rev
Log:
	PR c++/59766
	* decl.c (grokdeclarator): Do not flag friends with deduced return.

Added:
    trunk/gcc/testsuite/g++.dg/cpp1y/auto-fn26.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/decl.c
Comment 12 Jason Merrill 2015-04-22 20:53:23 UTC
Author: jason
Date: Wed Apr 22 20:52:52 2015
New Revision: 222337

URL: https://gcc.gnu.org/viewcvs?rev=222337&root=gcc&view=rev
Log:
	PR c++/59766
	* decl.c (grokdeclarator): Do not flag friends with deduced return.

Added:
    branches/gcc-5-branch/gcc/testsuite/g++.dg/cpp1y/auto-fn26.C
Modified:
    branches/gcc-5-branch/gcc/cp/ChangeLog
    branches/gcc-5-branch/gcc/cp/decl.c
Comment 13 Jason Merrill 2015-04-22 20:53:47 UTC
Fixed for 5.2/6.
Comment 14 Jason Merrill 2015-04-23 13:21:37 UTC
Author: jason
Date: Thu Apr 23 13:21:06 2015
New Revision: 222365

URL: https://gcc.gnu.org/viewcvs?rev=222365&root=gcc&view=rev
Log:
	PR c++/59766
	* decl.c (grokdeclarator): Do not flag friends with deduced return.

Added:
    branches/gcc-4_9-branch/gcc/testsuite/g++.dg/cpp1y/auto-fn26.C
Modified:
    branches/gcc-4_9-branch/gcc/cp/ChangeLog
    branches/gcc-4_9-branch/gcc/cp/decl.c
Comment 15 Jason Merrill 2015-04-23 13:23:37 UTC
And 4.9.3.