Skip to content

[clang][bytecode] Fix union copy/move operator active check #132238

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
Mar 20, 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
4 changes: 3 additions & 1 deletion clang/lib/AST/ByteCode/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ Function::Function(Program &P, FunctionDeclTy Source, unsigned ArgSize,
Kind = FunctionKind::Dtor;
} else if (const auto *MD = dyn_cast<CXXMethodDecl>(F)) {
Virtual = MD->isVirtual();
if (IsLambdaStaticInvoker) // MD->isLambdaStaticInvoker())
if (IsLambdaStaticInvoker)
Kind = FunctionKind::LambdaStaticInvoker;
else if (clang::isLambdaCallOperator(F))
Kind = FunctionKind::LambdaCallOperator;
else if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())
Kind = FunctionKind::CopyOrMoveOperator;
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/AST/ByteCode/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class Function final {
Dtor,
LambdaStaticInvoker,
LambdaCallOperator,
CopyOrMoveOperator,
};
using ParamDescriptor = std::pair<PrimType, Descriptor *>;

Expand Down Expand Up @@ -159,6 +160,10 @@ class Function final {
bool isConstructor() const { return Kind == FunctionKind::Ctor; }
/// Checks if the function is a destructor.
bool isDestructor() const { return Kind == FunctionKind::Dtor; }
/// Checks if the function is copy or move operator.
bool isCopyOrMoveOperator() const {
return Kind == FunctionKind::CopyOrMoveOperator;
}

/// Returns whether this function is a lambda static invoker,
/// which we generate custom byte code for.
Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/ByteCode/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1449,6 +1449,7 @@ bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
if (!CheckInvoke(S, OpPC, ThisPtr))
return cleanup();
if (!Func->isConstructor() && !Func->isDestructor() &&
!Func->isCopyOrMoveOperator() &&
!CheckActive(S, OpPC, ThisPtr, AK_MemberCall))
return false;
}
Expand Down
29 changes: 29 additions & 0 deletions clang/test/AST/ByteCode/unions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,4 +570,33 @@ namespace ActiveDestroy {
static_assert(foo2());
}

namespace MoveOrAssignOp {
struct min_pointer {
int *ptr_;
constexpr min_pointer(int *p) : ptr_(p) {}
min_pointer() = default;
};

class F {
struct __long {
min_pointer __data_;
};
union __rep {
int __s;
__long __l;
} __rep_;

public:
constexpr F() {
__rep_ = __rep();
__rep_.__l.__data_ = nullptr;
}
};

constexpr bool foo() {
F f{};
return true;
}
static_assert(foo());
}
#endif