Skip to content

Commit

Permalink
Merge pull request #581 from nathanwhit/salsa
Browse files Browse the repository at this point in the history
Upgrade salsa
  • Loading branch information
nikomatsakis committed Jul 31, 2020
2 parents d1ec6d8 + e4dfafb commit 248b464
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 35 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -17,7 +17,7 @@ bench = []
docopt = "1.1.0"
itertools = "0.9.0"
rustyline = "6.2.0"
salsa = "0.14.0"
salsa = "0.15.0"
serde = "1.0"
serde_derive = "1.0"

Expand Down
2 changes: 1 addition & 1 deletion chalk-integration/Cargo.toml
Expand Up @@ -11,7 +11,7 @@ publish = false

[dependencies]
string_cache = "0.8.0"
salsa = "0.14.0"
salsa = "0.15.0"
tracing = "0.1"

chalk-derive = { version = "0.20.0-dev.0", path = "../chalk-derive" }
Expand Down
20 changes: 10 additions & 10 deletions chalk-integration/src/db.rs
Expand Up @@ -17,22 +17,16 @@ use chalk_solve::rust_ir::{
};
use chalk_solve::{RustIrDatabase, Solution, Solver, SubstitutionResult};
use salsa::Database;
use std::fmt;
use std::sync::Arc;

#[salsa::database(Lowering)]
#[derive(Debug, Default)]
#[derive(Default)]
pub struct ChalkDatabase {
runtime: salsa::Runtime<ChalkDatabase>,
storage: salsa::Storage<Self>,
}

impl Database for ChalkDatabase {
fn salsa_runtime(&self) -> &salsa::Runtime<ChalkDatabase> {
&self.runtime
}
fn salsa_runtime_mut(&mut self) -> &mut salsa::Runtime<ChalkDatabase> {
&mut self.runtime
}
}
impl Database for ChalkDatabase {}

impl ChalkDatabase {
pub fn with(program_text: &str, solver_choice: SolverChoice) -> Self {
Expand Down Expand Up @@ -222,3 +216,9 @@ impl RustIrDatabase<ChalkIr> for ChalkDatabase {
self.program_ir().unwrap().fn_def_name(fn_def_id)
}
}

impl fmt::Debug for ChalkDatabase {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ChalkDatabase {{ }}")
}
}
49 changes: 38 additions & 11 deletions chalk-integration/src/query.rs
Expand Up @@ -25,7 +25,9 @@ use std::sync::Arc;
use std::sync::Mutex;

#[salsa::query_group(Lowering)]
pub trait LoweringDatabase: RustIrDatabase<ChalkIr> + Database {
pub trait LoweringDatabase:
RustIrDatabase<ChalkIr> + Database + Upcast<dyn RustIrDatabase<ChalkIr>>
{
#[salsa::input]
fn program_text(&self) -> Arc<String>;

Expand Down Expand Up @@ -58,6 +60,31 @@ pub trait LoweringDatabase: RustIrDatabase<ChalkIr> + Database {
fn solver(&self) -> ArcEq<Mutex<SolverImpl>>;
}

// Needed to go from dyn LoweringDatabase -> dyn RustIrDatabase
// These traits are basically vendored (slightly modified) from https://github.com/connicpu/upcast
pub trait Upcast<U: ?Sized> {
fn upcast(&self) -> &U;
}

pub trait UpcastFrom<T: ?Sized> {
fn upcast_from(val: &T) -> &Self;
}

impl<'a, T: RustIrDatabase<ChalkIr> + 'a> UpcastFrom<T> for dyn RustIrDatabase<ChalkIr> + 'a {
fn upcast_from(val: &T) -> &(dyn RustIrDatabase<ChalkIr> + 'a) {
val
}
}

impl<T: ?Sized, U: ?Sized> Upcast<U> for T
where
U: UpcastFrom<T>,
{
fn upcast(&self) -> &U {
U::upcast_from(self)
}
}

#[derive(Debug)]
#[repr(transparent)]
pub struct ArcEq<T>(Arc<T>);
Expand Down Expand Up @@ -95,19 +122,19 @@ impl<T> Clone for ArcEq<T> {
}
}

