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

Friendly error when using multiple spreads

authored by Carl Bordum Hansen and committed by Louis Pilfold 33315d5b 01f818f1

+9
compiler-core/src/parse.rs
··· 620 620 elements_after_tail = Some(elements); 621 621 } 622 622 }; 623 + 624 + // Give a better error when there is a another spread 625 + // like `[..wibble, wobble, ..wabble]` 626 + if self.maybe_one(&Token::DotDot).is_some() { 627 + return parse_error( 628 + ParseErrorType::ListSpreadWithAnotherSpread, 629 + SrcSpan { start, end }, 630 + ); 631 + } 623 632 }; 624 633 625 634 if tail.is_some() && !elements_end_with_comma {
+9
compiler-core/src/parse/error.rs
··· 161 161 "See: https://tour.gleam.run/basics/lists/".into(), 162 162 ], 163 163 ), 164 + ParseErrorType::ListSpreadWithAnotherSpread => ( 165 + "I wasn't expecting a spread here", 166 + vec![ 167 + "Lists are immutable and singly-linked, so to join two or more lists".into(), 168 + "all the elements of the lists would need to be copied into a new list.".into(), 169 + "This would be slow, so there is no built-in syntax for it.".into(), 170 + ], 171 + ), 164 172 ParseErrorType::ListSpreadFollowedByElements => ( 165 173 "I wasn't expecting elements after this", 166 174 vec![ ··· 389 397 UnknownTarget, // an unknown target was used 390 398 ListSpreadWithoutElements, // Pointless spread: `[..xs]` 391 399 ListSpreadFollowedByElements, // trying to append something after the spread: `[..xs, x]` 400 + ListSpreadWithAnotherSpread, // trying to use multiple spreads: `[..xs, ..ys]` 392 401 LowcaseBooleanPattern, // most likely user meant True or False in patterns 393 402 UnexpectedLabel, // argument labels were provided, but are not supported in this context 394 403 UnexpectedEof,
+23
compiler-core/src/parse/snapshots/gleam_core__parse__tests__list_spread_followed_by_another_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 [..xs, 4, ..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 + [..xs, 4, ..ys] 11 + } 12 + 13 + 14 + ----- ERROR 15 + error: Syntax error 16 + ┌─ /src/parse/error.gleam:5:4 17 + 18 + 5 │ [..xs, 4, ..ys] 19 + │ ^^ I wasn't expecting a spread here 20 + 21 + Lists are immutable and singly-linked, so to join two or more lists 22 + all the elements of the lists would need to be copied into a new list. 23 + This would be slow, so there is no built-in syntax for it.
+13
compiler-core/src/parse/tests.rs
··· 872 872 } 873 873 874 874 #[test] 875 + fn list_spread_followed_by_another_spread() { 876 + assert_module_error!( 877 + r#" 878 + pub fn main() -> Nil { 879 + let xs = [1, 2, 3] 880 + let ys = [5, 6, 7] 881 + [..xs, 4, ..ys] 882 + } 883 + "# 884 + ); 885 + } 886 + 887 + #[test] 875 888 fn list_spread_as_first_item_followed_by_other_items() { 876 889 assert_module_error!( 877 890 r#"