diff options
-rw-r--r-- | dhall/src/semantics/resolve/cache.rs | 16 | ||||
-rw-r--r-- | dhall/src/semantics/resolve/env.rs | 13 | ||||
-rw-r--r-- | dhall/src/semantics/resolve/resolve.rs | 30 |
3 files changed, 26 insertions, 33 deletions
diff --git a/dhall/src/semantics/resolve/cache.rs b/dhall/src/semantics/resolve/cache.rs index 2abe7a9..7763f18 100644 --- a/dhall/src/semantics/resolve/cache.rs +++ b/dhall/src/semantics/resolve/cache.rs @@ -4,8 +4,8 @@ use std::path::{Path, PathBuf}; use crate::error::{CacheError, Error}; use crate::parse::parse_binary; -use crate::semantics::TypedHir; use crate::syntax::{binary, Hash}; +use crate::Typed; use std::ffi::OsStr; use std::fs::File; @@ -52,7 +52,7 @@ impl Cache { self.cache_dir.join(filename_for_hash(hash)) } - pub fn get(&self, hash: &Hash) -> Result<TypedHir, Error> { + pub fn get(&self, hash: &Hash) -> Result<Typed, Error> { let path = self.entry_path(hash); let res = read_cache_file(&path, hash); if let Err(_) = res { @@ -64,14 +64,14 @@ impl Cache { res } - pub fn insert(&self, hash: &Hash, expr: &TypedHir) -> Result<(), Error> { + pub fn insert(&self, hash: &Hash, expr: &Typed) -> Result<(), Error> { let path = self.entry_path(hash); write_cache_file(&path, expr) } } /// Read a file from the cache, also checking that its hash is valid. -fn read_cache_file(path: &Path, hash: &Hash) -> Result<TypedHir, Error> { +fn read_cache_file(path: &Path, hash: &Hash) -> Result<Typed, Error> { let data = crate::utils::read_binary_file(path)?; match hash { @@ -83,14 +83,12 @@ fn read_cache_file(path: &Path, hash: &Hash) -> Result<TypedHir, Error> { } } - let crate::Typed { hir, ty } = - parse_binary(&data)?.skip_resolve()?.typecheck()?; - Ok((hir, ty)) + Ok(parse_binary(&data)?.skip_resolve()?.typecheck()?) } /// Write a file to the cache. -fn write_cache_file(path: &Path, expr: &TypedHir) -> Result<(), Error> { - let data = binary::encode(&expr.0.to_expr_noopts())?; +fn write_cache_file(path: &Path, expr: &Typed) -> Result<(), Error> { + let data = binary::encode(&expr.to_expr())?; File::create(path)?.write_all(data.as_slice())?; Ok(()) } diff --git a/dhall/src/semantics/resolve/env.rs b/dhall/src/semantics/resolve/env.rs index 06e27a5..a24c274 100644 --- a/dhall/src/semantics/resolve/env.rs +++ b/dhall/src/semantics/resolve/env.rs @@ -1,8 +1,9 @@ use std::collections::HashMap; use crate::error::{Error, ImportError}; -use crate::semantics::{AlphaVar, Cache, ImportLocation, TypedHir, VarEnv}; +use crate::semantics::{AlphaVar, Cache, ImportLocation, VarEnv}; use crate::syntax::{Hash, Label, V}; +use crate::Typed; /// Environment for resolving names. #[derive(Debug, Clone, Default)] @@ -10,7 +11,7 @@ pub struct NameEnv { names: Vec<Label>, } -pub type MemCache = HashMap<ImportLocation, TypedHir>; +pub type MemCache = HashMap<ImportLocation, Typed>; pub type CyclesStack = Vec<ImportLocation>; /// Environment for resolving imports @@ -78,7 +79,7 @@ impl ImportEnv { &mut self, location: &ImportLocation, hash: Option<&Hash>, - ) -> Option<TypedHir> { + ) -> Option<Typed> { if let Some(expr) = self.mem_cache.get(location) { return Some(expr.clone()); } @@ -91,7 +92,7 @@ impl ImportEnv { &mut self, location: ImportLocation, hash: Option<&Hash>, - expr: TypedHir, + expr: Typed, ) { if let Some(file_cache) = self.file_cache.as_ref() { if let Some(hash) = hash { @@ -104,8 +105,8 @@ impl ImportEnv { pub fn with_cycle_detection( &mut self, location: ImportLocation, - do_resolve: impl FnOnce(&mut Self) -> Result<TypedHir, Error>, - ) -> Result<TypedHir, Error> { + do_resolve: impl FnOnce(&mut Self) -> Result<Typed, Error>, + ) -> Result<Typed, Error> { if self.stack.contains(&location) { return Err( ImportError::ImportCycle(self.stack.clone(), location).into() diff --git a/dhall/src/semantics/resolve/resolve.rs b/dhall/src/semantics/resolve/resolve.rs index d6bc4cd..002490b 100644 --- a/dhall/src/semantics/resolve/resolve.rs +++ b/dhall/src/semantics/resolve/resolve.rs @@ -16,14 +16,11 @@ use crate::syntax::{ Expr, ExprKind, FilePath, FilePrefix, Hash, ImportMode, ImportTarget, Label, Span, UnspannedExpr, URL, }; -use crate::{Parsed, Resolved}; +use crate::{Parsed, Resolved, Typed}; // TODO: evaluate import headers pub type Import = syntax::Import<()>; -/// Owned Hir with a type. Different from Tir because the Hir is owned. -pub type TypedHir = (Hir, Type); - /// The location of some data, usually some dhall code. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum ImportLocation { @@ -220,14 +217,10 @@ fn make_aslocation_uniontype() -> Expr { mkexpr(ExprKind::UnionType(union)) } -fn check_hash( - import: &Import, - typed: &TypedHir, - span: Span, -) -> Result<(), Error> { +fn check_hash(import: &Import, typed: &Typed, span: Span) -> Result<(), Error> { match (import.mode, &import.hash) { (ImportMode::Code, Some(Hash::SHA256(hash))) => { - let actual_hash = typed.0.to_expr_alpha().sha256_hash()?; + let actual_hash = typed.hir.to_expr_alpha().sha256_hash()?; if hash[..] != actual_hash[..] { mkerr( ErrorBuilder::new("hash mismatch") @@ -251,27 +244,28 @@ fn resolve_one_import( import: &Import, location: ImportLocation, span: Span, -) -> Result<TypedHir, Error> { - match import.mode { +) -> Result<Typed, Error> { + let (hir, ty) = match import.mode { ImportMode::Code => { let parsed = location.fetch_dhall()?; let typed = resolve_with_env(env, parsed)?.typecheck()?; let hir = typed.normalize().to_hir(); - Ok((hir, typed.ty)) + (hir, typed.ty) } ImportMode::RawText => { let text = location.fetch_text()?; let hir = Hir::new(HirKind::Expr(ExprKind::TextLit(text.into())), span); - Ok((hir, Type::from_builtin(Builtin::Text))) + (hir, Type::from_builtin(Builtin::Text)) } ImportMode::Location => { let expr = location.into_location(); let hir = skip_resolve_expr(&expr)?; let ty = hir.typecheck_noenv()?.ty().clone(); - Ok((hir, ty)) + (hir, ty) } - } + }; + Ok(Typed { hir, ty }) } /// Desugar a `with` expression. @@ -335,7 +329,7 @@ fn desugar(expr: &Expr) -> Cow<'_, Expr> { fn traverse_resolve_expr( name_env: &mut NameEnv, expr: &Expr, - f: &mut impl FnMut(Import, Span) -> Result<TypedHir, Error>, + f: &mut impl FnMut(Import, Span) -> Result<Typed, Error>, ) -> Result<Hir, Error> { let expr = desugar(expr); Ok(match expr.kind() { @@ -375,7 +369,7 @@ fn traverse_resolve_expr( // TODO: evaluate import headers let import = import.traverse_ref(|_| Ok::<_, Error>(()))?; let imported = f(import, expr.span())?; - HirKind::Import(imported.0, imported.1) + HirKind::Import(imported.hir, imported.ty) } kind => HirKind::Expr(kind), }; |