-
Couldn't load subscription status.
- Fork 15k
[Clang] Fix a wrong diagnostic about a local variable inside a lambda in unevaluated context need capture #165098
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
base: main
Are you sure you want to change the base?
Conversation
| ✅ With the latest revision this PR passed the C/C++ code formatter. |
c55a21e to d4a1773 Compare | @llvm/pr-subscribers-clang Author: Zhikai Zeng (Backl1ght) ChangesFixes #163837 We should return true here llvm-project/clang/lib/Sema/SemaExpr.cpp Lines 19398 to 19400 in 035f811
VarDC and DC while DC is instantiated version of VarDC. The reason of mismatch is that llvm-project/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp Lines 7064 to 7065 in 059d90d
We should return instantiated version if we can find one. Full diff: https://github.com/llvm/llvm-project/pull/165098.diff 3 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index e6e33e7a9a280..dd7206ba2b253 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -429,6 +429,8 @@ Bug Fixes in This Version - Fixed a failed assertion with empty filename arguments in ``__has_embed``. (#GH159898) - Fixed a failed assertion with empty filename in ``#embed`` directive. (#GH162951) - Fixed a crash triggered by unterminated ``__has_embed``. (#GH162953) +- Fixed a wrong diagnostic about a local variable inside a lambda in unevaluated + contexts need capture. (#GH163837) Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index 28925cca8f956..439a5317f06f1 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -7061,8 +7061,15 @@ NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D, // anonymous unions in class templates). } - if (!ParentDependsOnArgs) + if (!ParentDependsOnArgs) { + if (auto Found = + CurrentInstantiationScope + ? CurrentInstantiationScope->getInstantiationOfIfExists(D) + : nullptr) { + return cast<NamedDecl>(Found->dyn_cast<Decl *>()); + } return D; + } ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs); if (!ParentDC) diff --git a/clang/test/SemaCXX/lambda-unevaluated.cpp b/clang/test/SemaCXX/lambda-unevaluated.cpp index 9289fc9ec1243..4b0e1abdb38f0 100644 --- a/clang/test/SemaCXX/lambda-unevaluated.cpp +++ b/clang/test/SemaCXX/lambda-unevaluated.cpp @@ -282,3 +282,22 @@ static_assert(__is_same_as(int, helper<int>)); } // namespace GH138018 + +namespace GH163837 { + +template<int N> +void f(); + +template<class> +struct X { + using type = decltype([](auto) { + f<[]{ + int result = 0; + return result; + }()>(); + }(0)); +}; + +X<void> l; + +} // namespace GH163837 |
d4a1773 to 1576d7e Compare 2adbbfd to 2340696 Compare 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.
FYI @mizvekov is working on a large patch that resolves a lot of lambda context related bugs and aims to remove these workarounds in the end.
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.
Nits in case this patch goes in before the mentioned patch
| if (auto Found = | ||
| CurrentInstantiationScope | ||
| ? CurrentInstantiationScope->getInstantiationOfIfExists(D) | ||
| : nullptr) { |
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.
I find ternary operator inside of an if uncommon, can we simply do
| if (auto Found = | |
| CurrentInstantiationScope | |
| ? CurrentInstantiationScope->getInstantiationOfIfExists(D) | |
| : nullptr) { | |
| if (CurrentInstantiationScope) { | |
| llvm::PointerUnion<... > * Found = CurrentInstantiationScope->getInstantiationOfIfExists(D) | |
| ... |
and spell out the type?
| CurrentInstantiationScope | ||
| ? CurrentInstantiationScope->getInstantiationOfIfExists(D) | ||
| : nullptr) { | ||
| return cast<NamedDecl>(Found->dyn_cast<Decl *>()); |
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.
Using of dyn_cast is strange here IMO, since the outer cast doesn't expect null. If that is always a Decl there, we probably need to use get instead.
Fixes #163837
We should return true here
llvm-project/clang/lib/Sema/SemaExpr.cpp
Lines 19398 to 19400 in 035f811
VarDCandDCwhileDCis instantiated version ofVarDC.The reason of mismatch is that
FindInstantiatedDecltoresultreturns uninstantiated version incorrectly herellvm-project/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
Lines 7064 to 7065 in 059d90d
We should return instantiated version if we can find one.