Bug 20494 – Appending derived class array to an interface array.

Status
NEW
Severity
minor
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
Windows
Creation time
2020-01-09T16:20:48Z
Last change time
2024-12-13T19:06:46Z
Assigned to
No Owner
Creator
blackbirdcry806
Moved to GitHub: dmd#17934 →

Comments

Comment #0 by blackbirdcry806 — 2020-01-09T16:20:48Z
import std.stdio; interface IFoo { } class Bar : IFoo { } void main() { IFoo[] f; Bar[] b = new Bar[5]; foreach(ref e; b) e = new Bar(); // this works well and as expected foreach(e; b) f ~= e; // this append the array with invalid values // f ~= b; writeln(f); } Hello there, So we have an interface IFoo, and a class Bar that implements IFoo. The issue occurs when I try to append an array of Bar to an array of IFoo, however when appending elements with a for loop works well and as expected. After appendding the array it seems that the elements appended aren't valid values. I had this issue with both dmd 2.087, dmd 2.089.1 and according to godbolt (https://godbolt.org/z/fTgzic) ldc 1.18.0
Comment #1 by moonlightsentinel — 2020-01-13T16:35:07Z
Test case without phobos: --------------------------- interface IFoo { size_t foo(size_t i); } class Bar : IFoo { size_t foo(size_t i) { return 2 * i; } } void main() { IFoo[] f; Bar[] b = [ new Bar() ]; // this works well and as expected f ~= b[0]; assert(f[0].foo(0) == 0); // this append the array with invalid values f ~= b; assert(f[0].foo(5) == 10); assert(f[1].foo(11) == 22); } --------------------------- The last assert fails with 13 != 22 and never worked before.
Comment #2 by moonlightsentinel — 2020-01-13T16:37:39Z
Windows -m64: 14 != 22 Windows -m32: Access Violation
Comment #3 by ng069976 — 2020-01-13T19:52:34Z
Attempt 1 this should work, but it doesn't: Foo[] x= new Foo[0]; Bar[] y= [ new Bar() ]; x ~= y; x ~= [y[0]]; // This should also work. Attempt 3 this shouldn't work, but some how it does: Foo[] x= new Foo[0]; Bar[] y= [ new Bar() ]; x ~= y[0];
Comment #4 by r.sagitario — 2020-01-13T22:51:40Z
I think this is an "accepts-invalid" issue: class Bar and interface IFoo are not the same pointer, an implicit conversion adds an offset. But appending arrays with implicitly conversion is not supported, just like you cannot append an array of floats to an array of ints or vice versa.
Comment #5 by robert.schadek — 2024-12-13T19:06:46Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17934 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB