···11+---
22+source: compiler-core/src/erlang/tests/case.rs
33+assertion_line: 197
44+expression: "\npub fn main(x) {\n case x {\n \"a\" as letter <> _ if letter == x -> letter\n _ -> \"wibble\"\n }\n}\n"
55+---
66+----- SOURCE CODE
77+88+pub fn main(x) {
99+ case x {
1010+ "a" as letter <> _ if letter == x -> letter
1111+ _ -> "wibble"
1212+ }
1313+}
1414+1515+1616+----- COMPILED ERLANG
1717+-module(my@mod).
1818+-compile([no_auto_import, nowarn_ignored, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
1919+-export([main/1]).
2020+2121+-file("project/test/my/mod.gleam", 2).
2222+-spec main(binary()) -> binary().
2323+main(X) ->
2424+ case X of
2525+ <<"a":(deafult)/utf8, _/binary>> when ~"a" =:= X ->
2626+ Letter = ~"a",
2727+ Letter;
2828+2929+ _ ->
3030+ ~"wibble"
3131+ end.
···790790 ///
791791 /// builder.bit_array_segment();
792792 /// builder.int(1);
793793- /// builder.atom("default");
794794- /// builder.atom("default");
793793+ /// builder.bit_array_segment_default_size();
794794+ /// builder.bit_array_segment_specifiers([]);
795795 ///
796796 /// builder.bit_array_segment();
797797 /// builder.string("hello");
798798- /// builder.atom("deafult");
799799- /// builder.atom("default");
798798+ /// builder.bit_array_segment_default_size();
799799+ /// builder.bit_array_segment_specifiers([]);
800800 ///
801801 /// builder.end_bit_array(bit_array);
802802 /// ```
···836836 /// If you wanna check an example of how this is used you can have a read at
837837 /// the ones in `start_bit_array`.
838838 fn bit_array_segment(&mut self);
839839+840840+ /// You can call this when a bit array segment has no explicit size, and the
841841+ /// default Erlang behaviour is fine.
842842+ ///
843843+ fn bit_array_segment_default_size(&mut self);
839844840845 /// This generates a specifiers list for the currently open bit array
841846 /// segment.
···11891194 ///
11901195 /// builder.bit_array_pattern_segment();
11911196 /// builder.int_pattern(1);
11921192- /// builder.atom("default");
11931193- /// builder.atom("default");
11971197+ /// builder.bit_array_segment_default_size();
11981198+ /// builder.bit_array_segment_specifiers([]);
11941199 ///
11951200 /// builder.bit_array_pattern_segment();
11961201 /// builder.discard_pattern();
11971197- /// builder.atom("deafult");
11981198- /// builder.atom("default");
12021202+ /// builder.bit_array_segment_default_size();
12031203+ /// builder.bit_array_segment_specifiers([]);
11991204 ///
12001205 /// builder.end_bit_array_pattern(bit_array);
12011206 /// ```
···22302235 })
22312236 }
2232223722382238+ fn bit_array_segment_default_size(&mut self) {
22392239+ if let Some(ErlangSourceBuilderPosition::BitArraySegment {
22402240+ expected: expected @ BitArraySegmentExpectedItem::Size,
22412241+ segment_value_needs_wrapping,
22422242+ segment_size_needs_wrapping,
22432243+ }) = self.position.last_mut()
22442244+ {
22452245+ // We are now expecting to see the specifiers list
22462246+ *expected = BitArraySegmentExpectedItem::Specifiers;
22472247+ if *segment_value_needs_wrapping {
22482248+ self.code.push(')');
22492249+ *segment_value_needs_wrapping = false;
22502250+ }
22512251+ // We have the default atom as a size, that means we don't have to
22522252+ // print anything at all! The size doesn't need any wrapping because
22532253+ // there's no size at all.
22542254+ *segment_size_needs_wrapping = false;
22552255+ } else {
22562256+ invalid_code_for_position!(self, "segment default size");
22572257+ }
22582258+ }
22592259+22332260 fn bit_array_segment_specifiers(
22342261 &mut self,
22352262 specifiers: impl IntoIterator<Item = BitArraySegmentSpecifier>,
···25162543 }
2517254425182545 fn atom(&mut self, name: &str) {
25192519- // There's one special case where we actually don't want to push the
25202520- // atom's text at all. That's when we're generating the `default` atom
25212521- // as the size of a bit array segment.
25222522- // That's how we can tell in the Erlang Abstract Format that the size
25232523- // should be the default value, but it's not actually spelled out in
25242524- // textual Erlang code.
25252525- if let Some(ErlangSourceBuilderPosition::BitArraySegment {
25262526- expected: expected @ BitArraySegmentExpectedItem::Size,
25272527- segment_value_needs_wrapping,
25282528- segment_size_needs_wrapping,
25292529- }) = self.position.last_mut()
25302530- {
25312531- // We are new expecting to see the specifiers list
25322532- *expected = BitArraySegmentExpectedItem::Specifiers;
25332533- if *segment_value_needs_wrapping {
25342534- self.code.push(')');
25352535- *segment_value_needs_wrapping = false;
25362536- }
25372537- // We have the default atom as a size, that means we don't have to
25382538- // print anything at all! The size doesn't need any wrapping because
25392539- // there's no size at all.
25402540- *segment_size_needs_wrapping = false;
25412541- } else {
25422542- self.new_expression();
25432543- self.code.push_str("e_atom_name(name));
25442544- }
25462546+ self.new_expression();
25472547+ self.code.push_str("e_atom_name(name));
25452548 }
25462549}
25472550