From 79595b1ae1a12fa2414d4b6c3c4bb2f7a0a9c094 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 3 Sep 2019 16:24:07 +0200 Subject: Resolve imports by mutating Expr instead of cloning it --- dhall/src/phase/resolve.rs | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) (limited to 'dhall/src') diff --git a/dhall/src/phase/resolve.rs b/dhall/src/phase/resolve.rs index a58f5e4..32e90c8 100644 --- a/dhall/src/phase/resolve.rs +++ b/dhall/src/phase/resolve.rs @@ -58,18 +58,16 @@ fn load_import( } fn do_resolve_expr( - Parsed(expr, root): Parsed, + parsed: Parsed, import_cache: &mut ImportCache, import_stack: &ImportStack, ) -> Result { - let resolve = |import: &Import| -> Result { - if import_stack.contains(import) { - return Err(ImportError::ImportCycle( - import_stack.clone(), - import.clone(), - )); + let Parsed(mut expr, root) = parsed; + let mut resolve = |import: Import| -> Result { + if import_stack.contains(&import) { + return Err(ImportError::ImportCycle(import_stack.clone(), import)); } - match import_cache.get(import) { + match import_cache.get(&import) { Some(expr) => Ok(expr.clone()), None => { // Copy the import stack and push the current import @@ -77,16 +75,20 @@ fn do_resolve_expr( import_stack.push(import.clone()); // Resolve the import recursively - let expr = - resolve_import(import, &root, import_cache, &import_stack)?; + let expr = resolve_import( + &import, + &root, + import_cache, + &import_stack, + )?; // Add the import to the cache - import_cache.insert(import.clone(), expr.clone()); + import_cache.insert(import, expr.clone()); Ok(expr) } } }; - let expr = expr.traverse_resolve(resolve)?; + expr.traverse_resolve_mut(&mut resolve)?; Ok(Resolved(expr)) } @@ -95,12 +97,13 @@ pub(crate) fn resolve(e: Parsed) -> Result { } pub(crate) fn skip_resolve_expr( - Parsed(expr, _root): Parsed, + parsed: Parsed, ) -> Result { - let resolve = |import: &Import| -> Result { - Err(ImportError::UnexpectedImport(import.clone())) + let mut expr = parsed.0; + let mut resolve = |import: Import| -> Result { + Err(ImportError::UnexpectedImport(import)) }; - let expr = expr.traverse_resolve(resolve)?; + expr.traverse_resolve_mut(&mut resolve)?; Ok(Resolved(expr)) } @@ -131,9 +134,9 @@ mod spec_tests { // import_success!(success_alternativeEnvNatural, "alternativeEnvNatural"); // import_success!(success_alternativeEnvSimple, "alternativeEnvSimple"); // import_success!(success_alternativeHashMismatch, "alternativeHashMismatch"); - // import_success!(success_alternativeNatural, "alternativeNatural"); - // import_success!(success_alternativeParseError, "alternativeParseError"); - // import_success!(success_alternativeTypeError, "alternativeTypeError"); + import_success!(success_alternativeNatural, "alternativeNatural"); + import_success!(success_alternativeParseError, "alternativeParseError"); + import_success!(success_alternativeTypeError, "alternativeTypeError"); // import_success!(success_asLocation, "asLocation"); // import_success!(success_asText, "asText"); // import_success!(success_customHeaders, "customHeaders"); -- cgit v1.2.3