Skip to content

[Clang] Do not defer variable template instantiation for undeduced types #141009

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 1 commit into from
May 22, 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 @@ -743,6 +743,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)
- Fixed a missed initializer instantiation bug for variable templates. (#GH134526), (#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
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20158,9 +20158,10 @@ static void DoMarkVarDeclReferenced(
Var->setTemplateSpecializationKind(TSK, PointOfInstantiation);
}

if (UsableInConstantExpr) {
if (UsableInConstantExpr || Var->getType()->isUndeducedType()) {
// Do not defer instantiations of variables that could be used in a
// constant expression.
// The type deduction also needs a complete initializer.
SemaRef.runWithSufficientStackSpace(PointOfInstantiation, [&] {
SemaRef.InstantiateVariableDefinition(PointOfInstantiation, Var);
});
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
44 changes: 43 additions & 1 deletion clang/test/SemaTemplate/cxx17-inline-variables.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %clang_cc1 -std=c++17 -verify %s
// expected-no-diagnostics

template<bool> struct DominatorTreeBase {
static constexpr bool IsPostDominator = true;
};
Expand Down Expand Up @@ -27,3 +27,45 @@ 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);

namespace GH135032 {

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 inline_auto = InlineAuto<int>::var<int>;
int partial_inline_auto = PartialInlineAuto<int>::var<int, int>;

}

namespace GH140773 {
template <class T> class ConstString { // #ConstString
ConstString(typename T::type) {} // #ConstString-Ctor
};

template <class = int>
struct Foo {
template <char>
static constexpr ConstString kFilename{[] { // #kFileName
return 42;
}};
};

// We don't want to instantiate the member template until it's used!
Foo<> foo;

auto X = Foo<>::kFilename<'a'>;
// expected-error@#kFileName {{no viable constructor}}
// expected-note@-2 {{in instantiation of static data member}}
// expected-note@#ConstString-Ctor {{candidate template ignored}}
// expected-note@#ConstString-Ctor {{implicit deduction guide}}
// expected-note@#ConstString {{candidate template ignored}}
// expected-note@#ConstString {{implicit deduction guide}}

}