From e502da276b4aac49d1ac3b8a8896aa2670a442fc Mon Sep 17 00:00:00 2001 From: fteychene Date: Tue, 12 May 2020 14:57:16 +0200 Subject: feat: Add cache resolution on resolve --- dhall/src/semantics/resolve/resolve.rs | 63 +++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 28 deletions(-) (limited to 'dhall/src/semantics/resolve/resolve.rs') diff --git a/dhall/src/semantics/resolve/resolve.rs b/dhall/src/semantics/resolve/resolve.rs index e96f16b..264b355 100644 --- a/dhall/src/semantics/resolve/resolve.rs +++ b/dhall/src/semantics/resolve/resolve.rs @@ -9,7 +9,7 @@ use crate::builtins::Builtin; use crate::error::ErrorBuilder; use crate::error::{Error, ImportError}; use crate::operations::{BinOp, OpKind}; -use crate::semantics::{mkerr, Hir, HirKind, ImportEnv, NameEnv, Type}; +use crate::semantics::{mkerr, Cache, Hir, HirKind, ImportEnv, NameEnv, Type}; use crate::syntax; use crate::syntax::{ Expr, ExprKind, FilePath, FilePrefix, Hash, ImportMode, ImportTarget, Span, @@ -209,6 +209,7 @@ fn make_aslocation_uniontype() -> Expr { fn resolve_one_import( env: &mut ImportEnv, + cache: &Cache, import: &Import, location: &ImportLocation, span: Span, @@ -217,32 +218,37 @@ fn resolve_one_import( let location = location.chain(&import.location, do_sanity_check)?; env.handle_import(location.clone(), |env| match import.mode { ImportMode::Code => { - let parsed = location.fetch_dhall()?; - let typed = resolve_with_env(env, parsed)?.typecheck()?; - let hir = typed.normalize().to_hir(); - let ty = typed.ty().clone(); - match &import.hash { - Some(Hash::SHA256(hash)) => { - let actual_hash = hir.to_expr_alpha().hash()?; - if hash[..] != actual_hash[..] { - mkerr( - ErrorBuilder::new("hash mismatch") - .span_err(span, "hash mismatch") - .note(format!( - "Expected sha256:{}", - hex::encode(hash) - )) - .note(format!( - "Found sha256:{}", - hex::encode(actual_hash) - )) - .format(), - )? + let (hir, ty) = cache.caching_import( + import, + || location.fetch_dhall(), + |parsed| { + let typed = resolve_with_env(env, cache, parsed)?.typecheck()?; + let hir = typed.normalize().to_hir(); + Ok((hir, typed.ty)) } + )?; + match &import.hash { + Some(Hash::SHA256(hash)) => { + let actual_hash = hir.to_expr_alpha().hash()?; + if hash[..] != actual_hash[..] { + mkerr( + ErrorBuilder::new("hash mismatch") + .span_err(span, "hash mismatch") + .note(format!( + "Expected sha256:{}", + hex::encode(hash) + )) + .note(format!( + "Found sha256:{}", + hex::encode(actual_hash) + )) + .format(), + )? + } + } + None => {} } - None => {} - } - Ok((hir, ty)) + Ok((hir, ty)) } ImportMode::RawText => { let text = location.fetch_text()?; @@ -346,19 +352,21 @@ fn traverse_resolve_expr( fn resolve_with_env( env: &mut ImportEnv, + cache: &Cache, parsed: Parsed, ) -> Result { let Parsed(expr, location) = parsed; let resolved = traverse_resolve_expr( &mut NameEnv::new(), &expr, - &mut |import, span| resolve_one_import(env, &import, &location, span), + &mut |import, span| resolve_one_import(env, cache, &import, &location, span), )?; Ok(Resolved(resolved)) } pub fn resolve(parsed: Parsed) -> Result { - resolve_with_env(&mut ImportEnv::new(), parsed) + let cache = Cache::new(); + resolve_with_env(&mut ImportEnv::new(), &cache, parsed) } pub fn skip_resolve_expr(expr: &Expr) -> Result { @@ -387,7 +395,6 @@ impl Canonicalize for FilePath { // ─────────────────────────────────────── // canonicalize(directory₀/.) = directory₁ "." => continue, - ".." => match file_path.last() { // canonicalize(directory₀) = ε // ──────────────────────────── -- cgit v1.2.3 From 43b360789afbed8adb958c95bb198b1618aa5c0d Mon Sep 17 00:00:00 2001 From: fteychene Date: Fri, 15 May 2020 00:55:30 +0200 Subject: fixup! Release serde_dhall version 0.5.2 --- dhall/src/semantics/resolve/resolve.rs | 63 ++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 30 deletions(-) (limited to 'dhall/src/semantics/resolve/resolve.rs') diff --git a/dhall/src/semantics/resolve/resolve.rs b/dhall/src/semantics/resolve/resolve.rs index 264b355..3b72b9e 100644 --- a/dhall/src/semantics/resolve/resolve.rs +++ b/dhall/src/semantics/resolve/resolve.rs @@ -218,37 +218,38 @@ fn resolve_one_import( let location = location.chain(&import.location, do_sanity_check)?; env.handle_import(location.clone(), |env| match import.mode { ImportMode::Code => { - let (hir, ty) = cache.caching_import( - import, - || location.fetch_dhall(), - |parsed| { - let typed = resolve_with_env(env, cache, parsed)?.typecheck()?; - let hir = typed.normalize().to_hir(); - Ok((hir, typed.ty)) - } - )?; - match &import.hash { - Some(Hash::SHA256(hash)) => { - let actual_hash = hir.to_expr_alpha().hash()?; - if hash[..] != actual_hash[..] { - mkerr( - ErrorBuilder::new("hash mismatch") - .span_err(span, "hash mismatch") - .note(format!( - "Expected sha256:{}", - hex::encode(hash) - )) - .note(format!( - "Found sha256:{}", - hex::encode(actual_hash) - )) - .format(), - )? - } + let (hir, ty) = cache.caching_import( + import, + || location.fetch_dhall(), + |parsed| { + let typed = + resolve_with_env(env, cache, parsed)?.typecheck()?; + let hir = typed.normalize().to_hir(); + Ok((hir, typed.ty)) + }, + )?; + match &import.hash { + Some(Hash::SHA256(hash)) => { + let actual_hash = hir.to_expr_alpha().hash()?; + if hash[..] != actual_hash[..] { + mkerr( + ErrorBuilder::new("hash mismatch") + .span_err(span, "hash mismatch") + .note(format!( + "Expected sha256:{}", + hex::encode(hash) + )) + .note(format!( + "Found sha256:{}", + hex::encode(actual_hash) + )) + .format(), + )? } - None => {} } - Ok((hir, ty)) + None => {} + } + Ok((hir, ty)) } ImportMode::RawText => { let text = location.fetch_text()?; @@ -359,7 +360,9 @@ fn resolve_with_env( let resolved = traverse_resolve_expr( &mut NameEnv::new(), &expr, - &mut |import, span| resolve_one_import(env, cache, &import, &location, span), + &mut |import, span| { + resolve_one_import(env, cache, &import, &location, span) + }, )?; Ok(Resolved(resolved)) } -- cgit v1.2.3