From 05454ab9936514409e1b3c97e36f3fb476d532ba Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 16 Mar 2019 23:21:51 +0100 Subject: Use Rc instead of Box in AST to allow structural sharing Closes #29 --- dhall/src/binary.rs | 3 ++- dhall/src/normalize.rs | 1 + dhall/src/typecheck.rs | 13 +++++++------ 3 files changed, 10 insertions(+), 7 deletions(-) (limited to 'dhall') diff --git a/dhall/src/binary.rs b/dhall/src/binary.rs index fbd343b..08bdba4 100644 --- a/dhall/src/binary.rs +++ b/dhall/src/binary.rs @@ -1,8 +1,9 @@ use dhall_core::*; use itertools::*; +use std::rc::Rc; use serde_cbor::value::value as cbor; -type ParsedExpr = Box>; +type ParsedExpr = Rc>; #[derive(Debug)] pub enum DecodeError { diff --git a/dhall/src/normalize.rs b/dhall/src/normalize.rs index c49d313..40112f1 100644 --- a/dhall/src/normalize.rs +++ b/dhall/src/normalize.rs @@ -2,6 +2,7 @@ use dhall_core::core::*; use dhall_generator::dhall_expr; use std::fmt; +use std::rc::Rc; /// Reduce an expression to its weak head normal form, i.e. normalize /// just enough to get the first constructor of the final expression diff --git a/dhall/src/typecheck.rs b/dhall/src/typecheck.rs index ca0d5af..f5670f7 100644 --- a/dhall/src/typecheck.rs +++ b/dhall/src/typecheck.rs @@ -2,6 +2,7 @@ use std::collections::BTreeMap; use std::collections::HashSet; use std::fmt; +use std::rc::Rc; use crate::normalize; use dhall_core::context::Context; @@ -415,7 +416,7 @@ where } ListLit(ref t, ref xs) => { let mut iter = xs.iter().enumerate(); - let t: Box> = match t { + let t: Rc> = match t { Some(t) => t.clone(), None => { let (_, first_x) = iter.next().unwrap(); @@ -426,7 +427,7 @@ where let s = normalize::<_, S, _>(&type_with(ctx, &t)?); match s { Const(Type) => {} - _ => return Err(TypeError::new(ctx, e, InvalidListType(*t))), + _ => return Err(TypeError::new(ctx, e, InvalidListType(t))), } for (i, x) in iter { let t2 = type_with(ctx, x)?; @@ -474,7 +475,7 @@ where )), OptionalLit(ref t, ref xs) => { let mut iter = xs.iter(); - let t: Box> = match t { + let t: Rc> = match t { Some(t) => t.clone(), None => { let x = iter.next().unwrap(); @@ -489,7 +490,7 @@ where return Err(TypeError::new( ctx, e, - InvalidOptionalType(*t), + InvalidOptionalType(t), )); } } @@ -689,10 +690,10 @@ pub enum TypeMessage { AnnotMismatch(Expr, Expr, Expr), Untyped, InvalidListElement(usize, Expr, Expr, Expr), - InvalidListType(Expr), + InvalidListType(Rc>), InvalidOptionalElement(Expr, Expr, Expr), InvalidOptionalLiteral(usize), - InvalidOptionalType(Expr), + InvalidOptionalType(Rc>), InvalidPredicate(Expr, Expr), IfBranchMismatch(Expr, Expr, Expr, Expr), IfBranchMustBeTerm(bool, Expr, Expr, Expr), -- cgit v1.2.3