Skip to content

Commit 752c172

Browse files
authored
[Clang][Sema] fix outline member function template with default align crash (#80288)
Try to fix [issue](#68490 ) and some extented problem. Root cause of current issue is that error handling in instantiation of function parameter with default initialization on sizeof or align expression. When instance an out-of-line template member function, depth of `TemplateTypeParmDecl` in default initialization doesn't change while depth of other template parameter does and this will lead to some template parameter uninstanced. Also, sometime it will leader to wrong instantiation when it uses the template parameter of the template class. Fix it by add template args of context. This will make MultiLevelTemplateArgumentList::getNumLevels matching the depth of template parameter. Testcase with some static_assert demonstrates the template parameter has been instanced correctly. But, the default initialization of lambda expression compiles failed when only checking if the member function is out-of-line. We should check the `PrimaryFunctionTemplateDecl` of the funtion if it's out-of-line. Co-authored-by: huqizhi <[email protected]>
1 parent 514d069 commit 752c172

File tree

3 files changed

+206
-2
lines changed

3 files changed

+206
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ Bug Fixes to C++ Support
197197
Fixes (`#67976 <https://github.com/llvm/llvm-project/issues/67976>`_)
198198
- Fix crash and diagnostic with const qualified member operator new.
199199
Fixes (`#79748 <https://github.com/llvm/llvm-project/issues/79748>`_)
200+
- Fix a crash when specializing an out-of-line member function with a default
201+
parameter where we did an incorrect specialization of the initialization of
202+
the default parameter.
203+
Fixes (`#68490 <https://github.com/llvm/llvm-project/issues/68490>`_)
200204

201205
Bug Fixes to AST Handling
202206
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaTemplateInstantiate.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3049,6 +3049,7 @@ bool Sema::SubstDefaultArgument(
30493049
// default argument expression appears.
30503050
ContextRAII SavedContext(*this, FD);
30513051
std::unique_ptr<LocalInstantiationScope> LIS;
3052+
MultiLevelTemplateArgumentList NewTemplateArgs = TemplateArgs;
30523053

30533054
if (ForCallExpr) {
30543055
// When instantiating a default argument due to use in a call expression,
@@ -3061,11 +3062,20 @@ bool Sema::SubstDefaultArgument(
30613062
/*ForDefinition*/ false);
30623063
if (addInstantiatedParametersToScope(FD, PatternFD, *LIS, TemplateArgs))
30633064
return true;
3065+
const FunctionTemplateDecl *PrimaryTemplate = FD->getPrimaryTemplate();
3066+
if (PrimaryTemplate && PrimaryTemplate->isOutOfLine()) {
3067+
TemplateArgumentList *CurrentTemplateArgumentList =
3068+
TemplateArgumentList::CreateCopy(getASTContext(),
3069+
TemplateArgs.getInnermost());
3070+
NewTemplateArgs = getTemplateInstantiationArgs(
3071+
FD, FD->getDeclContext(), /*Final=*/false,
3072+
CurrentTemplateArgumentList->asArray(), /*RelativeToPrimary=*/true);
3073+
}
30643074
}
30653075

30663076
runWithSufficientStackSpace(Loc, [&] {
3067-
Result = SubstInitializer(PatternExpr, TemplateArgs,
3068-
/*DirectInit*/false);
3077+
Result = SubstInitializer(PatternExpr, NewTemplateArgs,
3078+
/*DirectInit*/ false);
30693079
});
30703080
}
30713081
if (Result.isInvalid())
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
// RUN: %clang_cc1 -fsyntax-only -std=c++17 -verify %s
2+
// RUN: %clang_cc1 -fsyntax-only -std=c++20 -verify %s
3+
// expected-no-diagnostics
4+
5+
namespace std {
6+
7+
template<typename Signature> class function;
8+
9+
template<typename R, typename... Args> class invoker_base {
10+
public:
11+
virtual ~invoker_base() { }
12+
virtual R invoke(Args...) = 0;
13+
virtual invoker_base* clone() = 0;
14+
};
15+
16+
template<typename F, typename R, typename... Args>
17+
class functor_invoker : public invoker_base<R, Args...> {
18+
public:
19+
explicit functor_invoker(const F& f) : f(f) { }
20+
R invoke(Args... args) { return f(args...); }
21+
functor_invoker* clone() { return new functor_invoker(f); }
22+
23+
private:
24+
F f;
25+
};
26+
27+
template<typename R, typename... Args>
28+
class function<R (Args...)> {
29+
public:
30+
typedef R result_type;
31+
function() : invoker (0) { }
32+
function(const function& other) : invoker(0) {
33+
if (other.invoker)
34+
invoker = other.invoker->clone();
35+
}
36+
37+
template<typename F> function(const F& f) : invoker(0) {
38+
invoker = new functor_invoker<F, R, Args...>(f);
39+
}
40+
41+
~function() {
42+
if (invoker)
43+
delete invoker;
44+
}
45+
46+
function& operator=(const function& other) {
47+
function(other).swap(*this);
48+
return *this;
49+
}
50+
51+
template<typename F>
52+
function& operator=(const F& f) {
53+
function(f).swap(*this);
54+
return *this;
55+
}
56+
57+
void swap(function& other) {
58+
invoker_base<R, Args...>* tmp = invoker;
59+
invoker = other.invoker;
60+
other.invoker = tmp;
61+
}
62+
63+
result_type operator()(Args... args) const {
64+
return invoker->invoke(args...);
65+
}
66+
67+
private:
68+
invoker_base<R, Args...>* invoker;
69+
};
70+
71+
}
72+
73+
template<typename TemplateParam>
74+
struct Problem {
75+
template<typename FunctionTemplateParam>
76+
constexpr int FuncAlign(int param = alignof(FunctionTemplateParam));
77+
78+
template<typename FunctionTemplateParam>
79+
constexpr int FuncSizeof(int param = sizeof(FunctionTemplateParam));
80+
81+
template<typename FunctionTemplateParam>
82+
constexpr int FuncAlign2(int param = alignof(TemplateParam));
83+
84+
template<typename FunctionTemplateParam>
85+
constexpr int FuncSizeof2(int param = sizeof(TemplateParam));
86+
};
87+
88+
template<typename TemplateParam>
89+
struct Problem<TemplateParam*> {
90+
template<typename FunctionTemplateParam>
91+
constexpr int FuncAlign(int param = alignof(FunctionTemplateParam));
92+
93+
template<typename FunctionTemplateParam>
94+
constexpr int FuncSizeof(int param = sizeof(FunctionTemplateParam));
95+
96+
template<typename FunctionTemplateParam>
97+
constexpr int FuncAlign2(int param = alignof(TemplateParam));
98+
99+
template<typename FunctionTemplateParam>
100+
constexpr int FuncSizeof2(int param = sizeof(TemplateParam));
101+
};
102+
103+
template<typename TemplateParam>
104+
template<typename FunctionTemplateParam>
105+
constexpr int Problem<TemplateParam*>::FuncAlign(int param) {
106+
return 2U*param;
107+
}
108+
109+
template<typename TemplateParam>
110+
template<typename FunctionTemplateParam>
111+
constexpr int Problem<TemplateParam*>::FuncSizeof(int param) {
112+
return 2U*param;
113+
}
114+
115+
template<typename TemplateParam>
116+
template<typename FunctionTemplateParam>
117+
constexpr int Problem<TemplateParam*>::FuncAlign2(int param) {
118+
return 2U*param;
119+
}
120+
121+
template<typename TemplateParam>
122+
template<typename FunctionTemplateParam>
123+
constexpr int Problem<TemplateParam*>::FuncSizeof2(int param) {
124+
return 2U*param;
125+
}
126+
127+
template <>
128+
template<typename FunctionTemplateParam>
129+
constexpr int Problem<int>::FuncAlign(int param) {
130+
return param;
131+
}
132+
133+
template <>
134+
template<typename FunctionTemplateParam>
135+
constexpr int Problem<int>::FuncSizeof(int param) {
136+
return param;
137+
}
138+
139+
template <>
140+
template<typename FunctionTemplateParam>
141+
constexpr int Problem<int>::FuncAlign2(int param) {
142+
return param;
143+
}
144+
145+
template <>
146+
template<typename FunctionTemplateParam>
147+
constexpr int Problem<int>::FuncSizeof2(int param) {
148+
return param;
149+
}
150+
151+
void foo() {
152+
Problem<int> p = {};
153+
static_assert(p.FuncAlign<char>() == alignof(char));
154+
static_assert(p.FuncSizeof<char>() == sizeof(char));
155+
static_assert(p.FuncAlign2<char>() == alignof(int));
156+
static_assert(p.FuncSizeof2<char>() == sizeof(int));
157+
Problem<short*> q = {};
158+
static_assert(q.FuncAlign<char>() == 2U * alignof(char));
159+
static_assert(q.FuncSizeof<char>() == 2U * sizeof(char));
160+
static_assert(q.FuncAlign2<char>() == 2U *alignof(short));
161+
static_assert(q.FuncSizeof2<char>() == 2U * sizeof(short));
162+
}
163+
164+
template <typename T>
165+
class A {
166+
public:
167+
void run(
168+
std::function<void(T&)> f1 = [](auto&&) {},
169+
std::function<void(T&)> f2 = [](auto&&) {});
170+
private:
171+
class Helper {
172+
public:
173+
explicit Helper(std::function<void(T&)> f2) : f2_(f2) {}
174+
std::function<void(T&)> f2_;
175+
};
176+
};
177+
178+
template <typename T>
179+
void A<T>::run(std::function<void(T&)> f1,
180+
std::function<void(T&)> f2) {
181+
Helper h(f2);
182+
}
183+
184+
struct B {};
185+
186+
int main() {
187+
A<B> a;
188+
a.run([&](auto& l) {});
189+
return 0;
190+
}

0 commit comments

Comments
 (0)