Issue 20077 - Bogus 'need this' for lambda used inside struct
Summary: Bogus 'need this' for lambda used inside struct
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2019-07-23 19:45 UTC by Simen Kjaeraas
Modified: 2023-02-02 14:40 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Simen Kjaeraas 2019-07-23 19:45:08 UTC
This fails with 'function `S.test!((a) => a).test` need this to access member test':

struct S {
    pragma(msg, test!(a => a));
}

int test(alias fn)() {
    return fn(3);
}

Clearly, a => a does not need access to `this` in S, and something's gone wrong in the inference here.
Comment 1 Luís Ferreira 2020-10-19 17:04:50 UTC
This also happens on accessing static fields on a struct.

auto foobar(alias func)(string val)
{
    return func(val);
}

struct S {
    static immutable foo = "foo";
    static immutable bar = foobar!((v) => v[0])(S.foo);
}
Comment 2 Luís Ferreira 2020-10-19 17:40:41 UTC
This seems related to what's happening on #21332.

When type is explicitly known on the lambda, this doesn't happen. See this snippet here:

```d
auto foobar(alias func)(string val)
{
    return func(val);
}

struct S {
    static immutable foo = "foo";
    static immutable bar = foobar!((string v) => v[0])(S.foo);
}
```

This is the exact same code snippet reported on the latest comment, but the type of `v` is explicitly declared.