Skip to content

[Sema] LambdaScopeForCallOperatorInstantiationRAII - fix typo in early out logic #96888

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 1 commit into from
Jun 28, 2024

Conversation

RKSimon
Copy link
Collaborator

@RKSimon RKSimon commented Jun 27, 2024

We should be checking for a failed dyn_cast on the ParentFD result - not the loop invariant FD root value.

Seems to have been introduced in #65193

Noticed by static analyser (I have no specific test case).

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

llvmbot commented Jun 27, 2024

@llvm/pr-subscribers-clang

Author: Simon Pilgrim (RKSimon)

Changes

We should be checking for a failed dyn_cast on the ParentFD result - not the loop invariant FD root value.

Seems to have been introduced in #65193

Noticed by static analyser (I have no specific test case).


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

1 Files Affected:

  • (modified) clang/lib/Sema/SemaLambda.cpp (+1-1)
diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp
index e9476a0c93c5d..ca9c7cb9faadf 100644
--- a/clang/lib/Sema/SemaLambda.cpp
+++ b/clang/lib/Sema/SemaLambda.cpp
@@ -2391,7 +2391,7 @@ Sema::LambdaScopeForCallOperatorInstantiationRAII::
       Pattern =
           dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext(Pattern));
 
-      if (!FD || !Pattern)
+      if (!ParentFD || !Pattern)
         break;
 
       SemaRef.addInstantiatedParametersToScope(ParentFD, Pattern, Scope, MLTAL);

Copy link
Contributor

@zyn0217 zyn0217 left a comment

Choose a reason for hiding this comment

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

Thanks for the patch.
While I think this makes sense, it still confuses me whether there was some redundancy in the guard condition. Do we have any situation where Pattern is non-null whereas ParentFD becomes null? I assume they are always paired, at least for the lambda cases.

@RKSimon
Copy link
Collaborator Author

RKSimon commented Jun 27, 2024

It would probably explain why the typo hasn't caused regression - I'm going off static analysis reports here, not crash reports.

@RKSimon RKSimon force-pushed the lambda-scope-typo branch from 3194b59 to 5a967ae Compare June 27, 2024 12:31
@zyn0217
Copy link
Contributor

zyn0217 commented Jun 28, 2024

Looks like we have hit the assert in SemaTemplate/alias-template-with-lambdas.cpp. I brought that dirty test, and I'll take a look this weekend.

@0x59616e
Copy link
Contributor

This is indeed a typo.

@0x59616e
Copy link
Contributor

Thanks for this fix!

…y out logic

We should be checking for a failed dyn_cast on the ParentFD result - not the loop invariant FD root value.

Seems to have been introduced in llvm#65193

Noticed by static analyser (I have no specific test case).
@RKSimon RKSimon force-pushed the lambda-scope-typo branch from 5a967ae to 7f6614d Compare June 28, 2024 17:40
@RKSimon
Copy link
Collaborator Author

RKSimon commented Jun 28, 2024

Cheers - I'll push this (without the assert) in a moment - that shouldn't get in the way of working out why they aren't always paired.

@RKSimon RKSimon merged commit dac3661 into llvm:main Jun 28, 2024
4 of 6 checks passed
@RKSimon RKSimon deleted the lambda-scope-typo branch June 28, 2024 17:54
@zyn0217
Copy link
Contributor

zyn0217 commented Jun 29, 2024

So, here is the offending case:

template <class>
concept True = true;

template <class... U>
using MeowMeow = decltype([]<True>(U...) {}.template operator()<char>(U()...));

void foo() {
  using T = MeowMeow<char, int, long, unsigned>;
}

In this example, the lambda does not have the TypeAliasTemplateDecl where it is defined as its DeclContext - the TypeAliasTemplateDecl / TypeAliasDecl does not constitute a DeclContext. So, it's owned by TranslationUnitDecl. OTOH, we will instantiate the lambda inside the function foo() and thus the DeclContext of the instantiation is foo(), hence the discrepancy.

@zyn0217
Copy link
Contributor

zyn0217 commented Jun 29, 2024

Thinking more, I still maintain that the check for ParentFD is redundant.

So, suppose we need to find a case to compromise the previous logic. In that case, we need to find a generic lambda whose Pattern is defined inside a function (i.e. Pattern is non-null) while its Instantiation (i.e. ParentFD is null) is not. I don't think that is possible because we can't instantiate any lambda definition outside the function that owns them.

That being said, I think keeping the patch as-is is fine because it's more intelligible than explaining the subtle implications above.

lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
…y out logic (llvm#96888)

We should be checking for a failed dyn_cast on the ParentFD result - not the loop invariant FD root value.

Seems to have been introduced in llvm#65193

Noticed by static analyser (I have no specific test case).
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.

4 participants