Skip to content

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

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 22, 2025

Conversation

tbaederr
Copy link
Contributor

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

The copy constructor of an invalid declaration might still be perfectly
valid, but we still need to reject it.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 22, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 22, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/128295.diff

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Interp.cpp (+3-1)
  • (modified) clang/test/AST/ByteCode/records.cpp (+8)
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
 }

@tbaederr tbaederr merged commit 6db96c9 into llvm:main Feb 22, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants