www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18529] New: .ptr on arrays can no longer be used in safe


          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 puremagic.com
          Reporter: timothee.cour2 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]

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:

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

Feb 26 2018