[Issue 2412] New: Hole in type system: array conversion to supertypes
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Fri Oct 10 02:03:05 PDT 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2412
Summary: Hole in type system: array conversion to supertypes
Product: D
Version: 1.035
Platform: PC
OS/Version: Windows
Status: NEW
Keywords: accepts-invalid, spec
Severity: major
Priority: P2
Component: DMD
AssignedTo: bugzilla at digitalmars.com
ReportedBy: fraserofthenight at gmail.com
Automatic conversion of an array of a subtype to an array of its supertype
creates a hole in the type system (that is, you can get an invalid type without
doing any casting). Consider the following legal D code:
module bug;
class A { }
class X : A { void hello() { } }
class Y : A { }
void main()
{
X[] xArray = new X[1];
A[] aArray = xArray;
aArray[0] = new Y();
X x = xArray[0]; // Automatic conversion of Y -> X without cast
x.hello(); // Likely segfault
}
The solution is to require an explicit cast from X[] to A[] (that is, any
subtype array to its supertype).
--
More information about the Digitalmars-d-bugs
mailing list