D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7529 - IFTI does not support template argument dependent template alias instances as parameter types
Summary: IFTI does not support template argument dependent template alias instances as...
Status: RESOLVED DUPLICATE of issue 1807
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2012-02-16 18:35 UTC by Kyle Foley
Modified: 2020-12-24 02:31 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Kyle Foley 2012-02-16 18:35:01 UTC
DMD 2.058

template Type(T) { alias T Type; }
void f(T)(T, Type!(T)) {}

void main()
{	
	f(0, 0); // fail
	f!(int)(0, 0); // success
}

---

.\ifti_alias.d(6): Error: template ifti_alias.f(T) does not match any function template declaration
.\ifti_alias.d(6): Error: template ifti_alias.f(T) cannot deduce template function from argument types !()(int,int)
Comment 1 timon.gehr 2012-02-16 20:23:46 UTC
This is an enhancement, a very desirable one.
Comment 2 Martin Nowak 2012-02-16 20:49:38 UTC
The issue is what do you make of related cases.

// not deducible
void f(T)(Type!T) {}
f(0);

// ?
void f(T)(Type!T, T) {}
f(0, 0);
Comment 3 timon.gehr 2012-02-16 21:24:43 UTC
Both of those(In reply to comment #2)
> The issue is what do you make of related cases.
> 
> // not deducible
> void f(T)(Type!T) {}
> f(0);
> 
> // ?
> void f(T)(Type!T, T) {}
> f(0, 0);

Both of those should work. The enhancement demands special treatment of templated aliases.

For example, this would work too:

template LList(T){alias Lazy!(List!T)) LList;}
alias f(T)(LList!T){}

f(new LList);
Comment 4 timon.gehr 2012-02-16 21:25:43 UTC
I meant
void f(T)(LList!T){}
Comment 5 Martin Nowak 2012-02-16 22:03:04 UTC
The reason why this works with structs but not with templates is that a struct preserves the full type information while an alias template transforms a type.

void bar(Ty)(Ty) { pragma(msg, Ty); }

struct SList(T) {}
template TList(T) { alias T TList; }

void main()
{
    bar(SList!int.init); // type SList!int => SList!int is preserved
    bar(TList!int.init); // type int       => TList!int is lost
}

