Replies: 2 comments 5 replies
-
Consider the following example: using System;
Foo.Bar[1] = 1;
Console.WriteLine(Foo.Bar[1]);
struct Accessor
{
private int _value;
public int this[int p]
{
get => _value;
set => _value = value;
}
}
static class Foo
{
public static Accessor Bar { get; }
} What do you expect to printed by that
var tmp = Foo.Bar;
tmp[1] = 1;
Foo.Bar = tmp;
Hopefully this helps. |
Beta Was this translation helpful? Give feedback.
-
We had a good chat about this over at discord: https://discord.com/channels/732297728826277939/732297994699014164/908109390798786600 To sum up the language side of this:
Clearly though the above could could work. So we have to reconcile our desire to catch real bugs, with the limits not being too onerous for code that would work. In this case the above code doesn't fit under this formalization because indexer isn't mutating the copied value. However, with the code as structured like above, this would be hard to tell. Specifically, Accessor might be from metadata, so we wouldn't know that it wasn't mutating itself. We could attempt to refine the rules here if the struct itself were potentially readonly (or something like that), but we risk ending up with a very subtle exception that doesn't really end up adding that much value. In general, the two LDM members participating here (@333fred and myself) feel that the amount of true-positives this catches is worthwhile, and the amount of false-positives it reports is minimal. And as such, the simplicity and clarity of hte current approach is likely where we'd want to stay. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Beta Was this translation helpful? Give feedback.
All reactions