This is an archive of the discontinued LLVM Phabricator instance.

[Clang] Do not change the type of captured vars when checking lambda constraints
ClosedPublic

Authored by cor3ntin on Aug 21 2023, 9:15 AM.

Details

Summary

When checking the constraint of a lambda, we need to respect the constness
of the call operator when establishing the type of capture variables.

In D124351, this was done by adding const to the captured variable...
However, that would change the type of the variable outside of the scope
of the lambda, which is clearly not the desired outcome.

Instead, to ensure const-correctness, we need to populate
a LambdaScopeInfo with the capture variables before checking the
constraints of a generic lambda.

There is no changelog as I'd like to tentatively propose we backport
this change to RC3 as it is a regression introduced in the Clang 17
cycle.

Fixes #61267

Diff Detail

Event Timeline

cor3ntin created this revision.Aug 21 2023, 9:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 21 2023, 9:15 AM
Herald added a subscriber: ChuanqiXu. · View Herald Transcript
cor3ntin requested review of this revision.Aug 21 2023, 9:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 21 2023, 9:15 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Adding a few more reviewers for extra sets of eyes given that we'd like this to land in 17.x. The changes look correct to me, but a confirmation would be appreciated.

shafik added inline comments.Aug 21 2023, 10:41 AM
clang/lib/Sema/SemaConcept.cpp
722

This comment does not really explain to me the effect of LSI->AfterParameterList = false;

clang/lib/Sema/SemaExpr.cpp
19753

Why move this block of code for?

cor3ntin added inline comments.Aug 22 2023, 12:04 AM
clang/lib/Sema/SemaConcept.cpp
722

AfterParameterList affects in which context we look for captured variables. I will try to think of a better comment.

clang/lib/Sema/SemaExpr.cpp
19753

isVariableAlreadyCapturedInScopeInfo is kinda badly name because it will adjust DeclRefType by adding const as necessary.

if we capture a parameter, and then use it in a concept - which are not checked from within the scope of the lambda, we need to add const to it, which we can only do by reordering these paths. It's a bit subtle, and could do with some improvement as I'm not sure parameters will always be const correct in attributes currently.
But I tried to do a minimal fix

cor3ntin updated this revision to Diff 552329.Aug 22 2023, 6:17 AM

Improve comment

@aaron.ballman Should we land that? it already missed rc3...

I agree with this approach, and think we should backport this if at all possible. The original solution is a bit problematic, and I don't believe this solution is particularly risky.

clang/lib/Sema/SemaConcept.cpp
727

curleys required here.

923–924

here too

clang/lib/Sema/SemaExpr.cpp
19753

Yeah, this function is unfortunately doing a lot of work and is a bit of a mess unfortunately. I think I see the logic of the reorder, but its a sensitive enough function that a minimal fix is prudent.

cor3ntin updated this revision to Diff 553047.Aug 24 2023, 2:25 AM

Missing curlies

This revision is now accepted and ready to land.Aug 24 2023, 5:41 AM
This revision was landed with ongoing or failed builds.Aug 24 2023, 7:10 AM
This revision was automatically updated to reflect the committed changes.