Implement val escape for switch expressions#45242
Conversation
|
@dotnet/roslyn-compiler May I please have a couple reviews of this tiny bug fix? |
| _ => default, | ||
| }; | ||
|
|
||
| return span; // 3 |
There was a problem hiding this comment.
span; // 3 [](start = 15, length = 10)
Why is this error in GetSpan3 reported on the return while the error in GetSpan2 is reported within the switch expression? I'm curious in case the difference points to an issue. #Resolved
There was a problem hiding this comment.
There is no problem in GetSpan3 until the return. The switch expression simply computes a Span value that cannot be returned, and is used to declare the local span which therefore cannot be returned. It is only when you attempt to return that value that there is an issue.
In reply to: 441674063 [](ancestors = 441674063)
There was a problem hiding this comment.
There is no problem in
GetSpan3until thereturn.
Isn't that true for GetSpan2 as well? It looks like the only difference between the two cases is whether the local is assigned in the declaration. #Resolved
There was a problem hiding this comment.
We don't track whether a variable can be returned based on the flow of data (like we do with nullable) but based on the "returnable" property of the local computed at the declaration point.
In GetSpan2 the local is returnable so we cannot even assign the switch expression to it.
In GetSpan3 the local is not returnable (because its initializing expression isn't returnable) so the assignment of the switch expression to it isn't a problem.
See https://github.com/dotnet/csharplang/blob/master/proposals/csharp-7.2/span-safety.md#locals for the precise rules.
In reply to: 441738383 [](ancestors = 441738383)
Fixes #44588