3990 – Deferencing a dynamic array as pointer

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3990 - Deferencing a dynamic array as pointer
Summary: Deferencing a dynamic array as pointer
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid, patch
: 1381 4772 (view as issue list)
Depends on:
Blocks:
Reported: 2010年03月20日 05:57 UTC by bearophile_hugs
Modified: 2014年08月31日 04:46 UTC (History)
4 users (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010年03月20日 05:57:22 UTC
This D2 code is currrently allowed, but I think it has to become a syntax error, dynamic arrays are not pointers:
void main() {
 int[] a1 = [5, 4, 3];
 assert(*a1 == 5);
 alias typeof(a1) T1;
 assert(is(typeof(*T1)));
 int* p1 = cast(int*)a1;
 assert(p1 == a1.ptr);
}
----------------
Similar code can be written for a fixed-size array like:
int[3] a2 = [5, 4, 3];
For fixed-size arrays such conversions to pointers can be more acceptable.
Comment 1 bearophile_hugs 2010年05月06日 04:15:13 UTC
Another case that I think is related:
import std.stdio: writeln;
struct Arr(int N) {
 int[N] data;
 alias data this;
}
void main() {
 auto p = new Arr!(10);
 *p = 10;
 writeln(p.data); // Output: 10 10 10 10 10 10 10 10 10 10
}
Comment 2 bearophile_hugs 2011年04月26日 10:39:52 UTC
More very good rationale from Steven Schveighoffer, that I think settles the situation:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=135391 
Comment 3 Kenji Hara 2011年10月31日 04:30:21 UTC
(In reply to comment #0)
1.
> assert(*a1 == 5);
> alias typeof(a1) T1;
> assert(is(typeof(*T1)));
Dereferencing dynamic or static array without -d option is deprecated.
I think this is 'accepts-invalid'.
2.
> int* p1 = cast(int*)a1;
> assert(p1 == a1.ptr);
Explicit casting is still valid.
 cast(int*)a1
means
 cast(int*)a1.ptr
Comment 4 Kenji Hara 2011年10月31日 04:33:36 UTC
(In reply to comment #1)
This is not related.
> auto p = new Arr!(10);
> *p = 10;
means
 Arr!(10)* p = new Arr!(10);
 (*p).data = 10; // see alias this after deref-ing of p
And, the assignment an element type into static array is valid.
 int[10] sa;
 sa = 10; // translated to sa[] = 10
Comment 5 bearophile_hugs 2011年10月31日 05:36:43 UTC
(In reply to comment #3)
> > assert(*a1 == 5);
> > alias typeof(a1) T1;
> > assert(is(typeof(*T1)));
> 
> Dereferencing dynamic or static array without -d option is deprecated.
> I think this is 'accepts-invalid'.
Don agrees:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=148054
> 2.
> > int* p1 = cast(int*)a1;
> > assert(p1 == a1.ptr);
> 
> Explicit casting is still valid.
> 
> cast(int*)a1
> 
> means
> 
> cast(int*)a1.ptr
I don't see the need to accept this cast. There is the ".ptr" so this cast is not useful in practice. And generally this cast is refused by DMD 2.056 as you see in the following program, so you are introducing/leaving an useless special case:
struct Foo {
 int* p;
 size_t n;
}
void main() {
 Foo f;
 auto x = cast(int*)f;
}
So I think cast(int*)a1 should be forbidden.
Comment 6 bearophile_hugs 2011年10月31日 05:39:53 UTC
(In reply to comment #4)
> This is not related.
You are right, it's not related, it's a different problem.
> And, the assignment an element type into static array is valid.
> 
> int[10] sa;
> sa = 10; // translated to sa[] = 10
It's currently valid, but I think it should be forbidden. I think D should require the [] here. See bug 3971
Comment 7 Kenji Hara 2011年10月31日 07:43:01 UTC
(In reply to comment #5)
> I don't see the need to accept this cast. There is the ".ptr" so this cast is
> not useful in practice. And generally this cast is refused by DMD 2.056 as you
> see in the following program, so you are introducing/leaving an useless special
> case:
> 
[snip]
> So I think cast(int*)a1 should be forbidden.
The title of this issue is "Deferencing a dynamic array as pointer".
Therefore even if you think it is not useful, we should not discuss it in here.
Instead, you can file it as a new issue.
Comment 8 Kenji Hara 2011年10月31日 07:49:27 UTC
https://github.com/D-Programming-Language/dmd/pull/483 
Comment 9 bearophile_hugs 2011年10月31日 12:24:36 UTC
(In reply to comment #7)
Thank you for the patch.
> The title of this issue is "Deferencing a dynamic array as pointer".
> Therefore even if you think it is not useful, we should not discuss it in here.
>
> Instead, you can file it as a new issue.
I have lumped them together because to me they look like quite similar issues: in both cases an array is seen as a pointer. But OK, I have opened the new report bug 6869
Comment 11 Don 2011年12月22日 00:48:28 UTC
*** Issue 1381 has been marked as a duplicate of this issue. ***
Comment 12 yebblies 2012年01月29日 03:37:16 UTC
*** Issue 4772 has been marked as a duplicate of this issue. ***
Comment 13 yebblies 2014年08月28日 15:28:55 UTC
Deprecation -> Error
https://github.com/D-Programming-Language/dmd/pull/3912 
Comment 14 github-bugzilla 2014年08月31日 04:46:07 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dmd
https://github.com/D-Programming-Language/dmd/commit/5bbacc54e37a036ff5288135bd5ae9f8868ed40c
Fix Issue 3990 - Deferencing a dynamic array as pointer
Deprecation -> Error
https://github.com/D-Programming-Language/dmd/commit/6adcefdfab9e304944eb552ed3b835d3bf979ec3
Merge pull request #3912 from yebblies/issue3990
Issue 3990 - Deferencing a dynamic array as pointer


AltStyle によって変換されたページ (->オリジナル) /