Skip to content
16 changes: 16 additions & 0 deletions compiler-core/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2117,6 +2117,22 @@ where
)?;
let (_, rpar_e) =
self.expect_one_following_series(&Token::RightParen, "a function parameter")?;

// Check for TypeScript-style return type annotation (:) instead of arrow (->)
if let Some((colon_start, colon_end)) = self.maybe_one(&Token::Colon) {
return Err(ParseError {
error: ParseErrorType::UnexpectedToken {
token: Token::Colon,
expected: vec!["`->`".into()],
hint: Some("Maybe you meant to write return value type annotation?\nReturn type annotations are written using `->`, not `:`".into()),
},
location: SrcSpan {
start: colon_start,
end: colon_end,
},
});
};

let return_annotation = self.parse_type_annotation(&Token::RArrow)?;

let (body_start, body, end, end_position) = match self.maybe_one(&Token::LeftBrace) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: compiler-core/src/parse/tests.rs
expression: "\npub fn main(): Nil {}\n "
---
----- SOURCE CODE

pub fn main(): Nil {}


----- ERROR
error: Syntax error
┌─ /src/parse/error.gleam:2:14
2 │ pub fn main(): Nil {}
│ ^ I was not expecting this

Found `:`, expected one of:
- `->`
Hint: Maybe you meant to write return value type annotation?
Return type annotations are written using `->`, not `:`
9 changes: 9 additions & 0 deletions compiler-core/src/parse/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1992,3 +1992,12 @@ pub fn main() {
"#
);
}

#[test]
fn wrong_function_return_type_declaration_using_colon_instead_of_right_arrow() {
assert_module_error!(
r#"
pub fn main(): Nil {}
"#
);
}
Loading