Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang] Dynamic initialization of an array of objects with a private destructor is rejected by clang #121319

Open
Rush10233 opened this issue Dec 30, 2024 · 5 comments
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" diverges-from:edg Does the clang frontend diverge from edg compiler diverges-from:gcc Does the clang frontend diverge from gcc on this issue diverges-from:msvc Does the clang frontend diverge from msvc on this issue

Comments

@Rush10233
Copy link

Very surprised to see that:

class S {
  ~S()=default;
};


int main(){
    S *ss = new S[100];
}

This is rejected by clang:

<source>:7:17: error: temporary of type 'S' has private destructor
    7 |     S *ss = new S[100];
      |                 ^
<source>:2:3: note: implicitly declared private here
    2 |   ~S()=default;
      |   ^

I wonder if I have missed some instructions on the standard, but it appears that other compilers including GCC, MSVC and EDG can normally accept it.

https://godbolt.org/z/9d6Y5b3Mr

@github-actions github-actions bot added the clang Clang issues not falling into any other category label Dec 30, 2024
@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" diverges-from:gcc Does the clang frontend diverge from gcc on this issue diverges-from:msvc Does the clang frontend diverge from msvc on this issue diverges-from:edg Does the clang frontend diverge from edg compiler and removed clang Clang issues not falling into any other category labels Dec 30, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 30, 2024

@llvm/issue-subscribers-clang-frontend

Author: None (Rush10233)

Very surprised to see that:
class S {
  ~S()=default;
};


int main(){
    S *ss = new S[100];
}

This is rejected by clang:

&lt;source&gt;:7:17: error: temporary of type 'S' has private destructor
    7 |     S *ss = new S[100];
      |                 ^
&lt;source&gt;:2:3: note: implicitly declared private here
    2 |   ~S()=default;
      |   ^

I wonder if I have missed some instructions on the standard, but it appears that other compilers including GCC, MSVC and EDG can normally accept it.

https://godbolt.org/z/9d6Y5b3Mr

@cor3ntin
Copy link
Contributor

I think Clang is correct

https://eel.is/c++draft/expr.new#26

If the new-expression creates an array of objects of class type, the destructor is potentially invoked ([class.dtor]).

https://eel.is/c++draft/class.dtor#14.sentence-7

A program is ill-formed if a destructor that is potentially invoked is deleted or not accessible from the context of the invocation.

The fact that we diverge from everyone else gives me pause though.
GCC produce the error message I would expect with a private non-defaulted destructor https://godbolt.org/z/9zxs8srKf

@shafik
Copy link
Collaborator

shafik commented Dec 30, 2024

Looks similar to: #47370

@shafik
Copy link
Collaborator

shafik commented Dec 30, 2024

Also see cwg2426 which covers a similar situation for return statements.

Worth noting gcc/clang agree that the code is ill-formed, if it is not defaulted: https://godbolt.org/z/GW55zs6fW

class S {
  ~S() ;
};


int main(){
    S *ss = new S[100];
}

@zygoloid
Copy link
Collaborator

I'd guess the other compilers are incorrectly skipping the check when the type is trivially destructible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" diverges-from:edg Does the clang frontend diverge from edg compiler diverges-from:gcc Does the clang frontend diverge from gcc on this issue diverges-from:msvc Does the clang frontend diverge from msvc on this issue
Projects
None yet
Development

No branches or pull requests

6 participants