Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

Update wording for suffix concat match error

author
Gavin Morrow
committer
Louis Pilfold
date (Jun 3, 2026, 3:38 PM +0100) commit ad6dc782 parent dc25ba5a change-id znxzmrym
+106 -18
+31 -9
compiler-core/src/parse.rs
··· 1366 1366 self.advance(); 1367 1367 let (r_start, right, r_end) = self.expect_assign_name()?; 1368 1368 1369 - // Can't match on prefix and suffix 1370 - if let Some((_, Token::Concatenate, _)) = self.tok0 { 1371 - return concat_pattern_variable_in_infix_position_error( 1372 - r_start, r_end, 1369 + // Can't match on suffix literal 1370 + if let Some(( 1371 + second_concat_start, 1372 + Token::Concatenate, 1373 + second_concat_end, 1374 + )) = self.tok0 1375 + { 1376 + let suffix_end = match &self.tok1 { 1377 + Some((_start, Token::String { .. }, end)) => *end, 1378 + _ => second_concat_end, 1379 + }; 1380 + return concat_pattern_variable_with_suffix( 1381 + second_concat_start, 1382 + right.name().clone(), 1383 + suffix_end, 1373 1384 ); 1374 1385 } 1375 1386 ··· 1408 1419 self.advance(); 1409 1420 let (r_start, right, r_end) = self.expect_assign_name()?; 1410 1421 1411 - // Can't match on prefix and suffix 1412 - if let Some((_, Token::Concatenate, _)) = self.tok0 { 1413 - return concat_pattern_variable_in_infix_position_error(r_start, r_end); 1422 + // Can't match on suffix literal 1423 + if let Some((second_concat_start, Token::Concatenate, second_concat_end)) = 1424 + self.tok0 1425 + { 1426 + let suffix_end = match &self.tok1 { 1427 + Some((_start, Token::String { .. }, end)) => *end, 1428 + _ => second_concat_end, 1429 + }; 1430 + return concat_pattern_variable_with_suffix( 1431 + second_concat_start, 1432 + right.name().clone(), 1433 + suffix_end, 1434 + ); 1414 1435 } 1415 1436 1416 1437 Pattern::StringPrefix { ··· 4664 4685 }) 4665 4686 } 4666 4687 4667 - fn concat_pattern_variable_in_infix_position_error<T>( 4688 + fn concat_pattern_variable_with_suffix<T>( 4668 4689 start: u32, 4690 + name: EcoString, 4669 4691 end: u32, 4670 4692 ) -> Result<T, ParseError> { 4671 4693 Err(ParseError { 4672 - error: ParseErrorType::ConcatPatternVariableInInfixPosition, 4694 + error: ParseErrorType::ConcatPatternVariableWithSuffix { name }, 4673 4695 location: SrcSpan::new(start, end), 4674 4696 }) 4675 4697 }
+7 -7
compiler-core/src/parse/error.rs
··· 110 110 ConcatPatternVariableLeftHandSide, 111 111 /// A variable was assigned as infix between prefix and suffix string 112 112 /// patterns using <> 113 - ConcatPatternVariableInInfixPosition, 113 + ConcatPatternVariableWithSuffix { 114 + name: EcoString, 115 + }, 114 116 ListSpreadWithoutTail, // let x = [1, ..] 115 117 ExpectedFunctionBody, // let x = fn() 116 118 RedundantInternalAttribute, // for a private definition marked as internal ··· 557 559 extra_labels: vec![], 558 560 }, 559 561 560 - ParseErrorType::ConcatPatternVariableInInfixPosition => ParseErrorDetails { 562 + ParseErrorType::ConcatPatternVariableWithSuffix { name}=> ParseErrorDetails { 561 563 text: [ 562 - "We can't tell what size this infix should be so we don't know", 563 - "how to handle this pattern.", 564 + "A string pattern can only match on a literal string prefix.", 564 565 "", 565 - "If you want to match one character consider using `pop_grapheme`", 566 - "from the stdlib's `gleam/string` module.", 566 + &format!("Matching on a literal suffix is not possible, because `{}` would have an unknown size.", name), 567 567 ] 568 568 .join("\n"), 569 569 hint: None, 570 - label_text: "This must be a string literal prefix".into(), 570 + label_text: "This pattern is not allowed".into(), 571 571 extra_labels: vec![], 572 572 }, 573 573
+19
compiler-core/src/parse/snapshots/gleam_core__parse__tests__assign_infix_and_match_suffix_of_concat_pattern.snap
··· 1 + --- 2 + source: compiler-core/src/parse/tests.rs 3 + expression: "case \"\" {\n \"prefix\" <> infix <> \"suffix\" -> infix\n}" 4 + --- 5 + ----- SOURCE CODE 6 + case "" { 7 + "prefix" <> infix <> "suffix" -> infix 8 + } 9 + 10 + ----- ERROR 11 + error: Syntax error 12 + ┌─ /src/parse/error.gleam:2:23 13 + 14 + 2 │ "prefix" <> infix <> "suffix" -> infix 15 + │ ^^^^^^^^^^^ This pattern is not allowed 16 + 17 + A string pattern can only match on a literal string prefix. 18 + 19 + Matching on a literal suffix is not possible, because `infix` would have an unknown size.
+19
compiler-core/src/parse/snapshots/gleam_core__parse__tests__discard_infix_and_match_suffix_of_concat_pattern.snap
··· 1 + --- 2 + source: compiler-core/src/parse/tests.rs 3 + expression: "case \"\" {\n \"prefix\" <> _ <> \"suffix\" -> Nil\n}" 4 + --- 5 + ----- SOURCE CODE 6 + case "" { 7 + "prefix" <> _ <> "suffix" -> Nil 8 + } 9 + 10 + ----- ERROR 11 + error: Syntax error 12 + ┌─ /src/parse/error.gleam:2:19 13 + 14 + 2 │ "prefix" <> _ <> "suffix" -> Nil 15 + │ ^^^^^^^^^^^ This pattern is not allowed 16 + 17 + A string pattern can only match on a literal string prefix. 18 + 19 + Matching on a literal suffix is not possible, because `_` would have an unknown size.
+19
compiler-core/src/parse/snapshots/gleam_core__parse__tests__incomplete_suffix_match_in_concat_pattern.snap
··· 1 + --- 2 + source: compiler-core/src/parse/tests.rs 3 + expression: "case \"\" {\n \"prefix\" <> wibble <> -> wibble\n}" 4 + --- 5 + ----- SOURCE CODE 6 + case "" { 7 + "prefix" <> wibble <> -> wibble 8 + } 9 + 10 + ----- ERROR 11 + error: Syntax error 12 + ┌─ /src/parse/error.gleam:2:24 13 + 14 + 2 │ "prefix" <> wibble <> -> wibble 15 + │ ^^ This pattern is not allowed 16 + 17 + A string pattern can only match on a literal string prefix. 18 + 19 + Matching on a literal suffix is not possible, because `wibble` would have an unknown size.
+11 -2
compiler-core/src/parse/tests.rs
··· 635 635 } 636 636 637 637 #[test] 638 - fn discard_infix_of_concat_pattern() { 638 + fn discard_infix_and_match_suffix_of_concat_pattern() { 639 639 assert_error!( 640 640 r#"case "" { 641 641 "prefix" <> _ <> "suffix" -> Nil ··· 644 644 } 645 645 646 646 #[test] 647 - fn assign_infix_of_concat_pattern() { 647 + fn assign_infix_and_match_suffix_of_concat_pattern() { 648 648 assert_error!( 649 649 r#"case "" { 650 650 "prefix" <> infix <> "suffix" -> infix 651 + }"# 652 + ); 653 + } 654 + 655 + #[test] 656 + fn incomplete_suffix_match_in_concat_pattern() { 657 + assert_error!( 658 + r#"case "" { 659 + "prefix" <> wibble <> -> wibble 651 660 }"# 652 661 ); 653 662 }