-
Notifications
You must be signed in to change notification settings - Fork 15.5k
[Clang][Sema] Fix the lambda call expression inside of a type alias declaration #82310
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
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
25f493d
The lambda call inside of a type alias
zyn0217 303fd1b
Format & Comments
zyn0217 62a00af
fixup
zyn0217 d3a76a1
Slightly refactor & Fix GH82104
zyn0217 be41641
Mention GH82104 in the release note
zyn0217 2e95a41
Rephrase & Format
zyn0217 4fcafbe
Validate deduced types
zyn0217 ba8e948
Merge branch 'main' into lambda-call-inside-of-type-aliases
zyn0217 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Format & Comments
- Loading branch information
commit 303fd1bc662f2dbb4a63cc28744a1e3c92b7a34a
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -283,8 +283,7 @@ Response HandleFunctionTemplateDecl(const FunctionTemplateDecl *FTD, | |
| return Response::ChangeDecl(FTD->getLexicalDeclContext()); | ||
| } | ||
|
|
||
| Response HandleRecordDecl(Sema &SemaRef, | ||
| const CXXRecordDecl *Rec, | ||
| Response HandleRecordDecl(Sema &SemaRef, const CXXRecordDecl *Rec, | ||
| MultiLevelTemplateArgumentList &Result, | ||
| ASTContext &Context, | ||
| bool ForConstraintInstantiation) { | ||
|
|
@@ -318,35 +317,68 @@ Response HandleRecordDecl(Sema &SemaRef, | |
| if (Rec->isLambda()) { | ||
| if (const Decl *LCD = Rec->getLambdaContextDecl()) | ||
| return Response::ChangeDecl(LCD); | ||
| // Attempt to retrieve the template arguments for a using alias declaration. | ||
| // This is necessary for constraint checking, since we always keep | ||
| // constraints relative to the primary template. | ||
| if (ForConstraintInstantiation && !SemaRef.CodeSynthesisContexts.empty()) { | ||
| for (auto &CSC : llvm::reverse(SemaRef.CodeSynthesisContexts)) { | ||
| if (CSC.Kind == Sema::CodeSynthesisContext::SynthesisKind::TypeAliasTemplateInstantiation) { | ||
| auto *TATD = cast<TypeAliasTemplateDecl>(CSC.Entity), *CurrentTATD = TATD; | ||
| FunctionDecl *LambdaCallOperator = Rec->getLambdaCallOperator(); | ||
| while (true) { | ||
| auto *FTD = dyn_cast_if_present<FunctionTemplateDecl>( | ||
| LambdaCallOperator->getDescribedTemplate()); | ||
| if (FTD && FTD->getInstantiatedFromMemberTemplate()) { | ||
| LambdaCallOperator = | ||
| FTD->getInstantiatedFromMemberTemplate()->getTemplatedDecl(); | ||
| } else if (auto *Prev = cast<CXXMethodDecl>(LambdaCallOperator) | ||
| ->getInstantiatedFromMemberFunction()) | ||
| LambdaCallOperator = Prev; | ||
| else | ||
| break; | ||
| } | ||
| while (TATD->getInstantiatedFromMemberTemplate()) | ||
| TATD = TATD->getInstantiatedFromMemberTemplate(); | ||
| // Constraint template parameters have a deeper depth. | ||
| if (cast<CXXRecordDecl>(LambdaCallOperator->getDeclContext()) | ||
| ->getTemplateDepth() == TATD->getTemplateDepth() && | ||
| getLambdaAwareParentOfDeclContext(LambdaCallOperator) == | ||
| TATD->getDeclContext()) { | ||
| Result.addOuterTemplateArguments(CurrentTATD, | ||
| CSC.template_arguments(), | ||
| /*Final=*/false); | ||
| return Response::ChangeDecl(CurrentTATD->getDeclContext()); | ||
| } | ||
| if (CSC.Kind != Sema::CodeSynthesisContext::SynthesisKind:: | ||
| TypeAliasTemplateInstantiation) | ||
| continue; | ||
| auto *TATD = cast<TypeAliasTemplateDecl>(CSC.Entity), | ||
| *CurrentTATD = TATD; | ||
| FunctionDecl *LambdaCallOperator = Rec->getLambdaCallOperator(); | ||
| // Retrieve the 'primary' template for a lambda call operator. It's | ||
| // unfortunate that we only have the mappings of call operators rather | ||
| // than lambda classes. | ||
| while (true) { | ||
| auto *FTD = dyn_cast_if_present<FunctionTemplateDecl>( | ||
| LambdaCallOperator->getDescribedTemplate()); | ||
| if (FTD && FTD->getInstantiatedFromMemberTemplate()) { | ||
| LambdaCallOperator = | ||
| FTD->getInstantiatedFromMemberTemplate()->getTemplatedDecl(); | ||
| } else if (auto *Prev = cast<CXXMethodDecl>(LambdaCallOperator) | ||
| ->getInstantiatedFromMemberFunction()) | ||
| LambdaCallOperator = Prev; | ||
| else | ||
| break; | ||
| } | ||
zyn0217 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Same applies for type alias Decl. We perform this to obtain the | ||
zyn0217 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // "canonical" template parameter depths. | ||
zyn0217 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| while (TATD->getInstantiatedFromMemberTemplate()) | ||
| TATD = TATD->getInstantiatedFromMemberTemplate(); | ||
| // Tell if we're currently inside of a lambda expression that is | ||
zyn0217 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // surrounded by a using alias declaration. e.g. | ||
| // template <class> using type = decltype([](auto) { ^ }()); | ||
| // By checking if: | ||
| // 1. The lambda expression and the using alias declaration share the | ||
| // same declaration context. | ||
| // 2. They have the same template depth. | ||
| // Then we assume the template arguments from the using alias | ||
| // declaration are essential for constraint instantiation. We have to do | ||
zyn0217 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // so since a TypeAliasTemplateDecl (or a TypeAliasDecl) is never a | ||
| // DeclContext, nor does it have an associated specialization Decl from | ||
| // which we could collect these template arguments. | ||
| if (cast<CXXRecordDecl>(LambdaCallOperator->getDeclContext()) | ||
| ->getTemplateDepth() == TATD->getTemplateDepth() && | ||
| getLambdaAwareParentOfDeclContext(LambdaCallOperator) == | ||
| TATD->getDeclContext()) { | ||
| Result.addOuterTemplateArguments(CurrentTATD, | ||
| CSC.template_arguments(), | ||
| /*Final=*/false); | ||
| // Visit the parent of the current type alias declaration rather than | ||
| // the lambda thereof. We have the following case: | ||
zyn0217 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // struct S { | ||
| // template <class> using T = decltype([]<Concept> {} ()); | ||
| // }; | ||
| // void foo() { | ||
| // S::T var; | ||
| // } | ||
| // The instantiated lambda expression (which we're visiting at 'var') | ||
| // has a function DeclContext 'foo' rather than the Record DeclContext | ||
| // S. This seems to be an oversight that we may want to set a Sema | ||
| // Context from the CXXScopeSpec before substituting into T to me. | ||
|
||
| return Response::ChangeDecl(CurrentTATD->getDeclContext()); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -447,7 +479,8 @@ MultiLevelTemplateArgumentList Sema::getTemplateInstantiationArgs( | |
| R = HandleFunction(Function, Result, Pattern, RelativeToPrimary, | ||
| ForConstraintInstantiation); | ||
| } else if (const auto *Rec = dyn_cast<CXXRecordDecl>(CurDecl)) { | ||
| R = HandleRecordDecl(*this, Rec, Result, Context, ForConstraintInstantiation); | ||
| R = HandleRecordDecl(*this, Rec, Result, Context, | ||
| ForConstraintInstantiation); | ||
| } else if (const auto *CSD = | ||
| dyn_cast<ImplicitConceptSpecializationDecl>(CurDecl)) { | ||
| R = HandleImplicitConceptSpecializationDecl(CSD, Result); | ||
|
|
@@ -1583,7 +1616,8 @@ namespace { | |
| CXXRecordDecl::LambdaDependencyKind | ||
| ComputeLambdaDependency(LambdaScopeInfo *LSI) { | ||
| auto &CCS = SemaRef.CodeSynthesisContexts.back(); | ||
| if (CCS.Kind == Sema::CodeSynthesisContext::TypeAliasTemplateInstantiation) { | ||
| if (CCS.Kind == | ||
| Sema::CodeSynthesisContext::TypeAliasTemplateInstantiation) { | ||
| unsigned TypeAliasDeclDepth = CCS.Entity->getTemplateDepth(); | ||
| if (TypeAliasDeclDepth >= TemplateArgs.getNumSubstitutedLevels()) | ||
| return CXXRecordDecl::LambdaDependencyKind::LDK_AlwaysDependent; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.