[clang] [clang][bytecode] Always reject ctors of invalid parent decls (PR #128295)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 21 23:42:08 PST 2025


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/128295

The copy constructor of an invalid declaration might still be perfectly valid, but we still need to reject it.

>From 0dbe55f8a54048ea381ce39e641921c2a923475f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sat, 22 Feb 2025 08:40:29 +0100
Subject: [PATCH] [clang][bytecode] Always reject ctors of invalid parent decls

The copy constructor of an invalid declaration might still be perfectly
valid, but we still need to reject it.
---
 clang/lib/AST/ByteCode/Interp.cpp   | 4 +++-
 clang/test/AST/ByteCode/records.cpp | 8 ++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index dfa59a50b2711..e383828cd676d 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1238,8 +1238,10 @@ static bool checkConstructor(InterpState &S, CodePtr OpPC, const Function *Func,
                              const Pointer &ThisPtr) {
   assert(Func->isConstructor());
 
-  const Descriptor *D = ThisPtr.getFieldDesc();
+  if (Func->getParentDecl()->isInvalidDecl())
+    return false;
 
+  const Descriptor *D = ThisPtr.getFieldDesc();
   // FIXME: I think this case is not 100% correct. E.g. a pointer into a
   // subobject of a composite array.
   if (!D->ElemRecord)
diff --git a/clang/test/AST/ByteCode/records.cpp b/clang/test/AST/ByteCode/records.cpp
index 608b94e55560a..cb3d6111fd2bf 100644
--- a/clang/test/AST/ByteCode/records.cpp
+++ b/clang/test/AST/ByteCode/records.cpp
@@ -1738,4 +1738,12 @@ namespace DeadUpcast {
 namespace CtorOfInvalidClass {
   constexpr struct { Unknown U; } InvalidCtor; // both-error {{unknown type name 'Unknown'}} \
                                                // both-error {{must be initialized by a constant expression}}
+
+#if __cplusplus >= 202002L
+  template <typename T, auto Q>
+  concept ReferenceOf = Q;
+  /// This calls a valid and constexpr copy constructor of InvalidCtor, 
+  /// but should still be rejected.
+  template<ReferenceOf<InvalidCtor> auto R, typename Rep> int F; // both-error {{non-type template argument is not a constant expression}}
+#endif
 }



More information about the cfe-commits mailing list