diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 537f29521fb7f..4a3c1bee82831 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -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 diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index d4f99c1fa16f6..b12085c6f6935 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -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())) { diff --git a/clang/test/CodeGenCXX/cxx1z-inline-variables.cpp b/clang/test/CodeGenCXX/cxx1z-inline-variables.cpp index 812e438f30c9a..1cb30b178e06f 100644 --- a/clang/test/CodeGenCXX/cxx1z-inline-variables.cpp +++ b/clang/test/CodeGenCXX/cxx1z-inline-variables.cpp @@ -1,5 +1,19 @@ // RUN: %clang_cc1 -std=c++1z %s -emit-llvm -o - -triple x86_64-linux-gnu | FileCheck %s +template struct InlineAuto { + template inline static auto var = 5; +}; +int inlineauto = InlineAuto::var; +// CHECK: @_ZN10InlineAutoIiE3varIiEE = {{.*}}i32 5{{.*}}comdat +// +template struct PartialInlineAuto { + template inline static auto var = 6; + template inline static auto var = 7; +}; + +int partialinlineauto = PartialInlineAuto::var; +// CHECK: @_ZN17PartialInlineAutoIiE3varIiiEE = {{.*}}i32 7{{.*}}comdat + struct Q { // CHECK: @_ZN1Q1kE = linkonce_odr constant i32 5, comdat static constexpr int k = 5; diff --git a/clang/test/SemaTemplate/cxx17-inline-variables.cpp b/clang/test/SemaTemplate/cxx17-inline-variables.cpp index 7fc0aa8eeeb0c..06f1ec17fb7a8 100644 --- a/clang/test/SemaTemplate/cxx17-inline-variables.cpp +++ b/clang/test/SemaTemplate/cxx17-inline-variables.cpp @@ -27,3 +27,15 @@ template constexpr int A::n = sizeof(A) + sizeof(T); template inline constexpr int A::m = sizeof(A) + sizeof(T); static_assert(A().f() == 5); static_assert(A().g() == 5); + +template struct InlineAuto { + template inline static auto var = 5; +}; + +template struct PartialInlineAuto { + template inline static auto var = 6; + template inline static auto var = 7; +}; + +int inlineauto = InlineAuto::var; +int partialinlineauto = PartialInlineAuto::var;