Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue 21220 - [DIP1000] scope variable may escape through scope dynamic array parameter #12204

Closed
wants to merge 1 commit into from

Conversation

RazvanN7
Copy link
Contributor

@safe:

struct A
{
    char[] value;                                                                                                                             
}

A f(scope A[] array)
{
    return array[0]; //  this line here escapes the `value`
}

It seems that [1] introduced this behavior on purpose, but I don't understand why. If array is made into a static array then returning array[0] is going to issue an error. However, it does not make any sense to me that the behavior is changed for dynamic arrays.

cc @WalterBright

[1] #8030

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @RazvanN7! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Auto-close Bugzilla Severity Description
21220 regression [DIP1000] scope variable may escape through scope dynamic array parameter

⚠️⚠️⚠️ Warnings ⚠️⚠️⚠️

To target stable perform these two steps:

  1. Rebase your branch to upstream/stable:
git rebase --onto upstream/stable upstream/master
  1. Change the base branch of your PR to stable

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#12204"

@RazvanN7 RazvanN7 added the Severity:Regression PRs that fix regressions label Feb 17, 2021
@PetarKirov
Copy link
Member

I'm afraid that it is by design - scope is not transitive.
I haven't checked, but I expect that replacing the array slice with a pointer to have the same behavior (as it should, as slices have pointers). Static arrays are fundamentally different - they're embedded inside the parent object, and so struct S1 { T x, y, z; } is equivalent to struct S2 { T[3] xyz; } (as far as object lifetime is concerned).

That said, there's plenty of cases where scope should be transitive, so I don't agree with that aspect of the design.

@Geod24
Copy link
Member

Geod24 commented Feb 18, 2021

I'm afraid that it is by design - scope is not transitive.

That's the gist of it. I closed the issue as a duplicate of 20505.
There's also a PR for this: #10951
And yeah, the lack of transitivity really makes it unusable (e.g. #8369 ).

@RazvanN7 RazvanN7 closed this Feb 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants