feat(tvix/eval): add mechanism for emitting warnings from compiler

These can be used predominantly to emit warnings about things that the
compiler can infer, such as deprecated language features.

Change-Id: I3649c625459d7f3f95cdf42d5c651d23d66569ec
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6174
Reviewed-by: grfn <grfn@gws.fyi>
Tested-by: BuildkiteCI
This commit is contained in:
Vincent Ambo 2022-08-12 17:07:32 +03:00 committed by tazjin
parent 5512108ca7
commit 7e77972d71
4 changed files with 39 additions and 5 deletions

View file

@ -17,12 +17,22 @@ use crate::chunk::Chunk;
use crate::errors::EvalResult; use crate::errors::EvalResult;
use crate::opcode::{CodeIdx, OpCode}; use crate::opcode::{CodeIdx, OpCode};
use crate::value::Value; use crate::value::Value;
use crate::warnings::EvalWarning;
use rnix; use rnix;
use rnix::types::{BinOpKind, EntryHolder, TokenWrapper, TypedNode, Wrapper}; use rnix::types::{BinOpKind, EntryHolder, TokenWrapper, TypedNode, Wrapper};
/// Represents the result of compiling a piece of Nix code. If
/// compilation was successful, the resulting bytecode can be passed
/// to the VM.
pub struct CompilationResult {
pub chunk: Chunk,
pub warnings: Vec<EvalWarning>,
}
struct Compiler { struct Compiler {
chunk: Chunk, chunk: Chunk,
warnings: Vec<EvalWarning>,
} }
impl Compiler { impl Compiler {
@ -573,12 +583,16 @@ impl Compiler {
} }
} }
pub fn compile(ast: rnix::AST) -> EvalResult<Chunk> { pub fn compile(ast: rnix::AST) -> EvalResult<CompilationResult> {
let mut c = Compiler { let mut c = Compiler {
chunk: Chunk::default(), chunk: Chunk::default(),
warnings: vec![],
}; };
c.compile(ast.node())?; c.compile(ast.node())?;
Ok(c.chunk) Ok(CompilationResult {
chunk: c.chunk,
warnings: c.warnings,
})
} }

View file

@ -14,8 +14,16 @@ pub fn interpret(code: &str) -> EvalResult<Value> {
println!("{}", ast.root().dump()); println!("{}", ast.root().dump());
} }
let code = crate::compiler::compile(ast)?; let result = crate::compiler::compile(ast)?;
println!("code: {:?}", code); println!("code: {:?}", result.chunk);
crate::vm::run_chunk(code) for warning in result.warnings {
eprintln!(
"warning: {:?} at {:?}",
warning.kind,
warning.node.text_range().start()
)
}
crate::vm::run_chunk(result.chunk)
} }

View file

@ -5,6 +5,7 @@ mod eval;
mod opcode; mod opcode;
mod value; mod value;
mod vm; mod vm;
mod warnings;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;

11
tvix/eval/src/warnings.rs Normal file
View file

@ -0,0 +1,11 @@
/// Warnings are emitted in cases where code passed to Tvix exhibits
/// problems that the user could address.
#[derive(Debug)]
pub enum WarningKind {}
#[derive(Debug)]
pub struct EvalWarning {
pub node: rnix::SyntaxNode,
pub kind: WarningKind,
}