Bug 98441 - [11 Regression] member function pointer incorrectly parsed as having trailing return type
Summary: [11 Regression] member function pointer incorrectly parsed as having trailing...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 11.0
: P1 normal
Target Milestone: 11.0
Assignee: Marek Polacek
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2020-12-25 00:24 UTC by Daniel Santos
Modified: 2021-01-07 21:23 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 10.2.0, 9.3.0
Known to fail: 11.0
Last reconfirmed: 2020-12-27 00:00:00


Attachments
Gentoo gcc 10.2.0-r2 patches (22.23 KB, application/x-bzip)
2020-12-29 09:07 UTC, Daniel Santos
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Santos 2020-12-25 00:24:37 UTC
When declaring pointer to a member function pointer using atuo& as the function's return type, we get a bad parse:

struct a {
    int& mfn();
};

void fn()
{
    int&  (a::*myvar1)(void) = &a::mfn;
    auto& (a::*myvar2)(void) = &a::mfn;
    auto  (a::*myvar3)(void) = &a::mfn;
}

Results in:

<source>: In function 'void fn()':
<source>:8:5: error: 'myvar2' function with trailing return type has 'auto&' as its type rather than plain 'auto'
    8 |     auto& (a::*myvar2)(void) = &a::mfn;
      |     ^~~~

However, it builds on GCC 9 and is alleged to build on MSVC.  The above example is simplified from the original sources:

https://github.com/freeorion/freeorion/blob/v0.4.10.1/python/UniverseWrapper.cpp#L193
Comment 1 Daniel Santos 2020-12-25 00:29:29 UTC
Also, I build gcc with:

-O42 -ffast-math -ffuzzy-dice -felide-function-bodies -pipe-clogged

but that shouldn't make a difference.
Comment 2 Jonathan Wakely 2020-12-27 20:23:53 UTC
Started to be rejected with r11-2085:

    c++: Improve checking of decls with trailing return type [PR95820]
    
    This is an ICE-on-invalid but I've been seeing it when reducing
    various testcases, so it's more important for me than usually.
    
    splice_late_return_type now checks that if we've seen a late return
    type, the function return type was auto.  That's a fair assumption
    but grokdeclarator/cdk_function wasn't giving errors for function
    pointers and similar.  So we want to perform various checks not only
    when funcdecl_p || inner_declarator == NULL.  But only give the
    !late_return_type errors when funcdecl_p, to accept e.g.
    
    auto (*fp)() = f;
Comment 3 Jonathan Wakely 2020-12-27 20:26:29 UTC
(In reply to Daniel Santos from comment #0)
> However, it builds on GCC 9 and is alleged to build on MSVC.  The above
> example is simplified from the original sources:

Are you sure this fails with 10.2.0? I only see it fail with 11.0 and not gcc version 10.2.1 20201125 (but I didn't try a newer build from the gcc-10 branch).
Comment 4 Daniel Santos 2020-12-29 09:07:02 UTC
Created attachment 49850 [details]
Gentoo gcc 10.2.0-r2 patches

(In reply to Jonathan Wakely from comment #3)
> (In reply to Daniel Santos from comment #0)
> > However, it builds on GCC 9 and is alleged to build on MSVC.  The above
> > example is simplified from the original sources:
> 
> Are you sure this fails with 10.2.0? I only see it fail with 11.0 and not
> gcc version 10.2.1 20201125 (but I didn't try a newer build from the gcc-10
> branch).

Well, yes, but this is Gentoo gcc-10.2.0-r2, so includes a patchset (attached).  In that, 34_all_fundecl-ICE-PR95820.patch contains the following:

It's an unofficial backport of PR95820 where gcc ICEs on
invalid syntax. As creduce frequently end up in these ICEs
as in #730406 let's backport it to gcc-10.

https://gcc.gnu.org/PR95820
https://bugs.gentoo.org/730406
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -12029,14 +12029,11 @@ grokdeclarator (const cp_declarator *declarator,
 
            /* Handle a late-specified return type.  */
            tree late_return_type = declarator->u.function.late_return_type;
-           if (funcdecl_p
-               /* This is the case e.g. for
-                  using T = auto () -> int.  */
-               || inner_declarator == NULL)
+           if (true)
              {
                if (tree auto_node = type_uses_auto (type))
                  {
-                   if (!late_return_type)
+                   if (!late_return_type && funcdecl_p)
                      {
                        if (current_class_type
                            && LAMBDA_TYPE_P (current_class_type))
Comment 5 Jonathan Wakely 2020-12-29 10:17:00 UTC
That's why you're asked to provide the output of 'gcc -v' by the instructions at https://gcc.gnu.org/bugs/ (because we can't guess that your 10.2.0 is different from ours).
Comment 6 Daniel Santos 2020-12-29 18:19:42 UTC
(In reply to Jonathan Wakely from comment #5)
> That's why you're asked to provide the output of 'gcc -v' by the
> instructions at https://gcc.gnu.org/bugs/ (because we can't guess that your
> 10.2.0 is different from ours).

You're correct; my apologies.  Sorry for the extra work!
Comment 7 Marek Polacek 2020-12-31 19:17:48 UTC
Mine.
Comment 8 Marek Polacek 2021-01-05 23:11:45 UTC
Looks like a simple thinko:

--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -12241,9 +12241,11 @@ grokdeclarator (const cp_declarator *declarator,
        tree late_return_type = declarator->u.function.late_return_type;
        if (tree auto_node = type_uses_auto (type))
          {
-       if (!late_return_type && funcdecl_p)
+       if (!late_return_type)
          {
-           if (current_class_type
+           if (!funcdecl_p)
+             /* auto (*fp)() = f; is OK.  */;
+           else if (current_class_type
            && LAMBDA_TYPE_P (current_class_type))
              /* OK for C++11 lambdas.  */;
            else if (cxx_dialect < cxx14)
Comment 9 GCC Commits 2021-01-07 21:20:13 UTC
The master branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:2f359597e49a15a2aef8f83ea7a14649854334cb

commit r11-6531-g2f359597e49a15a2aef8f83ea7a14649854334cb
Author: Marek Polacek <polacek@redhat.com>
Date:   Tue Jan 5 19:17:10 2021 -0500

    c++: Fix thinko in auto return type checking [PR98441]
    
    This fixes a thinko in my r11-2085 patch: when I said "But only give the
    !late_return_type errors when funcdecl_p, to accept e.g. auto (*fp)() = f;
    in C++11" I should've done this, otherwise we give bogus errors mentioning
    "function with trailing return type" when there is none.
    
    gcc/cp/ChangeLog:
    
            PR c++/98441
            * decl.c (grokdeclarator): Move the !funcdecl_p check inside the
            !late_return_type block.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/98441
            * g++.dg/cpp0x/auto55.C: New test.
Comment 10 Marek Polacek 2021-01-07 21:23:25 UTC
Fixed.  Thanks for the report.