Bug 9980 – [CTFE] Allow interpreting function with variable arguments when their values aren't used

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-04-23T02:56:00Z
Last change time
2013-08-14T02:29:43Z
Assigned to
nobody
Creator
verylonglogin.reg

Comments

Comment #0 by verylonglogin.reg — 2013-04-23T02:56:57Z
--- int f(int) { return 1; } int g(bool first, int a, int b) { return first ? a : b; } void main() { int i; static assert(f(i) == 1); static assert(g(true, 2, i) == 2); static assert(g(false, i, 3) == 3); } --- Currently code fails with "variable i cannot be read at compile time" errors.
Comment #1 by verylonglogin.reg — 2013-04-23T02:59:21Z
E.g. this will allow such `isLvalue` library implementation: --- bool isLvalue(T)(auto ref T t) { return __traits(isRef, t); } void main() { int i; static assert( isLvalue(i)); static assert(!isLvalue(i + 1)); } ---
Comment #2 by timon.gehr — 2013-04-23T03:26:01Z
I think fixing the following issue should already allow the implementation of an isLvalue function: http://d.puremagic.com/issues/show_bug.cgi?id=9981 The enhancement is still valid. Are there other use cases you have in mind?
Comment #3 by clugdbug — 2013-08-12T17:34:27Z
The values _are_ used, though. Function arguments are evaluated when the function is called, unless it's a 'lazy' argument. Would you want this to also happen with: static assert(g(true, 2, i + 1) == 2); ? since 'i + 1' is used exactly as much as 'i' is in the original example. So what this request is, is quite difficult to describe. It's kind of "delay evaluation of function arguments in CTFE until the point at which they are used in the function, if the argument has no side-effects".
Comment #4 by timon.gehr — 2013-08-12T18:49:07Z
(In reply to comment #3) > ... > > So what this request is, is quite difficult to describe. It's kind of "delay > evaluation of function arguments in CTFE until the point at which they are used > in the function, if the argument has no side-effects". It should be sufficient to delay error reporting. (But I see no use case.)
Comment #5 by clugdbug — 2013-08-14T02:29:43Z
(In reply to comment #4) > (In reply to comment #3) > > ... > > > > So what this request is, is quite difficult to describe. It's kind of "delay > > evaluation of function arguments in CTFE until the point at which they are used > > in the function, if the argument has no side-effects". > > It should be sufficient to delay error reporting. (But I see no use case.) Yeah. But it's more complicated than the example suggests, since the 'unused variable' may be passed to another CTFE function... And this is actually extremely common. Very many functions only use their arguments by passing them to functions. I doubt you'd actually want this, since it would be hard to track down where the unused variable actually came from. A backtrace is not an answer, you only want the error messages to be exactly the same as they are now. It's also not clear in this proposal what constitutes "using" a value.