From c785b7c0c6cd8b3b1cc15eb79caf982a757020ba Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 6 Dec 2020 23:55:21 +0000 Subject: Thread cx through normalization --- serde_dhall/src/options/de.rs | 40 +++++++++++++++++++++------------------- serde_dhall/src/value.rs | 4 ++-- 2 files changed, 23 insertions(+), 21 deletions(-) (limited to 'serde_dhall/src') diff --git a/serde_dhall/src/options/de.rs b/serde_dhall/src/options/de.rs index 39ab5ca..d57759a 100644 --- a/serde_dhall/src/options/de.rs +++ b/serde_dhall/src/options/de.rs @@ -1,6 +1,6 @@ use std::path::{Path, PathBuf}; -use dhall::Parsed; +use dhall::{Ctxt, Parsed}; use crate::options::{HasAnnot, ManualAnnot, NoAnnot, StaticAnnot, TypeAnnot}; use crate::SimpleType; @@ -234,24 +234,26 @@ impl<'a, A> Deserializer<'a, A> { A: TypeAnnot, T: HasAnnot, { - let parsed = match &self.source { - Source::Str(s) => Parsed::parse_str(s)?, - Source::File(p) => Parsed::parse_file(p.as_ref())?, - Source::BinaryFile(p) => Parsed::parse_binary_file(p.as_ref())?, - }; - let resolved = if self.allow_imports { - parsed.resolve()? - } else { - parsed.skip_resolve()? - }; - let typed = match &T::get_annot(self.annot) { - None => resolved.typecheck()?, - Some(ty) => resolved.typecheck_with(&ty.to_hir())?, - }; - Ok(Value::from_nir_and_ty( - typed.normalize().as_nir(), - typed.ty().as_nir(), - )) + Ctxt::with_new(|cx| { + let parsed = match &self.source { + Source::Str(s) => Parsed::parse_str(s)?, + Source::File(p) => Parsed::parse_file(p.as_ref())?, + Source::BinaryFile(p) => Parsed::parse_binary_file(p.as_ref())?, + }; + let resolved = if self.allow_imports { + parsed.resolve(cx)? + } else { + parsed.skip_resolve()? + }; + let typed = match &T::get_annot(self.annot) { + None => resolved.typecheck(cx)?, + Some(ty) => resolved.typecheck_with(cx, &ty.to_hir())?, + }; + Ok(Value::from_nir_and_ty( + typed.normalize(cx).as_nir(), + typed.ty().as_nir(), + )) + }) } /// Parses the chosen dhall value with the options provided. diff --git a/serde_dhall/src/value.rs b/serde_dhall/src/value.rs index 4b22158..e5f5acd 100644 --- a/serde_dhall/src/value.rs +++ b/serde_dhall/src/value.rs @@ -335,7 +335,7 @@ impl SimpleValue { // Converts this to `Hir`, using the optional type annotation. Without the type, things like // empty lists and unions will fail to convert. - fn to_hir(&self, ty: Option<&SimpleType>) -> Result { + fn to_hir<'cx>(&self, ty: Option<&SimpleType>) -> Result> { use SimpleType as T; use SimpleValue as V; let hir = |k| Hir::new(HirKind::Expr(k), Span::Artificial); @@ -481,7 +481,7 @@ impl SimpleType { }) } - pub(crate) fn to_hir(&self) -> Hir { + pub(crate) fn to_hir<'cx>(&self) -> Hir<'cx> { let hir = |k| Hir::new(HirKind::Expr(k), Span::Artificial); hir(match self { SimpleType::Bool => ExprKind::Builtin(Builtin::Bool), -- cgit v1.2.3