From fedb732270eb24e544b047e19e5a7533028a2f5b Mon Sep 17 00:00:00 2001 From: Markus Westerlind Date: Sat, 6 Jun 2020 14:46:50 +0200 Subject: [PATCH] feat: Add Default for Root/OwnedExpr --- base/src/ast.rs | 62 ++++++++++++++++++++++++++++------------------ scripts/publish.sh | 1 - 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/base/src/ast.rs b/base/src/ast.rs index 53a916ea23..e44e4934b4 100644 --- a/base/src/ast.rs +++ b/base/src/ast.rs @@ -1287,6 +1287,30 @@ impl_ast_arena! { Metadata => metadata, } +#[doc(hidden)] +#[derive(Default)] +pub struct InvariantLifetime<'a>(std::marker::PhantomData &'a ()>); + +// Copied from the compact_arena crate +#[macro_export] +macro_rules! mk_ast_arena { + ($name: ident) => { + let tag = $crate::ast::InvariantLifetime::default(); + let $name = unsafe { std::sync::Arc::new($crate::ast::Arena::new(&tag)) }; + let _guard; + // this doesn't make it to MIR, but ensures that borrowck will not + // unify the lifetimes of two macro calls by binding the lifetime to + // drop scope + if false { + struct Guard<'tag>(&'tag $crate::ast::InvariantLifetime<'tag>); + impl<'tag> ::core::ops::Drop for Guard<'tag> { + fn drop(&mut self) {} + } + _guard = Guard(&tag); + } + }; +} + pub struct RootExpr { // Only used to keep `expr` alive #[allow(dead_code)] @@ -1294,6 +1318,14 @@ pub struct RootExpr { expr: *mut SpannedExpr<'static, Id>, } +impl Default for RootExpr { + fn default() -> Self { + mk_ast_arena!(arena); + let expr = arena.alloc(SpannedExpr::default()); + RootExpr::new(arena.clone(), expr) + } +} + impl Eq for RootExpr {} impl PartialEq for RootExpr { fn eq(&self, other: &Self) -> bool { @@ -1393,6 +1425,12 @@ pub struct OwnedExpr { expr: *mut SpannedExpr<'static, Id>, } +impl Default for OwnedExpr { + fn default() -> Self { + RootExpr::default().try_into_send().ok().unwrap_or_default() + } +} + impl Eq for OwnedExpr {} impl PartialEq for OwnedExpr { fn eq(&self, other: &Self) -> bool { @@ -1449,30 +1487,6 @@ impl OwnedExpr { } } -#[doc(hidden)] -#[derive(Default)] -pub struct InvariantLifetime<'a>(std::marker::PhantomData &'a ()>); - -// Copied from the compact_arena crate -#[macro_export] -macro_rules! mk_ast_arena { - ($name: ident) => { - let tag = $crate::ast::InvariantLifetime::default(); - let $name = unsafe { std::sync::Arc::new($crate::ast::Arena::new(&tag)) }; - let _guard; - // this doesn't make it to MIR, but ensures that borrowck will not - // unify the lifetimes of two macro calls by binding the lifetime to - // drop scope - if false { - struct Guard<'tag>(&'tag $crate::ast::InvariantLifetime<'tag>); - impl<'tag> ::core::ops::Drop for Guard<'tag> { - fn drop(&mut self) {} - } - _guard = Guard(&tag); - } - }; -} - pub trait AstClone<'ast, Id> { fn ast_clone(&self, arena: ArenaRef<'_, 'ast, Id>) -> Self; } diff --git a/scripts/publish.sh b/scripts/publish.sh index 60ee669a5c..6bca4bdd0e 100755 --- a/scripts/publish.sh +++ b/scripts/publish.sh @@ -4,7 +4,6 @@ VERSION=$(echo $1 | sed 's/v//') shift declare -a PROJECTS=( - gluon_codegen gluon_base gluon_parser gluon_check