[Issue 18529] New: .ptr on arrays can no longer be used in @safe code prevents valid code

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Feb 27 06:43:25 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=18529

          Issue ID: 18529
           Summary: .ptr on arrays can no longer be used in @safe code
                    prevents valid code
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Severity: regression
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: timothee.cour2 at gmail.com

We should un-deprecate .ptr on arrays in @safe code.

It prevents valid code, eg:

// from core.demangle
printf( “shifting (%.*s)\n”, cast(int) val.length, val.ptr );
compiler issues error: Error: val.ptr cannot be used in @safe code, use &val[0]
instead

However, `&val[0]` will throw if val.length==0
and the following is perfectly valid even with val.length==0:

printf( “shifting (%.*s)\n”, cast(int) val.length, val.ptr );


NOTE: the rationale for that deprecation was that the pointer can't be
dereferenced. However that's no different from the following:
class A{...}
A a;
// valid in safe code even though `a` is null and can't be derefenced.

In short, &arr[0] does not replace .ptr even in safe code, because arr could
have 0 elements.

See other discussions on slack:
https://dlang.slack.com/archives/C1ZDHBB2S/p1519504756000002
https://dlang.slack.com/archives/C1ZDHBB2S/p1519439288000141

and other use case that gets broken:
int[]a=[0,1];
auto ptr = &a[0];
a.length=0;
a.assumeSafeAppend;
assert(a.ptr == ptr);  // disallowed
assert( &a[0] == ptr);  // would throw bounds error

--


More information about the Digitalmars-d-bugs mailing list