fn program_ir(db: &impl LoweringDatabase) -> Result<Arc<Program>, ChalkError> {
fn program_ir(db: &dyn LoweringDatabase) -> Result<Arc<Program>, ChalkError> {
let text = db.program_text();
Ok(Arc::new(chalk_parse::parse_program(&text)?.lower()?))
}

fn orphan_check(db: &impl LoweringDatabase) -> Result<(), ChalkError> {
fn orphan_check(db: &dyn LoweringDatabase) -> Result<(), ChalkError> {
let program = db.program_ir()?;

tls::set_current_program(&program, || -> Result<(), ChalkError> {
let local_impls = program.local_impl_ids();
for impl_id in local_impls {
orphan::perform_orphan_check::<ChalkIr, SolverImpl, SolverChoice>(
db,
db.upcast(),
db.solver_choice(),
impl_id,
)?;
Expand All @@ -117,7 +144,7 @@ fn orphan_check(db: &impl LoweringDatabase) -> Result<(), ChalkError> {
}

fn coherence(
db: &impl LoweringDatabase,
db: &dyn LoweringDatabase,
) -> Result<BTreeMap<TraitId<ChalkIr>, Arc<SpecializationPriorities<ChalkIr>>>, ChalkError> {
let program = db.program_ir()?;

Expand All @@ -127,7 +154,7 @@ fn coherence(
.keys()
.map(|&trait_id| {
let solver: CoherenceSolver<ChalkIr, SolverImpl, SolverChoice> =
CoherenceSolver::new(db, db.solver_choice(), trait_id);
CoherenceSolver::new(db.upcast(), db.solver_choice(), trait_id);
let priorities = solver.specialization_priorities()?;
Ok((trait_id, priorities))
})
Expand All @@ -139,14 +166,14 @@ fn coherence(
priorities_map
}

fn checked_program(db: &impl LoweringDatabase) -> Result<Arc<Program>, ChalkError> {
fn checked_program(db: &dyn LoweringDatabase) -> Result<Arc<Program>, ChalkError> {
let program = db.program_ir()?;

db.coherence()?;

let () = tls::set_current_program(&program, || -> Result<(), ChalkError> {
let solver: wf::WfSolver<ChalkIr, SolverImpl, SolverChoice> =
wf::WfSolver::new(db, db.solver_choice());
wf::WfSolver::new(db.upcast(), db.solver_choice());
for &id in program.adt_data.keys() {
solver.verify_adt_decl(id)?;
}
Expand All @@ -161,7 +188,7 @@ fn checked_program(db: &impl LoweringDatabase) -> Result<Arc<Program>, ChalkErro
Ok(program)
}

fn environment(db: &impl LoweringDatabase) -> Result<Arc<ProgramEnvironment>, ChalkError> {
fn environment(db: &dyn LoweringDatabase) -> Result<Arc<ProgramEnvironment>, ChalkError> {
let program = db.program_ir()?;

// Construct the set of *clauses*; these are sort of a compiled form
Expand All @@ -170,7 +197,7 @@ fn environment(db: &impl LoweringDatabase) -> Result<Arc<ProgramEnvironment>, Ch
// forall P0...Pn. Something :- Conditions
let mut program_clauses = program.custom_clauses.clone();

let builder = &mut ClauseBuilder::new(db, &mut program_clauses);
let builder = &mut ClauseBuilder::new(db.upcast(), &mut program_clauses);

let env = chalk_ir::Environment::new(builder.interner());

Expand Down Expand Up @@ -215,7 +242,7 @@ fn environment(db: &impl LoweringDatabase) -> Result<Arc<ProgramEnvironment>, Ch
Ok(Arc::new(ProgramEnvironment::new(program_clauses)))
}

fn solver(db: &impl LoweringDatabase) -> ArcEq<Mutex<SolverImpl>> {
fn solver(db: &dyn LoweringDatabase) -> ArcEq<Mutex<SolverImpl>> {
db.salsa_runtime().report_untracked_read();
let choice = db.solver_choice();
ArcEq::new(Mutex::new(choice.into()))
Expand Down

0 comments on commit 248b464

Please sign in to comment.