From 50baf0bcfc5ce29f7eed114535e7bb27ffd06cb6 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 6 Oct 2022 14:33:09 +0300 Subject: refactor(tvix/eval): move `spans` module to crate root This is also useful for error-handling related logic, outside of just the compiler module. Change-Id: I5c386e2b4c31cda0a0209b31136ca07f00e39e45 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6869 Tested-by: BuildkiteCI Reviewed-by: grfn --- tvix/eval/src/compiler/mod.rs | 9 ++++- tvix/eval/src/compiler/spans.rs | 84 ----------------------------------------- tvix/eval/src/lib.rs | 1 + tvix/eval/src/spans.rs | 79 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 86 deletions(-) delete mode 100644 tvix/eval/src/compiler/spans.rs create mode 100644 tvix/eval/src/spans.rs (limited to 'tvix/eval/src') diff --git a/tvix/eval/src/compiler/mod.rs b/tvix/eval/src/compiler/mod.rs index 472f4aaf363d..1463bb100eae 100644 --- a/tvix/eval/src/compiler/mod.rs +++ b/tvix/eval/src/compiler/mod.rs @@ -15,7 +15,6 @@ mod bindings; mod scope; -mod spans; use codemap::Span; use path_clean::PathClean; @@ -31,11 +30,11 @@ use crate::chunk::Chunk; use crate::errors::{Error, ErrorKind, EvalResult}; use crate::observer::CompilerObserver; use crate::opcode::{CodeIdx, Count, JumpOffset, OpCode, UpvalueIdx}; +use crate::spans::ToSpan; use crate::value::{Closure, Lambda, Thunk, Value}; use crate::warnings::{EvalWarning, WarningKind}; use self::scope::{LocalIdx, LocalPosition, Scope, Upvalue, UpvalueKind}; -use self::spans::ToSpan; /// Represents the result of compiling a piece of Nix code. If /// compilation was successful, the resulting bytecode can be passed @@ -99,6 +98,12 @@ struct Compiler<'observer> { observer: &'observer mut dyn CompilerObserver, } +impl Compiler<'_> { + pub(super) fn span_for(&self, to_span: &S) -> Span { + to_span.span_for(&self.file) + } +} + /// Compiler construction impl<'observer> Compiler<'observer> { pub(crate) fn new( diff --git a/tvix/eval/src/compiler/spans.rs b/tvix/eval/src/compiler/spans.rs deleted file mode 100644 index 6c11961e0eae..000000000000 --- a/tvix/eval/src/compiler/spans.rs +++ /dev/null @@ -1,84 +0,0 @@ -//! Utilities for dealing with span tracking in the compiler. - -use super::*; -use codemap::{File, Span}; -use rowan::ast::AstNode; - -/// Trait implemented by all types from which we can retrieve a span. -pub(super) trait ToSpan { - fn span_for(&self, file: &File) -> Span; -} - -impl ToSpan for Span { - fn span_for(&self, _: &File) -> Span { - *self - } -} - -impl ToSpan for rnix::SyntaxToken { - fn span_for(&self, file: &File) -> Span { - let rowan_span = self.text_range(); - file.span.subspan( - u32::from(rowan_span.start()) as u64, - u32::from(rowan_span.end()) as u64, - ) - } -} - -impl ToSpan for rnix::SyntaxNode { - fn span_for(&self, file: &File) -> Span { - let rowan_span = self.text_range(); - file.span.subspan( - u32::from(rowan_span.start()) as u64, - u32::from(rowan_span.end()) as u64, - ) - } -} - -/// Generates a `ToSpan` implementation for a type implementing -/// `rowan::AstNode`. This is impossible to do as a blanket -/// implementation because `rustc` forbids these implementations for -/// traits from third-party crates due to a belief that semantic -/// versioning truly could work (it doesn't). -macro_rules! expr_to_span { - ( $type:path ) => { - impl ToSpan for $type { - fn span_for(&self, file: &File) -> Span { - self.syntax().span_for(file) - } - } - }; -} - -expr_to_span!(ast::Expr); -expr_to_span!(ast::Apply); -expr_to_span!(ast::Assert); -expr_to_span!(ast::Attr); -expr_to_span!(ast::AttrSet); -expr_to_span!(ast::Attrpath); -expr_to_span!(ast::AttrpathValue); -expr_to_span!(ast::BinOp); -expr_to_span!(ast::HasAttr); -expr_to_span!(ast::Ident); -expr_to_span!(ast::IdentParam); -expr_to_span!(ast::IfElse); -expr_to_span!(ast::Inherit); -expr_to_span!(ast::Interpol); -expr_to_span!(ast::Lambda); -expr_to_span!(ast::LegacyLet); -expr_to_span!(ast::LetIn); -expr_to_span!(ast::List); -expr_to_span!(ast::Literal); -expr_to_span!(ast::PatBind); -expr_to_span!(ast::Path); -expr_to_span!(ast::Pattern); -expr_to_span!(ast::Select); -expr_to_span!(ast::Str); -expr_to_span!(ast::UnaryOp); -expr_to_span!(ast::With); - -impl Compiler<'_> { - pub(super) fn span_for(&self, to_span: &S) -> Span { - to_span.span_for(&self.file) - } -} diff --git a/tvix/eval/src/lib.rs b/tvix/eval/src/lib.rs index 847447cc8484..b4ffd25854ce 100644 --- a/tvix/eval/src/lib.rs +++ b/tvix/eval/src/lib.rs @@ -6,6 +6,7 @@ mod eval; pub mod observer; mod opcode; mod source; +mod spans; mod upvalues; mod value; mod vm; diff --git a/tvix/eval/src/spans.rs b/tvix/eval/src/spans.rs new file mode 100644 index 000000000000..c17ad2102ccf --- /dev/null +++ b/tvix/eval/src/spans.rs @@ -0,0 +1,79 @@ +//! Utilities for dealing with span tracking in the compiler and in +//! error reporting. + +use codemap::{File, Span}; +use rnix::ast; +use rowan::ast::AstNode; + +/// Trait implemented by all types from which we can retrieve a span. +pub trait ToSpan { + fn span_for(&self, file: &File) -> Span; +} + +impl ToSpan for Span { + fn span_for(&self, _: &File) -> Span { + *self + } +} + +impl ToSpan for rnix::SyntaxToken { + fn span_for(&self, file: &File) -> Span { + let rowan_span = self.text_range(); + file.span.subspan( + u32::from(rowan_span.start()) as u64, + u32::from(rowan_span.end()) as u64, + ) + } +} + +impl ToSpan for rnix::SyntaxNode { + fn span_for(&self, file: &File) -> Span { + let rowan_span = self.text_range(); + file.span.subspan( + u32::from(rowan_span.start()) as u64, + u32::from(rowan_span.end()) as u64, + ) + } +} + +/// Generates a `ToSpan` implementation for a type implementing +/// `rowan::AstNode`. This is impossible to do as a blanket +/// implementation because `rustc` forbids these implementations for +/// traits from third-party crates due to a belief that semantic +/// versioning truly could work (it doesn't). +macro_rules! expr_to_span { + ( $type:path ) => { + impl ToSpan for $type { + fn span_for(&self, file: &File) -> Span { + self.syntax().span_for(file) + } + } + }; +} + +expr_to_span!(ast::Expr); +expr_to_span!(ast::Apply); +expr_to_span!(ast::Assert); +expr_to_span!(ast::Attr); +expr_to_span!(ast::AttrSet); +expr_to_span!(ast::Attrpath); +expr_to_span!(ast::AttrpathValue); +expr_to_span!(ast::BinOp); +expr_to_span!(ast::HasAttr); +expr_to_span!(ast::Ident); +expr_to_span!(ast::IdentParam); +expr_to_span!(ast::IfElse); +expr_to_span!(ast::Inherit); +expr_to_span!(ast::Interpol); +expr_to_span!(ast::Lambda); +expr_to_span!(ast::LegacyLet); +expr_to_span!(ast::LetIn); +expr_to_span!(ast::List); +expr_to_span!(ast::Literal); +expr_to_span!(ast::PatBind); +expr_to_span!(ast::Path); +expr_to_span!(ast::Pattern); +expr_to_span!(ast::Select); +expr_to_span!(ast::Str); +expr_to_span!(ast::UnaryOp); +expr_to_span!(ast::With); -- cgit 1.4.1