roc-lang / roc

A fast, friendly, functional language.
https://roc-lang.org
Universal Permissive License v1.0
4.46k stars 314 forks source link

REPL panic instead of SYNTAX PROBLEM error #6922

Open basile-henry opened 3 months ago

basile-henry commented 3 months ago
Enter an expression to evaluate, or a definition (like x = 1) to use later.
» "hello" : Str
panicked at crates/repl_ui/src/repl_state.rs:133:25:
not yet implemented: handle pattern other than identifier (which repl doesn't support).
Tip: this error can be triggered when trying to define a variable with a character that is not allowed, like starting with an uppercase character or using underdash (_).

Stack:

Error
    at imports.wbg.__wbg_new_abda76e883ba8a5f (https://www.roc-lang.org/repl/roc_repl_wasm.js:314:21)
    at console_error_panic_hook::hook::h6a88c36edf37c7ee (https://www.roc-lang.org/repl/roc_repl_wasm_bg.wasm:wasm-function[1180]:0x390cf8)
    at std::panicking::rust_panic_with_hook::h9aabd906218897c3 (https://www.roc-lang.org/repl/roc_repl_wasm_bg.wasm:wasm-function[2668]:0x410e9d)
    at std::panicking::begin_panic_handler::{{closure}}::h96d2bc381fa6ee1e (https://www.roc-lang.org/repl/roc_repl_wasm_bg.wasm:wasm-function[3094]:0x425233)
    at std::sys_common::backtrace::__rust_end_short_backtrace::ha76513a70bb070b0 (https://www.roc-lang.org/repl/roc_repl_wasm_bg.wasm:wasm-function[5226]:0x44fff4)
    at rust_begin_unwind (https://www.roc-lang.org/repl/roc_repl_wasm_bg.wasm:wasm-function[4443]:0x448ceb)
    at core::panicking::panic_fmt::h3aff855fe938c13f (https://www.roc-lang.org/repl/roc_repl_wasm_bg.wasm:wasm-function[4508]:0x449d3b)
    at roc_repl_ui::repl_state::ReplState::step::h55f73d441a8f1c10 (https://www.roc-lang.org/repl/roc_repl_wasm_bg.wasm:wasm-function[74]:0x1534da)
    at wasm_bindgen_futures::future_to_promise::{{closure}}::{{closure}}::hb7e81c410d97a7d8 (https://www.roc-lang.org/repl/roc_repl_wasm_bg.wasm:wasm-function[414]:0x2bb161)
    at wasm_bindgen_futures::queue::Queue::new::{{closure}}::h0ac4b2913c4ef62d (https://www.roc-lang.org/repl/roc_repl_wasm_bg.wasm:wasm-function[1788]:0x3d7661)

This was on https://www.roc-lang.org/#try-roc but I get a similar error in the CLI REPL

My expectation was that what the REPL prints was valid code, so I thought a type signature/annotation would be allowed. Is that not the case?

Anton-4 commented 3 months ago

Type signatures are allowed, but not with a literal like "hello" or 123, this is the case for all Roc code, not just in the repl. You can do:

hello : Str
hello = "hey"

We should provide a nice error message here though. When using "hello" : Str in a roc file you get a SYNTAX PROBLEM error. The repl should also throw/report this error

My expectation was that what the REPL prints was valid code [...]

It is convenient to know the type of your output and the : Str is a familiar syntax to indicate this. Do you have any suggestions how we could fix/improve the expectations around this?

basile-henry commented 3 months ago

Ah right, I see, type annotations cannot be used as expressions like they can in Haskell where something like ("hello" :: String) is a valid expression. I thought Elm was the same as Haskell, but it looks like it's exactly like Roc: the type annotation is not an expression but the REPL makes it look like it could be :sweat_smile:

Do you have any suggestions how we could fix/improve the expectations around this?

I guess if it didn't panic and gave an error message I would understand that type annotations aren't expressions in Roc (and I don't need them to be).

For reference here is what Elm gives me:

---- Elm 0.19.1 ----------------------------------------------------------------
Say :help for help and :exit to exit! More at <https://elm-lang.org/0.19.1/repl>
--------------------------------------------------------------------------------
> "Hello"
"Hello" : String
> "Hello" : String
|
-- UNEXPECTED SYMBOL ------------------------------------------------------ REPL

I was not expecting to run into the "has type" symbol here:

3|   "Hello" : String
             ^
Maybe you want :: instead? To put something on the front of a list?

Note: The single colon is reserved for type annotations and record types, but I
think I am parsing the definition of `repl_input_value_` right now.

Note: I may be getting confused by your indentation. Is this supposed to be part
of a type annotation AFTER the `repl_input_value_` definition? If so, the
problem may be a bit before the "has type" symbol. I need all definitions to be
exactly aligned (with exactly the same indentation) so the problem may be that
this new definition is indented a bit too much.