Skip to content

Commit dbc39de

Browse files
jnm2jaredpar
andauthored
Work the expansion into the proposal for readonly setter calls on non-variables (#9307)
* Fix the spec section * Turn expansion into an answered question * Fix object initializers link in meeting notes * Work the expansion into the proposal * Include partial reference decl of System.ArraySegment<T> * Update proposals/readonly-setter-calls-on-non-variables.md Clarify impl details in API surface Co-authored-by: Jared Parsons <[email protected]> --------- Co-authored-by: Jared Parsons <[email protected]>
1 parent 15c9af2 commit dbc39de

File tree

2 files changed

+74
-40
lines changed

2 files changed

+74
-40
lines changed

meetings/2025/LDM-2025-04-02.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Question: https://github.com/dotnet/csharplang/blob/e34ecf622058b53f0fb37705baa8
7171
7272
There are cases where it is appropriate to error if a non-readonly setter is called, that may be reasonable when a `readonly` setter is called. We explored two of those cases that currently error with _CS1918 Member of property ... cannot be assigned with an object initializer because it is of a value type._ Should we include allowing readonly setters in these cases as part of the broader feature.
7373

74-
A change would be needed to the C# spec on object initializers ([§11.18.2](https://github.com/dotnet/csharpstandard/blob/standard-v6/standard/expressions.md#11182-simple-assignment)), which will be added to this proposal.
74+
A change would be needed to the C# spec on object initializers ([§12.8.16.3](https://github.com/dotnet/csharpstandard/blob/standard-v7/standard/expressions.md#128163-object-initializers)), which will be added to this proposal.
7575
7676
It was noted that expansions could be split off and done after the rest of the work in this proposal.
7777

proposals/readonly-setter-calls-on-non-variables.md

Lines changed: 73 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Champion issue: <https://github.com/dotnet/csharplang/issues/9174>
44

55
## Summary
66

7-
Permits a readonly setter to be called on _all_ non-variable expressions:
7+
Permits a readonly setter to be called on non-variable expressions, and permits object initializers to be used with value types:
88

99
```cs
1010
var c = new C();
@@ -17,13 +17,28 @@ c.ArraySegmentMethod()[10] = new object();
1717
// In limited cases, ref-returning indexers can be used to work around this:
1818
c.RefReturningIndexerWorkaround[10] = new object();
1919

20+
_ = new C
21+
{
22+
// Remove the current CS1918 error:
23+
ArraySegmentProp = { [10] = new object() },
24+
// Remove the current CS1918 error:
25+
RefReturningIndexerWorkaround = { [10] = new object() },
26+
};
27+
2028
class C
2129
{
2230
public ArraySegment<object> ArraySegmentProp { get; set; }
2331
public ArraySegment<object> ArraySegmentMethod() => ArraySegmentProp;
2432

2533
public Span<object> RefReturningIndexerWorkaround => ArraySegmentProp.AsSpan();
2634
}
35+
36+
// Partial declaration of System.ArraySegment<T> for demonstration
37+
public readonly struct ArraySegment<T>
38+
{
39+
// Implicitly readonly due to the readonly modifier on the struct
40+
public T this[int index] { get => ...; set => ...; }
41+
}
2742
```
2843

2944
Currently, the code above gives the error CS1612 "Cannot modify the return value of 'C.ArraySegmentProp' because it is not a variable." This restriction is unnecessary when the setter is readonly. The restriction is there to remind you to assign the modified struct value back to the property. But there is no supported modification to the struct value when the setter is readonly, so there is no reason to assign back to the property.
@@ -75,7 +90,9 @@ These use cases would no longer be blocked if CS1612 is fully updated with an un
7590

7691
## Detailed design
7792

78-
The CS1612 error is not produced for assignments where the setter is readonly. (If the whole struct is readonly, then the setter is also readonly.) The setter call is emitted the same way as any non-accessor readonly instance method call.
93+
For part 1, the CS1612 error is not produced for assignments where the setter is readonly. (If the whole struct is readonly, then the setter is also readonly.) The setter call is emitted the same way as any non-accessor readonly instance method call.
94+
95+
For part 2, object initializers are now permitted to be used on value types. This means that the CS1918 error will no longer be produced at all. This opens the door to assignments using readonly setters or using refs returned by getters. However, this does not open the door to assignments that would not be permitted in the desugared form. Errors such as CS1612 and CS0313 will be updated to appear within object initializers now that CS1918 is no longer blocking off the entire space.
7996

8097
### Null-conditional assignment
8198

@@ -120,51 +137,23 @@ It's desirable for this error to remain, because the setter _does_ mutate the st
120137

121138
## Specification
122139

123-
### Waiting for v8 and corrected v7 specification
124-
125-
The published v7 spec and the draft specs are all missing the wording that removed the CS1612 error for invocation expressions. This is tracked by <https://github.com/dotnet/csharpstandard/issues/1277> which suggests the following addition to describe the current behavior:
126-
127-
[§12.21.2](https://github.com/dotnet/csharpstandard/blob/standard-v7/standard/expressions.md#12212-simple-assignment) _Simple assignment_
128-
> When a property or indexer declared in a _struct_type_ is the target of an assignment, **unless the _struct_type_ has the `readonly` modifier ([§16.2.2](https://github.com/dotnet/csharpstandard/blob/standard-v7/standard/structs.md#1622-struct-modifiers)) and the instance expression is an invocation**, the instance expression associated with the property or indexer access shall be classified as a variable. If the instance expression is classified as a value, a binding-time error occurs.
140+
Insertions are in **bold**, deletions are in ~~strikethrough~~.
129141

130-
Then, the v8 spec is still in draft and the updates for readonly members have not yet merged. When it merges, it will need to redefine the condition as whether the setter itself is readonly, directly or indirectly.
142+
### Updates permitting readonly setter calls on non-variables
131143

132-
### Spec updates
144+
The current v8 specification draft does not yet specify readonly members (<https://github.com/dotnet/csharplang/blob/main/proposals/csharp-8.0/readonly-instance-members.md>). The following updates intend to leverage the concept of a _readonly member_. A _readonly member_ is a member which either is directly marked with the `readonly` modifier or which is contained inside a _struct_type_ which is marked with the `readonly` modifier.
133145

134-
This proposal would remove the text "**and the instance expression is an invocation**" from the paragraph mentioned above in [Waiting for v8 and corrected v7 specification](#waiting-for-v8-and-corrected-v7-specification).
135-
136-
## Expansions
137-
138-
There's another location where this kind of assignment is blocked, which is in object initializers:
139-
140-
```cs
141-
// ❌ CS1918 Members of property 'C.ArraySegmentProp' of type 'ArraySegment<object>' cannot be assigned with an object
142-
// initializer because it is of a value type
143-
_ = new C { ArraySegmentProp = { [42] = new object() } };
144-
// ~~~~~~~~~~~~~~~~
146+
[§12.21.2](https://github.com/dotnet/csharpstandard/blob/standard-v7/standard/expressions.md#12212-simple-assignment) _Simple assignment_ is updated:
145147

146-
class C
147-
{
148-
public ArraySegment<object> ArraySegmentProp { get; set; }
149-
}
150-
```
148+
> When a property or indexer declared in a _struct_type_ is the target of an assignment, **either** the instance expression associated with the property or indexer access shall be classified as a variable, **or the set accessor of the property or indexer shall be a readonly member ([§16.2.2](https://github.com/dotnet/csharpstandard/blob/standard-v7/standard/structs.md#1622-struct-modifiers))**. If the instance expression is classified as a value **and the set accessor is not a readonly member**, a binding-time error occurs.
151149
152-
For the same reasons as above, this error is unnecessary when the properties being initialized have readonly `set`/`init` accessors. The error could be made more granular, placed on each property initializer which calls a _non-readonly_ setter.
150+
### Updates permitting object initializers for value types
153151

154-
Even in the limited cases where a ref-returning indexer is applicable, it does not help with CS1918, where it still unnecessarily broad:
152+
The CS1918 error is completely removed. There is no need to block value types. "\[T]he assignments in the nested object initializer are treated as assignments to members of the field or property." Such assignments must already conform to rules such as the one enforced by CS1612, including when inside an object initializer.
155153

156-
```cs
157-
// ❌ CS1918 Members of property 'C.StructWithRefReturningIndexer' of type 'Span<object>' cannot be assigned with an object
158-
// initializer because it is of a value type
159-
_ = new C { StructWithRefReturningIndexer = { [42] = new object() } };
160-
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
154+
[§12.8.16.3](https://github.com/dotnet/csharpstandard/blob/standard-v7/standard/expressions.md#128163-object-initializers) _Object initializers_ is updated:
161155

162-
class C
163-
{
164-
public ArraySegment<object> ArraySegmentProp { get; set; }
165-
public Span<object> StructWithRefReturningIndexer => ArraySegmentProp.AsSpan();
166-
}
167-
```
156+
> A member initializer that specifies an object initializer after the equals sign is a ***nested object initializer***, i.e., an initialization of an embedded object. Instead of assigning a new value to the field or property, the assignments in the nested object initializer are treated as assignments to members of the field or property. ~~Nested object initializers cannot be applied to properties with a value type, or to read-only fields with a value type.~~
168157
169158
## Downsides
170159

@@ -195,3 +184,48 @@ public struct S2
195184
public int Prop { get => 0; readonly set { } }
196185
}
197186
```
187+
188+
## Answered LDM questions
189+
190+
### Should similar assignments be permitted in object initializers?
191+
192+
There's a separate error, CS1918 that blocks assignments through readonly setters when the assignments appear in object initializers. In addition, this error even blocks assignments to ref-returning properties and indexers, and those assignments are not blocked when they appear outside of object initializers.
193+
194+
```cs
195+
// ❌ CS1918 Members of property 'C.ArraySegmentProp' of type 'ArraySegment<object>' cannot be assigned with an object
196+
// initializer because it is of a value type
197+
_ = new C { ArraySegmentProp = { [42] = new object() } };
198+
// ~~~~~~~~~~~~~~~~
199+
200+
// ❌ CS1918 Members of property 'C.StructWithRefReturningIndexer' of type 'Span<object>' cannot be assigned with an object
201+
// initializer because it is of a value type
202+
_ = new C { StructWithRefReturningIndexer = { [42] = new object() } };
203+
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
204+
205+
class C
206+
{
207+
public ArraySegment<object> ArraySegmentProp { get; set; }
208+
public Span<object> StructWithRefReturningIndexer => ArraySegmentProp.AsSpan();
209+
}
210+
```
211+
212+
Such assignments desugar to the following form, the same form in which the CS1612 warning is being removed:
213+
214+
```cs
215+
var temp = new C();
216+
// Warning being removed:
217+
// CS1612 Cannot modify the return value of 'C.ArraySegmentProp' because it is not a variable
218+
temp.ArraySegmentProp[42] = new object();
219+
```
220+
221+
```cs
222+
var temp = new C();
223+
// Permitted today
224+
temp.StructWithRefReturningIndexer[42] = new object();
225+
```
226+
227+
Should this check be made more granular, so that members of struct types may be assigned when they would be allowed to be assigned in the desugared form?
228+
229+
#### Answer
230+
231+
Yes. This expansion will be included. [(LDM 2025-04-02)](https://github.com/dotnet/csharplang/blob/main/meetings/2025/LDM-2025-04-02.md#expansions)

0 commit comments

Comments
 (0)