[PATCH] D129488: [Sema] Delay evaluation of std::source_location::current() in default arguments

Ilya Biryukov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 31 09:22:29 PDT 2022


ilya-biryukov added a comment.

In D129488#3761398 <https://reviews.llvm.org/D129488#3761398>, @jyknight wrote:

> I believe this should print `fn=14 fn2=14`. I don't see how we can get that by special-casing calls to `std::source_location::current`, and indeed, with this version of the patch, the program instead prints `fn=14 fn2=9`.

There is another issue in Clang that prevents it from producing correct results with this patch. We should not evaluate immediate calls inside default arguments of immediate function parameters as they are deemed to be inside the immediate function context <https://eel.is/c++draft/expr.const#14.1>. Clang currently evaluates these calls as all parameter scopes are `PotentiallyEvaluatedIfUsed` and `ImmediateFunctionContext` is a different value inside the same enumeration :)

Notice how GCC does not produce an error here as it does not attempt to run the invalid constant calculation:
https://gcc.godbolt.org/z/Encr3Kzbs
I believe the GCC behavior is correct here. If we fix this bug, Clang will stop producing an error too and we will have the behavior you described.

The results of calling `fn()` are also different if you do it inside a default argument for non-consteval function (I use `constexpr` here for static assert, but dynamic calls will give the same result):
https://gcc.godbolt.org/z/P1x8PGsh6
MSVC and GCC disagree about the value for a recursive call here, I would say that GCC behavior seems reasonable to me (it evaluates consteval calls at the first non-immediate function context).


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D129488



More information about the cfe-commits mailing list