-
-
Notifications
You must be signed in to change notification settings - Fork 609
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
Issue 2095 - arrays of classes should not implicitly convert to arrays of base classes #110
Conversation
…s of base classes
In the current compiler the following code is allowed:
class A {}
class B : A {}
B[] b = [new B()];
A[] a = b;
a[0] = new A();
This aliasing allow you to create an invalid array of B.
This is fixed by ensuring the element type of the target is not mutable.
|
This is terrific, but perhaps a bit lax. Are we sure there's never a pointer adjustment when converting from derived to base? How does conversion to interfaces (as opposed to base class) work? If we're solid on the above, this is very expressive. I suggest you also allow conversion of immutable(D)[] to immutable(B)[]. With that we'd have a very powerful battery of conversions. |
|
From my understanding, there is never a pointer adjustment when converting from a derived class to a base class. Despite this, the code actually explicitly checks the offset is zero! I'm not 100% sure on the interface abi, but I think casting to an interface always requires an offset. It might be possible to support with C++ interfaces. immutable(D)[] to immutable(B)[] was already supported. This patch only disallows the single case D[] -> B[]. |
|
It seems to me that static array has same problem. |
|
Good point! static -> dynamic should be disallowed in the same way. static -> static is still ok, as no aliasing occurs. |
fix std.random uniform for ulong
Issue 2095 - arrays of classes should not implicitly convert to arrays of base classes
| Object[] foo38(C38[3] c) | ||
| { Object[] x = c; | ||
| const(Object)[] foo38(C38[3] c) | ||
| { const(Object)[] x = c; | ||
| return x.dup; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This currently causes the error:
test28.d(767): Error: cannot implicitly convert element type const(Object) to mutable in x.dup
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, some of my pull requests need slight tweaking to bring them up to date.
I'll update them when I can, but it won't be today.
It's probably best not to bother with any of mine that currently fail on http://yebblies.com/results/
In the current compiler the following code is allowed:
class A {}
class B : A {}
B[] b = [new B()];
A[] a = b;
a[0] = new A();
This aliasing allow you to create an invalid array of B.
This is fixed by ensuring the element type of the target is not mutable.
http://d.puremagic.com/issues/show_bug.cgi?id=2095