Now if you take a function void foo(T)(TList!T) {} and gives you 'TList(T) = int'.
What your asking for is an inverted template 'TList^-1(int) = T'.
Comment 6 timon.gehr 2012-02-17 04:34:10 UTC
(In reply to comment #5)
> The reason why this works with structs but not with templates is that a struct
> preserves the full type information while an alias template transforms a type.
> 
> void bar(Ty)(Ty) { pragma(msg, Ty); }
> 
> struct SList(T) {}
> template TList(T) { alias T TList; }
> 
> void main()
> {
>     bar(SList!int.init); // type SList!int => SList!int is preserved
>     bar(TList!int.init); // type int       => TList!int is lost
> }
> 
> Now if you take a function void foo(T)(TList!T) {} and gives you 'TList(T) =
> int'.
> What your asking for is an inverted template 'TList^-1(int) = T'.

Well, yes. But only for a simple set of cases where this is workable. Basically the alias would need to be expanded before IFTI matching.
Comment 7 Martin Nowak 2012-02-17 06:52:19 UTC
void foo(T)(Unsigned!T val)
{
}

foo(2u);

???
Comment 8 timon.gehr 2012-02-17 06:54:08 UTC
Unsigned is not an alias template. Your example is unrelated to this enhancement.
Comment 9 Kenji Hara 2012-02-17 07:10:54 UTC
I think this is invalid issue.
Reduced case:

void f(T)(T, T) { pragma(msg, T); }
void main()
{
  f(0L, 0);   // first argument is long, and second one is int
  // Error: template test.f(T) does not match any function template declaration
}

In IFTI, arguments don't have any dependencies each other about the deduction.
Comment 10 timon.gehr 2012-02-17 07:20:59 UTC
If you think it is invalid you misunderstand the issue. This is about extending IFTI so that it can match templated aliases.

template Alias(T){ alias Foo!T Alias;}

void foo(T)(Alias!T x){ pragma(msg, T); }

foo(Foo!int); // prints 'int'

What effectively would need to be done at the compiler side:

- If a template parameter type is a template, check whether it is an eponymous alias template (like template Alias(T){ alias Foo!T Alias; }
- If so, expand the template without knowing the type it is instantiated with

void foo(T)(Alias!T x){ ... } => void foo(T)(Foo!T x){ ... }

This is always a valid transformation for eponymous alias templates.

This does not introduce any dependencies across parameters.
Comment 11 Martin Nowak 2012-02-17 07:25:35 UTC
Is that what you mean by alias template.
template Wrap(T) { alias T Wrap; } // no members, no static if

This has a bidirectional mapping, but you can't change the type any longer.
'void foo(T)(Wrap!T val)' would always be equal to 'void foo(T)(T val)'
Comment 12 timon.gehr 2012-02-17 07:35:44 UTC
An alias template is like a function template, just for aliases.

Those are all alias templates:

template A(T){alias T A;}
template B(T){alias Foo!T B;}
template C(T){alias foo.bar.Qux!T C;}
template D(T){alias Foo!(Bar!(Qux!T)) D;}

Those are functions that use them:
void fooa(T)(A!T a){ ... }
void foob(T)(B!T b){ ... }
void fooc(T)(C!T c){ ... }
void food(T)(D!T d){ ... }

Those are the versions that do exactly the same thing but work with IFTI:
void fooa(T)(T a){ ... }
void foob(T)(Foo!T b){ ... }
void fooc(T)(foo.bar.Qux!T c){ ... }
void food(T)(Foo!(Bar!(Qux!T)) d){ ... }

What I am asking for is for IFTI to work the same for the former and latter versions of the functions. This is easy to implement and matches what most programmers would expect the language to be capable of.
Comment 13 Martin Nowak 2012-02-17 07:47:33 UTC
MidAir collision but thanks for the clarification.

Most of those cases can be handled by aliasing
the template declaration rather than the instance.

alias Foo Alias;
void foo(T)(Alias!T x){ pragma(msg, T); }
foo(Foo!int.init);

Which wouldn't work for recursion.
template D(T){alias Foo!(Bar!(Qux!T)) D;}

It seems to me that what you want is an AST macro not a template expansion.
Comment 14 Kenji Hara 2012-02-17 07:59:59 UTC
(In reply to comment #12)
> An alias template is like a function template, just for aliases.
> 
> Those are all alias templates:
> 
> template A(T){alias T A;}
> template B(T){alias Foo!T B;}
> template C(T){alias foo.bar.Qux!T C;}
> template D(T){alias Foo!(Bar!(Qux!T)) D;}
> 
> Those are functions that use them:
> void fooa(T)(A!T a){ ... }
> void foob(T)(B!T b){ ... }
> void fooc(T)(C!T c){ ... }
> void food(T)(D!T d){ ... }
> 
> Those are the versions that do exactly the same thing but work with IFTI:
> void fooa(T)(T a){ ... }
> void foob(T)(Foo!T b){ ... }
> void fooc(T)(foo.bar.Qux!T c){ ... }
> void food(T)(Foo!(Bar!(Qux!T)) d){ ... }
> 
> What I am asking for is for IFTI to work the same for the former and latter
> versions of the functions. This is easy to implement and matches what most
> programmers would expect the language to be capable of.

OK, I almost understand what you expects.

When the two declarations exist,
> template B(T){alias Foo!T B;}
> void foob(T)(B!T b){ ... }

and calling foob with IFTI like follows:
Foo!int x;
foob(x);  // foob(T)(B!T) is converted to foob(T)(Foo!T) 
          // *with expanding template B*, then foob deduces T as int.

OK?

-----
There is some problems about this enhancement.

1) This enhancement requires adding a phase to expanding eponymous templates used as function parameter type. This makes IFTI process more complicate.

2) Some eponymous templates are not one liner.

template X(T) {
   template Y(U) { ... }  // massive type calculation
   alias Y!T X;   // also eponymous template
}
void foo(T)(X!T) { ... }

Should compiler calculate T from X!T? It is almost impossible!

If it only works with one-linear eponymous template, it is less benefit than the semantic complexity.
Comment 15 timon.gehr 2012-02-17 08:05:50 UTC
(In reply to comment #14)
> 
> OK?
> 

Yes.

> -----
> There is some problems about this enhancement.
> 
> 1) This enhancement requires adding a phase to expanding eponymous templates
> used as function parameter type. This makes IFTI process more complicate.
> 

It can presumably be done while semantically analyzing the parameter types.

> 2) Some eponymous templates are not one liner.
> 
> template X(T) {
>    template Y(U) { ... }  // massive type calculation
>    alias Y!T X;   // also eponymous template
> }
> void foo(T)(X!T) { ... }
> 
> Should compiler calculate T from X!T? It is almost impossible!
> 

It shouldn't, because it *is* impossible.

> If it only works with one-linear eponymous template, it is less benefit than
> the semantic complexity.

I disagree. There is almost no semantic complexity.
Comment 16 timon.gehr 2012-02-17 08:06:44 UTC
(In reply to comment #13)
> MidAir collision but thanks for the clarification.
> 
> Most of those cases can be handled by aliasing
> the template declaration rather than the instance.
> 
> alias Foo Alias;
> void foo(T)(Alias!T x){ pragma(msg, T); }
> foo(Foo!int.init);
> 
> Which wouldn't work for recursion.
> template D(T){alias Foo!(Bar!(Qux!T)) D;}
> 
> It seems to me that what you want is an AST macro not a template expansion.

IFTI matching already works that way.
Comment 17 Martin Nowak 2012-02-17 08:44:58 UTC
>IFTI matching already works that way.
No it does not.

With IFTI and structs you recursively match template arguments.
Foo!(Bar!(Baz!T)))
Foo!(Bar!(Baz!int)))

With the alias you need to instantiate the template, probably using a bottom type and then do the above.
Foo!(Bar!(Baz!(T))) => Result!T
Result!int
Comment 18 Steven Schveighoffer 2012-02-17 12:52:32 UTC
Is this not a duplicate of bug 1807?
Comment 19 timon.gehr 2012-02-17 14:20:37 UTC
Good catch.

*** This issue has been marked as a duplicate of issue 1807 ***