Skip to content

Conversation

@tbaederr
Copy link
Contributor

@tbaederr tbaederr commented Oct 24, 2025

Don't use hasBody(), which checks all declarations.

Fixes #164995

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:bytecode Issues for the clang bytecode constexpr interpreter labels Oct 24, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 24, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

But don't have one, yet. That happens for class methods, which are "defined" but have no body, hence they willHaveBody.

Fixes #164995


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

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Interp.cpp (+1-1)
  • (modified) clang/test/AST/ByteCode/records.cpp (+9)
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index a72282caf5e73..40e66153a5b11 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -997,7 +997,7 @@ static bool CheckCallable(InterpState &S, CodePtr OpPC, const Function *F) {
       // If the declaration is defined, declared 'constexpr' _and_ has a body,
       // the below diagnostic doesn't add anything useful.
       if (DiagDecl->isDefined() && DiagDecl->isConstexpr() &&
-          DiagDecl->hasBody())
+          (DiagDecl->hasBody() || DiagDecl->willHaveBody()))
         return false;
 
       S.FFDiag(S.Current->getLocation(OpPC),
diff --git a/clang/test/AST/ByteCode/records.cpp b/clang/test/AST/ByteCode/records.cpp
index 00218ba02bb31..02dd26fa15394 100644
--- a/clang/test/AST/ByteCode/records.cpp
+++ b/clang/test/AST/ByteCode/records.cpp
@@ -1861,3 +1861,12 @@ namespace PrimitiveInitializedByInitList {
   } c{ 17 };
   static_assert(c.b == 17, "");
 }
+
+namespace MethodWillHaveBody {
+  class A {
+  public:
+    static constexpr int get_value2() { return 1 + get_value(); }
+    static constexpr int get_value() { return 1; }
+  };
+  static_assert(A::get_value2() == 2, "");
+}

@tbaederr
Copy link
Contributor Author

This breaks SemaTemplate/instantiate-self.cpp, in particular

  template<typename T> constexpr T f(T);
  template<typename T> constexpr T g(T t) {
    // FIXME: It'd be nice to say that the function is currently being defined, rather than being undefined.
    typedef int arr[f(T())]; // expected-error {{variable length array}} expected-note {{undefined function 'f<int>'}}
    return t;
  }
  template<typename T> constexpr T f(T t) { // expected-note {{declared here}}
    typedef int arr[g(T())]; // expected-error {{zero size array}} expected-note {{instantiation of}}
    return t;
  }
  int n = f(0); // expected-note 2{{instantiation of}}

@tbaederr tbaederr changed the title [clang][bytecode] Don't diagnose defined functions that will have a body [clang][bytecode] Don't diagnose defined that have a body Oct 24, 2025
@tbaederr tbaederr changed the title [clang][bytecode] Don't diagnose defined that have a body [clang][bytecode] Don't diagnose defined functions that will have a body Oct 24, 2025
But don't have one, yet. That happens for class methods, which are
"defined" but have no body, hence they willHaveBody.

Fixes llvm#164995
@tbaederr tbaederr merged commit f29235d into llvm:main Oct 27, 2025
10 checks passed
dvbuka pushed a commit to dvbuka/llvm-project that referenced this pull request Oct 27, 2025
…ody (llvm#165002)

Don't use `hasBody()`, which checks all declarations.

Fixes llvm#164995
Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Oct 29, 2025
…ody (llvm#165002)

Don't use `hasBody()`, which checks all declarations.

Fixes llvm#164995
aokblast pushed a commit to aokblast/llvm-project that referenced this pull request Oct 30, 2025
…ody (llvm#165002)

Don't use `hasBody()`, which checks all declarations.

Fixes llvm#164995
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:bytecode Issues for the clang bytecode constexpr interpreter 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.

[clang][bytecode] Constexpr function never produces a constant expression

3 participants