Bug 23234 – Delegate literal with inferred return value that requires following alias-this uses class cast instead.

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2022-07-08T11:41:02Z
Last change time
2022-08-31T15:57:18Z
Keywords
industry, pull
Assigned to
No Owner
Creator
FeepingCreature

Comments

Comment #0 by default_357-line — 2022-07-08T11:41:02Z
Consider the following code: class Bar { } class Foo { Bar get() { return new Bar; } alias get this; } void main() { auto foo = new Foo; void test(Bar delegate() dg) { assert(dg() !is null); } test({ return foo; }); } -vcg-ast reveals that the delegate is compiled to `return cast(Bar) foo`, rather than `return foo.get`. `test(delegate Bar() { return foo; })` works.
Comment #1 by maxhaton — 2022-07-08T14:31:53Z
Doesn't this require propagating the type of the delegate from the function declaration back to the delegate expression? Do we do this anywhere else?
Comment #2 by maxhaton — 2022-07-08T14:34:52Z
OK so we do do that for implicit conversions it looks like
Comment #3 by maxhaton — 2022-07-09T21:43:10Z
I think I have a patch for this
Comment #4 by razvan.nitu1305 — 2022-07-13T09:07:10Z
This issue is invalid. The return type of a delegate should be decided based upon what the function returns, not on how the returned result is used. For example, consider this code: alias fd = () => foo; test(fd); By the time the compiler calls test, fd's type has already been established to foo. The fact that -vcg-ast shows that the delegate return contains a cast to Bar is indeed a bug, however, the expectation that it should return `foo.get` is not justified.
Comment #5 by razvan.nitu1305 — 2022-07-13T09:43:48Z
Actually, looking at the internals, it seems that what should the compiler do is actually cast the entire delegate to `Bar delegate()` not the innards of the delegate.
Comment #6 by razvan.nitu1305 — 2022-07-13T11:16:01Z
Further investigation has shown that this bug report is indeed valid. It turns out that dmd does tweak the return type for delegates depending on context. This is surprising for me and maybe this should be documented somewhere.
Comment #7 by dlang-bot — 2022-07-13T11:40:08Z
@RazvanN7 created dlang/dmd pull request #14300 "Fix Issue 23234 - Delegate literal with inferred return value that re…" fixing this issue: - Fix Issue 23234 - Delegate literal with inferred return value that requires following alias-this uses class cast instead https://github.com/dlang/dmd/pull/14300
Comment #8 by dlang-bot — 2022-07-20T06:34:46Z
dlang/dmd pull request #14300 "Fix Issue 23234 - Delegate literal with inferred return value that re…" was merged into stable: - b3fd7754b93ce44e1fc39a26d9c8a878c7d78934 by RazvanN7: Fix Issue 23234 - Delegate literal with inferred return value that requires following alias-this uses class cast instead https://github.com/dlang/dmd/pull/14300
Comment #9 by nick — 2022-08-31T15:57:18Z
> maybe this should be documented somewhere https://github.com/dlang/dlang.org/pull/3401