Skip to content

Unclear what is the (ref) safe context of an assignment expression #1444

@jjonescz

Description

@jjonescz

The standard has this section, but it is not clear to me from that what is the safe context of an assignment expression (is it its LHS, RHS, combination of both?):

16.4.15.5 Operators

The application of a user-defined operator is treated as a method invocation (§16.4.15.6).

For an operator that yields a value, such as e1 + e2 or c ? e1 : e2, the safe-context of the result is the narrowest context among the safe-contexts of the operands of the operator. As a consequence, for a unary operator that yields a value, such as +e, the safe-context of the result is the safe-context of the operand.

Note: The first operand of a conditional operator is a bool, so its safe-context is caller-context. It follows that the resulting safe-context is the narrowest safe-context of the second and third operand. end note

There is also a similar section for ref safe context:

9.7.2.5 Operators

The conditional operator (§12.19), c ? ref e1 : ref e2, and reference assignment operator, = ref e (§12.22.1) have reference variables as operands and yield a reference variable. For those operators, the ref-safe-context of the result is the narrowest context among the ref-safe-contexts of all ref operands.

The standard sections seem to come from the proposal, but that doesn't provide additional clarity.

I think the safe context of an assignment should be its RHS because that's the result of the assignment expression. Same for ref assignment. Not for null-coalescing assignment though (there it's probably clear already from the spec that it should be the combination of LHS and RHS).

Related change in Roslyn: dotnet/roslyn#80633

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions