Bug 7529 – IFTI does not support template argument dependent template alias instances as parameter types

Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-02-16T18:35:01Z
Last change time
2020-12-24T02:31:10Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
Kyle Foley
See also
https://issues.dlang.org/show_bug.cgi?id=1807

Comments

Comment #0 by kyfolee — 2012-02-16T18:35:01Z
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 by timon.gehr — 2012-02-16T20:23:46Z
This is an enhancement, a very desirable one.
Comment #2 by code — 2012-02-16T20:49:38Z
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 by timon.gehr — 2012-02-16T21:24:43Z
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 by timon.gehr — 2012-02-16T21:25:43Z
I meant void f(T)(LList!T){}
Comment #5 by code — 2012-02-16T22:03:04Z
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 by timon.gehr — 2012-02-17T04:34:10Z
(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 by code — 2012-02-17T06:52:19Z
void foo(T)(Unsigned!T val) { } foo(2u); ???
Comment #8 by timon.gehr — 2012-02-17T06:54:08Z
Unsigned is not an alias template. Your example is unrelated to this enhancement.
Comment #9 by k.hara.pg — 2012-02-17T07:10:54Z
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 by timon.gehr — 2012-02-17T07:20:59Z
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 by code — 2012-02-17T07:25:35Z
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 by timon.gehr — 2012-02-17T07:35:44Z
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 by code — 2012-02-17T07:47:33Z
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 by k.hara.pg — 2012-02-17T07:59:59Z
(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 by timon.gehr — 2012-02-17T08:05:50Z
(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 by timon.gehr — 2012-02-17T08:06:44Z
(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 by code — 2012-02-17T08:44:58Z
>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 by schveiguy — 2012-02-17T12:52:32Z
Is this not a duplicate of bug 1807?
Comment #19 by timon.gehr — 2012-02-17T14:20:37Z
Good catch. *** This issue has been marked as a duplicate of issue 1807 ***