Skip to content

Commit

Permalink
Add UnexpectedFunction error for functions called outside other funct…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
Ian-GL authored and lpil committed Dec 1, 2022
1 parent bcd2854 commit 2acce02
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 1 addition & 4 deletions compiler-core/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2171,10 +2171,7 @@ where
let _ = self.next_tok(); // name

parse_error(
ParseErrorType::UnexpectedToken {
expected: vec!["A constant definition".to_string()],
hint: Some("Functions cannot be called in constant declarations. You can use let instead (and make sure it is inside a scope)".to_string())
},
ParseErrorType::UnexpectedFunction,
SrcSpan { start, end: end + 1},
)
} else {
Expand Down
5 changes: 5 additions & 0 deletions compiler-core/src/parse/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ utf16_codepoint, utf32_codepoint, signed, unsigned, big, little, native, size, u
"from the stdlib's `gleam/string` module".into(),
],
),
ParseErrorType::UnexpectedFunction => (
"Functions can only be called within other functions.",
vec![]
)
}
}
}
Expand Down Expand Up @@ -246,6 +250,7 @@ pub enum ParseErrorType {
hint: Option<String>,
},
ExpectedBoolean,
UnexpectedFunction, // a function was used called outside of another function
// A variable was assigned or discarded on the left hand side of a <> pattern
ConcatPatternVariableLeftHandSide,
}
Expand Down
5 changes: 5 additions & 0 deletions compiler-core/src/parse/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,8 @@ fn assign_left_hand_side_of_concat_pattern() {
"#
);
}

#[test]
fn function_called_outside_function() {
assert_error!("const first = list.at([1], 0)");
}

0 comments on commit 2acce02

Please sign in to comment.