From 5c342a5688fe7a4bb337ce0622968226d524022e Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 9 Feb 2020 15:32:27 +0000 Subject: Resolve by ref instead of by mut --- dhall/src/syntax/ast/expr.rs | 53 ++++---------------------------------------- 1 file changed, 4 insertions(+), 49 deletions(-) (limited to 'dhall/src/syntax/ast/expr.rs') diff --git a/dhall/src/syntax/ast/expr.rs b/dhall/src/syntax/ast/expr.rs index b493fdb..420df5b 100644 --- a/dhall/src/syntax/ast/expr.rs +++ b/dhall/src/syntax/ast/expr.rs @@ -1,5 +1,5 @@ use crate::syntax::map::{DupTreeMap, DupTreeSet}; -use crate::syntax::visitor::{self, ExprKindMutVisitor, ExprKindVisitor}; +use crate::syntax::visitor::{self, ExprKindVisitor}; use crate::syntax::*; pub type Integer = isize; @@ -208,13 +208,6 @@ impl ExprKind { self.traverse_ref_maybe_binder(|_, e| visit_subexpr(e)) } - fn traverse_mut<'a, Err>( - &'a mut self, - visit_subexpr: impl FnMut(&'a mut SE) -> Result<(), Err>, - ) -> Result<(), Err> { - visitor::TraverseMutVisitor { visit_subexpr }.visit(self) - } - pub fn map_ref_maybe_binder<'a, SE2>( &'a self, mut map: impl FnMut(Option<&'a Label>, &'a SE) -> SE2, @@ -248,16 +241,15 @@ impl ExprKind { { self.map_ref_maybe_binder(|_, e| map_subexpr(e)) } - - pub fn map_mut<'a>(&'a mut self, mut map_subexpr: impl FnMut(&'a mut SE)) { - trivial_result(self.traverse_mut(|x| Ok(map_subexpr(x)))) - } } impl Expr { pub fn as_ref(&self) -> &UnspannedExpr { &self.kind } + pub fn kind(&self) -> &UnspannedExpr { + &self.kind + } pub fn span(&self) -> Span { self.span.clone() } @@ -281,43 +273,6 @@ impl Expr { span, } } - - pub fn traverse_resolve_mut( - &mut self, - f: &mut F1, - ) -> Result<(), Err> - where - E: Clone, - F1: FnMut(Import>) -> Result, - { - match self.kind.as_mut() { - ExprKind::BinOp(BinOp::ImportAlt, l, r) => { - let garbage_expr = ExprKind::BoolLit(false); - let new_self = if l.traverse_resolve_mut(f).is_ok() { - l - } else { - r.traverse_resolve_mut(f)?; - r - }; - *self.kind = - std::mem::replace(new_self.kind.as_mut(), garbage_expr); - } - _ => { - self.kind.traverse_mut(|e| e.traverse_resolve_mut(f))?; - if let ExprKind::Import(import) = self.kind.as_mut() { - let garbage_import = Import { - mode: ImportMode::Code, - location: ImportLocation::Missing, - hash: None, - }; - // Move out of &mut import - let import = std::mem::replace(import, garbage_import); - *self.kind = ExprKind::Embed(f(import)?); - } - } - } - Ok(()) - } } pub fn trivial_result(x: Result) -> T { -- cgit v1.2.3 From 5a2538d174fd36a8ed7f4fa344b9583fc48bd977 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 11 Feb 2020 13:12:13 +0000 Subject: Remove the Embed variant from ExprKind --- dhall/src/syntax/ast/expr.rs | 58 +++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 39 deletions(-) (limited to 'dhall/src/syntax/ast/expr.rs') diff --git a/dhall/src/syntax/ast/expr.rs b/dhall/src/syntax/ast/expr.rs index 420df5b..722630f 100644 --- a/dhall/src/syntax/ast/expr.rs +++ b/dhall/src/syntax/ast/expr.rs @@ -96,19 +96,19 @@ pub enum Builtin { // Each node carries an annotation. #[derive(Debug, Clone)] -pub struct Expr { - kind: Box, Embed>>, +pub struct Expr { + kind: Box>, span: Span, } -pub type UnspannedExpr = ExprKind, Embed>; +pub type UnspannedExpr = ExprKind; /// Syntax tree for expressions // Having the recursion out of the enum definition enables writing // much more generic code and improves pattern-matching behind // smart pointers. #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum ExprKind { +pub enum ExprKind { Const(Const), /// `x` /// `x@n` @@ -169,18 +169,13 @@ pub enum ExprKind { Completion(SubExpr, SubExpr), /// `./some/path` Import(Import), - /// Embeds the result of resolving an import - Embed(Embed), } -impl ExprKind { +impl ExprKind { pub fn traverse_ref_maybe_binder<'a, SE2, Err>( &'a self, visit: impl FnMut(Option<&'a Label>, &'a SE) -> Result, - ) -> Result, Err> - where - E: Clone, - { + ) -> Result, Err> { visitor::TraverseRefMaybeBinderVisitor(visit).visit(self) } @@ -188,10 +183,7 @@ impl ExprKind { &'a self, mut visit_subexpr: impl FnMut(&'a SE) -> Result, mut visit_under_binder: impl FnMut(&'a Label, &'a SE) -> Result, - ) -> Result, Err> - where - E: Clone, - { + ) -> Result, Err> { self.traverse_ref_maybe_binder(|l, x| match l { None => visit_subexpr(x), Some(l) => visit_under_binder(l, x), @@ -201,20 +193,14 @@ impl ExprKind { pub(crate) fn traverse_ref<'a, SE2, Err>( &'a self, mut visit_subexpr: impl FnMut(&'a SE) -> Result, - ) -> Result, Err> - where - E: Clone, - { + ) -> Result, Err> { self.traverse_ref_maybe_binder(|_, e| visit_subexpr(e)) } pub fn map_ref_maybe_binder<'a, SE2>( &'a self, mut map: impl FnMut(Option<&'a Label>, &'a SE) -> SE2, - ) -> ExprKind - where - E: Clone, - { + ) -> ExprKind { trivial_result(self.traverse_ref_maybe_binder(|l, x| Ok(map(l, x)))) } @@ -222,10 +208,7 @@ impl ExprKind { &'a self, mut map_subexpr: impl FnMut(&'a SE) -> SE2, mut map_under_binder: impl FnMut(&'a Label, &'a SE) -> SE2, - ) -> ExprKind - where - E: Clone, - { + ) -> ExprKind { self.map_ref_maybe_binder(|l, x| match l { None => map_subexpr(x), Some(l) => map_under_binder(l, x), @@ -235,33 +218,30 @@ impl ExprKind { pub fn map_ref<'a, SE2>( &'a self, mut map_subexpr: impl FnMut(&'a SE) -> SE2, - ) -> ExprKind - where - E: Clone, - { + ) -> ExprKind { self.map_ref_maybe_binder(|_, e| map_subexpr(e)) } } -impl Expr { - pub fn as_ref(&self) -> &UnspannedExpr { +impl Expr { + pub fn as_ref(&self) -> &UnspannedExpr { &self.kind } - pub fn kind(&self) -> &UnspannedExpr { + pub fn kind(&self) -> &UnspannedExpr { &self.kind } pub fn span(&self) -> Span { self.span.clone() } - pub fn new(kind: UnspannedExpr, span: Span) -> Self { + pub fn new(kind: UnspannedExpr, span: Span) -> Self { Expr { kind: Box::new(kind), span, } } - pub fn rewrap(&self, kind: UnspannedExpr) -> Expr { + pub fn rewrap(&self, kind: UnspannedExpr) -> Expr { Expr { kind: Box::new(kind), span: self.span.clone(), @@ -317,15 +297,15 @@ impl From