Skip to content

Commit a432746

Browse files
committed
[clang][bytecode] Loosen assertion This() for array elements
getRecord() returns null on array elements, even for composite arrays. The assertion here was overly restrictive and having an array element as instance pointer should be fine otherwise.
1 parent c6b9d5c commit a432746

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

clang/lib/AST/ByteCode/Interp.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2431,9 +2431,12 @@ inline bool This(InterpState &S, CodePtr OpPC) {
24312431
// Ensure the This pointer has been cast to the correct base.
24322432
if (!This.isDummy()) {
24332433
assert(isa<CXXMethodDecl>(S.Current->getFunction()->getDecl()));
2434-
assert(This.getRecord());
2434+
[[maybe_unused]] const Record *R = This.getRecord();
2435+
if (!R)
2436+
R = This.narrow().getRecord();
2437+
assert(R);
24352438
assert(
2436-
This.getRecord()->getDecl() ==
2439+
R->getDecl() ==
24372440
cast<CXXMethodDecl>(S.Current->getFunction()->getDecl())->getParent());
24382441
}
24392442

clang/test/AST/ByteCode/placement-new.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,30 @@ namespace PR48606 {
339339
static_assert(f());
340340
}
341341

342+
/// This used to crash because of an assertion in the implementation
343+
/// of the This instruction.
344+
namespace ExplicitThisOnArrayElement {
345+
struct S {
346+
int a = 12;
347+
constexpr S(int a) {
348+
this->a = a;
349+
}
350+
};
351+
352+
template <class _Tp, class... _Args>
353+
constexpr void construct_at(_Tp *__location, _Args &&...__args) {
354+
new (__location) _Tp(__args...);
355+
}
356+
357+
constexpr bool foo() {
358+
auto *M = std::allocator<S>().allocate(13); // both-note {{allocation performed here was not deallocated}}
359+
construct_at(M, 12);
360+
return true;
361+
}
362+
363+
static_assert(foo()); // both-error {{not an integral constant expression}}
364+
}
365+
342366
#ifdef BYTECODE
343367
constexpr int N = [] // expected-error {{must be initialized by a constant expression}} \
344368
// expected-note {{assignment to dereferenced one-past-the-end pointer is not allowed in a constant expression}} \

0 commit comments

Comments
 (0)