Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More precise error message for wrong lambda #10096

Closed
dlangBugzillaToGithub opened this issue Nov 4, 2014 · 7 comments
Closed

More precise error message for wrong lambda #10096

dlangBugzillaToGithub opened this issue Nov 4, 2014 · 7 comments

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2014-11-04T10:53:56Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=13683

CC List

Description

This is wrong code:

class Foo {}
void main() {
    import std.algorithm: all;
    Foo[] data;
    assert(data.all!(f => f != null));
}


DMD 2.067alpha gives:

test.d(5,16): Error: template std.algorithm.all cannot deduce function from argument types !((f) => f != null)(Foo[]), candidates are:
..\dmd2\src\phobos\std\algorithm.d(12251,1):        std.algorithm.all(alias pred = "a")


The correct code needs "!is" instead of "!=":

assert(data.all!(f => f !is null));


The error message should tell me more precisely what's the problem.
@dlangBugzillaToGithub
Copy link
Author

nick (@ntrel) commented on 2018-06-23T13:27:33Z

Changing to phobos bug because `all`'s constraint uses:

is(typeof(unaryFun!pred(range.front)))

Instead this could be made a static assert, so at least the user can be informed that the predicate is not callable with Foo.

@dlangBugzillaToGithub
Copy link
Author

greensunny12 commented on 2018-06-24T14:59:55Z

An attempt at adding the better error message to Phobos: https://github.com/dlang/phobos/pull/6607

@dlangBugzillaToGithub
Copy link
Author

github-bugzilla commented on 2018-11-19T07:45:08Z

Commits pushed to master at https://github.com/dlang/phobos

https://github.com/dlang/phobos/commit/887f7558e01535cc570b169d65358a7282361564
Fix Issue 13683 - More precise error message for wrong lambda

https://github.com/dlang/phobos/commit/f953e2c31137ce529da89a256ce9057cb06c8ec5
Merge pull request #6607 from wilzbach/fix-13683

[RFC] Fix Issue 13683 - More precise error message for wrong lambda

@dlangBugzillaToGithub
Copy link
Author

b2.temp commented on 2018-11-19T08:10:02Z

More phobos algorithm that use lambda must be chnaged, in the same fashion, before closing. Seb's PR was "just" a RFC.

@dlangBugzillaToGithub
Copy link
Author

nick (@ntrel) commented on 2023-03-11T15:15:51Z

static assert is an improvement over the constraint, but it still hides the actual error message which should be:

Error: use `!is` instead of `!=` when comparing with `null`

Removing the constraint allows the correct error to be seen. But then the constraint may be needed to disambiguate overloads. Without a compiler solution,  we could have a new trait - __traits(canPass, pred, range.front) which ignores the body of pred:

https://forum.dlang.org/post/lekofsbnstkoqpspramg@forum.dlang.org

In the absence of that, the best I've come up with is:

if (__traits(isTemplate, pred) || is(typeof(pred(range.front))))

Which doesn't check that pred can take that argument when pred is a lambda, but it doesn't hide the actual error unlike the constraint status quo or the static assert way.

A compiler solution might not work in the presence of overloads, because currently failing constraints are only shown when there are no overloads.

@dlangBugzillaToGithub
Copy link
Author

nick (@ntrel) commented on 2023-03-11T15:19:05Z

> Without a compiler solution

BTW that would show the failing lambda with Phobos as is. The static assert PR discussion said a compiler solution is needed rather than having to update every template that accepts a lambda in Phobos.

@thewilsonator
Copy link
Contributor

This now gives

onlineapp.d(5): Error: use `!is` instead of `!=` when comparing with `null`
/dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/searching.d(1622):        instantiated from here: `not!(Foo)`
/dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/searching.d(128):        instantiated from here: `find!(not, Foo[])`
onlineapp.d(5):        instantiated from here: `all!(Foo[])`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants