Skip to content

Commit

Permalink
feat: Add Default for Root/OwnedExpr
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes authored and Markus Westerlind committed Jun 20, 2020
1 parent b605ea8 commit fedb732
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
62 changes: 38 additions & 24 deletions base/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1287,13 +1287,45 @@ impl_ast_arena! {
Metadata => metadata,
}

#[doc(hidden)]
#[derive(Default)]
pub struct InvariantLifetime<'a>(std::marker::PhantomData<fn(&'a ()) -> &'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<Id: 'static> {
// Only used to keep `expr` alive
#[allow(dead_code)]
arena: Arc<Arena<'static, Id>>,
expr: *mut SpannedExpr<'static, Id>,
}

impl<Id: 'static> Default for RootExpr<Id> {
fn default() -> Self {
mk_ast_arena!(arena);
let expr = arena.alloc(SpannedExpr::default());
RootExpr::new(arena.clone(), expr)
}
}

impl<Id: Eq> Eq for RootExpr<Id> {}
impl<Id: PartialEq> PartialEq for RootExpr<Id> {
fn eq(&self, other: &Self) -> bool {
Expand Down Expand Up @@ -1393,6 +1425,12 @@ pub struct OwnedExpr<Id: 'static> {
expr: *mut SpannedExpr<'static, Id>,
}

impl<Id: 'static> Default for OwnedExpr<Id> {
fn default() -> Self {
RootExpr::default().try_into_send().ok().unwrap_or_default()
}
}

impl<Id: Eq> Eq for OwnedExpr<Id> {}
impl<Id: PartialEq> PartialEq for OwnedExpr<Id> {
fn eq(&self, other: &Self) -> bool {
Expand Down Expand Up @@ -1449,30 +1487,6 @@ impl<Id> OwnedExpr<Id> {
}
}

#[doc(hidden)]
#[derive(Default)]
pub struct InvariantLifetime<'a>(std::marker::PhantomData<fn(&'a ()) -> &'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;
}
Expand Down
1 change: 0 additions & 1 deletion scripts/publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ VERSION=$(echo $1 | sed 's/v//')
shift

declare -a PROJECTS=(
gluon_codegen
gluon_base
gluon_parser
gluon_check
Expand Down

0 comments on commit fedb732

Please sign in to comment.