From b83e497fa5ab938c33c9d4b5db4f48f4b4861c3f Mon Sep 17 00:00:00 2001 From: Giacomo Cavalieri Date: Tue, 8 Oct 2024 18:09:17 +0200 Subject: [PATCH] handle echo not followed by an expression to print --- compiler-core/src/error.rs | 16 ++++++++++++++++ compiler-core/src/type_/error.rs | 15 +++++++++++++++ compiler-core/src/type_/expression.rs | 7 +------ compiler-core/src/type_/pipe.rs | 7 +++++++ ...__errors__echo_followed_by_no_expression.snap | 11 +++++++++++ ...rrors__echo_followed_by_no_expression_10.snap | 11 +++++++++++ ...errors__echo_followed_by_no_expression_2.snap | 11 +++++++++++ ...errors__echo_followed_by_no_expression_3.snap | 11 +++++++++++ ...errors__echo_followed_by_no_expression_4.snap | 11 +++++++++++ ...errors__echo_followed_by_no_expression_5.snap | 11 +++++++++++ ...errors__echo_followed_by_no_expression_6.snap | 11 +++++++++++ ...errors__echo_followed_by_no_expression_7.snap | 11 +++++++++++ ...errors__echo_followed_by_no_expression_8.snap | 11 +++++++++++ ...errors__echo_followed_by_no_expression_9.snap | 11 +++++++++++ 14 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression.snap create mode 100644 compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_10.snap create mode 100644 compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_2.snap create mode 100644 compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_3.snap create mode 100644 compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_4.snap create mode 100644 compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_5.snap create mode 100644 compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_6.snap create mode 100644 compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_7.snap create mode 100644 compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_8.snap create mode 100644 compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_9.snap diff --git a/compiler-core/src/error.rs b/compiler-core/src/error.rs index 5ae8042d42a..760e77f7f06 100644 --- a/compiler-core/src/error.rs +++ b/compiler-core/src/error.rs @@ -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(), diff --git a/compiler-core/src/type_/error.rs b/compiler-core/src/type_/error.rs index d860fc17dad..6e31ba6fcc2 100644 --- a/compiler-core/src/type_/error.rs +++ b/compiler-core/src/type_/error.rs @@ -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)] @@ -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) diff --git a/compiler-core/src/type_/expression.rs b/compiler-core/src/type_/expression.rs index dff76048d41..54ab583a7e4 100644 --- a/compiler-core/src/type_/expression.rs +++ b/compiler-core/src/type_/expression.rs @@ -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 }) } } diff --git a/compiler-core/src/type_/pipe.rs b/compiler-core/src/type_/pipe.rs index 7f055e28974..2de5046ac1f 100644 --- a/compiler-core/src/type_/pipe.rs +++ b/compiler-core/src/type_/pipe.rs @@ -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)?, }; diff --git a/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression.snap b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression.snap new file mode 100644 index 00000000000..dab338442bf --- /dev/null +++ b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression.snap @@ -0,0 +1,11 @@ +--- +source: compiler-core/src/type_/tests/errors.rs +expression: echo +--- +error: Invalid echo use + ┌─ /src/one/two.gleam:1:1 + │ +1 │ echo + │ ^^^^ I was expecting a value after this + +The `echo` keyword should be followed by a value to print. diff --git a/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_10.snap b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_10.snap new file mode 100644 index 00000000000..d55aa71dabc --- /dev/null +++ b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_10.snap @@ -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 + │ +3 │ echo + │ ^^^^ I was expecting a value after this + +The `echo` keyword should be followed by a value to print. diff --git a/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_2.snap b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_2.snap new file mode 100644 index 00000000000..c2dbf3048da --- /dev/null +++ b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_2.snap @@ -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 + │ +5 │ wibble(echo) + │ ^^^^ I was expecting a value after this + +The `echo` keyword should be followed by a value to print. diff --git a/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_3.snap b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_3.snap new file mode 100644 index 00000000000..68819d56a73 --- /dev/null +++ b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_3.snap @@ -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 + │ +3 │ echo + 1 + │ ^^^^ I was expecting a value after this + +The `echo` keyword should be followed by a value to print. diff --git a/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_4.snap b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_4.snap new file mode 100644 index 00000000000..c477a930b61 --- /dev/null +++ b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_4.snap @@ -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. diff --git a/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_5.snap b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_5.snap new file mode 100644 index 00000000000..c5d3bc23d3d --- /dev/null +++ b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_5.snap @@ -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 + │ +3 │ panic as echo + │ ^^^^ I was expecting a value after this + +The `echo` keyword should be followed by a value to print. diff --git a/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_6.snap b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_6.snap new file mode 100644 index 00000000000..2326a8d3bb4 --- /dev/null +++ b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_6.snap @@ -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. diff --git a/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_7.snap b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_7.snap new file mode 100644 index 00000000000..4e1e04800c3 --- /dev/null +++ b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_7.snap @@ -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. diff --git a/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_8.snap b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_8.snap new file mode 100644 index 00000000000..bf99b47f081 --- /dev/null +++ b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_8.snap @@ -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. diff --git a/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_9.snap b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_9.snap new file mode 100644 index 00000000000..107b993ba60 --- /dev/null +++ b/compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__echo_followed_by_no_expression_9.snap @@ -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.