Bug 14962 – [REG2.068] compiler inference of attributes for nested map seems broken
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-08-25T18:03:00Z
Last change time
2015-09-02T04:10:19Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
schveiguy
Comments
Comment #0 by schveiguy — 2015-08-25T18:03:09Z
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.
(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.