Skip to content

[C++] Fix a crash with __thread and dependent types #140542

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 2 commits into from
May 19, 2025

Conversation

AaronBallman
Copy link
Collaborator

We were checking whether the initializer is a valid constant expression even if the variable was dependent. Now we delay that checking until after the template has been instantiated.

Fixes #140509

We were checking whether the initializer is a valid constant expression
even if the variable was dependent. Now we delay that checking until
after the template has been instantiated.

Fixes llvm#140509
@AaronBallman AaronBallman added clang Clang issues not falling into any other category c++ clang:frontend Language frontend issues, e.g. anything involving "Sema" crash-on-valid labels May 19, 2025
@llvmbot
Copy link
Member

llvmbot commented May 19, 2025

@llvm/pr-subscribers-clang

Author: Aaron Ballman (AaronBallman)

Changes

We were checking whether the initializer is a valid constant expression even if the variable was dependent. Now we delay that checking until after the template has been instantiated.

Fixes #140509


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+3)
  • (modified) clang/lib/Sema/SemaDecl.cpp (+2-1)
  • (added) clang/test/SemaCXX/thread-specifier.cpp (+24)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4a3c1bee82831..472b70b46fcc0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -558,6 +558,9 @@ Improvements to Clang's diagnostics
   between different Unicode character types (``char8_t``, ``char16_t``, ``char32_t``).
   This warning only triggers in C++ as these types are aliases in C. (#GH138526)
 
+- Fixed a crash when checking a ``__thread``-specified variable declaration
+  with a dependent type in C++. (#GH140509)
+
 Improvements to Clang's time-trace
 ----------------------------------
 
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 6dae243b520f0..3f52d1b1a65cb 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -14622,7 +14622,8 @@ void Sema::CheckCompleteVariableDeclaration(VarDecl *var) {
       Diag(var->getLocation(), diag::err_thread_nontrivial_dtor);
       if (getLangOpts().CPlusPlus11)
         Diag(var->getLocation(), diag::note_use_thread_local);
-    } else if (getLangOpts().CPlusPlus && var->hasInit()) {
+    } else if (getLangOpts().CPlusPlus && var->hasInit() &&
+               !var->getType()->isDependentType()) {
       if (!checkConstInit()) {
         // GNU C++98 edits for __thread, [basic.start.init]p4:
         //   An object of thread storage duration shall not require dynamic
diff --git a/clang/test/SemaCXX/thread-specifier.cpp b/clang/test/SemaCXX/thread-specifier.cpp
new file mode 100644
index 0000000000000..8e245987063ff
--- /dev/null
+++ b/clang/test/SemaCXX/thread-specifier.cpp
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c++23 -verify %s
+
+namespace GH140509 {
+template <typename T>
+void not_instantiated() {
+  static __thread T my_wrapper;
+}
+
+template <typename T>
+void instantiated() {
+  static __thread T my_wrapper = T{}; // expected-error {{initializer for thread-local variable must be a constant expression}} \
+                                         expected-note {{use 'thread_local' to allow this}}
+}
+
+struct S {
+  S() {}
+};
+
+void f() {
+  instantiated<int>();
+  instantiated<S>(); // expected-note {{in instantiation of function template specialization 'GH140509::instantiated<GH140509::S>' requested here}}
+}
+} // namespace GH140509
+

@AaronBallman AaronBallman merged commit 7cf2860 into llvm:main May 19, 2025
12 checks passed
@AaronBallman AaronBallman deleted the aballman-gh140509 branch May 19, 2025 15:28
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Jun 3, 2025
We were checking whether the initializer is a valid constant expression
even if the variable was dependent. Now we delay that checking until
after the template has been instantiated.

Fixes llvm#140509
ajaden-codes pushed a commit to Jaddyen/llvm-project that referenced this pull request Jun 6, 2025
We were checking whether the initializer is a valid constant expression
even if the variable was dependent. Now we delay that checking until
after the template has been instantiated.

Fixes llvm#140509
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category crash-on-valid
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clang crashes with ICE on dependent expression in __thread static initializer since clang 9.0.0
3 participants