-
Notifications
You must be signed in to change notification settings - Fork 14.5k
One more fix for P3144R2 implementation #149406
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
94e652b
a2123f9
69a65b2
2f751bc
6c21226
eff9ccb
268265f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8392,17 +8392,19 @@ def ext_default_init_const : ExtWarn< | |
"is a Microsoft extension">, | ||
InGroup<MicrosoftConstInit>; | ||
def err_delete_operand : Error<"cannot delete expression of type %0">; | ||
def err_delete_void_ptr_operand : Error< | ||
"cannot delete expression with pointer-to-'void' type %0">; | ||
def ext_delete_void_ptr_operand : ExtWarn< | ||
"cannot delete expression with pointer-to-'void' type %0">, | ||
InGroup<DeleteIncomplete>; | ||
def err_ambiguous_delete_operand : Error< | ||
"ambiguous conversion of delete expression of type %0 to a pointer">; | ||
def warn_delete_incomplete : Warning< | ||
"deleting pointer to incomplete type %0 is incompatible with C++2c" | ||
"deleting pointer to incomplete %select{struct|union}0 %1 is incompatible with C++2c" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure there is value in distinguishing unions here; unions are class types. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A diagnostic referring to a union But I see in yet other places that it's making sure to use either There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In C++, types introduced by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not going to add diagnostics that are technically correct but will confuse users. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
My reason for wanting to change the diagnostic is that the diagnostic is telling users that it is an error to delete a pointer to an incomplete type when that is not the case. That said, I'd be okay with taking the diagnostic changes out of this PR and leaving this as just the fix for the rejects-valid. We can figure out whether we can come up with improved wording in a separate PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, with this update, the diagnostics are exactly as they were before. I've kept the other updates to the tests, but adjusted the message that is being checked for. If this looks okay we can just merge this. I can open a new issue or PR for the diagnostic later, but since that has proved contentious I'll make sure to write in far more detail about the different options and their advantages and disadvantages. |
||
" and may cause undefined behavior">, | ||
InGroup<DeleteIncomplete>; | ||
def err_delete_incomplete : Error< | ||
"cannot delete pointer to incomplete type %0">; | ||
"cannot delete pointer to incomplete %select{struct|union}0 %1">; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
def err_delete_incomplete_class_type : Error< | ||
"deleting incomplete class type %0; no conversions to pointer type">; | ||
def err_delete_explicit_conversion : Error< | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -225,10 +225,10 @@ void bad_deletes() | |
delete [0] (int*)0; // expected-error {{expected variable name or 'this' in lambda capture list}} | ||
delete (void*)0; | ||
// cxx98-23-warning@-1 {{cannot delete expression with pointer-to-'void' type 'void *'}} | ||
// since-cxx26-error@-2 {{cannot delete pointer to incomplete type 'void'}} | ||
// since-cxx26-error@-2 {{cannot delete expression with pointer-to-'void' type 'void *'}} | ||
delete (T*)0; | ||
// cxx98-23-warning@-1 {{deleting pointer to incomplete type}} | ||
// since-cxx26-error@-2 {{cannot delete pointer to incomplete type 'T'}} | ||
// cxx98-23-warning@-1 {{deleting pointer to incomplete struct 'T'}} | ||
// since-cxx26-error@-2 {{cannot delete pointer to incomplete struct 'T'}} | ||
::S::delete (int*)0; // expected-error {{expected unqualified-id}} | ||
} | ||
|
||
|
@@ -570,8 +570,8 @@ namespace DeleteIncompleteClassPointerError { | |
struct A; // expected-note {{forward declaration}} | ||
void f(A *x) { 1+delete x; } | ||
// expected-error@-1 {{invalid operands to binary expression}} | ||
// cxx98-23-warning@-2 {{deleting pointer to incomplete type}} | ||
// since-cxx26-error@-3 {{cannot delete pointer to incomplete type 'A'}} | ||
// cxx98-23-warning@-2 {{deleting pointer to incomplete struct 'A'}} | ||
// since-cxx26-error@-3 {{cannot delete pointer to incomplete struct 'A'}} | ||
} | ||
|
||
namespace PR10504 { | ||
|
@@ -595,6 +595,10 @@ struct GH99278_2 { | |
} f; | ||
}; | ||
GH99278_2<void> e; | ||
void GH99278_3(int(*p)[]) { | ||
delete p; | ||
// expected-warning@-1 {{'delete' applied to a pointer-to-array type 'int (*)[]' treated as 'delete[]'}} | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe also test deleting an incomplete array of a complete class type? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, that made me think, what about a pointer to an array of incomplete class type? If that's done through Comparing to what GCC does, GCC doesn't accept this example (even with The example does not appear to violate any rule (at least when the pointer is a null pointer) and clearly was not intended to be banned by P3144R2, yet at the same time I am getting the feeling it never should have been allowed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What EDG does (thanks Compiler Explorer for having that available) is allow it for arrays of complete class type, and disallow it for arrays of incomplete class type. That seems like a sensible way of going about it, it effectively interprets "If the object being deleted has incomplete class type at the point of deletion, the program is ill-formed" as applying also to subobjects of the object being deleted. Which is not what the standard says, but necessary for what P3144R2 aimed to accomplish, so I'll see if I can implement that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That turned out to be a trivial one-line change, so I've done it and added tests for it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The general issue here is that there's a hole in the standard: a non-array new-expression can't return a pointer to an array, so [expr.delete]p2 effectively says it's always undefined behavior to delete a pointer to an array (unless it's null). Existing compilers treat this as if you wrote Probably we should ask the committee to address this. That said, given the current state of things, this patch seems fine. On a sort of related note, the following currently crashes in codegen:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
True for the tests that I added with struct S { ~S(); };
void del(S(*p)[]) { delete[] p; }
void test() { del(new S[4][4]); } The checks that I implemented in this PR handle this, they allow it in that test, but reject it if The codegen crash is worrying, the test I'm showing here shows that this can occur in legitimate code. I'll have a look at that when I have some extra time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've changed the array tests to use |
||
#endif | ||
|
||
struct PlacementArg {}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be more consistent to say type 'void*'
There are just a couple of diagnostics mentioning "pointer to void" - but we never say pointer-to-void, let alone "pointer-to-'void' - that way it can be merged with
err_delete_incomplete ie
{|class}or
{|class|union}` (see below)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This text is literally copied and pasted from what it says directly below for the pre-C++26 case where it is a warning rather than an error, it's not true that we never say pointer-to-void.