-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[Clang] Implement P2747 constexpr placement new #104586
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
67201d1
[Clang] Implement P2747 constexpr placement new
cor3ntin 0042fbf
Do not make it an extension as there is no good way to introduce a w…
cor3ntin f8a15eb
remove commented code
cor3ntin 5c4225b
Address Aarons's feedback
cor3ntin ab02d7f
Address Aarons's remaining feedback
cor3ntin f0dbe73
cleanup test
cor3ntin 95a178e
Merge branch 'main' into corentin/P2747R2
cor3ntin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -23,3 +23,29 @@ struct S { | |||||
friend class C<Ts>::Nested...; // expected-error {{friend declaration expands pack 'Ts' that is declared it its own template parameter list}} | ||||||
}; | ||||||
} // namespace cwg2917 | ||||||
|
||||||
#if __cplusplus >= 202400L | ||||||
|
||||||
namespace std { | ||||||
using size_t = decltype(sizeof(0)); | ||||||
}; | ||||||
void *operator new(std::size_t, void *p) { return p; } | ||||||
void* operator new[] (std::size_t, void* p) {return p;} | ||||||
|
||||||
|
||||||
namespace cwg2922 { // cwg2922: 20 open 2024-07-10 | ||||||
union U { int a, b; }; | ||||||
constexpr U nondeterministic(bool i) { | ||||||
if(i) { | ||||||
U u; | ||||||
new (&u) int(); | ||||||
// expected-note@-1 {{placement new would change type of storage from 'U' to 'int'}} | ||||||
return u; | ||||||
} | ||||||
return {}; | ||||||
} | ||||||
constexpr U _ = nondeterministic(true); | ||||||
// expected-error@-1 {{constexpr variable '_' must be initialized by a constant expression}} \ | ||||||
// expected-note@-1 {{in call to 'nondeterministic(true)'}} | ||||||
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.
Suggested change
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. This is still outstanding, albeit minor. |
||||||
} | ||||||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// RUN: %clang_cc1 -std=c++2c -verify %s | ||
|
||
|
||
namespace std { | ||
using size_t = decltype(sizeof(0)); | ||
} | ||
|
||
void *operator new(std::size_t, void *p) { return p; } | ||
void* operator new[] (std::size_t, void* p) {return p;} | ||
|
||
|
||
consteval int ok() { | ||
int i; | ||
new (&i) int(0); | ||
new (&i) int[1]{1}; | ||
new (static_cast<void*>(&i)) int(0); | ||
return 0; | ||
} | ||
|
||
consteval int conversion() { | ||
int i; | ||
new (static_cast<void*>(&i)) float(0); | ||
// expected-note@-1 {{placement new would change type of storage from 'int' to 'float'}} | ||
return 0; | ||
} | ||
|
||
consteval int indeterminate() { | ||
int * indeterminate; | ||
new (indeterminate) int(0); | ||
// expected-note@-1 {{read of uninitialized object is not allowed in a constant expression}} | ||
return 0; | ||
} | ||
|
||
consteval int array1() { | ||
int i[2]; | ||
new (&i) int[]{1,2}; | ||
new (&i) int[]{1}; | ||
new (&i) int(0); | ||
new (static_cast<void*>(&i)) int[]{1,2}; | ||
new (static_cast<void*>(&i)) int[]{1}; | ||
return 0; | ||
} | ||
|
||
consteval int array2() { | ||
int i[1]; | ||
new (&i) int[2]; | ||
//expected-note@-1 {{placement new would change type of storage from 'int[1]' to 'int[2]'}} | ||
return 0; | ||
} | ||
|
||
struct S{ | ||
int* i; | ||
constexpr S() : i(new int(42)) {} // #no-deallocation | ||
constexpr ~S() {delete i;} | ||
}; | ||
|
||
consteval void alloc() { | ||
S* s = new S(); | ||
s->~S(); | ||
new (s) S(); | ||
delete s; | ||
} | ||
|
||
|
||
consteval void alloc_err() { | ||
S* s = new S(); | ||
new (s) S(); | ||
delete s; | ||
} | ||
|
||
AaronBallman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
int a = ok(); | ||
int b = conversion(); // expected-error {{call to consteval function 'conversion' is not a constant expression}} \ | ||
// expected-note {{in call to 'conversion()'}} | ||
int c = indeterminate(); // expected-error {{call to consteval function 'indeterminate' is not a constant expression}} \ | ||
// expected-note {{in call to 'indeterminate()'}} | ||
int d = array1(); | ||
int e = array2(); // expected-error {{call to consteval function 'array2' is not a constant expression}} \ | ||
// expected-note {{in call to 'array2()'}} | ||
int alloc1 = (alloc(), 0); | ||
int alloc2 = (alloc_err(), 0); // expected-error {{call to consteval function 'alloc_err' is not a constant expression}} | ||
// expected-note@#no-deallocation {{allocation performed here was not deallocated}} | ||
|
||
constexpr int *intptr() { | ||
return new int; | ||
} | ||
|
||
constexpr bool yay() { | ||
int *ptr = new (intptr()) int(42); | ||
bool ret = *ptr == 42; | ||
delete ptr; | ||
return ret; | ||
} | ||
static_assert(yay()); | ||
|
||
constexpr bool blah() { | ||
int *ptr = new (intptr()) int[3]{ 1, 2, 3 }; // expected-note {{placement new would change type of storage from 'int' to 'int[3]'}} | ||
AaronBallman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bool ret = ptr[0] == 1 && ptr[1] == 2 && ptr[2] == 3; | ||
delete [] ptr; | ||
return ret; | ||
} | ||
static_assert(blah()); // expected-error {{not an integral constant expression}} \ | ||
// expected-note {{in call to 'blah()'}} | ||
|
||
constexpr int *get_indeterminate() { | ||
int *evil; | ||
return evil; // expected-note {{read of uninitialized object is not allowed in a constant expression}} | ||
} | ||
|
||
constexpr bool bleh() { | ||
int *ptr = new (get_indeterminate()) int; // expected-note {{in call to 'get_indeterminate()'}} | ||
return true; | ||
} | ||
static_assert(bleh()); // expected-error {{not an integral constant expression}} \ | ||
// expected-note {{in call to 'bleh()'}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.