Skip to content
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

Merged
merged 3 commits into from
Nov 24, 2011

Conversation

yebblies
Copy link
Member

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

…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.
@andralex
Copy link
Member

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.

@yebblies
Copy link
Member Author

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[].

@9rnsr
Copy link
Contributor

9rnsr commented Jun 11, 2011

It seems to me that static array has same problem.
You can fix it in same way.

@yebblies
Copy link
Member Author

Good point! static -> dynamic should be disallowed in the same way. static -> static is still ok, as no aliasing occurs.

braddr pushed a commit to braddr/dmd that referenced this pull request Sep 15, 2011
fix std.random uniform for ulong
WalterBright added a commit that referenced this pull request Nov 24, 2011
Issue 2095 - arrays of classes should not implicitly convert to arrays of base classes
@WalterBright WalterBright merged commit 7adef95 into dlang:master Nov 24, 2011
Object[] foo38(C38[3] c)
{ Object[] x = c;
const(Object)[] foo38(C38[3] c)
{ const(Object)[] x = c;
return x.dup;
Copy link
Member

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

Copy link
Member Author

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants