This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Lambdas are not part of immediate context for deduction
ClosedPublic

Authored by ilya-biryukov on Apr 20 2023, 6:58 AM.

Details

Summary

This commit implements [temp.deduct]p9.
Test updates include:

  • New notes in cxx1y-init-captures.cpp, lambda-expressions.cpp and 'warn-unused-lambda-capture.cpp'. This seems to be caused by diagnosing errors earlier (during deduction) that were previously surfaced later (during instantiation).
  • New error lambda-unevaluated.cpp is in line with [temp.deduct]p9.

Diff Detail

Event Timeline

ilya-biryukov created this revision.Apr 20 2023, 6:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 20 2023, 6:58 AM
ilya-biryukov requested review of this revision.Apr 20 2023, 6:59 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 20 2023, 6:59 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
ilya-biryukov added a reviewer: Restricted Project.Apr 20 2023, 6:59 AM

My one concern is that this is going to expose our incorrect instantiation of lambdas further and more painfully (see https://github.com/llvm/llvm-project/issues/58872). Else, I don't see anything to be concerned about here.

I think the FIXME of adding a note diagnostic here should be fixed before committing however.

clang/lib/Sema/SemaTemplateInstantiate.cpp
965

Would really like this note here, it shouldn't be too difficult, right?

ilya-biryukov added inline comments.Apr 20 2023, 7:46 AM
clang/lib/Sema/SemaTemplateInstantiate.cpp
965

Ah, sorry, I added a comment here that I forgot to submit. The question is: could it be that we want to skip this note?

I wanted to double-check if folks find this note useful.
On one hand, this probably creates some noise as there will always be other notes that point into the location of a corresponding substitution location that contains the lambda.
On the other hand, since the lambda is not an immediate context, this may give hints to users on why SFINAE does not apply.

If you feel like the note is useful, I will follow up with an implementation.

erichkeane added inline comments.Apr 20 2023, 8:02 AM
clang/lib/Sema/SemaTemplateInstantiate.cpp
965

I think it is useful for exactly the reason you mentioned: this is going to be somewhat shocking behavior to most people, so explaining it better will be helpful.

shafik added a subscriber: shafik.Apr 20 2023, 1:59 PM
shafik added inline comments.
clang/test/CXX/temp/temp.deduct/p9.cpp
2

Maybe helpful to quote p9?

36

I know the note in the standard does not explain why this is ok but a comment would be helpful to future readers.

39

I think the comment from the draft is helpful here "deduction fails on #1, calls #2"

clang/test/SemaCXX/warn-unused-lambda-capture.cpp
192

Why the 12 extra notes here, I feel I am missing something but not sure what. I see the increase in notes in other cases as well.

ilya-biryukov marked 5 inline comments as done.
  • Add a note when substituting into a lambda
  • Quote the standard and add explanation for the test

My one concern is that this is going to expose our incorrect instantiation of lambdas further and more painfully (see https://github.com/llvm/llvm-project/issues/58872). Else, I don't see anything to be concerned about here.

That's true, but it seems that as a result of this patch, Clang will start rejecting some valid C++ programs when lambdas are used in unevaluated contexts, but will stop (incorrectly) accepting a class of programs that are invalid according to C++ standard.
I would err on the side of false negatives (correct program not compiled) instead of false positives (incorrect program complied). Otherwise, future changes that make Clang compliant can potentially require rewriting the code that relied on incorrect behavior.
It's still unfortunate that we don't accept valid programs, but at least existing code will not need to be changed after GH58872 is fixed.
My perspective is based on the need to support a large codebase, in which codebase-wide refactorings are expensive, other might have a different opinion here.

clang/lib/Sema/SemaTemplateInstantiate.cpp
965

Makes sense. Added a corresponding note.

clang/test/SemaCXX/warn-unused-lambda-capture.cpp
192

I'm not entirely sure, but it seems there is some deduplication of notes that's happening when the stacks of code-synthesis-contexts for subsequent errors are the same.
However, this patch introduces a different code synthesis context for lambda substitutions, forcing the notes to be added.
In the new version of the patch, the added context actually shows up in the notes as well.

Friendly ping for another round of review.

Another friendly ping for review.
@erichkeane let me know if you feel that exposing the incorrect lambda instantiation behavior is a blocker here.

erichkeane accepted this revision.May 8 2023, 6:52 AM

Another friendly ping for review.
@erichkeane let me know if you feel that exposing the incorrect lambda instantiation behavior is a blocker here.

I don't... we're up to ~4 active bugs on that, so its not like it is 'unexposed' now. I think this looks good enough to accept;

clang/test/SemaCXX/warn-unused-lambda-capture.cpp
192

Note that this test is organized poorly, and the 12 additions are likely the levels added above, and it is 1 per error, so its just 1 more level of 'in instantiation...' in each of those cases above.

This revision is now accepted and ready to land.May 8 2023, 6:52 AM
  • Moved the relase note to 'C++20 feature support', fixed typos
  • Fix a typo (forgot to commit last time)