Bug 21538 – Overriding with more attributes on delegate parameter is allowed

Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-01-10T01:03:29Z
Last change time
2021-11-13T12:19:30Z
Keywords
accepts-invalid, pull, safe
Assigned to
No Owner
Creator
Bolpat

Comments

Comment #0 by qs.il.paperinik — 2021-01-10T01:03:29Z
interface I { void f(const void delegate() dg) const @safe; } class C : I { // this overrride should not be legal override void f(const void delegate() @safe dg) const @safe { } } void main() @safe { const void delegate() @system dg = { }; C c = new C; // c.f(dg); // error, expected (cast(I) c).f(dg); // no error }
Comment #1 by qs.il.paperinik — 2021-01-10T01:09:13Z
DMD rejected the override from version 2.066 to 2.074.1; the regression affects DMD since 2.075.1. Just to prove that the cast and the actual delegate isn't the issue, the following compiles, too: auto dg = delegate() @system { int* p; int x; p = &x; }; // unsafe actions I i = new C; i.f(dg); // okay, unexpeced It works with function pointers, too: auto fp = () @system { int* p; int x; p = &x; }; i.g(fp);
Comment #2 by razvan.nitu1305 — 2021-01-27T11:27:08Z
From spec [1]: "A function in a derived class with the same name and covariant with a function in a base class overrides that function". Also, [2] (point 8) defines covariance: "A covariant function has a type that is implicitly convertible to the type of the overridden function.". Finally, [3] (point 6) shows that @safe functions are covariant with @system functions. PR that explicitly introduced this: https://github.com/dlang/dmd/pull/6731 . I think that this case was simply overlooked. Functions that implement interface functions should match exactly. [1] https://dlang.org/spec/function.html#function-inheritance [2] https://dlang.org/spec/function.html#virtual-functions [3] https://dlang.org/spec/function.html#safe-functions
Comment #3 by dlang-bot — 2021-01-27T16:10:54Z
@RazvanN7 created dlang/dmd pull request #12160 "Fix Issue 21538 - Overriding with more attributes on delegate parameter is allowed" fixing this issue: - Fix Issue 21538 - Overriding with more attributes on delegate parameter is allowed https://github.com/dlang/dmd/pull/12160
Comment #4 by qs.il.paperinik — 2021-03-31T00:59:28Z
*** Issue 21602 has been marked as a duplicate of this issue. ***
Comment #5 by moonlightsentinel — 2021-11-05T00:23:57Z
*** Issue 20409 has been marked as a duplicate of this issue. ***
Comment #6 by dlang-bot — 2021-11-05T00:45:05Z
@MoonlightSentinel created dlang/dmd pull request #13267 "Fix 21538,20904: Correct implicitConvTo order for delegate parameters in `Type.covariant`" fixing this issue: - Fix 21538,20904: Correct implicitConvTo order for delegate parameters... ... in `Type.covariant` The ordering was inversed when compared to function pointers. That caused the check to accept e.g. less restrictive attributes for callbacks. Also changed the equality checks for `TypeFuntion` and `TypeDelegate` to `equals` because `==` didn't call the former and hence caused problems for duplicate instances. https://github.com/dlang/dmd/pull/13267
Comment #7 by dlang-bot — 2021-11-13T12:19:30Z
dlang/dmd pull request #13267 "Fix 21538,20904: Correct implicitConvTo order for delegate parameters in `Type.covariant`" was merged into master: - f81a1ccaeb0aa3d5575332ceeca4280b5c585d7d by MoonlightSentinel: Fix 21538,20904: Correct implicitConvTo order for delegate parameters... ... in `Type.covariant` The ordering was inversed when compared to function pointers. That caused the check to accept e.g. less restrictive attributes for callbacks. Also changed the equality checks for `TypeFuntion` and `TypeDelegate` to `equals` because `==` didn't call the former and hence caused problems for duplicate instances. https://github.com/dlang/dmd/pull/13267