Bug 3921 – Compile time evaluation requirements not well-specified
Status
RESOLVED
Resolution
INVALID
Severity
major
Priority
P2
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-03-10T04:44:00Z
Last change time
2014-02-15T02:45:22Z
Keywords
spec
Assigned to
nobody
Creator
jlquinn
Comments
Comment #0 by jlquinn — 2010-03-10T04:44:19Z
The language of the spec does not clearly state what a compile must be able to evaluate at compile time. Through example, clearly constant folding must be done, to support mixins as shown in various examples, but the full rules of what the D language requires to be done at compile time is not given.
Similarly, CTFE states that the compiler can execute functions at compile time if they meet certain constraints. However, it doesn't clearly state whether the D language requires that these functions be executed at compile time.
Other writing I've seen suggests that CTFE is an optimization, not a language requirement. What exactly is required of any D compiler?
Comment #1 by clugdbug — 2010-03-10T11:21:59Z
Quoting the spec:
"In order to be executed at compile time, the function must appear in a context where it must be so executed"
Note the word MUST. I don't see anything in the spec to indicate that it is optional. It's pretty simple actually -- if the compiler *needs* a constant, and it sees an expression, it attempts to constant-fold that expression. If the expression contains a function call, CTFE will be used. CTFE is not used in any other context - it never happens unless requested.
> Other writing I've seen suggests that CTFE is an optimization, not a language
requirement.
Can you remember where you saw that writing, or got that impression? If it's anywhere public, we need to get rid of it, since it is not correct.
Comment #2 by jlquinn — 2010-03-11T15:21:50Z
(In reply to comment #1)
> Quoting the spec:
>
> "In order to be executed at compile time, the function must appear in a context
> where it must be so executed"
>
> Note the word MUST. I don't see anything in the spec to indicate that it is
> optional.
No, the language above says that the correct function context is necessary, not sufficient. CTFE won't succeed unless the condition is met. It definitely does not say that the compiler is required to try.
> It's pretty simple actually -- if the compiler *needs* a constant,
> and it sees an expression, it attempts to constant-fold that expression. If the
> expression contains a function call, CTFE will be used. CTFE is not used in any
> other context - it never happens unless requested.
In principle, it's simple. But typically constant folding is an optimization, not a requirement in languages, so D is relatively unique in this regard. It's important for the spec to explicitly say so rather than imply through example.
> > Other writing I've seen suggests that CTFE is an optimization, not a language
> requirement.
>
> Can you remember where you saw that writing, or got that impression? If it's
> anywhere public, we need to get rid of it, since it is not correct.
I can't find what I thought I read about. So it's possible I hallucinated that and it's a false impression. I still think that the fact that the compiler is guaranteed to execute expressions and functions at compile time has to be more clearly stated, though. Also, the rules for expressions to be executed at compile time should also be explicitly stated, separate from the function rules.
Comment #3 by bugzilla — 2012-01-23T15:41:42Z
(In reply to comment #2)
> (In reply to comment #1)
> > Quoting the spec:
> >
> > "In order to be executed at compile time, the function must appear in a context
> > where it must be so executed"
> >
> > Note the word MUST. I don't see anything in the spec to indicate that it is
> > optional.
> No, the language above says that the correct function context is necessary, not
> sufficient. CTFE won't succeed unless the condition is met. It definitely
> does not say that the compiler is required to try.
The second 'must' covers it.