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

Conversation

jcsxky
Copy link
Contributor

@jcsxky jcsxky commented Jan 17, 2024

Try to fix issue 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 class.
Fix it by add template args of context when it's out-of-line. This will make MultiLevelTemplateArgumentList::getNumLevels matching the depth of template parameter. Testcase with some static_assert demonstrates the template parameter has been instanced correctly.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jan 17, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 17, 2024

@llvm/pr-subscribers-clang

Author: Qizhi Hu (jcsxky)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/78400.diff

1 Files Affected:

  • (modified) clang/lib/Sema/SemaTemplateInstantiate.cpp (+1-1)
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index fc80515b45e35b..92bbc4e51cb4c7 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -201,7 +201,7 @@ Response HandleFunction(const FunctionDecl *Function,
     // If this function was instantiated from a specialized member that is
     // a function template, we're done.
     assert(Function->getPrimaryTemplate() && "No function template?");
-    if (Function->getPrimaryTemplate()->isMemberSpecialization())
+    if (Function->getPrimaryTemplate()->isMemberSpecialization() && !Function->isOutOfLine())
       return Response::Done();
 
     // If this function is a generic lambda specialization, we are done.

Copy link

github-actions bot commented Jan 17, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@jcsxky jcsxky force-pushed the fix_member_function_template_with_default_align_crash branch 8 times, most recently from 07b6d21 to 774f1ab Compare January 20, 2024 04:16
@jcsxky jcsxky force-pushed the fix_member_function_template_with_default_align_crash branch from 774f1ab to e4ac395 Compare January 21, 2024 00:26
Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

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

Also needs a release note.

@@ -3051,6 +3051,7 @@ bool Sema::SubstDefaultArgument(
// default argument expression appears.
ContextRAII SavedContext(*this, FD);
std::unique_ptr<LocalInstantiationScope> LIS;
auto NewTemplateArgs = TemplateArgs;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't use auto here.

getASTContext(), TemplateArgs.getInnermost());
NewTemplateArgs = getTemplateInstantiationArgs(
FD, FD->getDeclContext(), true, CurrentTemplateArgumentList, true,
nullptr, false, false);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Needs /*argname=*/ comments on bools/nullptrs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All review has been applied.

@jcsxky jcsxky force-pushed the fix_member_function_template_with_default_align_crash branch 2 times, most recently from b9de8c5 to 852e2d2 Compare January 23, 2024 04:54
@jcsxky jcsxky requested a review from erichkeane January 23, 2024 05:35
@@ -859,6 +859,9 @@ Bug Fixes in This Version
Fixes (`#78290 <https://github.com/llvm/llvm-project/issues/78290>`_)
- Fixed assertion failure with deleted overloaded unary operators.
Fixes (`#78314 <https://github.com/llvm/llvm-project/issues/78314>`_)
- Fix crash when specialize out-of-line member function with default parameter
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
- Fix crash when specialize out-of-line member function with default parameter
- 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.

Is this accurate?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, thanks for your guidance of grammar and expression! I have update the note according to you suggestion.

@jcsxky jcsxky force-pushed the fix_member_function_template_with_default_align_crash branch from 852e2d2 to 7166061 Compare January 24, 2024 01:16
@jcsxky jcsxky requested a review from erichkeane January 24, 2024 01:20
Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

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

LGTM, let me know if you need someone to merge this for you.

@jcsxky jcsxky force-pushed the fix_member_function_template_with_default_align_crash branch from 7166061 to fbf5cca Compare January 26, 2024 06:37
@jcsxky jcsxky merged commit 7b33899 into llvm:main Jan 26, 2024
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

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

nitpick

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)

@bgra8
Copy link
Contributor

bgra8 commented Jan 31, 2024

@jcsxky this patch introduced a regression. Please see the reproducer here: https://godbolt.org/z/rEfWP75Ta. It compiles before this change and fails at this change. It also compiles with gcc and clang-17.

@jcsxky
Copy link
Contributor Author

jcsxky commented Jan 31, 2024

@jcsxky this patch introduced a regression. Please see the reproducer here: https://godbolt.org/z/rEfWP75Ta. It compiles before this change and fails at this change. It also compiles with gcc and clang-17.

Sorry for importing new bugs. @erichkeane Could you help me revert this patch in order to not influence others since I am not familiar with github reverting operation? I afraid I would spent a long time to look into the root cause of the issue. Appreciate for you help and many thanks!

erichkeane added a commit to erichkeane/llvm-project that referenced this pull request Jan 31, 2024
…lt align crash (llvm#78400)"

This reverts commit 7b33899.

A regression was discovered here:
llvm#78400

and the author requested a revert to give time to review.
@erichkeane
Copy link
Collaborator

@jcsxky this patch introduced a regression. Please see the reproducer here: https://godbolt.org/z/rEfWP75Ta. It compiles before this change and fails at this change. It also compiles with gcc and clang-17.

Sorry for importing new bugs. @erichkeane Could you help me revert this patch in order to not influence others since I am not familiar with github reverting operation? I afraid I would spent a long time to look into the root cause of the issue. Appreciate for you help and many thanks!

No worries, happens all the time :) Reverting because you requested it, feel free to submit a new version of this review that fixes that as well! Revert will be commited from here: #80144

erichkeane added a commit that referenced this pull request Jan 31, 2024
#80144)

…lt align crash (#78400)"

This reverts commit 7b33899.

A regression was discovered here:
#78400

and the author requested a revert to give time to review.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang] template specialization + member function template with default arg and alignof crash
5 participants