Skip to content

Commit

Permalink
Disable the inliner completely
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Aug 15, 2020
1 parent 97b63c4 commit 908dd24
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 119 deletions.
2 changes: 2 additions & 0 deletions codegen/src/functor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern crate proc_macro;

use proc_macro2::{Ident, Span, TokenStream};
use syn::{self, Data, DeriveInput, Generics};

Expand Down
1 change: 0 additions & 1 deletion repl/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ async fn eval_line_(vm: RootedThread, line: &str) -> gluon::Result<()> {
let mut eval_expr;
let value = {
let mut db = vm.get_database();
let mut db = gluon::salsa::OwnedDb::<dyn gluon::query::Compilation>::from(&mut db);
let mut module_compiler = vm.module_compiler(&mut db);
eval_expr = {
let eval_expr = {
Expand Down
114 changes: 39 additions & 75 deletions src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,40 +104,6 @@ impl Importer for DefaultImporter {
}
}

pub struct DatabaseSnapshot {
snapshot: Option<salsa::Snapshot<CompilerDatabase>>,
}

impl Deref for DatabaseSnapshot {
type Target = CompilerDatabase;
fn deref(&self) -> &Self::Target {
self.snapshot.as_ref().unwrap()
}
}

impl<'a> From<&'a mut DatabaseSnapshot> for salsa::OwnedDb<'a, dyn Compilation> {
fn from(db: &'a mut DatabaseSnapshot) -> Self {
salsa::cast_owned_db!(salsa::OwnedDb::<CompilerDatabase>::from(db.snapshot.as_mut().unwrap()) => &mut dyn Compilation)
}
}

pub struct DatabaseFork {
fork: Option<salsa::Snapshot<CompilerDatabase>>,
}

impl Deref for DatabaseFork {
type Target = CompilerDatabase;
fn deref(&self) -> &Self::Target {
self.fork.as_ref().unwrap()
}
}

impl<'a> From<&'a mut DatabaseFork> for salsa::OwnedDb<'a, dyn Compilation> {
fn from(db: &'a mut DatabaseFork) -> Self {
salsa::cast_owned_db!(salsa::OwnedDb::<CompilerDatabase>::from(db.fork.as_mut().unwrap()) => &mut dyn Compilation)
}
}

pub struct DatabaseMut {
// Only needed to ensure that the the `Compiler` the guard points to lives long enough
_import: Arc<dyn Macro>,
Expand Down Expand Up @@ -181,8 +147,12 @@ pub(crate) trait ImportApi: Send + Sync {
vm: &Thread,
module_id: &Symbol,
) -> SalvageResult<ArcType>;
fn snapshot(&self, thread: RootedThread) -> DatabaseSnapshot;
fn fork(&mut self, forker: salsa::ForkState, thread: RootedThread) -> DatabaseFork;
fn snapshot(&self, thread: RootedThread) -> salsa::Snapshot<CompilerDatabase>;
fn fork(
&mut self,
forker: salsa::ForkState,
thread: RootedThread,
) -> salsa::Snapshot<CompilerDatabase>;
}

#[async_trait]
Expand All @@ -209,10 +179,14 @@ where

self.importer.import(compiler, vm, &modulename).await
}
fn snapshot(&self, thread: RootedThread) -> DatabaseSnapshot {
fn snapshot(&self, thread: RootedThread) -> salsa::Snapshot<CompilerDatabase> {
Self::snapshot(self, thread)
}
fn fork(&mut self, forker: salsa::ForkState, thread: RootedThread) -> DatabaseFork {
fn fork(
&mut self,
forker: salsa::ForkState,
thread: RootedThread,
) -> salsa::Snapshot<CompilerDatabase> {
Self::fork(self, forker, thread)
}
}
Expand Down Expand Up @@ -309,18 +283,16 @@ impl<I> Import<I> {
compiler
}

pub fn snapshot(&self, thread: RootedThread) -> DatabaseSnapshot {
let snapshot = self.compiler.lock().unwrap().snapshot(thread);

DatabaseSnapshot {
snapshot: Some(snapshot),
}
pub fn snapshot(&self, thread: RootedThread) -> salsa::Snapshot<CompilerDatabase> {
self.compiler.lock().unwrap().snapshot(thread)
}

pub fn fork(&self, forker: salsa::ForkState, thread: RootedThread) -> DatabaseFork {
let fork = self.compiler.lock().unwrap().fork(forker, thread);

DatabaseFork { fork: Some(fork) }
pub fn fork(
&mut self,
forker: salsa::ForkState,
thread: RootedThread,
) -> salsa::Snapshot<CompilerDatabase> {
self.compiler.lock().unwrap().fork(forker, thread)
}

pub(crate) fn get_module_source(
Expand Down Expand Up @@ -489,7 +461,7 @@ where
Some(Box::new(
arc_self.clone().downcast_arc::<Self>().ok().unwrap() as Arc<dyn ImportApi>,
))
} else if id == TypeId::of::<DatabaseSnapshot>() {
} else if id == TypeId::of::<salsa::Snapshot<CompilerDatabase>>() {
Some(Box::new(self.snapshot(thread.root_thread())))
} else if id == TypeId::of::<DatabaseMut>() {
Some(Box::new(
Expand Down Expand Up @@ -541,14 +513,13 @@ where

info!("import! {}", modulename);

let db = try_future!(macros
let mut db = try_future!(macros
.userdata
.fork(macros.vm.root_thread())
.downcast::<salsa::Snapshot<CompilerDatabase>>()
.map_err(|_| MacroError::new(Error::String(
"`import` requires a `CompilerDatabase` as user data during macro expansion".into(),
))));
let mut db = DatabaseFork { fork: Some(*db) };

let span = args[0].span;

Expand All @@ -559,23 +530,18 @@ where
let (tx, rx) = tokio::sync::oneshot::channel();
spawn
.spawn(Box::pin(async move {
let result = {
let mut db = salsa::OwnedDb::<dyn Compilation>::from(&mut db);
std::panic::AssertUnwindSafe(db.import(modulename))
.catch_unwind()
.await
.map(|r| r.map_err(|err| MacroError::message(err.to_string())))
.unwrap_or_else(|err| {
Err(MacroError::message(
err.downcast::<String>()
.map(|s| *s)
.or_else(|e| {
e.downcast::<&str>().map(|s| String::from(&s[..]))
})
.unwrap_or_else(|_| "Unknown panic".to_string()),
))
})
};
let result = std::panic::AssertUnwindSafe(db.import(modulename))
.catch_unwind()
.await
.map(|r| r.map_err(|err| MacroError::message(err.to_string())))
.unwrap_or_else(|err| {
Err(MacroError::message(
err.downcast::<String>()
.map(|s| *s)
.or_else(|e| e.downcast::<&str>().map(|s| String::from(&s[..])))
.unwrap_or_else(|_| "Unknown panic".to_string()),
))
});
// Drop the database before sending the result, otherwise the forker may drop before the forked database
drop(db);
let _ = tx.send(result);
Expand All @@ -595,13 +561,11 @@ where
Box::pin(async move {
Ok(From::from(move || {
async move {
let result = {
let mut db = salsa::OwnedDb::<dyn Compilation>::from(&mut db);
db.import(modulename)
.await
.map_err(|err| MacroError::message(err.to_string()))
.map(move |id| pos::spanned(span, Expr::Ident(id)))
};
let result = db
.import(modulename)
.await
.map_err(|err| MacroError::message(err.to_string()))
.map(move |id| pos::spanned(span, Expr::Ident(id)));
drop(db);
result
}
Expand Down
41 changes: 29 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ use crate::vm::{
use crate::{
compiler_pipeline::*,
import::{add_extern_module, add_extern_module_with_deps, DefaultImporter, Import},
query::{AsyncCompilation, Compilation, CompilationBase},
query::{AsyncCompilation, Compilation, CompilationBase, CompilerDatabase},
};

quick_error! {
Expand Down Expand Up @@ -305,15 +305,38 @@ impl Default for Settings {
}
}

#[doc(hidden)]
pub trait IntoDb<'a, 'b> {
fn into_db(self) -> salsa::OwnedDb<'a, dyn Compilation + 'b>;
}

impl<'a, 'b> IntoDb<'a, 'b> for &'a mut salsa::OwnedDb<'_, dyn Compilation + 'b> {
fn into_db(self) -> salsa::OwnedDb<'a, dyn Compilation + 'b> {
self.into()
}
}

impl<'a, 'b> IntoDb<'a, 'b> for salsa::OwnedDb<'a, dyn Compilation + 'b> {
fn into_db(self) -> salsa::OwnedDb<'a, dyn Compilation + 'b> {
self
}
}

impl<'a, 'b> IntoDb<'a, 'b> for &'a mut salsa::Snapshot<CompilerDatabase> {
fn into_db(self) -> salsa::OwnedDb<'a, dyn Compilation + 'b> {
salsa::cast_owned_db!(salsa::OwnedDb::<CompilerDatabase>::from(self) => &mut dyn Compilation)
}
}

pub struct ModuleCompiler<'a, 'b> {
pub database: salsa::OwnedDb<'a, dyn Compilation + 'b>,
symbols: Symbols,
}

impl<'a, 'b> ModuleCompiler<'a, 'b> {
fn new(database: impl Into<salsa::OwnedDb<'a, dyn Compilation + 'b>>) -> Self {
fn new(database: impl IntoDb<'a, 'b>) -> Self {
Self {
database: database.into(),
database: database.into_db(),
symbols: Symbols::default(),
}
}
Expand Down Expand Up @@ -405,7 +428,7 @@ impl import::DatabaseMut {

#[async_trait::async_trait]
pub trait ThreadExt: Send + Sync {
fn get_database(&self) -> import::DatabaseSnapshot;
fn get_database(&self) -> salsa::Snapshot<CompilerDatabase>;
fn get_database_mut(&self) -> import::DatabaseMut;

fn run_io(&self, run: bool) {
Expand All @@ -415,10 +438,7 @@ pub trait ThreadExt: Send + Sync {
#[doc(hidden)]
fn thread(&self) -> &Thread;

fn module_compiler<'a, 'b>(
&'a self,
database: impl Into<salsa::OwnedDb<'a, dyn Compilation + 'b>>,
) -> ModuleCompiler<'a, 'b> {
fn module_compiler<'a, 'b>(&'a self, database: impl IntoDb<'a, 'b>) -> ModuleCompiler<'a, 'b> {
ModuleCompiler::new(database)
}

Expand Down Expand Up @@ -490,7 +510,6 @@ pub trait ThreadExt: Send + Sync {
db.add_module(file.into(), expr_str.into());
}
let mut db = vm.get_database();
let mut db = salsa::OwnedDb::<dyn Compilation>::from(&mut db);

let TypecheckValue { expr, typ, .. } = db
.typechecked_source_module(file.into(), expected_type.cloned())
Expand Down Expand Up @@ -591,7 +610,6 @@ pub trait ThreadExt: Send + Sync {
}

let mut db = vm.get_database();
let mut db = salsa::OwnedDb::<dyn Compilation>::from(&mut db);

let TypecheckValue {
expr,
Expand Down Expand Up @@ -632,7 +650,6 @@ pub trait ThreadExt: Send + Sync {
db.add_module(module_name.clone(), input.into());
}
let mut db = vm.get_database();
let mut db = salsa::OwnedDb::<dyn Compilation>::from(&mut db);

db.import(module_name).await.map(|_| ())
}
Expand Down Expand Up @@ -793,7 +810,7 @@ fn skip_implicit_prelude<'a, 'ast>(
}

impl ThreadExt for Thread {
fn get_database(&self) -> import::DatabaseSnapshot {
fn get_database(&self) -> salsa::Snapshot<CompilerDatabase> {
self.global_env()
.get_capability(self)
.expect("Database is missing")
Expand Down
2 changes: 1 addition & 1 deletion src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use {

use crate::{compiler_pipeline::*, import::PtrEq, Error, ModuleCompiler, Result, Settings};

pub use {crate::import::DatabaseSnapshot, salsa};
pub use salsa;

#[derive(Debug, Trace)]
#[gluon(crate_name = "gluon_vm")]
Expand Down
2 changes: 1 addition & 1 deletion std/cmp.glu
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let (/=) ?eq l r : [Eq a] -> a -> a -> Bool = if (eq.(==) l r) then False else T
type Ord a = {
eq : Eq a,
/// Compares two values and returns wheter the first is less than, equal or greater than the second.
compare : a -> a -> Ordering,
compare : a -> a -> Ordering
}

let compare ?ord : [Ord a] -> a -> a -> Ordering = ord.compare
Expand Down
Loading

0 comments on commit 908dd24

Please sign in to comment.