[clang] [clang][bytecode] Diagnose placement-new construction to inactive field (PR #114047)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 29 06:08:48 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Timm Baeder (tbaederr)
<details>
<summary>Changes</summary>
We can reuse CheckActive() for this.
---
Full diff: https://github.com/llvm/llvm-project/pull/114047.diff
2 Files Affected:
- (modified) clang/lib/AST/ByteCode/Interp.cpp (+5)
- (modified) clang/test/AST/ByteCode/placement-new.cpp (+15-1)
``````````diff
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 6e45cfb7e8a20c..513d4512b45cff 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1451,6 +1451,11 @@ bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E,
<< StorageType << AllocType;
return false;
}
+
+ // Can't activate fields in a union, unless the direct base is the union.
+ if (Ptr.inUnion() && !Ptr.isActive() && !Ptr.getBase().getRecord()->isUnion())
+ return CheckActive(S, OpPC, Ptr, AK_Construct);
+
return true;
}
diff --git a/clang/test/AST/ByteCode/placement-new.cpp b/clang/test/AST/ByteCode/placement-new.cpp
index 5673b5cba3f700..56f54ff168f3e8 100644
--- a/clang/test/AST/ByteCode/placement-new.cpp
+++ b/clang/test/AST/ByteCode/placement-new.cpp
@@ -14,7 +14,9 @@ namespace std {
template<typename T, typename ...Args>
constexpr void construct_at(void *p, Args &&...args) {
new (p) T((Args&&)args...); // both-note {{in call to}} \
- // both-note {{placement new would change type of storage from 'int' to 'float'}}
+ // both-note {{placement new would change type of storage from 'int' to 'float'}} \
+ // both-note {{construction of subobject of member 'x' of union with active member 'a' is not allowed in a constant expression}}
+
}
}
@@ -284,6 +286,18 @@ namespace ConstructAt {
static_assert(bad_construct_at_type()); // both-error {{not an integral constant expression}} \
// both-note {{in call}}
+ constexpr bool bad_construct_at_subobject() {
+ struct X { int a, b; };
+ union A {
+ int a;
+ X x;
+ };
+ A a = {1};
+ std::construct_at<int>(&a.x.a, 1); // both-note {{in call}}
+ return true;
+ }
+ static_assert(bad_construct_at_subobject()); // both-error{{not an integral constant expression}} \
+ // both-note {{in call}}
}
namespace UsedToCrash {
``````````
</details>
https://github.com/llvm/llvm-project/pull/114047
More information about the cfe-commits
mailing list