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

Configure Feed

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

Improve error message for missing target name

+69 -11
+12 -11
compiler-core/src/parse.rs
··· 3482 3482 } 3483 3483 } 3484 3484 3485 - // Expect a target name. e.g. `javascript` or `erlang` 3486 - fn expect_target(&mut self) -> Result<Target, ParseError> { 3485 + // Expect a target name. e.g. `javascript` or `erlang`. 3486 + // The location of the preceding left parenthesis is required 3487 + // to give the correct error span in case the target name is missing. 3488 + fn expect_target(&mut self, paren_location: SrcSpan) -> Result<Target, ParseError> { 3487 3489 let (start, t, end) = match self.next_tok() { 3488 3490 Some(t) => t, 3489 3491 None => { 3490 3492 return parse_error(ParseErrorType::UnexpectedEof, SrcSpan { start: 0, end: 0 }); 3491 3493 } 3492 3494 }; 3495 + let location = SrcSpan::new(start, end); 3493 3496 match t { 3494 3497 Token::Name { name } => match name.as_str() { 3495 3498 "javascript" => Ok(Target::JavaScript), ··· 3497 3500 "js" => { 3498 3501 self.warnings 3499 3502 .push(DeprecatedSyntaxWarning::DeprecatedTargetShorthand { 3500 - location: SrcSpan { start, end }, 3503 + location, 3501 3504 target: Target::JavaScript, 3502 3505 }); 3503 3506 Ok(Target::JavaScript) ··· 3505 3508 "erl" => { 3506 3509 self.warnings 3507 3510 .push(DeprecatedSyntaxWarning::DeprecatedTargetShorthand { 3508 - location: SrcSpan { start, end }, 3511 + location, 3509 3512 target: Target::Erlang, 3510 3513 }); 3511 3514 Ok(Target::Erlang) 3512 3515 } 3513 - _ => parse_error(ParseErrorType::UnknownTarget, SrcSpan::new(start, end)), 3516 + _ => parse_error(ParseErrorType::UnknownTarget, location), 3514 3517 }, 3515 - _ => self.next_tok_unexpected(Target::variant_strings()), 3518 + _ => parse_error(ParseErrorType::ExpectedTargetName, paren_location), 3516 3519 } 3517 3520 } 3518 3521 ··· 3761 3764 let _ = self.expect_one(&Token::LeftParen)?; 3762 3765 self.parse_external_attribute(start, end, attributes) 3763 3766 } 3764 - "target" => { 3765 - let _ = self.expect_one(&Token::LeftParen)?; 3766 - self.parse_target_attribute(start, end, attributes) 3767 - } 3767 + "target" => self.parse_target_attribute(start, end, attributes), 3768 3768 "deprecated" => { 3769 3769 let _ = self.expect_one(&Token::LeftParen)?; 3770 3770 self.parse_deprecated_attribute(start, end, attributes) ··· 3782 3782 end: u32, 3783 3783 attributes: &mut Attributes, 3784 3784 ) -> Result<u32, ParseError> { 3785 - let target = self.expect_target()?; 3785 + let (paren_start, paren_end) = self.expect_one(&Token::LeftParen)?; 3786 + let target = self.expect_target(SrcSpan::new(paren_start, paren_end))?; 3786 3787 if attributes.target.is_some() { 3787 3788 return parse_error(ParseErrorType::DuplicateAttribute, SrcSpan { start, end }); 3788 3789 }
+5
compiler-core/src/parse/error.rs
··· 60 60 ParseErrorType::ExpectedFunctionDefinition => { 61 61 ("I was expecting a function definition after this", vec![]) 62 62 } 63 + ParseErrorType::ExpectedTargetName => ( 64 + "I was expecting a target name after this", 65 + vec!["Try `erlang`, `javascript`.".into()], 66 + ), 63 67 ParseErrorType::ExtraSeparator => ( 64 68 "This is an extra delimiter", 65 69 vec!["Hint: Try removing it?".into()], ··· 351 355 ExpectedDefinition, // after attributes 352 356 ExpectedDeprecationMessage, // after "deprecated" 353 357 ExpectedFunctionDefinition, // after function-only attributes 358 + ExpectedTargetName, // after "@target(" 354 359 ExprLparStart, // it seems "(" was used to start an expression 355 360 ExtraSeparator, // #(1,,) <- the 2nd comma is an extra separator 356 361 IncorrectName, // UpName or DiscardName used when Name was expected
+17
compiler-core/src/parse/snapshots/gleam_core__parse__tests__missing_target.snap
··· 1 + --- 2 + source: compiler-core/src/parse/tests.rs 3 + expression: "\n@target()\npub fn one() {}" 4 + --- 5 + ----- SOURCE CODE 6 + 7 + @target() 8 + pub fn one() {} 9 + 10 + ----- ERROR 11 + error: Syntax error 12 + ┌─ /src/parse/error.gleam:2:8 13 + 14 + 2 │ @target() 15 + │ ^ I was expecting a target name after this 16 + 17 + Try `erlang`, `javascript`.
+17
compiler-core/src/parse/snapshots/gleam_core__parse__tests__missing_target_and_bracket.snap
··· 1 + --- 2 + source: compiler-core/src/parse/tests.rs 3 + expression: "\n@target(\npub fn one() {}" 4 + --- 5 + ----- SOURCE CODE 6 + 7 + @target( 8 + pub fn one() {} 9 + 10 + ----- ERROR 11 + error: Syntax error 12 + ┌─ /src/parse/error.gleam:2:8 13 + 14 + 2 │ @target( 15 + │ ^ I was expecting a target name after this 16 + 17 + Try `erlang`, `javascript`.
+18
compiler-core/src/parse/tests.rs
··· 669 669 } 670 670 671 671 #[test] 672 + fn missing_target() { 673 + assert_module_error!( 674 + r#" 675 + @target() 676 + pub fn one() {}"# 677 + ); 678 + } 679 + 680 + #[test] 681 + fn missing_target_and_bracket() { 682 + assert_module_error!( 683 + r#" 684 + @target( 685 + pub fn one() {}"# 686 + ); 687 + } 688 + 689 + #[test] 672 690 fn unknown_attribute() { 673 691 assert_module_error!( 674 692 r#"@go_faster()