]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: Reject cdtors and conversion operators with a single * as return type [PR118304...
authorSimon Martin <simon@nasilyan.com>
Tue, 11 Feb 2025 14:59:02 +0000 (15:59 +0100)
committerSimon Martin <simon@nasilyan.com>
Tue, 11 Feb 2025 14:59:02 +0000 (15:59 +0100)
commitc74e7f651a014d59631361bcc9be05d797928c5c
tree1327627382391664163f3f087e8ccdad1966ccd0
parente8c5013b6b7820d77edc45d04e634d49b20c05ce
c++: Reject cdtors and conversion operators with a single * as return type [PR118304, PR118306]

We currently accept the following constructor declaration (clang, EDG
and MSVC do as well), and ICE on the destructor declaration

=== cut here ===
struct A {
  *A ();
  ~A () = default;
};
=== cut here ===

The problem is that we end up in grokdeclarator with a cp_declarator of
kind cdk_pointer but no type, and we happily go through (if we have a
reference instead we eventually error out trying to form a reference to
void).

This patch makes sure that grokdeclarator errors out and strips the
invalid declarator when processing a cdtor (or a conversion operator
with no return type specified) with a declarator representing a pointer
or a reference type.

PR c++/118306
PR c++/118304

gcc/cp/ChangeLog:

* decl.cc (maybe_strip_indirect_ref): New.
(check_special_function_return_type): Take declarator as input.
Call maybe_strip_indirect_ref and error out if it returns true.
(grokdeclarator): Update call to
check_special_function_return_type.

gcc/testsuite/ChangeLog:

* g++.old-deja/g++.jason/operator.C: Adjust bogus test
expectation (char** vs char*).
* g++.dg/parse/constructor4.C: New test.
* g++.dg/parse/constructor5.C: New test.
* g++.dg/parse/conv_op2.C: New test.
* g++.dg/parse/default_to_int.C: New test.
gcc/cp/decl.cc
gcc/testsuite/g++.dg/parse/constructor4.C [new file with mode: 0644]
gcc/testsuite/g++.dg/parse/constructor5.C [new file with mode: 0644]
gcc/testsuite/g++.dg/parse/conv_op2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/parse/default_to_int.C [new file with mode: 0644]
gcc/testsuite/g++.old-deja/g++.jason/operator.C