www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3950] New: Wrong error message in recursive template call with no !

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3950

           Summary: Wrong error message in recursive template call with no
                    !
           Product: D
           Version: 2.041
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: diagnostic
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2010-03-13 04:51:48 PST ---
This is wrong D2 code (there is a ToString(x % 10) with no bang):


template ToString(ulong x) {
    static if (x < 10)
        enum string ToString = "" ~ cast(char)(x + '0');
    else
        enum string ToString = ToString!(x / 10) ~
                               ToString(x % 10); // missing ! here
}
pragma(msg, ToString!(10));
void main() {}


The compiler gives a wrong error message:
test.d(8): Error: template instance bug3.ToString!(10) recursive expansion

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 13 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3950


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |schveiguy yahoo.com
         Resolution|                            |INVALID


--- Comment #1 from Steven Schveighoffer <schveiguy yahoo.com> 2010-03-15
06:40:52 PDT ---
Inside a template, the symbol without the bang is equivalent to the template
instantiation being created.  In other words, it's not an error simply to use a
template inside itself without the bang.

example:

struct Foo(T)
{
   bool opEquals(ref const Foo f) const
   {
      assert(is(typeof(f) == const(Foo!T));
      return true;
   }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 15 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3950



--- Comment #2 from bearophile_hugs eml.cc 2010-03-15 07:37:30 PDT ---
The "ToString" name with no bang inside the template refers to itself. Then for
the compiler what's the meaning of "ToString(x % 10)" inside this template?

The error message given by DMD doesn't help a lot in finding the bug (the bug
being a missing ! ).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 15 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3950



--- Comment #3 from Steven Schveighoffer <schveiguy yahoo.com> 2010-03-15
07:44:20 PDT ---
The error is a recursive expansion.  I think it's fairly obvious.  The compiler
cannot expand the template because the template depends on the result of
itself.

Many error messages do not reflect an actual logic error.  The compiler is not
a psychiatrist, it cannot determine what the user was trying to do from nuances
in the code.  Instead of suggesting a fix, it simply tells you why what you
requested cannot be done.  Maybe the user meant to do a recursive expansion but
didn't realize that was an error.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 15 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3950



--- Comment #4 from bearophile_hugs eml.cc 2010-03-15 07:59:27 PDT ---
This code:

template ToString(ulong x) {
    static if (x < 10)
        enum string ToString = "" ~ cast(char)(x + '0');
    else
        enum string ToString = ToString!(x / 10) ~
                               ToString!(x % 10); // missing ! here
}
pragma(msg, ToString(10));
void main() {}


Produces this error:
bug1.d(1): Error: template bug1.ToString(ulong x) is not a function template

I'd like a similar error inside the template too because I think ToString(x %
10) is a syntax error inside the the template.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 15 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3950



--- Comment #5 from Steven Schveighoffer <schveiguy yahoo.com> 2010-03-15
08:16:28 PDT ---
The compiler can't figure out whether ToString is a function or not, because it
can't evaluate it.  It's like asking for this:

void 12345()
{
}

to return an error of "12345 cannot be a function" when it actually is a
parsing error.  Often times, the compiler short circuits the compilation before
it can determine root cause, that is just a fact of life.

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