Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement val escape for switch expressions #45242

Merged
merged 1 commit into from
Jun 17, 2020
Merged

Conversation

gafter
Copy link
Member

@gafter gafter commented Jun 16, 2020

Fixes #44588

@gafter gafter added this to the 16.7 milestone Jun 16, 2020
@gafter gafter requested a review from a team as a code owner June 16, 2020 23:28
@gafter gafter self-assigned this Jun 16, 2020
@gafter
Copy link
Member Author

gafter commented Jun 17, 2020

@dotnet/roslyn-compiler May I please have a couple reviews of this tiny bug fix?

_ => default,
};

return span; // 3
Copy link
Member

@cston cston Jun 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Member

@cston cston Jun 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no problem in GetSpan3 until the return.

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

Copy link
Member Author

@gafter gafter Jun 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@gafter gafter merged commit d957f3e into dotnet:master Jun 17, 2020
@ghost ghost modified the milestones: 16.7, Next Jun 17, 2020
@dibarbet dibarbet modified the milestones: Next, 16.7.P4 Jun 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The compiler should allow the return of a span declared and initialized by switch expression.
4 participants