Skip to content

[Clang][Sema] fix outline member function template with default align crash #78400

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
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
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ Bug Fixes to C++ Support

- Fix crash when using lifetimebound attribute in function with trailing return.
Fixes (`#73619 <https://github.com/llvm/llvm-project/issues/73619>`_)
- Fix a crash when specializing an out-of-line member function with a default
parameter where we did an incorrect specialization of the initialization of
the default parameter.
Fixes (`#68490 <https://github.com/llvm/llvm-project/issues/68490>`_)

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
13 changes: 11 additions & 2 deletions clang/lib/Sema/SemaTemplateInstantiate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3049,6 +3049,7 @@ bool Sema::SubstDefaultArgument(
// default argument expression appears.
ContextRAII SavedContext(*this, FD);
std::unique_ptr<LocalInstantiationScope> LIS;
MultiLevelTemplateArgumentList NewTemplateArgs = TemplateArgs;

if (ForCallExpr) {
// When instantiating a default argument due to use in a call expression,
Expand All @@ -3061,11 +3062,19 @@ bool Sema::SubstDefaultArgument(
/*ForDefinition*/ false);
if (addInstantiatedParametersToScope(FD, PatternFD, *LIS, TemplateArgs))
return true;
if (FD->isOutOfLine()) {
TemplateArgumentList *CurrentTemplateArgumentList =
TemplateArgumentList::CreateCopy(getASTContext(),
TemplateArgs.getInnermost());
NewTemplateArgs = getTemplateInstantiationArgs(
FD, FD->getDeclContext(), /*Final=*/false,
CurrentTemplateArgumentList, /*RelativeToPrimary=*/true);
}
}

runWithSufficientStackSpace(Loc, [&] {
Result = SubstInitializer(PatternExpr, TemplateArgs,
/*DirectInit*/false);
Result = SubstInitializer(PatternExpr, NewTemplateArgs,
/*DirectInit*/ false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/*DirectInit=*/false)

});
}
if (Result.isInvalid())
Expand Down
50 changes: 50 additions & 0 deletions clang/test/SemaTemplate/default-parm-init.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// RUN: %clang_cc1 -fsyntax-only -std=c++17 -verify %s
// RUN: %clang_cc1 -fsyntax-only -std=c++20 -verify %s
// expected-no-diagnostics

template<typename TemplateParam>
struct Problem{
template<typename FunctionTemplateParam>
constexpr int FuncAlign(int param = alignof(FunctionTemplateParam));

template<typename FunctionTemplateParam>
constexpr int FuncSizeof(int param = sizeof(FunctionTemplateParam));

template<typename FunctionTemplateParam>
constexpr int FuncAlign2(int param = alignof(TemplateParam));

template<typename FunctionTemplateParam>
constexpr int FuncSizeof2(int param = sizeof(TemplateParam));
};

template <>
template<typename FunctionTemplateParam>
constexpr int Problem<int>::FuncAlign(int param) {
return param;
}

template <>
template<typename FunctionTemplateParam>
constexpr int Problem<int>::FuncSizeof(int param) {
return param;
}

template <>
template<typename FunctionTemplateParam>
constexpr int Problem<int>::FuncAlign2(int param) {
return param;
}

template <>
template<typename FunctionTemplateParam>
constexpr int Problem<int>::FuncSizeof2(int param) {
return param;
}

int main(){
Problem<int> p = {};
static_assert(p.FuncAlign<char>() == alignof(char));
static_assert(p.FuncSizeof<char>() == sizeof(char));
static_assert(p.FuncAlign2<char>() == alignof(int));
static_assert(p.FuncSizeof2<char>() == sizeof(int));
}