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

Missing "CS8656: Call to non-readonly member from a 'readonly' member results in an implicit copy of 'this'" warning on inner field method calls #36794

Closed
controlflow opened this issue Jun 26, 2019 · 3 comments

Comments

@controlflow
Copy link

controlflow commented Jun 26, 2019

Version Used:

master branch 06/26/2019

Steps to Reproduce:

Compile the following code:

struct A {
    int X;
    public void Incr() => X++;

    public readonly void Readonly() {
        Incr(); // CS8656: Call to non-readonly member 'I.Incr()' from a 'readonly' member results in an implicit copy of 'this'.
    }
}

struct B {
    A _inner;
    
    public readonly void Readonly() {
        _inner/*copy*/.Incr(); // Missing CS8656
    }
    
    public void Mutable() {
        _inner.Incr();
    }
}

sharplab

Expected Behavior:

Compiler warning

CS8656: Call to non-readonly member 'A.Incr()' from a 'readonly' member results in an implicit copy of 'this'.

Actual Behavior:

No warnings, field _inner being copied at runtime to prevent this mutation.

@jmarolf
Copy link
Contributor

jmarolf commented Jun 27, 2019

related to dotnet/runtime#64654

unfortunately this is by design. @RikkiGibson for more info....

@RikkiGibson
Copy link
Contributor

key bit, which I really should place more prominently in that issue:

Note that we've decided to give a warning out of the box when a non-readonly member is called from a readonly member and the receiver is this. Implicit copy warnings on fields will only be handled by this warning wave, regardless of whether the copy is occurring due to the field being readonly or the containing method being readonly.

@gafter gafter added the Feature - Warning Waves Warning Waves label Jul 9, 2019
@gafter gafter added this to the Compiler.Next milestone Jul 9, 2019
@gafter gafter added the Bug label Sep 17, 2019
@RikkiGibson
Copy link
Contributor

dotnet/runtime#64654

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants