Bug 9831 – Error message with failed lambda inference
Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-03-29T11:45:00Z
Last change time
2013-11-17T09:35:36Z
Keywords
diagnostic, pull
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2013-03-29T11:45:40Z
void main() {
immutable int c;
int function(int x) func;
func = x => c;
}
DMD 2.063alpha gives the error:
temp.d(4): Error: cannot infer function literal type from int function(int x)
But I expect an error similar to other cases of unfit lambdas, similar to:
test.d(4): Error: cannot implicitly convert expression (__lambda1) of type int delegate(int) nothrow pure @safe to int function(int).
Comment #1 by yebblies — 2013-11-16T21:16:54Z
x => c does not have a type yet, the problem is in inferring the type, not converting.
I'm tempted to close as invalid, but I'll leave it up to you.
Comment #2 by bearophile_hugs — 2013-11-17T03:59:26Z
(In reply to comment #1)
> x => c does not have a type yet, the problem is in inferring the type, not
> converting.
>
> I'm tempted to close as invalid, but I'll leave it up to you.
Dropping the immutable annotation, this simpler program gives (dmd 2.065alpha):
void main() {
int c;
int function(int x) func;
func = x => c;
}
temp.d(4): Error: cannot infer function literal type from int function(int x)
While if I copy the value of 'c' inside the lambda it compiles with no errors:
void main() {
int c;
int function(int x) func;
func = x => 0;
}
So I think this issue is valid, the type inferencer could become a bit smarter and compile the first case too. But perhaps this issue should be regarded as an Enhancement.
Comment #3 by yebblies — 2013-11-17T04:09:26Z
(In reply to comment #2)
>
> So I think this issue is valid, the type inferencer could become a bit smarter
> and compile the first case too. But perhaps this issue should be regarded as an
> Enhancement.
What makes you think the first case is valid?
void main() {
immutable int c; // immutable local variable
int function(int x) func;
func = x => c; // needs context to access c
}
What exactly would you expect to happen?
A similar case:
void main(string[] args) {
immutable int c = args.length;
int function(int x) func;
func = x => c;
}
Comment #4 by k.hara.pg — 2013-11-17T05:17:18Z
(In reply to comment #0)
> void main() {
> immutable int c;
> int function(int x) func;
> func = x => c;
> }
>
> DMD 2.063alpha gives the error:
>
> temp.d(4): Error: cannot infer function literal type from int function(int x)
I can agree that the current error message does not have so many information, but
> But I expect an error similar to other cases of unfit lambdas, similar to:
>
> test.d(4): Error: cannot implicitly convert expression (__lambda1) of type int
> delegate(int) nothrow pure @safe to int function(int).
the op code should produce "__lambda1 cannot access frame of function D main" error, because required type is function pointer, not delegate.
https://github.com/D-Programming-Language/dmd/pull/2798
Comment #5 by bearophile_hugs — 2013-11-17T05:34:46Z
(In reply to comment #3)
> What makes you think the first case is valid?
>
> void main() {
> immutable int c; // immutable local variable
> int function(int x) func;
> func = x => c; // needs context to access c
> }
>
> What exactly would you expect to happen?
Right, I was wrong, sorry.
(In reply to comment #4)
> the op code should produce "__lambda1 cannot access frame of function D main"
> error, because required type is function pointer, not delegate.
>
> https://github.com/D-Programming-Language/dmd/pull/2798
Thank you, the new error message is good and helps me avoid similar mistakes.
Comment #6 by github-bugzilla — 2013-11-17T09:08:55Z