Thread overview
[Issue 9980] New: [CTFE] Allow interpreting function with variable arguments when their values aren't used
Apr 23, 2013
Denis Shelomovskij
Apr 23, 2013
Denis Shelomovskij
Apr 23, 2013
timon.gehr@gmx.ch
Aug 13, 2013
Don
Aug 13, 2013
timon.gehr@gmx.ch
Aug 14, 2013
Don
April 23, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9980

           Summary: [CTFE] Allow interpreting function with variable
                    arguments when their values aren't used
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: verylonglogin.reg@gmail.com


--- Comment #0 from Denis Shelomovskij <verylonglogin.reg@gmail.com> 2013-04-23 13:56:57 MSD ---
---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 23, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9980



--- Comment #1 from Denis Shelomovskij <verylonglogin.reg@gmail.com> 2013-04-23 13:59:21 MSD ---
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));
}
---

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 23, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9980


timon.gehr@gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |timon.gehr@gmx.ch


--- Comment #2 from timon.gehr@gmx.ch 2013-04-23 03:26:01 PDT ---
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?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 13, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9980



--- Comment #3 from Don <clugdbug@yahoo.com.au> 2013-08-12 17:34:27 PDT ---
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".

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 13, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9980



--- Comment #4 from timon.gehr@gmx.ch 2013-08-12 18:49:07 PDT ---
(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.)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 14, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=9980


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WONTFIX


--- Comment #5 from Don <clugdbug@yahoo.com.au> 2013-08-14 02:29:43 PDT ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------