about summary refs log tree commit diff
path: root/tvix/eval/src
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@tvl.su>2024-02-20T08·38+0700
committertazjin <tazjin@tvl.su>2024-02-20T09·18+0000
commit94f582341e4f37228fbdf0997255d6374264b4b3 (patch)
treea3425ba313777076559203174ba5755864b05c98 /tvix/eval/src
parent3c87687798a3cfb6c3cfcc231e6c60511e3341ab (diff)
refactor(tvix/eval): use internal SourceCode field in error printers r/7572
Makes use of the SourceCode field now being stored directly in
errors (see parent CL). With this change, the default `Display`
implementation can now format errors correctly, and there is no need
to keep a `SourceCode` around just for error formatting.

Updates dependent crates (CLI, serde, tvixbolt) to use this correctly.

Change-Id: Iddc5d7a6b4bab391f30a999e4c68aca34304c059
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10987
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/eval/src')
-rw-r--r--tvix/eval/src/errors.rs33
1 files changed, 16 insertions, 17 deletions
diff --git a/tvix/eval/src/errors.rs b/tvix/eval/src/errors.rs
index 0f17aafe87..06a60fbbb9 100644
--- a/tvix/eval/src/errors.rs
+++ b/tvix/eval/src/errors.rs
@@ -514,7 +514,7 @@ to a missing value in the attribute set(s) included via `with`."#,
 
 impl Display for Error {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        write!(f, "{}", self.kind)
+        write!(f, "{}", self.fancy_format_str())
     }
 }
 
@@ -739,17 +739,16 @@ fn spans_for_parse_errors(file: &File, errors: &[rnix::parser::ParseError]) -> V
 }
 
 impl Error {
-    pub fn fancy_format_str(&self, source: &SourceCode) -> String {
+    pub fn fancy_format_str(&self) -> String {
         let mut out = vec![];
-        Emitter::vec(&mut out, Some(&*source.codemap())).emit(&self.diagnostics(source));
+        Emitter::vec(&mut out, Some(&*self.source.codemap())).emit(&self.diagnostics());
         String::from_utf8_lossy(&out).to_string()
     }
 
     /// Render a fancy, human-readable output of this error and print
     /// it to stderr.
-    pub fn fancy_format_stderr(&self, source: &SourceCode) {
-        Emitter::stderr(ColorConfig::Auto, Some(&*source.codemap()))
-            .emit(&self.diagnostics(source));
+    pub fn fancy_format_stderr(&self) {
+        Emitter::stderr(ColorConfig::Auto, Some(&*self.source.codemap())).emit(&self.diagnostics());
     }
 
     /// Create the optional span label displayed as an annotation on
@@ -863,14 +862,14 @@ impl Error {
         }
     }
 
-    fn spans(&self, source: &SourceCode) -> Vec<SpanLabel> {
+    fn spans(&self) -> Vec<SpanLabel> {
         let mut spans = match &self.kind {
             ErrorKind::ImportParseError { errors, file, .. } => {
                 spans_for_parse_errors(file, errors)
             }
 
             ErrorKind::ParseErrors(errors) => {
-                let file = source.get_file(self.span);
+                let file = self.source.get_file(self.span);
                 spans_for_parse_errors(&file, errors)
             }
 
@@ -949,22 +948,22 @@ impl Error {
     }
 
     /// Create the primary diagnostic for a given error.
-    fn diagnostic(&self, source: &SourceCode) -> Diagnostic {
+    fn diagnostic(&self) -> Diagnostic {
         Diagnostic {
             level: Level::Error,
             message: self.to_string(),
-            spans: self.spans(source),
+            spans: self.spans(),
             code: Some(self.code().into()),
         }
     }
 
     /// Return the primary diagnostic and all further associated diagnostics (if
     /// any) of an error.
-    fn diagnostics(&self, source: &SourceCode) -> Vec<Diagnostic> {
+    fn diagnostics(&self) -> Vec<Diagnostic> {
         match &self.kind {
             ErrorKind::ImportCompilerError { errors, .. } => {
-                let mut out = vec![self.diagnostic(source)];
-                out.extend(errors.iter().map(|e| e.diagnostic(source)));
+                let mut out = vec![self.diagnostic()];
+                out.extend(errors.iter().map(|e| e.diagnostic()));
                 out
             }
 
@@ -991,7 +990,7 @@ impl Error {
                 let mut this_span = self.span;
 
                 // Diagnostic spans for *this* error.
-                let mut this_spans = self.spans(source);
+                let mut this_spans = self.spans();
 
                 loop {
                     if is_new_span(
@@ -1008,7 +1007,7 @@ impl Error {
 
                     this_message = next.to_string();
                     this_span = next.span;
-                    this_spans = next.spans(source);
+                    this_spans = next.spans();
 
                     match next.kind {
                         ErrorKind::NativeError { err: inner, .. }
@@ -1017,7 +1016,7 @@ impl Error {
                             continue;
                         }
                         _ => {
-                            diagnostics.extend(next.diagnostics(source));
+                            diagnostics.extend(next.diagnostics());
                             break;
                         }
                     }
@@ -1026,7 +1025,7 @@ impl Error {
                 diagnostics
             }
 
-            _ => vec![self.diagnostic(source)],
+            _ => vec![self.diagnostic()],
         }
     }
 }