⭐️ A friendly language for building type-safe, scalable systems!

add a test for the error, and fix small bug

authored by giacomocavalieri.me and committed by Louis Pilfold 2979a3b9 0134f2df

Changed files
+34 -1
compiler-core
+9 -1
compiler-core/src/parse.rs
··· 633 633 // Give a better error when there is two consecutive spreads 634 634 // like `[..wibble, ..wabble, woo]`. However, if there's other 635 635 // elements after the tail of the list 636 + println!("{:#?}", self.tok0); 637 + println!("{:#?}", elements_after_tail); 638 + 636 639 if let Some((second_start, second_end)) = self.maybe_one(&Token::DotDot) { 637 640 let _second_tail = self.parse_expression(); 638 - if elements_after_tail.is_none() { 641 + 642 + if elements_after_tail.is_none() 643 + || elements_after_tail 644 + .as_ref() 645 + .is_some_and(|vec| vec.is_empty()) 646 + { 639 647 return parse_error( 640 648 ParseErrorType::ListSpreadWithAnotherSpread { 641 649 first_spread_location: SrcSpan { start, end },
+25
compiler-core/src/parse/snapshots/gleam_core__parse__tests__list_spread_followed_by_other_spread.snap
··· 1 + --- 2 + source: compiler-core/src/parse/tests.rs 3 + expression: "\npub fn main() -> Nil {\n let xs = [1, 2, 3]\n let ys = [5, 6, 7]\n [1, ..xs, ..ys]\n}\n" 4 + --- 5 + ----- SOURCE CODE 6 + 7 + pub fn main() -> Nil { 8 + let xs = [1, 2, 3] 9 + let ys = [5, 6, 7] 10 + [1, ..xs, ..ys] 11 + } 12 + 13 + 14 + ----- ERROR 15 + error: Syntax error 16 + ┌─ /src/parse/error.gleam:5:13 17 + 18 + 5 │ [1, ..xs, ..ys] 19 + │ -- ^^ I wasn't expecting a second spread here 20 + │ │ 21 + │ You're using a spread here 22 + 23 + Lists are immutable and singly-linked, so to join two or more lists 24 + all the elements of the lists would need to be copied into a new list. 25 + This would be slow, so there is no built-in syntax for it.