Skip to content
Snippets Groups Projects
  • Simon Martin's avatar
    198f4df0
    c++: Reject default arguments for template class friend functions [PR118319] · 198f4df0
    Simon Martin authored
    We segfault upon the following invalid code
    
    === cut here ===
    template <int> struct S {
      friend void foo (int a = []{}());
    };
    void foo (int a) {}
    int main () {
      S<0> t;
      foo ();
    }
    === cut here ===
    
    The problem is that we end up with a LAMBDA_EXPR callee in
    set_flags_from_callee, and dereference its NULL_TREE
    TREE_TYPE (TREE_TYPE (..)).
    
    This patch sets the default argument to error_mark_node and gives a hard
    error for template class friend functions that do not meet the
    requirement in C++17 11.3.6/4 (the change is restricted to templates per
    discussion with Jason).
    
    	PR c++/118319
    
    gcc/cp/ChangeLog:
    
    	* decl.cc (grokfndecl): Inspect all friend function parameters.
    	If it's not valid for them to have a default value and we're
    	processing a template, set the default value to error_mark_node
    	and give a hard error.
    
    gcc/testsuite/ChangeLog:
    
    	* g++.dg/parse/defarg18.C: New test.
    	* g++.dg/parse/defarg18a.C: New test.
    198f4df0
    History
    c++: Reject default arguments for template class friend functions [PR118319]
    Simon Martin authored
    We segfault upon the following invalid code
    
    === cut here ===
    template <int> struct S {
      friend void foo (int a = []{}());
    };
    void foo (int a) {}
    int main () {
      S<0> t;
      foo ();
    }
    === cut here ===
    
    The problem is that we end up with a LAMBDA_EXPR callee in
    set_flags_from_callee, and dereference its NULL_TREE
    TREE_TYPE (TREE_TYPE (..)).
    
    This patch sets the default argument to error_mark_node and gives a hard
    error for template class friend functions that do not meet the
    requirement in C++17 11.3.6/4 (the change is restricted to templates per
    discussion with Jason).
    
    	PR c++/118319
    
    gcc/cp/ChangeLog:
    
    	* decl.cc (grokfndecl): Inspect all friend function parameters.
    	If it's not valid for them to have a default value and we're
    	processing a template, set the default value to error_mark_node
    	and give a hard error.
    
    gcc/testsuite/ChangeLog:
    
    	* g++.dg/parse/defarg18.C: New test.
    	* g++.dg/parse/defarg18a.C: New test.