Request for Comment assert(__ctfe)

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Apr 8 23:45:43 UTC 2020


On Wednesday, April 8, 2020 3:11:45 AM MDT Dennis via Digitalmars-d wrote:
> On Wednesday, 8 April 2020 at 08:42:52 UTC, Stefan Koch wrote:
> > When existing the language is perfectly able to express (only
> > runs at ctfe).
>
> assert(__ctfe) does not express that you STATICALLY know it only
> runs at ctfe, it is a RUNTIME error and it is impossible to
> statically decide whether the function may actually get called
> outside ctfe context.

Sure, but since you're asserting that it should never run except during
CTFE, turning that into a compiler or linker error shouldn't be a problem.
It's about as clear a case as you can get where the compiler can effectively
turn a runtime condition into a compile-time one, because it has enough
information to do that optimization, and if assert(__ctfe) is used where it
shouldn't be, then it will just result in an error when compiling/linking
your program instead of at runtime. Insisting that the compiler not do that
optimization doesn't really buy us anything, and having it do that
optimization could allow us to reduce compilation times in code with a lot
of CTFE-only functions.

It would arguably be cleaner if __ctfe were done in a way that it were a
static condition rather than a runtime one (and thus static assert could be
used instead), but that's not how it works, and from what I understand of
how CTFE in general works, it's highly unlikely that that will ever change.

- Jonathan M Davis





More information about the Digitalmars-d mailing list