Refactor dbg expression parsing to work more like function application

Instead of parsing dbg with an expression block, parse the dbg keyword
with no additional arguments. This way the parser treats dbg just like a
variable in function application. We desugar by pattern matching on
`Apply(Dbg, args, called_via)` nodes. This changes the output of syntax
tests since the initial AST is different, but does not change the output
of can or mono.

Add two new errors for dbg in expression position with either no args or
too many args. This is similar to the error behavior of `crash`.

Continue to parse dbg statements with an expression block, as before.
This commit is contained in:
Elias Mulhall 2024-08-29 14:31:35 -04:00
parent 14fabdff07
commit 56c5b790a7
11 changed files with 242 additions and 72 deletions

View file

@ -14494,6 +14494,76 @@ All branches in an `if` must have the same type!
"
);
test_report!(
dbg_unapplied,
indoc!(
r"
1 + dbg + 2
"
),
@r"
UNAPPLIED DBG in /code/proj/Main.roc
This `dbg` doesn't have a value given to it:
4 1 + dbg + 2
^^^
`dbg` must be passed a value to print at the exact place it's used. `dbg`
can't be used as a value that's passed around, like functions can be -
it must be applied immediately!
TYPE MISMATCH in /code/proj/Main.roc
This 2nd argument to + has an unexpected type:
4 1 + dbg + 2
^^^
This `63` value is a:
{}
But + needs its 2nd argument to be:
Num *
"
);
test_report!(
dbg_overapplied,
indoc!(
r#"
1 + dbg "" "" + 2
"#
),
@r#"
OVERAPPLIED DBG in /code/proj/Main.roc
This `dbg` has too many values given to it:
4 1 + dbg "" "" + 2
^^^^^
`dbg` must be given exactly one value to print.
TYPE MISMATCH in /code/proj/Main.roc
This 2nd argument to + has an unexpected type:
4 1 + dbg "" "" + 2
^^^^^^^^^
This `63` value is a:
{}
But + needs its 2nd argument to be:
Num *
"#
);
// TODO: add the following tests after built-in Tasks are added
// https://github.com/roc-lang/roc/pull/6836