Skip to content

C# 7.x ref assignment to input parameter #839

Closed
@KalleOlaviNiemitalo

Description

@KalleOlaviNiemitalo

Describe the bug

In the C# 7.x draft, 12.21.3 could be a bit clearer about input parameters here:

If the left operand is a writeable ref (i.e., it designates anything other than a ref readonly local or in parameter), then the right operand shall be a writeable variable_reference. If the right operand variable is writeable, the left operand may be declared ref or ref readonly.

Example

This should be valid.

class C {
    int j;
    void M(in int i) {
        i = ref j;
    }
}

Expected behavior

Say that the left operand may be declared in as an input parameter.

Additional context

Poking this, I met the ref-safe-to-escape rules

class C {
    ref readonly int M(in int i, int j) {
        ////i = ref j; // error CS8374
        return ref i;
    }
}

The ref assignment here looks benign on its own, but it must be rejected because the method could then return that ref. I have not checked whether the draft specifies an error here.

Metadata

Metadata

Assignees

Labels

meeting: proposalThere is an informal proposal in the issue, worth discussing in a meetingtype: bugThe Standard does not describe the language as intended or implemented

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions