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 23168 - return scope wrongly rewritten for structs with no indirections #14232

Merged
merged 1 commit into from Jun 23, 2022

Conversation

dkorpel
Copy link
Contributor

@dkorpel dkorpel commented Jun 21, 2022

Stripping scope off a parameter is covered by FuncDeclaration semantic, VarDeclaration semantic, and TypeFunction semantic. Only TypeFunction considers return scope, so remove the check in FuncDeclaration semantic and add return scope stripping to VarDeclaration.

@dkorpel dkorpel added the dip1000 memory safety with scope, ref, return label Jun 21, 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
23168 normal [DIP1000] return scope wrongly rewritten for structs with no indirections

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

@dkorpel dkorpel marked this pull request as ready for review June 21, 2022 13:29
@thewilsonator
Copy link
Contributor

I don't think the test failure is related, but I'm not sure. cc @9il

@dkorpel
Copy link
Contributor Author

dkorpel commented Jun 22, 2022

It is related, I can reproduce it locally. Looking at it now.

@dkorpel
Copy link
Contributor Author

dkorpel commented Jun 22, 2022

Okay, so the problem is a forward reference error:

struct S 
{
    alias _get_value this; // Error: undefined identifier `_get_value`

    static if (true)
        int _get_value() {return 3;}
    else
        int _get_value() {return 4;}
}

But here comes the magic trick: add a scope function before it and it will call hasPointers() on itself which will look forwards, and then it can find it:

struct S 
{
    void f() scope {}
    alias _get_value this; // works now!

    static if (true)
        int _get_value() {return 3;}
    else
        int _get_value() {return 4;}
}

Since this PR removes the scope check in FuncDeclaration semantic, it now misses the forward reference. Of course, other times eagerly calling hasPointers causes problems: Issue 21667 - scope parameter causes 'no size because of forward references'

@thewilsonator
Copy link
Contributor

Please add the forward reference issue to the test case.

@dlang-bot dlang-bot merged commit 5d5fe27 into dlang:master Jun 23, 2022
@dkorpel dkorpel deleted the return-scope-remove branch June 23, 2022 09:37
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
3 participants