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 22298 - Nested function's scope parameters can be assigned to variables in enclosing function #13530

Merged
merged 1 commit into from
Jan 14, 2022

Conversation

dkorpel
Copy link
Contributor

@dkorpel dkorpel commented Jan 13, 2022

In the initial implementation of lifetimes (#5972), a __gshared counter was added that was incremented whenever a VarDeclaration was created. This is supposed to give a lexical order to local variables, but it fails because there are multiple stages where VarDeclarations are created:

  • Simple local variables are generated in the parser
  • Parameters are created in semantic3 of a FuncDeclaration
  • Generated temporaries and variables from a string mixin are created during function body semantic

Special cases were added to VarDeclaration.enclosesLifetimeOf(VarDeclaration v), where temporaries were compared using line and column number, and parameters assumed that lifetime(parameter) > lifetime(local) always holds. Of course, this can fail when the parameter is from a nested function, and the local is in the enclosing function. It also doesn't account for mixin:

void foo() @safe 
{
    mixin("scope int* x;"); // sequence number 1 from semantic
    scope int* y; // sequence number 0 from parser
    x = y; // allowed, it thinks y has longer lifetime
}

I made the sequence counter increment during symbol semantic of the VarDeclaration instead of in the constructor, and moved the counter to the global struct so it can be reset easier. This is still hacky and not how I want to see lifetimes implemented, but it'll do for now.

@dkorpel dkorpel added the dip1000 memory safety with scope, ref, return label Jan 13, 2022
@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @dkorpel! 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
22298 normal [DIP1000] Nested function's scope parameters can be assigned to variables in enclosing function

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#13530"

@dkorpel dkorpel marked this pull request as ready for review January 13, 2022 20:39
@thewilsonator
Copy link
Contributor

This is a memory safety issue, it should probably go to stable

Copy link
Contributor

@RazvanN7 RazvanN7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

if (va &&
(va.enclosesLifetimeOf(v) && !(v.storage_class & (STC.parameter | STC.temp)) ||
if (va && !va.isDataseg() &&
(va.enclosesLifetimeOf(v) && !(v.storage_class & STC.temp) ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the check for STC.temp still necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe not, let's see what the test suite says.

Copy link
Contributor Author

@dkorpel dkorpel Jan 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std/datetime/systime.d(9085): Error: scope variable `__tup794` assigned to `foundTZ` with longer lifetime

Looks like it's still needed for tuple lowering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-merge Bug Fix dip1000 memory safety with scope, ref, return
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants