-
Notifications
You must be signed in to change notification settings - Fork 14.1k
[Clang] skip default argument instantiation for non-defining friend declarations without specialization info to meet [dcl.fct.default] p4 #113777
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
Conversation
@llvm/pr-subscribers-clang Author: Oleksandr T. (a-tarasyuk) ChangesFixes #113324 Full diff: https://github.com/llvm/llvm-project/pull/113777.diff 3 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6a95337815174b..428ec8c87a432e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -561,6 +561,8 @@ Bug Fixes to C++ Support
const-default-constructible even if a union member has a default member initializer.
(#GH95854).
- Fixed an assertion failure when evaluating an invalid expression in an array initializer (#GH112140)
+- Fixed an assertion failure caused by an invalid template instantiation pattern
+ for adding instantiated parameters to the scope in friend functions with defaulted parameters. (#GH113324).
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index 6a55861fe5af3b..cddfcc48312042 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -3435,10 +3435,10 @@ bool Sema::SubstDefaultArgument(
// template<typename T> void f(T a, int = decltype(a)());
// void g() { f(0); }
LIS = std::make_unique<LocalInstantiationScope>(*this);
- FunctionDecl *PatternFD = FD->getTemplateInstantiationPattern(
- /*ForDefinition*/ false);
- if (addInstantiatedParametersToScope(FD, PatternFD, *LIS, TemplateArgs))
- return true;
+ if (FunctionDecl *PatternFD =
+ FD->getTemplateInstantiationPattern(/*ForDefinition*/ false))
+ if (addInstantiatedParametersToScope(FD, PatternFD, *LIS, TemplateArgs))
+ return true;
}
runWithSufficientStackSpace(Loc, [&] {
diff --git a/clang/test/CXX/temp/temp.res/p4.cpp b/clang/test/CXX/temp/temp.res/p4.cpp
index f54d8649f5da88..62bd766e7e1140 100644
--- a/clang/test/CXX/temp/temp.res/p4.cpp
+++ b/clang/test/CXX/temp/temp.res/p4.cpp
@@ -185,3 +185,10 @@ template<typename T> struct S {
friend void X::f(T::type);
};
}
+
+namespace GH113324 {
+template <typename = int> struct ct {
+ friend void f(ct, int = 0); // expected-error {{friend declaration specifying a default argument must be a definition}}
+};
+void test() { f(ct<>{}); }
+}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM except for a couple of nits.
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM as well, thanks for working on it!
BTW, can you flesh out the commit message so we don't just have a single issue link there? Thanks |
@zyn0217 should all the commits be squashed into one? |
Of course. By saying “flesh out the commit message”, I meant to edit the PR description on github, which would become the final commit message when we merge the PR. |
…eclarations without specialization info to meet [dcl.fct.default]p4
94509e7
to
2ef4cc6
Compare
@zyn0217 meant the PR description, which is the body of the commit message, not the title. |
@mizvekov thanks |
FYI this is causing Clang to crash with Chrome: https://crbug.com/377956048 I'm currently working on getting a reduced repro with CReduce. |
@alanzhao1 thanks for catching that. should I revert it? |
Based on the patch reversion policy, I think reverting is the right thing to do. (I'm working on the reduced repro, but creduce is really slow. In the meantime, I attached the unreduced files). |
For posterity, here's the entire stacktrace I got:
|
…friend declarations without specialization info to meet [dcl.fct.default] p4" (#115404) Reverts #113777 Reverted due to regression reported here: #113777 (comment)
Reverted. The issue looks like the instantiation of the default argument was skipped, but without any previous error actually being produced, so it proceeded to CodeGen with invalid AST. |
Thanks! Still waiting on CReduce for that repro. |
I was also reducing this, got
|
Further reduced to: struct CacheRequestImpl {
friend void LoadOrRun(CacheRequestImpl, int = 42) {}
};
void CacheRequestTests_MakesCacheKey_TestTestBody() {
CacheRequestImpl req;
LoadOrRun(req);
} There is no template in this case, so obviously, As I have suggested initially, we should probably put the checking in |
Thanks for the revert - FWIW, I also ran into issues with this change when building Qt. Building the https://github.com/qt/qtbase repo fails with an error like this:
In a build with static libraries, where this didn't cause immediate errors, I also ran into build errors while building qtdeclarative. I didn't bisect that failure, but it seems closely related:
Before relanding, I would appreciate if you could try building qtbase with the new version of the suggested change! |
…eclarations (llvm#113777) This fixes a crash when instantiating default arguments for templated friend function declarations which lack a definition. There are implementation limits which prevents us from finding the pattern for such functions, and this causes difficulties setting up the instantiation scope for the function parameters. This patch skips instantiating the default argument in these cases, which causes a minor regression in error recovery, but otherwise avoids the crash. Fixes llvm#113324
…friend declarations without specialization info to meet [dcl.fct.default] p4" (llvm#115404) Reverts llvm#113777 Reverted due to regression reported here: llvm#113777 (comment)
…riend declarations to meet [dcl.fct.default] p4 (#115487) This fixes a crash when instantiating default arguments for templated friend function declarations which lack a definition. There are implementation limits which prevents us from finding the pattern for such functions, and this causes difficulties setting up the instantiation scope for the function parameters. This patch skips instantiating the default argument in these cases, which causes a minor regression in error recovery, but otherwise avoids the crash. The previous attempt #113777 accidentally skipped all default argument constructions, causing some regressions. This patch resolves that by moving the guard to InstantiateDefaultArgument() where the handling of templates takes place. Fixes #113324
This fixes a crash when instantiating default arguments for templated friend function declarations which lack a definition.
There are implementation limits which prevents us from finding the pattern for such functions, and this causes difficulties
setting up the instantiation scope for the function parameters.
This patch skips instantiating the default argument in these cases, which causes a minor regression in error recovery, but otherwise avoids the crash.
Fixes #113324