Issue 14962 - [REG2.068] compiler inference of attributes for nested map seems broken
Summary: [REG2.068] compiler inference of attributes for nested map seems broken
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 regression
Assignee: No Owner
URL:
Keywords: pull, rejects-valid
Depends on:
Blocks:
 
Reported: 2015-08-25 18:03 UTC by Steven Schveighoffer
Modified: 2015-09-02 04:10 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 Steven Schveighoffer 2015-08-25 18:03:09 UTC
Not sure if these are related, but they seem similar:

import std.algorithm;

struct Foo {
    int baz(int v) {
        static int id;
        return v + id++;
    }
    void bar() {
        auto arr1 = [1, 2, 3];
        auto arr2 = [4, 5, 6];
        arr1.map!(i =>
                  arr2.map!(j => baz(i + j))
                 );
    }
}

void main() {
} 

testfoo.d(12): Error: pure function 'testfoo.Foo.bar.__lambda1!int.__lambda1.__lambda2' cannot call impure function 'testfoo.Foo.baz'
/Users/steves/git/dmd2/osx/bin/../../src/phobos/std/algorithm/iteration.d(459):        instantiated from here: MapResult!(__lambda2, int[])
testfoo.d(12):        instantiated from here: map!(int[])
/Users/steves/git/dmd2/osx/bin/../../src/phobos/std/algorithm/iteration.d(549):        instantiated from here: __lambda1!int
/Users/steves/git/dmd2/osx/bin/../../src/phobos/std/algorithm/iteration.d(459):        instantiated from here: MapResult!(__lambda1, int[])
testfoo.d(11):        instantiated from here: map!(int[])

Another case:

import std.range;
import std.algorithm;

class Foo {
    int baz() {    return 1;}
    void bar() {
        auto s = [1].map!(i => baz()); // compiles
        auto r = [1].map!(i => [1].map!(j => baz())); // error
    }
}

testfoo.d(8): Error: need 'this' for 'baz' of type 'int()'
/Users/steves/git/dmd2/osx/bin/../../src/phobos/std/algorithm/iteration.d(459):        instantiated from here: MapResult!(__lambda2, int[])
testfoo.d(8):        instantiated from here: map!(int[])
/Users/steves/git/dmd2/osx/bin/../../src/phobos/std/algorithm/iteration.d(549):        instantiated from here: __lambda2!int
/Users/steves/git/dmd2/osx/bin/../../src/phobos/std/algorithm/iteration.d(459):        instantiated from here: MapResult!(__lambda2, int[])
testfoo.d(8):        instantiated from here: map!(int[])

I'm going to file this as a dmd issue, because it seems like it's a case of attribute inference for the lambdas, not an issue with map.
Comment 1 Kenji Hara 2015-08-27 09:06:58 UTC
Two cases have different root issues, but had been introduce at once by:
https://github.com/D-Programming-Language/dmd/pull/4464
Comment 2 Kenji Hara 2015-08-28 03:42:48 UTC
(In reply to Steven Schveighoffer from comment #0)
> Another case:
> 
> import std.range;
> import std.algorithm;
> 
> class Foo {
>     int baz() {    return 1;}
>     void bar() {
>         auto s = [1].map!(i => baz()); // compiles
>         auto r = [1].map!(i => [1].map!(j => baz())); // error
>     }
> }
[snip]

I separated the second case into issue 14973.
Comment 3 Kenji Hara 2015-08-28 04:18:47 UTC
The behavior comes from issue 14781. So I also changed it to a regression.

https://github.com/D-Programming-Language/dmd/pull/4970
Comment 4 github-bugzilla 2015-08-30 15:16:50 UTC
Commits pushed to stable at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/0c9f5a272bf47e1ad81d2a8a4c56adfd3461fde7
fix Issue 14962 - compiler inference of attributes for nested map seems broken

It has been revealed since the commit/PR:

f22d9dbb42b6a32d5b4c7c9f3db45e07c9f8aaf6
https://github.com/D-Programming-Language/dmd/pull/4464

By the fix, some attribute inference order bug for template functions had
been fixed - when foo() calls bar!(), and bar!() calls baz!(), the bar!()
attribute should be inferred after the attrs of baz!() determined.

And then, compiler infers the purity of nested two lambdas in the 14962
test case, and the case has been rejected by the 14781 behavior.

https://github.com/D-Programming-Language/dmd/commit/b30556bf2ce1b4e49fdba51ea759725c2fc6e48f
Merge pull request #4970 from 9rnsr/fix14781

[REG2.067/2.068] Issue 14781 & 14962 - fix problematic purity inference introduced in #3626
Comment 5 github-bugzilla 2015-09-02 04:10:19 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/0c9f5a272bf47e1ad81d2a8a4c56adfd3461fde7
fix Issue 14962 - compiler inference of attributes for nested map seems broken

https://github.com/D-Programming-Language/dmd/commit/b30556bf2ce1b4e49fdba51ea759725c2fc6e48f
Merge pull request #4970 from 9rnsr/fix14781