From d17d553a39aa3bffdfc19b7fe4801b85d6bd80f7 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 11 Apr 2019 16:41:06 +0200 Subject: Add lifetime parameters to Parsed and Resolved Future-proofing --- dhall/src/expr.rs | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) (limited to 'dhall/src/expr.rs') diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs index 1ce20e3..e3a2fe5 100644 --- a/dhall/src/expr.rs +++ b/dhall/src/expr.rs @@ -1,5 +1,6 @@ use crate::imports::ImportRoot; use dhall_core::*; +use std::marker::PhantomData; macro_rules! derive_other_traits { ($ty:ident) => { @@ -20,13 +21,39 @@ macro_rules! derive_other_traits { }; } +macro_rules! derive_other_traits_ { + ($ty:ident) => { + impl<'a> std::cmp::PartialEq for $ty<'a> { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 + } + } + + impl<'a> std::fmt::Display for $ty<'a> { + fn fmt( + &self, + f: &mut std::fmt::Formatter, + ) -> Result<(), std::fmt::Error> { + self.0.fmt(f) + } + } + }; +} + #[derive(Debug, Clone, Eq)] -pub struct Parsed(pub(crate) SubExpr, pub(crate) ImportRoot); -derive_other_traits!(Parsed); +pub struct Parsed<'a>( + pub(crate) SubExpr, + pub(crate) ImportRoot, + pub(crate) PhantomData<&'a ()>, +); +derive_other_traits_!(Parsed); #[derive(Debug, Clone, Eq)] -pub struct Resolved(pub(crate) SubExpr); -derive_other_traits!(Resolved); +pub struct Resolved<'a>( + pub(crate) SubExpr, + pub(crate) PhantomData<&'a ()>, +); +derive_other_traits_!(Resolved); #[derive(Debug, Clone, Eq)] pub struct Typed(pub(crate) SubExpr, pub(crate) Option); -- cgit v1.2.3 From d9a2a77a19e56edd8eb96eba002e39bc7be3bde3 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 11 Apr 2019 20:45:43 +0200 Subject: Thread lifetimes through other newtypes Closes #55 --- dhall/src/expr.rs | 80 +++++++++++++++++++++++++------------------------------ 1 file changed, 36 insertions(+), 44 deletions(-) (limited to 'dhall/src/expr.rs') diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs index e3a2fe5..3987896 100644 --- a/dhall/src/expr.rs +++ b/dhall/src/expr.rs @@ -3,25 +3,6 @@ use dhall_core::*; use std::marker::PhantomData; macro_rules! derive_other_traits { - ($ty:ident) => { - impl std::cmp::PartialEq for $ty { - fn eq(&self, other: &Self) -> bool { - self.0 == other.0 - } - } - - impl std::fmt::Display for $ty { - fn fmt( - &self, - f: &mut std::fmt::Formatter, - ) -> Result<(), std::fmt::Error> { - self.0.fmt(f) - } - } - }; -} - -macro_rules! derive_other_traits_ { ($ty:ident) => { impl<'a> std::cmp::PartialEq for $ty<'a> { fn eq(&self, other: &Self) -> bool { @@ -46,85 +27,96 @@ pub struct Parsed<'a>( pub(crate) ImportRoot, pub(crate) PhantomData<&'a ()>, ); -derive_other_traits_!(Parsed); +derive_other_traits!(Parsed); #[derive(Debug, Clone, Eq)] pub struct Resolved<'a>( - pub(crate) SubExpr, + pub(crate) SubExpr>, pub(crate) PhantomData<&'a ()>, ); -derive_other_traits_!(Resolved); +derive_other_traits!(Resolved); #[derive(Debug, Clone, Eq)] -pub struct Typed(pub(crate) SubExpr, pub(crate) Option); +pub struct Typed<'a>( + pub(crate) SubExpr>, + pub(crate) Option>, + pub(crate) PhantomData<&'a ()>, +); derive_other_traits!(Typed); #[derive(Debug, Clone, Eq)] -pub struct Normalized(pub(crate) SubExpr, pub(crate) Option); +pub struct Normalized<'a>( + pub(crate) SubExpr, + pub(crate) Option>, + pub(crate) PhantomData<&'a ()>, +); derive_other_traits!(Normalized); /// An expression of type `Type` (like `Bool` or `Natural -> Text`, but not `Type`) #[derive(Debug, Clone, Eq)] -pub struct SimpleType(pub(crate) SubExpr); +pub struct SimpleType<'a>( + pub(crate) SubExpr, + pub(crate) PhantomData<&'a ()>, +); derive_other_traits!(SimpleType); #[derive(Debug, Clone, PartialEq, Eq)] -pub struct Type(pub(crate) TypeInternal); +pub struct Type<'a>(pub(crate) TypeInternal<'a>); #[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) enum TypeInternal { - Expr(Box), +pub(crate) enum TypeInternal<'a> { + Expr(Box>), // The type of `Sort` SuperType, } // Exposed for the macros #[doc(hidden)] -impl From for SubExpr { - fn from(x: SimpleType) -> SubExpr { +impl<'a> From> for SubExpr { + fn from(x: SimpleType<'a>) -> SubExpr { x.0 } } // Exposed for the macros #[doc(hidden)] -impl From> for SimpleType { - fn from(x: SubExpr) -> SimpleType { - SimpleType(x) +impl<'a> From> for SimpleType<'a> { + fn from(x: SubExpr) -> SimpleType<'a> { + SimpleType(x, PhantomData) } } // Exposed for the macros #[doc(hidden)] -impl From for Typed { - fn from(x: Normalized) -> Typed { - Typed(x.0.absurd(), x.1) +impl<'a> From> for Typed<'a> { + fn from(x: Normalized<'a>) -> Typed<'a> { + Typed(x.0.absurd(), x.1, x.2) } } -impl Typed { - pub(crate) fn as_expr(&self) -> &SubExpr { +impl<'a> Typed<'a> { + pub(crate) fn as_expr(&self) -> &SubExpr> { &self.0 } - pub(crate) fn into_expr(self) -> SubExpr { + pub(crate) fn into_expr(self) -> SubExpr> { self.0 } } -impl Normalized { +impl<'a> Normalized<'a> { pub(crate) fn as_expr(&self) -> &SubExpr { &self.0 } pub(crate) fn into_expr(self) -> SubExpr { self.0.absurd() } - pub(crate) fn into_type(self) -> Type { + pub(crate) fn into_type(self) -> Type<'a> { crate::expr::Type(TypeInternal::Expr(Box::new(self))) } } -impl SimpleType { - pub(crate) fn into_type(self) -> Type { - Normalized(self.0, Some(Type::const_type())).into_type() +impl<'a> SimpleType<'a> { + pub(crate) fn into_type(self) -> Type<'a> { + Normalized(self.0, Some(Type::const_type()), PhantomData).into_type() } } -- cgit v1.2.3 From 5fcc7f69c7a68b08ff223217e8af9f8edb2cc761 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 12 Apr 2019 16:32:29 +0200 Subject: Capture `Span`s in the AST and thread them through Parsed and Resolved --- dhall/src/expr.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'dhall/src/expr.rs') diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs index 3987896..ab59ce0 100644 --- a/dhall/src/expr.rs +++ b/dhall/src/expr.rs @@ -23,17 +23,13 @@ macro_rules! derive_other_traits { #[derive(Debug, Clone, Eq)] pub struct Parsed<'a>( - pub(crate) SubExpr, + pub(crate) SubExpr, Import>, pub(crate) ImportRoot, - pub(crate) PhantomData<&'a ()>, ); derive_other_traits!(Parsed); #[derive(Debug, Clone, Eq)] -pub struct Resolved<'a>( - pub(crate) SubExpr>, - pub(crate) PhantomData<&'a ()>, -); +pub struct Resolved<'a>(pub(crate) SubExpr, Normalized<'static>>); derive_other_traits!(Resolved); #[derive(Debug, Clone, Eq)] -- cgit v1.2.3