Bug 6869 – Disallow array to pointer cast

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-10-31T12:22:10Z
Last change time
2023-05-13T12:52:22Z
Keywords
accepts-invalid
Assigned to
No Owner
Creator
bearophile_hugs
See also
https://issues.dlang.org/show_bug.cgi?id=23919

Comments

Comment #0 by bearophile_hugs — 2011-10-31T12:22:10Z
This is a spinoff of bug 3990 In DMD 2.056 this code compiles: void main() { int[] a1 = [5, 4, 3]; int* p1 = cast(int*)a1; // no compile error here } Similar code using user-created struct doesn't compile: struct Foo { int* p; size_t n; } void main() { Foo f; auto x = cast(int*)f; // compile error here } I don't see the need to accept this cast, because allowing the array to pointer cast means introducing/leaving an useless special case, and there in practice this special case is not useful because arrays have the ptr property: struct Foo { int* p; size_t n; } void main() { Foo f; auto x = f.ptr; // OK } So I think cast(int*)a1 should be forbidden.
Comment #1 by bearophile_hugs — 2011-10-31T17:38:33Z
Comment #2 by bearophile_hugs — 2012-01-31T19:49:31Z
Reverted to bug, this isn't an enhancement, it's a bug.
Comment #3 by yebblies — 2012-01-31T20:31:18Z
How is it a bug? Implicit conversions from arrays to pointers have been deprecated, but as far as I know this is an intentional feature of D, regardless of if it's a useful/bug prone one or not. Is there any reason you know of why this is not working as intended?
Comment #4 by bearophile_hugs — 2012-02-05T06:10:09Z
(In reply to comment #3) > How is it a bug? Implicit conversions from arrays to pointers have been > deprecated, but as far as I know this is an intentional feature of D, > regardless of if it's a useful/bug prone one or not. Is there any reason you > know of why this is not working as intended? OK, it's an enhancement then.
Comment #5 by salihdb — 2023-05-13T12:52:22Z
So are we going to see the problem reported here as a compilation error?