Skip to content

[[msvc::constexpr]] placement new is not supported outside of std namespace #74924

Closed
@RIscRIpt

Description

@RIscRIpt

Initially #71300 replicated semantics of [[msvc::constexpr]] from MSVC, so that it was possible to use [[msvc::constexpr]] return ::new from non-std namespace (constexpr placement new). E.g. https://godbolt.org/z/7eKh5Envz

// RUN: %clang_cc1 -fms-compatibility -fms-compatibility-version=19.33 -std=c++20 -ast-dump %s | FileCheck %s

// CHECK: used operator new
// CHECK: MSConstexprAttr 0x{{[0-9a-f]+}} <col:17, col:23>
[[nodiscard]] [[msvc::constexpr]] inline void* __cdecl operator new(decltype(sizeof(void*)), void* p) noexcept { return p; }

// CHECK: used constexpr construct_at
// CHECK: AttributedStmt 0x{{[0-9a-f]+}} <col:46, col:88>
// CHECK-NEXT: MSConstexprAttr 0x{{[0-9a-f]+}} <col:48, col:54>
// CHECK-NEXT: ReturnStmt 0x{{[0-9a-f]+}} <col:66, col:88>
constexpr int* construct_at(int* p, int v) { [[msvc::constexpr]] return ::new (p) int(v); }

constexpr bool check_construct_at() { int x; return *construct_at(&x, 42) == 42; }

static_assert(check_construct_at());

However, during the review a concern over changes in PointerExprEvaluator::VisitCXXNewExpr was raised:

   bool IsNothrow = false;
   bool IsPlacement = false;
+  bool IsMSConstexpr = Info.CurrentCall->CanEvalMSConstexpr &&
+                       OperatorNew->hasAttr<MSConstexprAttr>();
  if (OperatorNew->isReservedGlobalPlacementOperator() &&
-     Info.CurrentCall->isStdFunction() && !E->isArray()) {
+     (Info.CurrentCall->isStdFunction() || IsMSConstexpr) && !E->isArray()) {

Do we really need this change? Was our existing check of whether the caller is in namespace std not sufficient for MS' standard library? I'd strongly prefer not to have a documented, user-visible attribute that gives permission to use placement new directly.

I thought it was blocking the PR, so to speed up the review, I decided to move decision-making around this feature to this follow-up issue.

Originally posted by @RIscRIpt in #71300 (comment)

Metadata

Metadata

Assignees

Labels

clang:frontendLanguage frontend issues, e.g. anything involving "Sema"extension:microsoftgood first issuehttps://github.com/llvm/llvm-project/contribute

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions