refactor(tvix/eval): consume self in Evaluation::evaluate
				
					
				
			This simplifies lifetime management for observers in callers of tvix_eval. Change-Id: I2f47c8d89f22b1c766526e5d1483c0d026b500ae Reviewed-on: https://cl.tvl.fyi/c/depot/+/7546 Autosubmit: tazjin <tazjin@tvl.su> Reviewed-by: grfn <grfn@gws.fyi> Tested-by: BuildkiteCI
This commit is contained in:
		
							parent
							
								
									59e695a9d9
								
							
						
					
					
						commit
						6d3d237923
					
				
					 2 changed files with 8 additions and 9 deletions
				
			
		| 
						 | 
					@ -20,10 +20,10 @@ struct Args {
 | 
				
			||||||
/// and the result itself. The return value indicates whether
 | 
					/// and the result itself. The return value indicates whether
 | 
				
			||||||
/// evaluation succeeded.
 | 
					/// evaluation succeeded.
 | 
				
			||||||
fn interpret(code: &str, path: Option<PathBuf>) -> bool {
 | 
					fn interpret(code: &str, path: Option<PathBuf>) -> bool {
 | 
				
			||||||
    let mut eval = tvix_eval::Evaluation::new(code, path);
 | 
					    let eval = tvix_eval::Evaluation::new(code, path);
 | 
				
			||||||
 | 
					    let source_map = eval.source_map();
 | 
				
			||||||
    let result = eval.evaluate();
 | 
					    let result = eval.evaluate();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let source_map = eval.source_map();
 | 
					 | 
				
			||||||
    for error in &result.errors {
 | 
					    for error in &result.errors {
 | 
				
			||||||
        error.fancy_format_stderr(&source_map);
 | 
					        error.fancy_format_stderr(&source_map);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -85,9 +85,6 @@ pub struct Evaluation<'code, 'co, 'ro> {
 | 
				
			||||||
    /// Top-level file reference for this code inside the source map.
 | 
					    /// Top-level file reference for this code inside the source map.
 | 
				
			||||||
    file: Arc<codemap::File>,
 | 
					    file: Arc<codemap::File>,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Root expression of the Nix code after parsing.
 | 
					 | 
				
			||||||
    expr: Option<rnix::ast::Expr>,
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /// (optional) Nix search path, e.g. the value of `NIX_PATH` used
 | 
					    /// (optional) Nix search path, e.g. the value of `NIX_PATH` used
 | 
				
			||||||
    /// for resolving items on the search path (such as `<nixpkgs>`).
 | 
					    /// for resolving items on the search path (such as `<nixpkgs>`).
 | 
				
			||||||
    pub nix_path: Option<String>,
 | 
					    pub nix_path: Option<String>,
 | 
				
			||||||
| 
						 | 
					@ -115,6 +112,9 @@ pub struct EvaluationResult {
 | 
				
			||||||
    /// Warnings that occured during evaluation. Warnings are not critical, but
 | 
					    /// Warnings that occured during evaluation. Warnings are not critical, but
 | 
				
			||||||
    /// should be addressed either to modernise code or improve performance.
 | 
					    /// should be addressed either to modernise code or improve performance.
 | 
				
			||||||
    pub warnings: Vec<EvalWarning>,
 | 
					    pub warnings: Vec<EvalWarning>,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// AST node that was parsed from the code (on success only).
 | 
				
			||||||
 | 
					    pub expr: Option<rnix::ast::Expr>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl<'code, 'co, 'ro> Evaluation<'code, 'co, 'ro> {
 | 
					impl<'code, 'co, 'ro> Evaluation<'code, 'co, 'ro> {
 | 
				
			||||||
| 
						 | 
					@ -136,7 +136,6 @@ impl<'code, 'co, 'ro> Evaluation<'code, 'co, 'ro> {
 | 
				
			||||||
            location,
 | 
					            location,
 | 
				
			||||||
            source_map,
 | 
					            source_map,
 | 
				
			||||||
            file,
 | 
					            file,
 | 
				
			||||||
            expr: None,
 | 
					 | 
				
			||||||
            nix_path: None,
 | 
					            nix_path: None,
 | 
				
			||||||
            compiler_observer: None,
 | 
					            compiler_observer: None,
 | 
				
			||||||
            runtime_observer: None,
 | 
					            runtime_observer: None,
 | 
				
			||||||
| 
						 | 
					@ -150,7 +149,7 @@ impl<'code, 'co, 'ro> Evaluation<'code, 'co, 'ro> {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Evaluate the provided source code.
 | 
					    /// Evaluate the provided source code.
 | 
				
			||||||
    pub fn evaluate(&mut self) -> EvaluationResult {
 | 
					    pub fn evaluate(mut self) -> EvaluationResult {
 | 
				
			||||||
        let mut result = EvaluationResult::default();
 | 
					        let mut result = EvaluationResult::default();
 | 
				
			||||||
        let parsed = rnix::ast::Root::parse(self.code);
 | 
					        let parsed = rnix::ast::Root::parse(self.code);
 | 
				
			||||||
        let parse_errors = parsed.errors();
 | 
					        let parse_errors = parsed.errors();
 | 
				
			||||||
| 
						 | 
					@ -168,7 +167,7 @@ impl<'code, 'co, 'ro> Evaluation<'code, 'co, 'ro> {
 | 
				
			||||||
        //
 | 
					        //
 | 
				
			||||||
        // The root expression is persisted in self in case the caller wants
 | 
					        // The root expression is persisted in self in case the caller wants
 | 
				
			||||||
        // access to the parsed expression.
 | 
					        // access to the parsed expression.
 | 
				
			||||||
        self.expr = parsed.tree().expr();
 | 
					        result.expr = parsed.tree().expr();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let builtins =
 | 
					        let builtins =
 | 
				
			||||||
            crate::compiler::prepare_globals(Box::new(global_builtins(self.source_map())));
 | 
					            crate::compiler::prepare_globals(Box::new(global_builtins(self.source_map())));
 | 
				
			||||||
| 
						 | 
					@ -177,7 +176,7 @@ impl<'code, 'co, 'ro> Evaluation<'code, 'co, 'ro> {
 | 
				
			||||||
        let compiler_observer = self.compiler_observer.take().unwrap_or(&mut noop_observer);
 | 
					        let compiler_observer = self.compiler_observer.take().unwrap_or(&mut noop_observer);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let compiler_result = match compiler::compile(
 | 
					        let compiler_result = match compiler::compile(
 | 
				
			||||||
            self.expr.as_ref().unwrap(),
 | 
					            result.expr.as_ref().unwrap(),
 | 
				
			||||||
            self.location.take(),
 | 
					            self.location.take(),
 | 
				
			||||||
            self.file.clone(),
 | 
					            self.file.clone(),
 | 
				
			||||||
            builtins,
 | 
					            builtins,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue