Repository: tweag/nickel (rust)

Issue:
crash in nls
The following input crashes nls, with a panic in the pretty-printer.

```nickel
{
  multipleOf
    : Number -> Dyn -> [| 'Ok, 'Error { message | String, notes] } |]
    = 1
}
```

There's a syntax error, but that isn't the point: it shouldn't cause a crash.

Pull request description:
Fix nls crash when typechecking parse errors
Fixes #2153.

Nls attempts to typecheck the input even if parsing fails (as long as the failure is non-fatal). The problem is that the pretty-printing code (for diagnosing a type error) assumes that all dynamic fields of all records are `Term::StrChunks`. This may be true for successful parses, but if the parse fails then you can also have a `Term::ParseError` as a dynamic field.

Patch:
diff --git a/core/src/pretty.rs b/core/src/pretty.rs
index 50077d93..d0d78bb4 100644
--- a/core/src/pretty.rs
+++ b/core/src/pretty.rs
@@ -522,6 +522,7 @@ impl Allocator {
         match id_expr.as_ref() {
             // Nickel will not parse a multiline string literal in this position
             Term::StrChunks(chunks) => self.chunks(chunks, StringRenderStyle::ForceMonoline),
+            Term::ParseError(_) => docs![self, "<parse error>"],
             _ => unimplemented!("Dynamic record fields must be StrChunks currently"),
         }
         .append(self.field_body(field))


Test patch:
diff --git a/lsp/nls/tests/inputs/no-crash-on-pretty-print.ncl b/lsp/nls/tests/inputs/no-crash-on-pretty-print.ncl
new file mode 100644
index 00000000..2d04e81c
--- /dev/null
+++ b/lsp/nls/tests/inputs/no-crash-on-pretty-print.ncl
@@ -0,0 +1,7 @@
+### /main.ncl
+{
+  multiple_of
+    : Number -> Dyn -> [| 'Ok, 'Error { message | String, notes] } |]
+    = 1
+}
+### diagnostic = ["file:///main.ncl"]
diff --git a/lsp/nls/tests/snapshots/main__lsp__nls__tests__inputs__no-crash-on-pretty-print.ncl.snap b/lsp/nls/tests/snapshots/main__lsp__nls__tests__inputs__no-crash-on-pretty-print.ncl.snap
new file mode 100644
index 00000000..6de4221f
--- /dev/null
+++ b/lsp/nls/tests/snapshots/main__lsp__nls__tests__inputs__no-crash-on-pretty-print.ncl.snap
@@ -0,0 +1,11 @@
+---
+source: lsp/nls/tests/main.rs
+expression: output
+---
+(file:///main.ncl, 2:63-2:64: )
+(file:///main.ncl, 2:63-2:64: unexpected token)
+(file:///main.ncl, 3:6-3:7: incompatible types
+Expected an expression of type `Number -> Dyn -> [| 'Ok, 'Error { message | String, <parse error>, } |]`
+Found an expression of type `Number`
+These types are not compatible)
+(file:///main.ncl, 3:6-3:7: this expression)