Remove parse error on bytearray literals for trace, todo & error, parse as String instead.

This has been bothering me and the more I thought of it the more I
  disliked the idea of a warning. The rationale being that in this very
  context, there's absolutely no ambiguity. So it is only frustrating
  that the parser is even able to make the exact suggestion of what
  should be fixed, but still fails.

  I can imagine it is going to be very common for people to type:

  ```
  trace "foo"
  ```

  ...yet terribly frustrating if they have to remember each time that
  this should actually be a string. Because of the `trace`, `todo` and
  `error` keywords, we know exactly the surrounding context and what to
  expect here. So we can work it nicely.

  However, the formatter will re-format it to:

  ```
  trace @"foo"
  ```

  Just for the sake of remaining consistent with the type-system. This
  way, we still only manipulate `String` in the AST, but we conveniently
  parse a double-quote utf-8 literal when coupled with one of the
  specific keywords.

  I believe that's the best of both worlds.
This commit is contained in:
KtorZ
2023-02-19 09:47:06 +01:00
parent 78770d14b7
commit f307e214c3
3 changed files with 66 additions and 97 deletions

View File

@@ -344,7 +344,7 @@ fn test_nested_function_calls() {
),
when output.datum is {
InlineDatum(_) -> True
_ -> error "expected inline datum"
_ -> error @"expected inline datum"
},
]
|> list.and
@@ -382,7 +382,33 @@ fn format_trace_todo_error() {
}
"#};
assert_fmt(src, src);
let out = indoc! {r#"
fn foo_1() {
todo
}
fn foo_2() {
todo @"my custom message"
}
fn foo_3() {
when x is {
Foo -> True
_ -> error
}
}
fn foo_4() {
if 14 == 42 {
error @"I don't think so"
} else {
trace @"been there"
True
}
}
"#};
assert_fmt(src, out);
}
#[test]
@@ -508,3 +534,16 @@ fn test_bytearray_literals() {
assert_fmt(src, src);
}
#[test]
fn test_string_literal() {
let src = indoc! {r#"
const foo_const: String = @"foo"
fn foo() {
let foo_var: String = @"foo"
}
"#};
assert_fmt(src, src);
}