[PATCH] D106302: Implement P1937 consteval in unevaluated contexts

Corentin Jabot via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 23 09:32:35 PDT 2021


cor3ntin added inline comments.


================
Comment at: clang/test/SemaCXX/cxx2a-consteval.cpp:612
+static_assert(is_same<long, T>::value);
+
+} // namespace unevaluated
----------------
aaron.ballman wrote:
> cor3ntin wrote:
> > cor3ntin wrote:
> > > aaron.ballman wrote:
> > > > Here's an interesting test case:
> > > > ```
> > > > #include <typeinfo>
> > > > 
> > > > struct S {
> > > >   virtual void f();
> > > > };
> > > > 
> > > > struct D : S {
> > > >   void f() override;
> > > > };
> > > > 
> > > > consteval S *get_s() { return nullptr; }
> > > > 
> > > > void func() {
> > > >   (void)typeid(*get_s());
> > > > }
> > > > ```
> > > > `typeid` still needs to evaluate its operand (due to the polymorphic return type of `*get_s()`), and so you should get a diagnostic about evaluating the side effects by calling `get_s()`. I think this then runs into https://eel.is/c++draft/expr.const#13.sentence-3 and we should diagnose?
> > > Not sure!
> > > Also, in the context of this pr, the question is also whether `decltype(typeid(*get_s()))` should be ill-formed I think
> > Actually, I'm reading the wording again and I really don't know anymore.
> > `get_s()` is a constant expression, right?
> > `*get_s()` is not, I think but is that relevant here
> > 
> > I played with a bunch of things in the code but the more I look at it the less I'm convinced an action is needed.
> The changes to `Sema::CheckForImmediateInvocation()` to check for an unevaluated context and https://eel.is/c++draft/expr.const#13.sentence-3 that say an immediate invocation shall be a constant expression are what got me thinking about this code snippet in the first place. I was trying to decide whether `isUnevaluatedContext()` is correct or not because with `typeid`, it is potentially evaluated (so sometimes it's unevaluated).
> 
> Interestingly, everyone comes up with a different answer: https://godbolt.org/z/TqjGh1he6 and I don't (yet) know who is correct.
@rsmith Can you enlighten us here?
My take is that `get_s()` is a constant expression and therefore an immediate invocation. independently of what `*get_s()` does but I'm not sure if that's a correct reading.  

Thanks a lot!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106302/new/

https://reviews.llvm.org/D106302



More information about the cfe-commits mailing list