Skip to content

[Clang] Fix missed initializer instantiation bug for variable templates #138122

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 18, 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
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ Bug Fixes to C++ Support
in a ``constexpr`` function. (#GH131432)
- Fixed an incorrect TreeTransform for calls to ``consteval`` functions if a conversion template is present. (#GH137885)
- Clang now emits a warning when class template argument deduction for alias templates is used in C++17. (#GH133806)
- Fix missed initializer instantiation bug for variable templates. (#GH138122)
- Fix a crash when checking the template template parameters of a dependent lambda appearing in an alias declaration.
(#GH136432), (#GH137014), (#GH138018)
- Fixed an assertion when trying to constant-fold various builtins when the argument
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6032,11 +6032,11 @@ void Sema::BuildVariableInstantiation(
Context.setStaticLocalNumber(NewVar, Context.getStaticLocalNumber(OldVar));

// Figure out whether to eagerly instantiate the initializer.
if (InstantiatingVarTemplate || InstantiatingVarTemplatePartialSpec) {
// We're producing a template. Don't instantiate the initializer yet.
} else if (NewVar->getType()->isUndeducedType()) {
if (NewVar->getType()->isUndeducedType()) {
// We need the type to complete the declaration of the variable.
InstantiateVariableInitializer(NewVar, OldVar, TemplateArgs);
} else if (InstantiatingVarTemplate || InstantiatingVarTemplatePartialSpec) {
// We're producing a template. Don't instantiate the initializer yet.
} else if (InstantiatingSpecFromTemplate ||
(OldVar->isInline() && OldVar->isThisDeclarationADefinition() &&
!NewVar->isThisDeclarationADefinition())) {
Expand Down
14 changes: 14 additions & 0 deletions clang/test/CodeGenCXX/cxx1z-inline-variables.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
// RUN: %clang_cc1 -std=c++1z %s -emit-llvm -o - -triple x86_64-linux-gnu | FileCheck %s

template <typename T> struct InlineAuto {
template <typename G> inline static auto var = 5;
};
int inlineauto = InlineAuto<int>::var<int>;
// CHECK: @_ZN10InlineAutoIiE3varIiEE = {{.*}}i32 5{{.*}}comdat
//
template <typename> struct PartialInlineAuto {
template <typename, typename> inline static auto var = 6;
template <typename T> inline static auto var<int, T> = 7;
};

int partialinlineauto = PartialInlineAuto<int>::var<int, int>;
// CHECK: @_ZN17PartialInlineAutoIiE3varIiiEE = {{.*}}i32 7{{.*}}comdat

struct Q {
// CHECK: @_ZN1Q1kE = linkonce_odr constant i32 5, comdat
static constexpr int k = 5;
Expand Down
12 changes: 12 additions & 0 deletions clang/test/SemaTemplate/cxx17-inline-variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ template <typename T> constexpr int A<T>::n = sizeof(A) + sizeof(T);
template <typename T> inline constexpr int A<T>::m = sizeof(A) + sizeof(T);
static_assert(A<int>().f() == 5);
static_assert(A<int>().g() == 5);

template <typename T> struct InlineAuto {
template <typename G> inline static auto var = 5;
};

template <typename> struct PartialInlineAuto {
template <typename, typename> inline static auto var = 6;
template <typename T> inline static auto var<int, T> = 7;
};

int inlineauto = InlineAuto<int>::var<int>;
int partialinlineauto = PartialInlineAuto<int>::var<int, int>;
Loading