dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.01k stars 4.03k forks source link

No specification for how to treat restricted types in async/iterator methods #37725

Open agocke opened 5 years ago

agocke commented 5 years ago

Right now there's no rule on how restricted types (like ref structs) should behave in async and iterator methods. There are clearly illegal cases: pending restricted types over await and yield is not possible to implement, so it must be illegal.

However, it's unclear how this should behave if restricted types are not pended over a branch. It's possible to allow this behavior, but it becomes an obtuse and delicate balance -- all locals of restricted types are also currently disallowed because it hurts debuggibility, but not all expressions of restricted type are disallowed. While using the result of a restricted method as an expression may be safe, it seems reasonable that a user would assume that they could take the same code and introduce a local variable for the restricted type. This then becomes an error. Moreover, we may be allowing users to veer into very dangerous design territory, as minor refactorings can easily cause the code to pend the expression over an await, producing an error.

It seems the compiler already allows expressions of restricted types in async methods, as long as they don't pend the value over an await. We should decide if we like this behavior, or if we should treat this as a bug and produce a breaking change for code of this form.

agocke commented 5 years ago

My preference here would be adopt language of the kind, "it is an error to have an expression of a restricted type in async or iterator methods"