This is an archive of the discontinued LLVM Phabricator instance.

Properly diagnose constant evaluation issues at TU scope
ClosedPublic

Authored by aaron.ballman on Mar 8 2022, 6:09 AM.

Details

Summary

We were not creating an evaluation context for the TU scope, so we never popped an evaluation context for it. Popping the evaluation context triggers a number of diagnostics, including warnings about immediate invocations that we were previously missing.

Note: I think we have an additional issue that we should solve, but not as part of this patch. I don't think Clang is properly modeling static initialization as happening before constant expression evaluation. I think structure members members are zero initialized per http://eel.is/c++draft/basic.start.static#1, https://eel.is/c++draft/basic.start.static#2.sentence-2, and http://eel.is/c++draft/dcl.init#general-6.2 and the new test case actually should be accepted. However, it's also worth noting that other compilers behave the way this patch makes Clang behave: https://godbolt.org/z/T7noqhdPr

Diff Detail

Event Timeline

aaron.ballman created this revision.Mar 8 2022, 6:09 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 8 2022, 6:09 AM
aaron.ballman requested review of this revision.Mar 8 2022, 6:09 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 8 2022, 6:09 AM
aaron.ballman added a subscriber: Restricted Project.Mar 8 2022, 6:10 AM
cor3ntin accepted this revision.Mar 8 2022, 6:33 AM

There is already an evaluation context created in Sema but it's never popped back.
And because we rely heavily on there always being at least one evaluation context (ExprEvalContexts.back() is used all over the place for example), i think it make somewhat sense to have 2.

So this looks good to me. I wonder if we need a comment?

clang/test/SemaCXX/cxx2a-consteval.cpp
699

I know there are more patches coming up and i don't remember what's broken or not, but if at all possible, can you add a test for a variable template instantiation too?

This revision is now accepted and ready to land.Mar 8 2022, 6:33 AM
erichkeane accepted this revision.Mar 8 2022, 6:40 AM

Added a comment and an additional test case.

aaron.ballman closed this revision.Mar 8 2022, 7:20 AM

Thanks for the reviews! I've committed in 1c55f05c6a6b58f8cc7b15a37e79753fb8abe3e3.