D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 23565 - Change `$` semantics so that it works with `.ptr` too
Summary: Change `$` semantics so that it works with `.ptr` too
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2022-12-17 02:53 UTC by basile-z
Modified: 2022-12-20 21:46 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description basile-z 2022-12-17 02:53:57 UTC
We can use `.ptr` in slice and indexes expressions to bypass bounds verification but then we lose the opportunity to use `$` for the subscripts, even if an array and hence a `.length` is possible.

I propose to enhance the semantics of `$` so that this code compiles and execute

```d
void main()
{
    auto b = new int[](2);
    b.ptr[0 .. $] = [1,2]; // b.ptr[0 .. b.length] = [1,2];
    assert(b == [1,2]);
    b.ptr[$-1] = 3;
    assert(b == [1,3]);
}   
```

This would break nothing as this is currently rejected, e.g for the example with

> Error: undefined identifier `$`
Comment 1 Dlang Bot 2022-12-19 03:08:11 UTC
@SixthDot created dlang/dmd pull request #14716 "fix issue 23565 - Allow the use of `$` as index on `.ptr` expressions" fixing this issue:

- fix issue 23565 - Allow the use of `$` as index on `.ptr` expressions
  
  We can use `.ptr` in slice and indexes expressions to bypass bounds verification
  but then we loose the opportunity to use `$` in the expression that give the subscripts,
  even if an array and hence a `.length` is possible.
  
  This commit fix the problem as long as the `.ptr` LHS is a (optionally dotted) variable
  
  The implementation is based on a front-end lowering as the existing
  system was not designed for this case (it finalize the substitution in
  the mid-end function `resolveLengthVar()`)

https://github.com/dlang/dmd/pull/14716