Skip to content

Commit b26adac

Browse files
authored
[clang][bytecode] Check destructors for temporaries (#140039)
Also, increase the EvalID in isPotentialConstantExpr(), since this is its own evaluation.
1 parent 5f53ca3 commit b26adac

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

clang/lib/AST/ByteCode/Context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ bool Context::isPotentialConstantExpr(State &Parent, const FunctionDecl *FD) {
3737
Compiler<ByteCodeEmitter>(*this, *P).compileFunc(
3838
FD, const_cast<Function *>(Func));
3939

40+
++EvalID;
4041
// And run it.
4142
if (!Run(Parent, Func))
4243
return false;

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,8 @@ static bool checkConstructor(InterpState &S, CodePtr OpPC, const Function *Func,
13731373
bool CheckDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
13741374
if (!CheckLive(S, OpPC, Ptr, AK_Destroy))
13751375
return false;
1376+
if (!CheckTemporary(S, OpPC, Ptr, AK_Destroy))
1377+
return false;
13761378

13771379
// Can't call a dtor on a global variable.
13781380
if (Ptr.block()->isStatic()) {

clang/test/AST/ByteCode/cxx20.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,3 +997,13 @@ namespace NastyChar {
997997
template <ToNastyChar t> constexpr auto to_nasty_char() { return t; }
998998
constexpr auto result = to_nasty_char<"12345">();
999999
}
1000+
1001+
namespace TempDtor {
1002+
struct A {
1003+
int n;
1004+
};
1005+
constexpr A &&a_ref = A(); // both-note {{temporary created here}}
1006+
constexpr void destroy_extern_2() { // both-error {{never produces a constant expression}}
1007+
a_ref.~A(); // both-note {{destruction of temporary is not allowed in a constant expression outside the expression that created the temporary}}
1008+
}
1009+
}

0 commit comments

Comments
 (0)