Bug 37806 - CV-qualifiers on function typedef's are inconsistently accepted depending on typedef scope
Summary: CV-qualifiers on function typedef's are inconsistently accepted depending on ...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.3.2
: P3 normal
Target Milestone: 4.4.0
Assignee: Jason Merrill
URL:
Keywords: rejects-valid
: 39321 (view as bug list)
Depends on: 29993 39310
Blocks:
  Show dependency treegraph
 
Reported: 2008-10-11 19:50 UTC by Andrew Nelless
Modified: 2010-01-08 03:45 UTC (History)
3 users (show)

See Also:
Host: x86_64
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2009-03-02 01:20:48


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Nelless 2008-10-11 19:50:00 UTC
Hi,

First of all this is my first bug report to GNU GCC so please bare with me.

The issue I having is with typedef assisted declarations of class/struct member functions. It seems to me that cv-qualifiers on function typedef's are treated inconsistantly depending on the scope of the typedef.

The below code demonstrates the problem: with NS_SCOPE defined the code compiles fine with "-Wall -Wextra -pedantic" on 4.3.2 but when NS_SCOPE is undefined, the const qualifier is ignored with "error: ignoring ‘const’ qualifiers added to function type". To me this seems completely inconsistent and there is no explaination as to why the qualifer is ignored.

---snip---
#include <iostream>

#ifdef NS_SCOPE
typedef void (function_type)(int) const;
#endif

template <typename T>
struct S1
{
    #ifndef NS_SCOPE
    typedef void (function_type)(int) const;
    #endif  
};


struct S2: public S1<int>
{
    virtual function_type f = 0;
};


struct S3: public S2
{
    void 
    f (int i) const
    {
        std::cout << "Hello world: " << i << std::endl;
    }
};


int
main()
{
    S3 s;
    s.f(5);
}
---snip---


I did find a C++ defect report that may be relevent, but to be honest, i'm not 
certain what is going on with regard to the standard here

http://open-std.org/JTC1/SC22/WG21/docs/cwg_defects.html#295

I hope this can be resolved because it will allow for some really groovy template
generated interfaces.

Visual Studio 2005 (the only other compiler I have to hand) seems to compile this
code without NS_SCOPE defined, but I'm uncertain how to thoroughly test this there.
Comment 1 Richard Biener 2008-10-11 19:58:47 UTC
EDG accepts the code.  With GCC 4.3 and newer we reject the code with

t.C: In function ‘int main()’:
t.C:32: error: cannot declare variable ‘s’ to be of abstract type ‘S3’
t.C:21: note:   because the following virtual functions are pure within ‘S3’:
t.C:16: note: 	virtual void S2::f(int)

GCC 4.2 has this in addition to the CV error.  Of course the reason is the
same.

GCC never accepted the code, so this is not a regression.
Comment 2 Andrew Pinski 2008-10-13 22:59:49 UTC
Related to PR 29993 where we rejected this code before because of the const.  I want to say that even though we handle them now, we don't handle them fully now.
Comment 3 Jason Merrill 2009-03-02 01:20:47 UTC
My patch for 39310 also fixes this bug.
Comment 4 Jason Merrill 2009-03-02 01:22:41 UTC
*** Bug 39321 has been marked as a duplicate of this bug. ***
Comment 5 Jason Merrill 2009-03-31 18:31:47 UTC
Subject: Bug 37806

