about summary refs log tree commit diff
path: root/tvix/eval/src/warnings.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/warnings.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/warnings.rs')
-rw-r--r--tvix/eval/src/warnings.rs90
1 files changed, 90 insertions, 0 deletions
diff --git a/tvix/eval/src/warnings.rs b/tvix/eval/src/warnings.rs
index b37d0fc918..a09c5746d3 100644
--- a/tvix/eval/src/warnings.rs
+++ b/tvix/eval/src/warnings.rs
@@ -1,6 +1,9 @@
 //! Implements warnings that are emitted in cases where code passed to
 //! Tvix exhibits problems that the user could address.
 
+use codemap::CodeMap;
+use codemap_diagnostic::{ColorConfig, Diagnostic, Emitter, Level, SpanLabel, SpanStyle};
+
 #[derive(Debug)]
 pub enum WarningKind {
     DeprecatedLiteralURL,
@@ -18,3 +21,90 @@ pub struct EvalWarning {
     pub kind: WarningKind,
     pub span: codemap::Span,
 }
+
+impl EvalWarning {
+    /// Render a fancy, human-readable output of this warning and
+    /// return it as a String. Note that this version of the output
+    /// does not include any colours or font styles.
+    pub fn fancy_format_str(&self, codemap: &CodeMap) -> String {
+        let mut out = vec![];
+        Emitter::vec(&mut out, Some(codemap)).emit(&[self.diagnostic(codemap)]);
+        String::from_utf8_lossy(&out).to_string()
+    }
+
+    /// Render a fancy, human-readable output of this warning and
+    /// print it to stderr. If rendered in a terminal that supports
+    /// colours and font styles, the output will include those.
+    pub fn fancy_format_stderr(&self, codemap: &CodeMap) {
+        Emitter::stderr(ColorConfig::Auto, Some(codemap)).emit(&[self.diagnostic(codemap)]);
+    }
+
+    /// Create the optional span label displayed as an annotation on
+    /// the underlined span of the warning.
+    fn span_label(&self) -> Option<String> {
+        match self.kind {
+            WarningKind::UnusedBinding | WarningKind::ShadowedGlobal(_) => {
+                Some("variable declared here".into())
+            }
+            _ => None,
+        }
+    }
+
+    /// Create the primary warning message displayed to users for a
+    /// warning.
+    fn message(&self, codemap: &CodeMap) -> String {
+        match self.kind {
+            WarningKind::DeprecatedLiteralURL => {
+                format!("URL literal syntax is deprecated, use a quoted string instead")
+            }
+
+            WarningKind::UselessInherit => {
+                format!("inherited variable already exists with the same value")
+            }
+
+            WarningKind::UnusedBinding => {
+                let file = codemap.find_file(self.span.low());
+
+                format!(
+                    "variable '{}' is declared, but never used:",
+                    file.source_slice(self.span)
+                )
+            }
+
+            WarningKind::ShadowedGlobal(name) => {
+                format!("declared variable '{}' shadows a built-in global!", name)
+            }
+
+            WarningKind::NotImplemented(what) => {
+                format!("feature not yet implemented in tvix: {}", what)
+            }
+        }
+    }
+
+    /// Return the unique warning code for this variant which can be
+    /// used to refer users to documentation.
+    fn code(&self) -> &'static str {
+        match self.kind {
+            WarningKind::DeprecatedLiteralURL => "W001",
+            WarningKind::UselessInherit => "W002",
+            WarningKind::UnusedBinding => "W003",
+            WarningKind::ShadowedGlobal(_) => "W004",
+            WarningKind::NotImplemented(_) => "W999",
+        }
+    }
+
+    fn diagnostic(&self, codemap: &CodeMap) -> Diagnostic {
+        let span_label = SpanLabel {
+            label: self.span_label(),
+            span: self.span,
+            style: SpanStyle::Primary,
+        };
+
+        Diagnostic {
+            level: Level::Warning,
+            message: self.message(codemap),
+            spans: vec![span_label],
+            code: Some(self.code().into()),
+        }
+    }
+}