Bug 20000 – Casting to interfaces disallowed in @safe code

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-06-23T21:23:42Z
Last change time
2020-03-21T03:56:38Z
Keywords
safe, spec
Assigned to
No Owner
Creator
Nathan S.

Comments

Comment #0 by n8sh.secondary — 2019-06-23T21:23:42Z
``` interface A {} class C1 {} class C2 : A {} bool isA(Object obj) @safe { if (auto a = cast(A) obj) return true; return false; } void main() { import std.stdio : writeln; writeln(isA(new C1())); writeln(isA(new C2())); } ```
Comment #1 by b2.temp — 2019-06-23T21:55:24Z
This is more subtle: --- module runnable; import std.stdio; extern(C++) interface Foo { void foo(); } extern(C++) interface Bar { void bar(); } extern(C++) class C1 : Foo { void foo(){writeln("do I look like a Bar !!");} } void main(string[] args) { (cast(Bar) new C1).bar(); } --- With the C++ compat this is clearly not @safe. The fix would have to check carefully these details.
Comment #2 by dlang-bot — 2019-06-23T22:13:24Z
@n8sh created dlang/dmd pull request #10080 "Issue 20000 - Casting to interfaces arbitrarily disallowed in @safe code" mentioning this issue: - Issue 20000 - Casting to interfaces arbitrarily disallowed in @safe code https://github.com/dlang/dmd/pull/10080
Comment #3 by dfj1esp02 — 2019-06-24T14:00:17Z
Yeah, should work only for proper D interfaces.
Comment #4 by dlang-bot — 2019-06-25T13:36:23Z
dlang/dmd pull request #10080 "Issue 20000 - Casting to interfaces disallowed in @safe code" was merged into master: - c062cdd2b35a4f40f352ea65194994188a5f9ed8 by Nathan Sashihara: Fix Issue 20000 - Casting to interfaces arbitrarily disallowed in @safe code https://github.com/dlang/dmd/pull/10080