From c6e3116df9a44a871740318ebcf486f986b79dad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Fri, 13 Jan 2023 16:21:15 +0100 Subject: [PATCH] Add comment for `LiteralExpander` `select` (#12926) Co-authored-by: Beta Ziliani --- .../crystal/semantic/literal_expander.cr | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/compiler/crystal/semantic/literal_expander.cr b/src/compiler/crystal/semantic/literal_expander.cr index 74c59209838b..ead7901501c8 100644 --- a/src/compiler/crystal/semantic/literal_expander.cr +++ b/src/compiler/crystal/semantic/literal_expander.cr @@ -631,6 +631,48 @@ module Crystal final_exp end + # Convert a `select` statement into a `case` statement based on `Channel.select` + # + # From: + # + # select + # when foo then body + # when x = bar then x.baz + # end + # + # To: + # + # %index, %value = ::Channel.select({foo_select_action, bar_select_action}) + # case %index + # when 0 + # body + # when 1 + # x = value.as(typeof(foo)) + # x.baz + # else + # ::raise("BUG: invalid select index") + # end + # + # + # If there's an `else` branch, use `Channel.non_blocking_select`. + # + # From: + # + # select + # when foo then body + # else qux + # end + # + # To: + # + # %index, %value = ::Channel.non_blocking_select({foo_select_action}) + # case %index + # when 0 + # body + # else + # qux + # end + # def expand(node : Select) index_name = @program.new_temp_var_name value_name = @program.new_temp_var_name