D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 18831 - Weird interaction between std.variant, std.algorithm.iteration.map, and alias this
Summary: Weird interaction between std.variant, std.algorithm.iteration.map, and alias...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-05-06 04:20 UTC by Neia Neutuladh
Modified: 2020-06-13 19:57 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Neia Neutuladh 2018-05-06 04:20:42 UTC
---
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 Neia Neutuladh 2018-05-06 04:36:59 UTC
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 Paul Backus 2020-06-13 19:57:34 UTC
The given example compiles successfully as of DMD 2.092.1.