Bug 97663 - [c++17] Function with return type 'unsigned' in nested namespace misinterpreted as deduction guide
Summary: [c++17] Function with return type 'unsigned' in nested namespace misinterpret...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.2.0
: P3 normal
Target Milestone: 8.5
Assignee: Jakub Jelinek
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2020-11-01 15:07 UTC by Hans Dembinski
Modified: 2021-09-11 14:29 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-11-01 00:00:00


Attachments
gcc11-pr97663.patch (537 bytes, patch)
2020-11-02 11:40 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Hans Dembinski 2020-11-01 15:07:43 UTC
g++ with -std=c++17 since version 7.1 up to current trunk misdetects 'bar::foo' the following code as a deduction guide for 'struct foo':

```
template <class T> struct foo {};

namespace bar {

unsigned foo();

}
```

Code on Godbolt: https://godbolt.org/z/6E4efY

Workarounds:
- Use 'unsigned int foo()'
- Use 'auto foo() -> unsigned'.

Related issue with more context: https://github.com/boostorg/histogram/issues/290
Comment 1 Jakub Jelinek 2020-11-02 11:00:35 UTC
Started with r7-6608-ga56c0ac08242269bbcc4bd1f480eda2378336776
Comment 2 Jakub Jelinek 2020-11-02 11:40:48 UTC
Created attachment 49486 [details]
gcc11-pr97663.patch

Only lightly tested fix (so far), passes check-c++-all without regressions.
Comment 3 GCC Commits 2020-11-03 20:43:21 UTC
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:875225301e356759982573d5578ed7ca54f81f86

commit r11-4695-g875225301e356759982573d5578ed7ca54f81f86
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Nov 3 21:42:51 2020 +0100

    c++: Don't try to parse a function declaration as deduction guide [PR97663]
    
    While these function declarations have NULL decl_specifiers->type,
    they have still type specifiers specified from which the default int
    in the return type is added, so we shouldn't try to parse those as
    deduction guides.
    
    2020-11-03  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/97663
            * parser.c (cp_parser_init_declarator): Don't try to parse
            C++17 deduction guides if there are any type specifiers even when
            type is NULL.
    
            * g++.dg/cpp1z/class-deduction75.C: New test.
Comment 4 GCC Commits 2020-11-12 10:05:44 UTC
The releases/gcc-10 branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:cd41e4a1864c10c7f9141284e82e5cc0a3007806

commit r10-9008-gcd41e4a1864c10c7f9141284e82e5cc0a3007806
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Nov 3 21:42:51 2020 +0100

    c++: Don't try to parse a function declaration as deduction guide [PR97663]
    
    While these function declarations have NULL decl_specifiers->type,
    they have still type specifiers specified from which the default int
    in the return type is added, so we shouldn't try to parse those as
    deduction guides.
    
    2020-11-03  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/97663
            * parser.c (cp_parser_init_declarator): Don't try to parse
            C++17 deduction guides if there are any type specifiers even when
            type is NULL.
    
            * g++.dg/cpp1z/class-deduction75.C: New test.
    
    (cherry picked from commit 875225301e356759982573d5578ed7ca54f81f86)
Comment 5 GCC Commits 2021-04-20 23:30:01 UTC
The releases/gcc-9 branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:c3a583316776a399fea9eb79019245138a2be181

commit r9-9392-gc3a583316776a399fea9eb79019245138a2be181
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Nov 3 21:42:51 2020 +0100

    c++: Don't try to parse a function declaration as deduction guide [PR97663]
    
    While these function declarations have NULL decl_specifiers->type,
    they have still type specifiers specified from which the default int
    in the return type is added, so we shouldn't try to parse those as
    deduction guides.
    
    2020-11-03  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/97663
            * parser.c (cp_parser_init_declarator): Don't try to parse
            C++17 deduction guides if there are any type specifiers even when
            type is NULL.
    
            * g++.dg/cpp1z/class-deduction75.C: New test.
    
    (cherry picked from commit cd41e4a1864c10c7f9141284e82e5cc0a3007806)
Comment 6 GCC Commits 2021-04-22 16:48:52 UTC
The releases/gcc-8 branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:a35abb48b025291cabc364c1b335d3f675e9e97f

commit r8-10862-ga35abb48b025291cabc364c1b335d3f675e9e97f
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Nov 3 21:42:51 2020 +0100

    c++: Don't try to parse a function declaration as deduction guide [PR97663]
    
    While these function declarations have NULL decl_specifiers->type,
    they have still type specifiers specified from which the default int
    in the return type is added, so we shouldn't try to parse those as
    deduction guides.
    
    2020-11-03  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/97663
            * parser.c (cp_parser_init_declarator): Don't try to parse
            C++17 deduction guides if there are any type specifiers even when
            type is NULL.
    
            * g++.dg/cpp1z/class-deduction75.C: New test.
    
    (cherry picked from commit cd41e4a1864c10c7f9141284e82e5cc0a3007806)
Comment 7 Jakub Jelinek 2021-04-22 17:07:41 UTC
Fixed.