llvm-project/clang/test/CodeGenCXX/local-class-instantiation.cpp
Younan Zhang d1a80deae6
Reapply "[Clang] Fix dependent local class instantiation bugs" (#135914)
This reapplies #134038

Since the last patch, this fixes a null pointer dereference where the
TSI of the destructor wasn't properly propagated into the
DeclarationNameInfo. We now construct a LocInfoType for dependent cases,
as done elsewhere in getDestructorName, such that GetTypeFromParser can
correctly obtain the TSI.

---

This patch fixes two long-standing bugs that prevent Clang from
instantiating local class members inside a dependent context. These bugs
were introduced in commits
21eb1af469
and
919df9d75a.


21eb1af469
introduced a concept called eligible methods such that it did an attempt
to skip past ineligible method instantiation when instantiating class
members. Unfortunately, this broke the instantiation chain for local
classes - getTemplateInstantiationPattern() would fail to find the
correct definition pattern if the class was defined within a partially
transformed dependent context.


919df9d75a
introduced a separate issue by incorrectly copying the
DeclarationNameInfo during function definition instantiation from the
template pattern, even though that DNI might contain a transformed
TypeSourceInfo. Since that TSI was already updated when the declaration
was instantiated, this led to inconsistencies. As a result, the final
instantiated function could lose track of the transformed declarations,
hence we crash: https://compiler-explorer.com/z/vjvoG76Tf.

This PR corrects them by

1. Removing the bypass logic for method instantiation. The eligible flag
is independent of instantiation and can be updated properly afterward,
so skipping instantiation is unnecessary.

2. Carefully handling TypeSourceInfo by creating a new instance that
preserves the pattern's source location while using the already
transformed type.
2025-04-17 14:34:32 +08:00

65 lines
1.9 KiB
C++

// RUN: %clang_cc1 -std=c++17 %s -emit-llvm -triple %itanium_abi_triple -o - | FileCheck %s
namespace LambdaContainingLocalClasses {
template <typename F>
void GH59734() {
[&](auto param) {
struct Guard {
Guard() {
// Check that we're able to create DeclRefExpr to param at this point.
static_assert(__is_same(decltype(param), int), "");
}
~Guard() {
static_assert(__is_same(decltype(param), int), "");
}
operator decltype(param)() {
return decltype(param)();
}
};
Guard guard;
param = guard;
}(42);
}
// Guard::Guard():
// CHECK-DAG: define {{.*}} @_ZZZN28LambdaContainingLocalClasses7GH59734IiEEvvENKUlT_E_clIiEEDaS1_EN5GuardC2Ev
// Guard::operator int():
// CHECK-DAG: define {{.*}} @_ZZZN28LambdaContainingLocalClasses7GH59734IiEEvvENKUlT_E_clIiEEDaS1_EN5GuardcviEv
// Guard::~Guard():
// CHECK-DAG: define {{.*}} @_ZZZN28LambdaContainingLocalClasses7GH59734IiEEvvENKUlT_E_clIiEEDaS1_EN5GuardD2Ev
struct S {};
template <class T = void>
auto GH132208 = [](auto param) {
struct OnScopeExit {
OnScopeExit() {
static_assert(__is_same(decltype(param), S), "");
}
~OnScopeExit() {
static_assert(__is_same(decltype(param), S), "");
}
operator decltype(param)() {
return decltype(param)();
}
} pending;
param = pending;
};
void bar() {
GH59734<int>();
GH132208<void>(S{});
}
// OnScopeExit::OnScopeExit():
// CHECK-DAG: define {{.*}} @_ZZNK28LambdaContainingLocalClasses8GH132208IvEMUlT_E_clINS_1SEEEDaS2_EN11OnScopeExitC2Ev
// OnScopeExit::operator S():
// CHECK-DAG: define {{.*}} @_ZZNK28LambdaContainingLocalClasses8GH132208IvEMUlT_E_clINS_1SEEEDaS2_EN11OnScopeExitcvS5_Ev
// OnScopeExit::~OnScopeExit():
// CHECK-DAG: define {{.*}} @_ZZNK28LambdaContainingLocalClasses8GH132208IvEMUlT_E_clINS_1SEEEDaS2_EN11OnScopeExitD2Ev
} // namespace LambdaContainingLocalClasses