about summary refs log tree commit diff
path: root/tvix/eval/src/eval.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-09-11T22·44+0300
committertazjin <tazjin@tvl.su>2022-09-13T11·12+0000
commit4f67cf221ab4328637025c2b39e43d68a30c6813 (patch)
treed11ec50189c09a8cb1a218ecef8132e8766b6578 /tvix/eval/src/eval.rs
parentbeb78c710441f475f1b03437a381f75fb0e788d6 (diff)
feat(tvix/eval): implement initial fancy display for warnings r/4830
This implements an initial fancy display for warnings emitted by the
tvix compiler, using the codemap_diagnostic crate.

Each warning variant has an associated message, and optionally an
associated annotation for the span displayed to the user.

In theory we could get a lot more fancy with the display for specific
variants if needed (e.g. re-parse the AST and actually add multiple
semantic spans based on context), but this is already a good start.

Example:

  tvix-repl> let toString = https://tvl.fyi; in let inherit toString; in ({}: 42) rec {}
  warning[W004]: declared variable 'toString' shadows a built-in global!
   --> [tvix-repl]:1:5
    |
  1 | let toString = https://tvl.fyi; in let inherit toString; in ({}: 42) rec {}
    |     ^^^^^^^^ variable declared here

  warning[W001]: URL literal syntax is deprecated, use a quoted string instead
   --> [tvix-repl]:1:16
    |
  1 | let toString = https://tvl.fyi; in let inherit toString; in ({}: 42) rec {}
    |                ^^^^^^^^^^^^^^^

  warning[W002]: inherited variable already exists with the same value
   --> [tvix-repl]:1:40
    |
  1 | let toString = https://tvl.fyi; in let inherit toString; in ({}: 42) rec {}
    |                                        ^^^^^^^^^^^^^^^^^

  warning[W999]: feature not yet implemented in tvix: recursive attribute sets
   --> [tvix-repl]:1:70
    |
  1 | let toString = https://tvl.fyi; in let inherit toString; in ({}: 42) rec {}
    |                                                                      ^^^^^^

  warning[W999]: feature not yet implemented in tvix: closed formals
   --> [tvix-repl]:1:62
    |
  1 | let toString = https://tvl.fyi; in let inherit toString; in ({}: 42) rec {}
    |                                                              ^^

  warning[W003]: variable 'toString' is declared, but never used:
   --> [tvix-repl]:1:5
    |
  1 | let toString = https://tvl.fyi; in let inherit toString; in ({}: 42) rec {}
    |     ^^^^^^^^ variable declared here

  => 42 :: int

These are coloured when output to a terminal.

Change-Id: If315648a07e333895db4ae1d0915ee2013806585
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6532
Autosubmit: tazjin <tazjin@tvl.su>
Reviewed-by: grfn <grfn@gws.fyi>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval/src/eval.rs')
-rw-r--r--tvix/eval/src/eval.rs11
1 files changed, 3 insertions, 8 deletions
diff --git a/tvix/eval/src/eval.rs b/tvix/eval/src/eval.rs
index 585463dde22e..4ed97b329d80 100644
--- a/tvix/eval/src/eval.rs
+++ b/tvix/eval/src/eval.rs
@@ -13,7 +13,7 @@ pub fn interpret(code: &str, location: Option<PathBuf>) -> EvalResult<Value> {
         location
             .as_ref()
             .map(|p| p.to_string_lossy().to_string())
-            .unwrap_or_else(|| "<repl>".into()),
+            .unwrap_or_else(|| "[tvix-repl]".into()),
         code.into(),
     );
     let codemap = Rc::new(codemap);
@@ -47,7 +47,7 @@ pub fn interpret(code: &str, location: Option<PathBuf>) -> EvalResult<Value> {
             location,
             &file,
             global_builtins(),
-            &mut DisassemblingObserver::new(codemap, std::io::stderr()),
+            &mut DisassemblingObserver::new(codemap.clone(), std::io::stderr()),
         )
     } else {
         crate::compiler::compile(
@@ -60,12 +60,7 @@ pub fn interpret(code: &str, location: Option<PathBuf>) -> EvalResult<Value> {
     }?;
 
     for warning in result.warnings {
-        eprintln!(
-            "warning: {:?} at `{}`[line {}]",
-            warning.kind,
-            file.source_slice(warning.span),
-            file.find_line(warning.span.low()) + 1
-        )
+        warning.fancy_format_stderr(&codemap);
     }
 
     for error in &result.errors {