Bug 18831 – Weird interaction between std.variant, std.algorithm.iteration.map, and alias this

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2018-05-06T04:20:42Z
Last change time
2020-06-13T19:57:34Z
Assigned to
No Owner
Creator
Neia Neutuladh

Comments

Comment #0 by dhasenan — 2018-05-06T04:20:42Z
--- import std.variant, std.range, std.algorithm; struct Bar { int i; } struct Foo { auto i() { return iota(12) .map!(x => Bar(x)); } alias i this; } void main() { Foo controlGroup = Foo(); Algebraic!(Foo) treatmentGroup = Foo(); } --- The control group works without errors. The treatment group fails with: /home/dhasenan/.local/bin/../include/phobos/std/meta.d(799): Error: template instance `F!(__lambda1)` cannot use local __lambda1 as parameter to non-global template replaceTemplateArgs(T...) /home/dhasenan/.local/bin/../include/phobos/std/meta.d(805): Error: template instance `algebraicaliasthis.Foo.i.staticMap!(replaceTemplateArgs, __lambda1)` error instantiating /home/dhasenan/.local/bin/../include/phobos/std/typecons.d(7788): instantiated from here: staticMap!(replaceTemplateArgs, __lambda1, Result) /home/dhasenan/.local/bin/../include/phobos/std/variant.d(103): instantiated from here: ReplaceType!(This, VariantN!(1LU, Foo), Foo) /home/dhasenan/.local/bin/../include/phobos/std/variant.d(137): instantiated from here: This2Variant!(VariantN!(1LU, Foo), Foo) /home/dhasenan/.local/bin/../include/phobos/std/variant.d(1500): instantiated from here: VariantN!(1LU, Foo) algebraicaliasthis.d(20): instantiated from here: Algebraic!(Foo)
Comment #1 by dhasenan — 2018-05-06T04:36:59Z
Some notes on paring it down: * Omitting the call to map makes this compile. * Omitting the alias this makes it compile. * You don't actually need a struct Bar; `map!(x => x)` is enough. * You can use an array instead of iota. * Replacing the lambda parameter with a top-level function (map!identity) changes the error to: /home/dhasenan/.local/bin/../include/phobos/std/typecons.d(7784): Error: function algebraicaliasthis.identity(string s) is not callable using argument types () /home/dhasenan/.local/bin/../include/phobos/std/meta.d(799): Error: template instance `std.typecons.ReplaceType!(This, VariantN!(1LU, Foo), Foo).F!(identity)` error instantiating /home/dhasenan/.local/bin/../include/phobos/std/meta.d(805): instantiated from here: staticMap!(replaceTemplateArgs, identity) /home/dhasenan/.local/bin/../include/phobos/std/typecons.d(7788): instantiated from here: staticMap!(replaceTemplateArgs, identity, string[]) /home/dhasenan/.local/bin/../include/phobos/std/variant.d(103): instantiated from here: ReplaceType!(This, VariantN!(1LU, Foo), Foo) /home/dhasenan/.local/bin/../include/phobos/std/variant.d(137): instantiated from here: This2Variant!(VariantN!(1LU, Foo), Foo) /home/dhasenan/.local/bin/../include/phobos/std/variant.d(1500): instantiated from here: VariantN!(1LU, Foo) algebraicaliasthis.d(16): instantiated from here: Algebraic!(Foo)
Comment #2 by snarwin+bugzilla — 2020-06-13T19:57:34Z
The given example compiles successfully as of DMD 2.092.1.