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

“Defensive” copies – standardising a compiler strategy or language semantics? #1053

Open
Nigel-Ecma opened this issue Mar 4, 2024 · 0 comments

Comments

@Nigel-Ecma
Copy link
Contributor

@KalleOlaviNiemitalo uses the following code in a comment on PR #673:

struct S {
    int P => 0; // not readonly
    
    int this[string s] => 0; // not readonly

    readonly void M() {
        _ = P; // warning CS8656: Call to non-readonly member 'S.P.get' from a 'readonly' member results in an implicit copy of 'this'.
        _ = this[""]; // warning CS8656: Call to non-readonly member 'S.this[string].get' from a 'readonly' member results in an implicit copy of 'this'.
        N(); // warning CS8656: Call to non-readonly member 'S.N()' from a 'readonly' member results in an implicit copy of 'this'.
    }

    void N() {}
}

@KalleOlaviNiemitalo is making a point about the warnings Roslyn issues, not about the three copies of this the Standard mandates as “defensive copies” as being redundant.

Surely a different compiler might choose to analyze P, this[] & N; figure out they do not modify the struct; skip the defensive copies; produce the intended semantics; and yet be non-compliant for doing so…

Are not defensive copies a compiler strategy for achieving a particular semantics; is the Standard mandating the strategy rather than the semantics; and in doing so limiting compilers (and performance in this case)?

Should not the strategy be left to the compilers, and the semantics to the Standard?

Or is it argued that the strategy here is the intended semantics?

[Ducking ;-)] Maybe someone should look into this…

@Nigel-Ecma Nigel-Ecma changed the title “Defensive” copies – standardising a compiler or language semantics? “Defensive” copies – standardising a compiler strategy or language semantics? Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant