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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------------------------

Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14608,6 +14608,10 @@ void Sema::CheckCompleteVariableDeclaration(VarDecl *var) {
std::optional<bool> CacheHasConstInit;
const Expr *CacheCulprit = nullptr;
auto checkConstInit = [&]() mutable {
const Expr *Init = var->getInit();
if (Init->isInstantiationDependent())
return true;

if (!CacheHasConstInit)
CacheHasConstInit = var->getInit()->isConstantInitializer(
Context, var->getType()->isReferenceType(), &CacheCulprit);
Expand Down
30 changes: 30 additions & 0 deletions clang/test/SemaCXX/thread-specifier.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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}}
}

template <typename T>
void nondependent_var() {
// Verify that the dependence of the initializer is what really matters.
static __thread int my_wrapper = T{};
}

struct S {
S() {}
};

void f() {
instantiated<int>();
instantiated<S>(); // expected-note {{in instantiation of function template specialization 'GH140509::instantiated<GH140509::S>' requested here}}
nondependent_var<int>();
}
} // namespace GH140509