Thread overview
[Issue 408] New: Template instance matching doesn't work sometimes
Oct 08, 2006
d-bugmail
Oct 08, 2006
d-bugmail
Oct 16, 2006
d-bugmail
Instance type of a template value type parameter | Was: [Issue 408]
Oct 22, 2006
Bruno Medeiros
Oct 16, 2006
d-bugmail
October 08, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=408

           Summary: Template instance matching doesn't work sometimes
           Product: D
           Version: 0.169
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: burton-radons@smocky.com


Code fails compilation with:

        argh.d(5): function math.matrix.f!(2u).f (S ) does not match argument
types (S )
        argh.d(5): cannot implicitly convert expression (*this) of type S to S

As far as I can tell it is valid code.

==
        struct S (uint C)
        {
                void argh ()
                {
                        f! (C) (*this);
                }
        }

        template f (uint C)
        {
                void f (S! (C) a)
                {
                }
        }

        alias S! (2) s;


-- 

October 08, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=408


burton-radons@smocky.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |burton-radons@smocky.com




------- Comment #1 from burton-radons@smocky.com  2006-10-08 14:15 -------
Found it. If a template instantiation literal value is given the type "uint", D casts it to "int" implicitly so it no longer works against a second instantiation. So this works:

        struct O (int T)
        {
                .O! (T) f () { return *this; }
        }

        alias O! (2) o;

But this fails:

        struct O (uint T)
        {
                .O! (T) f () { return *this; }
        }

        alias O! (2) o;


-- 

October 16, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=408


bugzilla@digitalmars.com changed:

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




------- Comment #2 from bugzilla@digitalmars.com  2006-10-16 04:20 -------
This is working as designed (although the error message isn't great). The issue is, is the template instance made from the argument before the cast to the template value type, or after? D's design is to do make it from the original type. In the third example, 2 is an int, but T is a uint, hence the two instances are not the same.

The reason for this is so that template specializations can be added without upsetting the name mangling.


-- 

October 16, 2006
http://d.puremagic.com/issues/show_bug.cgi?id=408





------- Comment #3 from burton-radons@smocky.com  2006-10-16 12:13 -------
It's not complaining about the outside template instantiation, it's complaining about the nested one. In other words, 2 is treated happily as a uint by an explicit instantiation, but is not treated as anything compatible to a uint by a dependent instantiation.

This rule (which doesn't exist in the documentation) isn't even applied consistently! This, which has EXACTLY the same issue as the first example, works quite happily:

        struct S (float C)
        {
                void argh ()
                {
                        f! (C) (*this);
                }
        }

        template f (float C)
        {
                void f (S! (C) a)
                {
                }
        }

        alias S! (2) s;

Try to make a design of this and you'll come out with spagghetti, there's no rhyme or reason to it.


-- 

October 22, 2006
d-bugmail@puremagic.com wrote:
> http://d.puremagic.com/issues/show_bug.cgi?id=408
> 
> 
> ------- Comment #2 from bugzilla@digitalmars.com  2006-10-16 04:20 -------
> This is working as designed (although the error message isn't great). The issue
> is, is the template instance made from the argument before the cast to the
> template value type, or after? D's design is to do make it from the original
> type. In the third example, 2 is an int, but T is a uint, hence the two
> instances are not the same.
> 
> The reason for this is so that template specializations can be added without
> upsetting the name mangling.
> 
> 

I'm not following here, would someone explain what and how is this situation that this behavior aims to fix ("upsetting the name mangling" when adding specializations) ?


-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D