Open vitek-karas opened 1 year ago
I think it might not complicate to implement for C# state machine because compiler usually assign the field in single method body only.
Did you find that in some real code somewhere?
I didn't run into this in real code yet. But it's effectively hidden behind https://github.com/dotnet/linker/pull/3088. I agree that if we special case this for C# state machines this is definitely solvable.
Take for example this code:
The problem is that the compiler generated code produces the
if (AlwaysFalse)
where its body is just some state machine setup because there's a yield there. And so it means that even if that branch is removed, the call toRemovedMethod
is still kept because it's part of a different state in the state machine.In order to correctly implement this, the analysis would have to understand at least basics of state machines. Detecting this in a general case is really complicated because the state is stored in a field, and so we would have to be able to prove that the field can only have a certain set of values - which is really hard in multi-threaded environments.
So in theory doable, but pretty complex.
The downside of this issue is that it's hard to diagnose what's happening for the user. If this is used in combination of feature switches then this can lead to unexpected warnings, since code which is technically hidden behind a feature switch is still marked as reachable by the linker.