Skip to content

Commit

Permalink
handle echo not followed by an expression to print
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocavalieri committed Oct 9, 2024
1 parent f4fca50 commit b83e497
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 6 deletions.
16 changes: 16 additions & 0 deletions compiler-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3079,6 +3079,22 @@ Try: _{}", kind_str.to_title_case(), name.to_snake_case()),
}),
}
},

TypeError::EchoWithNoFollowingExpression { location } => Diagnostic {
title: format!("Invalid echo use"),
text: wrap("The `echo` keyword should be followed by a value to print."),
hint: None,
level: Level::Error,
location: Some(Location {
label: Label {
text: Some("I was expecting a value after this".into()),
span: *location,
},
path: path.clone(),
src: src.clone(),
extra_labels: vec![],
}),
},
}
}).collect_vec(),

Expand Down
15 changes: 15 additions & 0 deletions compiler-core/src/type_/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,20 @@ pub enum Error {
kind: Named,
name: EcoString,
},

/// When the echo keyword is not followed by an expression to be printed.
/// The only place where echo is allowed to appear on its own is as a step
/// of a pipeline, otherwise omitting the expression will result in this
/// error. For example:
///
/// ```gleam
/// call(echo, 1, 2)
/// // ^^^^ Error!
/// ```
///
EchoWithNoFollowingExpression {
location: SrcSpan,
},
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -938,6 +952,7 @@ impl Error {
}
| Error::UseFnDoesntTakeCallback { location, .. }
| Error::UseFnIncorrectArity { location, .. }
| Error::EchoWithNoFollowingExpression { location }
| Error::BadName { location, .. } => location.start,
Error::UnknownLabels { unknown, .. } => {
unknown.iter().map(|(_, s)| s.start).min().unwrap_or(0)
Expand Down
7 changes: 1 addition & 6 deletions compiler-core/src/type_/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,12 +479,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
let typed_expression = self.infer(*expression);
Ok(todo!("typed expr ECHO"))
} else {
todo!(
"
echo with no expression after it
should only be in a pipeline
"
)
Err(Error::EchoWithNoFollowingExpression { location })
}
}

Expand Down
7 changes: 7 additions & 0 deletions compiler-core/src/type_/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ impl<'a, 'b, 'c> PipeTyper<'a, 'b, 'c> {
}
}

UntypedExpr::Echo {
location,
expression: None,
} => {
todo!("This echo is actually ok!")
}

// right(left)
call => self.infer_apply_pipe(call)?,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: compiler-core/src/type_/tests/errors.rs
expression: echo
---
error: Invalid echo use
┌─ /src/one/two.gleam:1:1
1echo
^^^^ I was expecting a value after this

The `echo` keyword should be followed by a value to print.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: compiler-core/src/type_/tests/errors.rs
expression: "\npub fn main() {\n echo\n |> todo\n}\n"
---
error: Invalid echo use
┌─ /src/one/two.gleam:3:3
3echo
^^^^ I was expecting a value after this

The `echo` keyword should be followed by a value to print.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: compiler-core/src/type_/tests/errors.rs
expression: "\n pub fn wibble(a) { a }\n\n pub fn main() {\n wibble(echo)\n }\n"
---
error: Invalid echo use
┌─ /src/one/two.gleam:5:12
5wibble(echo)
^^^^ I was expecting a value after this

The `echo` keyword should be followed by a value to print.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: compiler-core/src/type_/tests/errors.rs
expression: "\n pub fn main() {\n echo + 1\n }\n"
---
error: Invalid echo use
┌─ /src/one/two.gleam:3:5
3echo + 1
^^^^ I was expecting a value after this

The `echo` keyword should be followed by a value to print.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: compiler-core/src/type_/tests/errors.rs
expression: "\n pub fn main() {\n \"wibble\" <> echo\n }\n"
---
error: Invalid echo use
┌─ /src/one/two.gleam:3:17
3"wibble" <> echo
^^^^ I was expecting a value after this

The `echo` keyword should be followed by a value to print.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: compiler-core/src/type_/tests/errors.rs
expression: "\npub fn main() {\n panic as echo\n}\n"
---
error: Invalid echo use
┌─ /src/one/two.gleam:3:12
3panic as echo
^^^^ I was expecting a value after this

The `echo` keyword should be followed by a value to print.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: compiler-core/src/type_/tests/errors.rs
expression: "\npub fn main() {\n [echo, 1, 2]\n}\n"
---
error: Invalid echo use
┌─ /src/one/two.gleam:3:4
3 │ [echo, 1, 2]
^^^^ I was expecting a value after this

The `echo` keyword should be followed by a value to print.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: compiler-core/src/type_/tests/errors.rs
expression: "\npub fn main() {\n #(1, echo)\n}\n"
---
error: Invalid echo use
┌─ /src/one/two.gleam:3:8
3 │ #(1, echo)
^^^^ I was expecting a value after this

The `echo` keyword should be followed by a value to print.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: compiler-core/src/type_/tests/errors.rs
expression: "\npub fn main() {\n todo\n |> fn(_) { echo }\n |> todo\n}\n"
---
error: Invalid echo use
┌─ /src/one/two.gleam:4:14
4|> fn(_) { echo }
^^^^ I was expecting a value after this

The `echo` keyword should be followed by a value to print.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: compiler-core/src/type_/tests/errors.rs
expression: "\npub fn main() {\n todo\n |> { echo }\n |> todo\n}\n"
---
error: Invalid echo use
┌─ /src/one/two.gleam:4:8
4|> { echo }
^^^^ I was expecting a value after this

The `echo` keyword should be followed by a value to print.

0 comments on commit b83e497

Please sign in to comment.