[PATCH] D137415: [clang][Interp] Implement switch statements

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 30 04:26:22 PST 2022


tbaeder marked an inline comment as done.
tbaeder added inline comments.


================
Comment at: clang/test/AST/Interp/switch.cpp:46
+constexpr int withInit() {
+  switch(int a = 2; a) {
+    case 1: return -1;
----------------
aaron.ballman wrote:
> I think it would be good to add a non-trivial init and show that it fails when appropriate. e.g.,
> ```
> struct Weirdo {
>   consteval Weirdo(int);
>   Weirdo(double);
> 
>   int Val = 1;
> };
> 
> constexpr int whatever() {
>   switch (Weirdo W(12); W.Val) {
>   case 1: return 12;
>   default: return 100;
>   }
> }
> 
> constexpr int whatever_else() {
>   switch (Weirdo W(1.2); W.Val) { // Should get an error because of the init not being constexpr
>   case 1: return 12;
>   default: return 100;
>   }
> }
> 
> static_assert(whatever() == 12, "");
> static_assert(whatever_else() == 12, ""); // Shouldn't compile because the function isn't constexpr
> ```
This is unfortunately hard do test with the new constant interpreter right now. It never emits the "never produces a constant expression" diagnostic because I removed the `Run()` call from `isPotentialConstantExpression()`. I need to revert that and instead fix the interpreter to correctly takes this mode into account.

And for the non-constexpr constructor, we will reject the call in the `static_assert()`, but not diagnose anything useful because we're not checking the constructor for constexpr-ness (this is part of https://reviews.llvm.org/D137563 I believe).


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

https://reviews.llvm.org/D137415



More information about the cfe-commits mailing list