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 20881, 22790 - make ref-return-scope consistent #13693

Merged
merged 1 commit into from
Mar 21, 2022

Conversation

dkorpel
Copy link
Contributor

@dkorpel dkorpel commented Feb 19, 2022

I'm working on the same issue as Walter: #13691

It's not finished, Phobos doesn't compile because sometimes methods such as std.typecons.Tuple.opBinary!"~" are inferred return ref when they should be return scope, which I'm still debugging. I'm already opening this to show @WalterBright the parts of the code I think need to be changed to properly fix issue 22790.

@dkorpel dkorpel added WIP Work In Progress - not ready for review or pulling dip1000 memory safety with scope, ref, return labels Feb 19, 2022
@dlang-bot
Copy link
Contributor

dlang-bot commented Feb 19, 2022

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
20881 normal [DIP1000] scope inference turns return-ref into return-scope
22790 enhancement ref-return-scope is always ref-return, scope, unless return-scope appear in that order

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

@dkorpel dkorpel force-pushed the return-scope-consistency branch 2 times, most recently from 5078d1e to 895420e Compare March 9, 2022 16:35
@dkorpel dkorpel marked this pull request as ready for review March 9, 2022 16:36
@dkorpel dkorpel requested a review from ibuclaw as a code owner March 9, 2022 16:36
@dkorpel dkorpel changed the title Make consistent use of STC.returnScope Fix issue 20881, 22790 - make ref-return-scope consistent Mar 9, 2022
@dkorpel
Copy link
Contributor Author

dkorpel commented Mar 9, 2022

I've found the bug: escape.d modifies the TypeFunction before attribute inference, which might get replaced at the end of semantic3 of a FuncDecl. This involves copying over the storage classes, and returnScope was forgotten in certain copy functions. This should be ready to go now, fixing 2 issues and unblocking #12689

The diff will be smaller when #13789 is merged first.

@dkorpel dkorpel removed the WIP Work In Progress - not ready for review or pulling label Mar 9, 2022
@WalterBright
Copy link
Member

I already have a (much simpler) PR that fixes this.

Dennis, I know where I'm going with this. Trying to do it another way is just confusing, we are working at cross-purposes.

@dkorpel
Copy link
Contributor Author

dkorpel commented Mar 10, 2022

I already have a (much simpler) PR that fixes this.

No, that only fixes a tiny part of it. I'm not making superfluous code changes for the kicks, visit(CallExp e) has to change to work properly with the new rules.

Dennis, I know where I'm going with this. Trying to do it another way is just confusing, we are working at cross-purposes.

I'm not doing it another way, you introduced STC.returnScope_ and buildScopeRef, I just make dmd actually use it.

@@ -130,7 +130,7 @@ fail_compilation/retscope2.d(721): Error: returning `s.get1()` escapes a referen

struct S700
{
@safe S700* get1() return scope
@safe S700* get1() return
Copy link
Member

Choose a reason for hiding this comment

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

Instead of return scope, rewrite as scope return.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay. I didn't add scope back because it gets stripped anyway since S700 has no (pointer) members.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

src/dmd/escape.d Outdated Show resolved Hide resolved
@WalterBright
Copy link
Member

Ok, I studied this for a bit. The issue is there are 3 ways of representing the dip1000 bits:

  1. storage class
  2. funcflags
  3. ScopeRef

This adds an isreturnscope to funcflags to mimic the behavior of returnScope as a storage class.

I need to think about this a bit more.

@WalterBright
Copy link
Member

How about this sequence of PRs:

  1. simply introduce isreturnscope, setting it correctly in the FuncFlags
  2. use isreturnscope to set returnScope in the storage class for the this parameter
  3. use ScopeRef in the escape analysis functions for the this parameter

Then it should be in a good position to fix bugs with it.

@WalterBright
Copy link
Member

Also, please rebase this PR, as the changes in #13802 are not reflected in the git diffs.

@dkorpel
Copy link
Contributor Author

dkorpel commented Mar 12, 2022

How about this sequence of PRs:

Sure

  1. simply introduce isreturnscope, setting it correctly in the FuncFlags

That would be #13789

@WalterBright
Copy link
Member

See #13811 for next step.

@WalterBright
Copy link
Member

And now #13817

@dkorpel dkorpel force-pushed the return-scope-consistency branch 3 times, most recently from 2e22e80 to e6957a2 Compare March 17, 2022 10:46
@dkorpel
Copy link
Contributor Author

dkorpel commented Mar 18, 2022

How about this sequence of PRs:
Then it should be in a good position to fix bugs with it.

Those 3 PRs are merged now. I looked into splitting this PR up further, but found it hard to isolate a fix for issue 20881 without also incorporating changes needed for 22790.

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 Enhancement
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants