roslyn: Variable from `using` cannot be used as `ref` or `out`
Version Used: 15.7 Preview 4
Steps to Reproduce:
using System;
public class C {
public void M() {
using (var d = new D())
{
d.Move();
d.Move2(); // error CS1657: Cannot use 'd' as a ref or out value because it is a 'using variable'
}
}
}
public struct D : IDisposable
{
public void Move() { }
public void Dispose() { }
}
public static class DExtensions
{
public static void Move2(ref this D d) { d.Move(); }
}
Expected Behavior:
The calls to Move()
and Move2()
are equivalent.
Actual Behavior:
The call to Move2()
results in the error described.
Additional Information:
The call to Move()
shows that invocations using a reference to a value used in a using
statement are allowed in some contexts. In these contexts, CS1657 appears to be an arbitrary language limitation that could be removed. Allowing by-ref calls in this context would simplify the implementation of an allocation-free OwnedDisposable<T>
, which I’m creating as part of an experimental way to address dotnet/roslyn-analyzers#1617. This limitation forces the code to use try
/finally
instead of the equivalent using
statement.
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 1
- Comments: 16