www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - CTFE calling a template: Error: expression ... is not a valid

reply Jens Mueller <jens.k.mueller gmx.de> writes:
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
Sep 20 2012
next sibling parent "Peter Alexander" <peter.alexander.au gmail.com> writes:
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.
Sep 20 2012
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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
Sep 20 2012
next sibling parent reply deadalnix <deadalnix gmail.com> writes:
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.
Sep 21 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
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.
Sep 21 2012
prev sibling parent reply Jens Mueller <jens.k.mueller gmx.de> writes:
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
Sep 21 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
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.
Sep 21 2012