Author: jason
Date: Tue Mar 31 18:31:17 2009
New Revision: 145365

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=145365
Log:
        PR c++/37806
        * typeck.c (cp_apply_type_quals_to_decl): Don't apply any quals
        to a typedef.
        * tree.c (cp_build_qualified_type_real): Don't apply restrict to a
        function type.
        * decl.h (enum decl_context): Add TEMPLATE_TYPE_ARG.
        * decl.c (groktypename): Add is_template_arg parameter.
        (grokdeclarator): Allow function cv-quals on a template type arg.
        * parser.c (cp_parser_new_type_id, cp_parser_type_id): Add
        is_template_arg argument in calls to groktypename.
        * cp-tree.h: Adjust prototype.
        * error.c (dump_type_prefix, dump_type_suffix): Fix plain
        FUNCTION_TYPE printing.

        PR libstdc++/39310
        * include/tr1_impl/type_traits (is_function): Add partial
        specializations with function cv-quals.
        (__is_function_helper): Remove.
        (is_member_pointer): Don't define in terms of is_member_*_pointer.

Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/cp-tree.h
    trunk/gcc/cp/decl.c
    trunk/gcc/cp/decl.h
    trunk/gcc/cp/error.c
    trunk/gcc/cp/parser.c
    trunk/gcc/cp/tree.c
    trunk/gcc/cp/typeck.c
    trunk/gcc/testsuite/g++.dg/template/qualttp20.C
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/include/tr1_impl/type_traits

Comment 6 Jason Merrill 2009-03-31 18:38:15 UTC
Subject: Bug 37806

Author: jason
Date: Tue Mar 31 18:37:49 2009
New Revision: 145366

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=145366
Log:
        PR c++/37806
        * g++.dg/template/typedef17.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/template/typedef17.C
Modified:
    trunk/gcc/testsuite/ChangeLog

Comment 7 Jason Merrill 2009-04-07 04:38:25 UTC
Subject: Bug 37806

Author: jason
Date: Tue Apr  7 04:38:10 2009
New Revision: 145648

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=145648
Log:
        * decl.c (grokdeclarator): Reject pointer to qualified function
        type.

        PR c++/37806, core issue 547
        * typeck.c (cp_apply_type_quals_to_decl): Don't apply any quals
        to a typedef.
        * tree.c (cp_build_qualified_type_real): Don't apply restrict to a
        function type.
        * decl.h (enum decl_context): Add TEMPLATE_TYPE_ARG.
        * decl.c (groktypename): Add is_template_arg parameter.
        (grokdeclarator): Allow function cv-quals on a template type arg.
        * parser.c (cp_parser_new_type_id, cp_parser_type_id): Add
        is_template_arg argument in calls to groktypename.
        * cp-tree.h: Adjust prototype.
        * error.c (dump_type_prefix, dump_type_suffix): Fix plain
        FUNCTION_TYPE printing.

        PR libstdc++/39310
        * include/tr1_impl/type_traits (is_function): Add partial
        specializations with function cv-quals.
        (__is_function_helper): Remove.
        (is_member_pointer): Don't define in terms of is_member_*_pointer.

Added:
    branches/gcc-4_4-branch/gcc/testsuite/g++.dg/other/typedef2.C
      - copied unchanged from r145367, trunk/gcc/testsuite/g++.dg/other/typedef2.C
    branches/gcc-4_4-branch/gcc/testsuite/g++.dg/template/typedef17.C
      - copied unchanged from r145367, trunk/gcc/testsuite/g++.dg/template/typedef17.C
Modified:
    branches/gcc-4_4-branch/gcc/cp/ChangeLog
    branches/gcc-4_4-branch/gcc/cp/cp-tree.h
    branches/gcc-4_4-branch/gcc/cp/decl.c
    branches/gcc-4_4-branch/gcc/cp/decl.h
    branches/gcc-4_4-branch/gcc/cp/error.c
    branches/gcc-4_4-branch/gcc/cp/parser.c
    branches/gcc-4_4-branch/gcc/cp/tree.c
    branches/gcc-4_4-branch/gcc/cp/typeck.c
    branches/gcc-4_4-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_4-branch/gcc/testsuite/g++.dg/template/qualttp20.C
    branches/gcc-4_4-branch/libstdc++-v3/ChangeLog
    branches/gcc-4_4-branch/libstdc++-v3/include/tr1_impl/type_traits

Comment 8 Jason Merrill 2009-04-07 04:38:51 UTC
Fixed for 4.4.