D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20298 - Passing field alias to alias predicate doesn't work, predicate needs explicit instantiation
Summary: Passing field alias to alias predicate doesn't work, predicate needs explicit...
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-10-13 17:25 UTC by Vladimir Panteleev
Modified: 2024-12-13 19:05 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 Vladimir Panteleev 2019-10-13 17:25:04 UTC
///////////// test.d /////////////
struct S
{
    int i;
    alias field = i;

    int fun(alias pred)() const
    {
        // Doesn't work:
        //return pred(field);

        // Works:
        alias ipred = pred!int;
        return ipred(field);
    }

    alias fun1 = fun!(i => i + 1);
}
//////////////////////////////////

Error is:

test.d(9,14): Error: template `test.S.__lambda5` cannot deduce function from argument types `!()(const(int)) const`, candidates are:
test.d(16,20):        `__lambda5`
test.d(16,15): Error: template instance `test.S.fun!((i) => i + 1)` error instantiating
Comment 1 RazvanN 2019-10-15 12:37:10 UTC
Isn't the real bug here the fact that calling ipred passes compilation? As far as I could tell the lambda template instantion is inside the scope of the S struct => you cannot call it from fun, since fun may call only const functions and pred is mutable.
Comment 2 Vladimir Panteleev 2019-10-15 12:59:51 UTC
Thanks for looking at this!

(In reply to RazvanN from comment #1)
> As 
> far as I could tell the lambda template instantion is inside the scope of
> the S struct

Hmm! But, shouldn't it infer that since it doesn't access anything other than its arguments, it should not have a context?

I see that ipred is typed as a function not delegate.

> you cannot call it from fun, since fun may call only const
> functions and pred is mutable.

Thanks, I actually didn't realize that fun being const was a crucial part of this. I guess the error message could be better.
Comment 3 dlangBugzillaToGithub 2024-12-13 19:05:51 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/19629

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB