Skip to content

[clang][bytecode] Fix returne value of array CXXNewExprs #127526

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 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clang/lib/AST/ByteCode/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,8 @@ bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm,
return false;
}

if (!Ptr.isRoot() || Ptr.isOnePastEnd() || Ptr.isArrayElement()) {
if (!Ptr.isRoot() || Ptr.isOnePastEnd() ||
(Ptr.isArrayElement() && Ptr.getIndex() != 0)) {
const SourceInfo &Loc = S.Current->getSource(OpPC);
S.FFDiag(Loc, diag::note_constexpr_delete_subobject)
<< Ptr.toDiagnosticString(S.getASTContext()) << Ptr.isOnePastEnd();
Expand Down
13 changes: 11 additions & 2 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2915,13 +2915,17 @@ inline bool AllocN(InterpState &S, CodePtr OpPC, PrimType T, const Expr *Source,
S.Stk.push<Pointer>(0, nullptr);
return true;
}
assert(NumElements.isPositive());

DynamicAllocator &Allocator = S.getAllocator();
Block *B =
Allocator.allocate(Source, T, static_cast<size_t>(NumElements),
S.Ctx.getEvalID(), DynamicAllocator::Form::Array);
assert(B);
S.Stk.push<Pointer>(B);
if (NumElements.isZero())
S.Stk.push<Pointer>(B);
else
S.Stk.push<Pointer>(Pointer(B).atIndex(0));
return true;
}

Expand All @@ -2941,13 +2945,18 @@ inline bool AllocCN(InterpState &S, CodePtr OpPC, const Descriptor *ElementDesc,
S.Stk.push<Pointer>(0, ElementDesc);
return true;
}
assert(NumElements.isPositive());

DynamicAllocator &Allocator = S.getAllocator();
Block *B =
Allocator.allocate(ElementDesc, static_cast<size_t>(NumElements),
S.Ctx.getEvalID(), DynamicAllocator::Form::Array);
assert(B);
S.Stk.push<Pointer>(B);
if (NumElements.isZero())
S.Stk.push<Pointer>(B);
else
S.Stk.push<Pointer>(Pointer(B).atIndex(0));

return true;
}

Expand Down
14 changes: 14 additions & 0 deletions clang/test/AST/ByteCode/new-delete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,20 @@ namespace NonConstexprArrayCtor {
// both-note {{in call to}}
}

namespace ArrayBaseCast {
struct A {};
struct B : A {};
constexpr bool test() {
B *b = new B[2];

A* a = b;

delete[] b;
return true;
}
static_assert(test());
}

#else
/// Make sure we reject this prior to C++20
constexpr int a() { // both-error {{never produces a constant expression}}
Expand Down