llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Clang] Accepts invalid constructor declaration with an asterisk

Open MagentaTreehouse opened this issue 1 year ago • 4 comments

struct A {
    *A();
};

See Compiler Explorer: https://compiler-explorer.com/z/ccf6rqenE

MagentaTreehouse avatar Jan 05 '25 19:01 MagentaTreehouse

Just FYI, GCC has a similar bug and was filed as https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118306 (there was also a recent regression with an ICE when defaulting the constructor which the reporter here reported to GCC as https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118304 ).

pinskia avatar Jan 05 '25 23:01 pinskia

@llvm/issue-subscribers-clang-frontend

Author: None (MagentaTreehouse)

```c++ struct A { *A(); }; ``` See Compiler Explorer: https://compiler-explorer.com/z/ccf6rqenE

llvmbot avatar Jan 06 '25 00:01 llvmbot

Fun to note it accepts any number of asterisks: https://compiler-explorer.com/z/naaP75err

struct A {
    ******************A();
};

we can bring the whole gang along: https://compiler-explorer.com/z/TY8aq8G9W

struct A {
    ****A();
    ***~A();
    **A(const A&);
    *A(A&&);
};

goes back to clang-2.6: https://compiler-explorer.com/z/zWv4b45s7

shafik avatar Jan 07 '25 04:01 shafik

To generalize further, Clang seems to allow any of the ptr-operators in the ptr-declarator for constructors and destructors.

Consider also these accepts-invalid cases: https://compiler-explorer.com/z/qdbdsjbWh

struct A {
    *&A();
};

struct B {
    *&&B();
};

struct C {
    *const C();
};

struct D {
    *const *D();
};

struct E {
    *E::*E();
};

struct F {
    *F::*const F();
};

Only to fail when there is a & in the front:

struct G {
    &G();
};

Output:

<source>:2:5: error: cannot form a reference to 'void'
    2 |     &G();
      |     ^

MagentaTreehouse avatar Jan 12 '25 04:01 MagentaTreehouse