Skip to content

Commit

Permalink
add helpful error message
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri authored and lpil committed Sep 11, 2024
1 parent ac37b27 commit e5c2212
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
32 changes: 32 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,38 @@
- Improved error title when using an unknown module value.
([Giacomo Cavalieri](https://github.com/giacomocavalieri))

- The compiler now shows an helpful error message if you try writing an `if`
expression instead of a case. For example, this code:

```gleam
pub fn main() {
let a = if wibble {
1
}
}
```

Results in the following error:

```txt
error: Syntax error
┌─ /src/parse/error.gleam:3:11
3 │ let a = if wibble {
│ ^^ Gleam doesn't have if expressions
If you want to write a conditional expression you can use a `case`:
case condition {
True -> todo
False -> todo
}
See: https://tour.gleam.run/flow-control/case-expressions/
```

([Giacomo Cavalieri](https://github.com/giacomocavalieri))

### Formatter

- The formatter now adds a `todo` after a `use` expression if it is the last
Expand Down
6 changes: 6 additions & 0 deletions compiler-core/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,12 @@ where
}
}

// Helpful error if trying to write an if expression instead of a
// case.
Some((start, Token::If, end)) => {
return parse_error(ParseErrorType::IfExpression, SrcSpan { start, end });
}

// helpful error on possibly trying to group with ""
Some((start, Token::LeftParen, _)) => {
return parse_error(ParseErrorType::ExprLparStart, SrcSpan { start, end: start });
Expand Down
14 changes: 14 additions & 0 deletions compiler-core/src/parse/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,19 @@ utf16_codepoint, utf32_codepoint, signed, unsigned, big, little, native, size, u
"Unsupported expression",
vec!["Functions cannot be called in clause guards.".into()],
),
ParseErrorType::IfExpression => (
"Gleam doesn't have if expressions",
vec![
"If you want to write a conditional expression you can use a `case`:".into(),
"".into(),
" case condition {".into(),
" True -> todo".into(),
" False -> todo".into(),
" }".into(),
"".into(),
"See: https://tour.gleam.run/flow-control/case-expressions/".into(),
],
),
}
}
}
Expand Down Expand Up @@ -361,6 +374,7 @@ pub enum ParseErrorType {
field_type: Option<TypeAst>,
},
CallInClauseGuard, // case x { _ if f() -> 1 }
IfExpression,
}

impl LexicalError {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: compiler-core/src/parse/tests.rs
expression: "\npub fn main() {\n let a = if wibble {\n wobble\n }\n}\n"
---
error: Syntax error
┌─ /src/parse/error.gleam:3:11
3let a = if wibble {
│ ^^ Gleam doesn't have if expressions

If you want to write a conditional expression you can use a `case`:

case condition {
True -> todo
False -> todo
}

See: https://tour.gleam.run/flow-control/case-expressions/
13 changes: 13 additions & 0 deletions compiler-core/src/parse/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1493,3 +1493,16 @@ type Wibble {
"#
);
}

#[test]
fn if_like_expression() {
assert_module_error!(
r#"
pub fn main() {
let a = if wibble {
wobble
}
}
"#
);
}

0 comments on commit e5c2212

Please sign in to comment.