Thread overview
CTFE calling a template: Error: expression ... is not a valid template value argument
Sep 20, 2012
Jens Mueller
Sep 20, 2012
Peter Alexander
Sep 20, 2012
Timon Gehr
Sep 21, 2012
deadalnix
Sep 21, 2012
Timon Gehr
Sep 21, 2012
Jens Mueller
Sep 21, 2012
Timon Gehr
September 20, 2012
Hi,

I do not understand the following error message given the code:

string foo(string f)
{
    if (f == "somestring")
    {
        return "got somestring";
    }
    return bar!(foo("somestring"));
}

template bar(string s)
{
    enum bar = s;
}

I'll with dmd v2.060 get:
test.d(7):        called from here: foo("somestring")
test.d(7):        called from here: foo("somestring")
test.d(7):        called from here: foo("somestring")
test.d(7): Error: expression foo("somestring") is not a valid template value argument
test.d(12):        called from here: foo("somestring")
test.d(12):        called from here: foo("somestring")
test.d(7): Error: template instance test.bar!(foo("somestring")) error instantiating

In line 7 I call the template bar. But I call with the string that is
returned by the CTFE of foo("somestring") which should return "got
somestring" but instead it seems that an expression is passed. How do I
force the evaluation foo("somestring")?
I haven't found a bug on this.

Jens
September 20, 2012
I'm guessing the problem is that it's trying to call CTFE on a function whose full AST isn't known yet (because it requires the CTFE param, which requires the function etc.)

This could work in theory, but I'm guessing the implementation is tricky.
September 20, 2012
On 09/20/2012 11:22 PM, Jens Mueller wrote:
> Hi,
>
> I do not understand the following error message given the code:
>
> string foo(string f)
> {
>      if (f == "somestring")
>      {
>          return "got somestring";
>      }
>      return bar!(foo("somestring"));
> }
>
> template bar(string s)
> {
>      enum bar = s;
> }
>
> I'll with dmd v2.060 get:
> test.d(7):        called from here: foo("somestring")
> test.d(7):        called from here: foo("somestring")
> test.d(7):        called from here: foo("somestring")
> test.d(7): Error: expression foo("somestring") is not a valid template value argument
> test.d(12):        called from here: foo("somestring")
> test.d(12):        called from here: foo("somestring")
> test.d(7): Error: template instance test.bar!(foo("somestring")) error instantiating
>
> In line 7 I call the template bar. But I call with the string that is
> returned by the CTFE of foo("somestring") which should return "got
> somestring" but instead it seems that an expression is passed. How do I
> force the evaluation foo("somestring")?
> I haven't found a bug on this.
>
> Jens
>

You can file a diagnostics-bug.

The issue is that CTFE can only interpret functions that are fully
analyzed and therefore the analysis of foo depends circularly on
itself. The compiler should spit out an error that indicates the
issue.

You could post an enhancement request to allow interpretation of
incompletely-analyzed functions, if you think it is of any use.

http://d.puremagic.com/issues
September 21, 2012
Le 21/09/2012 01:13, Timon Gehr a écrit :
> You could post an enhancement request to allow interpretation of
> incompletely-analyzed functions, if you think it is of any use.
>

I predict tricky implementation.
September 21, 2012
Timon Gehr wrote:
> On 09/20/2012 11:22 PM, Jens Mueller wrote:
> >Hi,
> >
> >I do not understand the following error message given the code:
> >
> >string foo(string f)
> >{
> >     if (f == "somestring")
> >     {
> >         return "got somestring";
> >     }
> >     return bar!(foo("somestring"));
> >}
> >
> >template bar(string s)
> >{
> >     enum bar = s;
> >}
> >
> >I'll with dmd v2.060 get:
> >test.d(7):        called from here: foo("somestring")
> >test.d(7):        called from here: foo("somestring")
> >test.d(7):        called from here: foo("somestring")
> >test.d(7): Error: expression foo("somestring") is not a valid template value argument
> >test.d(12):        called from here: foo("somestring")
> >test.d(12):        called from here: foo("somestring")
> >test.d(7): Error: template instance test.bar!(foo("somestring")) error instantiating
> >
> >In line 7 I call the template bar. But I call with the string that is
> >returned by the CTFE of foo("somestring") which should return "got
> >somestring" but instead it seems that an expression is passed. How do I
> >force the evaluation foo("somestring")?
> >I haven't found a bug on this.
> >
> >Jens
> >
> 
> You can file a diagnostics-bug.
> 
> The issue is that CTFE can only interpret functions that are fully analyzed and therefore the analysis of foo depends circularly on itself. The compiler should spit out an error that indicates the issue.

That is true. I will file such a diagnostics bug with low priority.

> You could post an enhancement request to allow interpretation of incompletely-analyzed functions, if you think it is of any use.

I think it is.
What do you think?

Jens
September 21, 2012
On 09/21/2012 10:29 AM, deadalnix wrote:
> Le 21/09/2012 01:13, Timon Gehr a écrit :
>> You could post an enhancement request to allow interpretation of
>> incompletely-analyzed functions, if you think it is of any use.
>>
>
> I predict tricky implementation.

This depends on the existing code base. It is not inherently tricky.
September 21, 2012
On 09/21/2012 12:23 PM, Jens Mueller wrote:
> Timon Gehr wrote:
>...
>> The issue is that CTFE can only interpret functions that are fully
>> analyzed and therefore the analysis of foo depends circularly on
>> itself. The compiler should spit out an error that indicates the
>> issue.
>
> That is true. I will file such a diagnostics bug with low priority.
>
>> You could post an enhancement request to allow interpretation of
>> incompletely-analyzed functions, if you think it is of any use.
>
> I think it is.
> What do you think?
>

I think if it has an obvious interpretation (and in this case, it even
has an obvious analysis strategy) it should compile.