This is an archive of the discontinued LLVM Phabricator instance.

[clang] Fix aggregate initialization inside lambda constexpr
ClosedPublic

Authored by Fznamznon on Feb 27 2023, 5:47 AM.

Details

Summary

Constant evaluator only considered access to this pointer to be
possible if this poitners was captured. However this can also appear if
there was a default member initializer.

Fixes https://github.com/llvm/llvm-project/issues/60936

Diff Detail

Event Timeline

Fznamznon created this revision.Feb 27 2023, 5:47 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 27 2023, 5:47 AM
Fznamznon requested review of this revision.Feb 27 2023, 5:47 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 27 2023, 5:47 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Thank you for this patch, it looks good.

clang/lib/AST/ExprConstant.cpp
8763–8766

It might be worth it to review all the examples here: https://eel.is/c++draft/expr.prim.lambda

and make sure the test we have actually covers all the scenarios. It looks like we capture most of them but I have not gone over them fully.

clang/test/SemaCXX/lambda-expressions.cpp
673

It would be helpful to add a comment about the implicit this access.

Also please add a release note before landing the fix, thank you.

Fznamznon updated this revision to Diff 501084.Feb 28 2023, 4:15 AM

Apply suggestions and rebase.

Fznamznon added inline comments.Feb 28 2023, 6:54 AM
clang/lib/AST/ExprConstant.cpp
8763–8766

At least the ones about this capture seem to be working and covered. Not all of them have the same behavior as described though, for example on https://eel.is/c++draft/expr.prim.lambda#closure-6 clang complains about missing capture, though gcc agrees https://godbolt.org/z/hfxbEP5fW , so this is probably a bug in the example. Anyway I think this is a bit out of the scope of the patch.

shafik accepted this revision.Feb 28 2023, 10:21 AM

LGTM, the modules failures is a previous known issue.

clang/lib/AST/ExprConstant.cpp
8763–8766

I will dig into that discrepancy.

This revision is now accepted and ready to land.Feb 28 2023, 10:21 AM
shafik added inline comments.Feb 28 2023, 10:45 AM
clang/lib/AST/ExprConstant.cpp
8763–8766

If you move the lambda to be a global, it works: https://godbolt.org/z/84ax7518o which is what the original example has.

Thanks for the review @shafik .

clang/lib/AST/ExprConstant.cpp
8763–8766

Yes, this is understandable. But in the example there is also an assert which doesn't do well in global scope.

BTW, in case this is interesting, another one that doesn't seem to be working with clang is https://eel.is/c++draft/expr.prim.lambda#capture-example-2
https://godbolt.org/z/dP71j3MxK.

shafik added inline comments.Mar 1 2023, 11:45 AM
clang/lib/AST/ExprConstant.cpp
8763–8766

Yes, this is understandable. But in the example there is also an assert which doesn't do well in global scope.

Oh interesting, I missed that one!

BTW, in case this is interesting, another one that doesn't seem to be working with clang is https://eel.is/c++draft/expr.prim.lambda#capture-example-2
https://godbolt.org/z/dP71j3MxK.

Another interesting one, if we change it to int y clang diagnoses it: https://godbolt.org/z/rjx71qj8j

I am not sure what the (y) means I think that should be ill-formed, seems like a clang bug. Let me look at it some more.

Thank you for doing this deeper look.

shafik added inline comments.Mar 1 2023, 1:53 PM
clang/lib/AST/ExprConstant.cpp
8763–8766

(y) is a type b/c it is a template parameter, duh!

I filed a bug report against clang: https://github.com/llvm/llvm-project/issues/61105

This revision was landed with ongoing or failed builds.Mar 6 2023, 12:43 AM
This revision was automatically updated to reflect the committed changes.