Skip to content

Conversation

@tbaederr
Copy link
Contributor

The follow-up diagnostic would otherwise be:

array.cpp:111:33: note: undefined constructor '(unnamed struct at array.cpp:111:11)' cannot be used in a constant expression array.cpp:111:11: note: declared here
111 | constexpr struct { Unknown U; } InvalidCtor;
| ^

... and complaining about the undefined constructor of a class that is invalid anyway doesn't make much sense.

The follow-up diagnostic would otherwise be:

array.cpp:111:33: note: undefined constructor '(unnamed struct at array.cpp:111:11)' cannot be used in a constant expression array.cpp:111:11: note: declared here
  111 | constexpr struct { Unknown U; } InvalidCtor;
      |           ^

... and complaining about the undefined constructor of a class that is invalid anyway doesn't make much sense.
@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 follow-up diagnostic would otherwise be:

array.cpp:111:33: note: undefined constructor '(unnamed struct at array.cpp:111:11)' cannot be used in a constant expression array.cpp:111:11: note: declared here
111 | constexpr struct { Unknown U; } InvalidCtor;
| ^

... and complaining about the undefined constructor of a class that is invalid anyway doesn't make much sense.


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

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Interp.cpp (+5)
  • (modified) clang/test/AST/ByteCode/records.cpp (+5)
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index c07690a3d941c..dfa59a50b2711 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -732,6 +732,11 @@ bool CheckCallable(InterpState &S, CodePtr OpPC, const Function *F) {
         DiagDecl = CD = Inherited;
     }
 
+    // Silently reject constructors of invalid classes. The invalid class
+    // has been rejected elsewhere before.
+    if (CD && CD->getParent()->isInvalidDecl())
+      return false;
+
     // FIXME: If DiagDecl is an implicitly-declared special member function
     // or an inheriting constructor, we should be much more explicit about why
     // it's not constexpr.
diff --git a/clang/test/AST/ByteCode/records.cpp b/clang/test/AST/ByteCode/records.cpp
index a13f30cd23119..608b94e55560a 100644
--- a/clang/test/AST/ByteCode/records.cpp
+++ b/clang/test/AST/ByteCode/records.cpp
@@ -1734,3 +1734,8 @@ namespace DeadUpcast {
   static_assert(foo(), "");
 }
 #endif
+
+namespace CtorOfInvalidClass {
+  constexpr struct { Unknown U; } InvalidCtor; // both-error {{unknown type name 'Unknown'}} \
+                                               // both-error {{must be initialized by a constant expression}}
+}

@tbaederr tbaederr merged commit 2c00b3b 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