Skip to content

Commit 16327a3

Browse files
authored
Merge pull request #23 from withoutboats/program-environment
Separate Program from ProgramEnvironment.
2 parents 2309110 + 8c70c67 commit 16327a3

File tree

6 files changed

+61
-33
lines changed

6 files changed

+61
-33
lines changed

chalk-rust/src/ir/mod.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ pub struct Program {
1212
/// For each struct/trait:
1313
pub type_kinds: HashMap<ItemId, TypeKind>,
1414

15+
/// For each struct:
16+
pub struct_data: HashMap<ItemId, StructDatum>,
17+
1518
/// For each impl:
1619
pub impl_data: HashMap<ItemId, ImplDatum>,
1720

@@ -20,12 +23,34 @@ pub struct Program {
2023

2124
/// For each trait:
2225
pub associated_ty_data: HashMap<ItemId, AssociatedTyDatum>,
26+
}
27+
28+
impl Program {
29+
pub fn split_projection<'p>(&self, projection: &'p ProjectionTy)
30+
-> (&AssociatedTyDatum, &'p [Parameter], &'p [Parameter]) {
31+
let ProjectionTy { associated_ty_id, ref parameters } = *projection;
32+
let associated_ty_data = &self.associated_ty_data[&associated_ty_id];
33+
let trait_datum = &self.trait_data[&associated_ty_data.trait_id];
34+
let trait_num_params = trait_datum.binders.len();
35+
let split_point = parameters.len() - trait_num_params;
36+
let (other_params, trait_params) = parameters.split_at(split_point);
37+
(associated_ty_data, trait_params, other_params)
38+
}
39+
}
40+
41+
#[derive(Clone, Debug, PartialEq, Eq)]
42+
pub struct ProgramEnvironment {
43+
/// For each trait:
44+
pub trait_data: HashMap<ItemId, TraitDatum>,
45+
46+
/// For each trait:
47+
pub associated_ty_data: HashMap<ItemId, AssociatedTyDatum>,
2348

2449
/// Compiled forms of the above:
2550
pub program_clauses: Vec<ProgramClause>,
2651
}
2752

28-
impl Program {
53+
impl ProgramEnvironment {
2954
pub fn split_projection<'p>(&self, projection: &'p ProjectionTy)
3055
-> (&AssociatedTyDatum, &'p [Parameter], &'p [Parameter]) {
3156
let ProjectionTy { associated_ty_id, ref parameters } = *projection;

chalk-rust/src/lower/mod.rs

+29-27
Original file line numberDiff line numberDiff line change
@@ -225,33 +225,7 @@ impl LowerProgram for Program {
225225
}
226226
}
227227

228-
// Construct the set of *clauses*; these are sort of a compiled form
229-
// of the data above that always has the form:
230-
//
231-
// forall P0...Pn. Something :- Conditions
232-
let mut program_clauses = vec![];
233-
234-
for struct_datum in struct_data.values() {
235-
program_clauses.extend(struct_datum.to_program_clauses());
236-
}
237-
238-
for trait_datum in trait_data.values() {
239-
program_clauses.extend(trait_datum.to_program_clauses());
240-
}
241-
242-
for (&id, associated_ty_datum) in &associated_ty_data {
243-
program_clauses.extend(associated_ty_datum.to_program_clauses(id));
244-
}
245-
246-
for impl_datum in impl_data.values() {
247-
program_clauses.push(impl_datum.to_program_clause());
248-
249-
for atv in &impl_datum.binders.value.associated_ty_values {
250-
program_clauses.extend(atv.to_program_clauses(impl_datum));
251-
}
252-
}
253-
254-
Ok(ir::Program { type_ids, type_kinds, trait_data, impl_data, associated_ty_data, program_clauses })
228+
Ok(ir::Program { type_ids, type_kinds, struct_data, trait_data, impl_data, associated_ty_data, })
255229
}
256230
}
257231

@@ -882,6 +856,34 @@ impl LowerQuantifiedGoal for Goal {
882856
}
883857
}
884858

859+
impl ir::Program {
860+
pub fn environment(&self) -> ir::ProgramEnvironment {
861+
// Construct the set of *clauses*; these are sort of a compiled form
862+
// of the data above that always has the form:
863+
//
864+
// forall P0...Pn. Something :- Conditions
865+
let mut program_clauses = vec![];
866+
867+
program_clauses.extend(self.struct_data.values().flat_map(|d| d.to_program_clauses()));
868+
program_clauses.extend(self.trait_data.values().flat_map(|d| d.to_program_clauses()));
869+
program_clauses.extend(self.associated_ty_data.iter().flat_map(|(&id, d)| {
870+
d.to_program_clauses(id)
871+
}));
872+
873+
for datum in self.impl_data.values() {
874+
program_clauses.push(datum.to_program_clause());
875+
program_clauses.extend(datum.binders.value.associated_ty_values.iter().flat_map(|atv| {
876+
atv.to_program_clauses(datum)
877+
}));
878+
}
879+
880+
let trait_data = self.trait_data.clone();
881+
let associated_ty_data = self.associated_ty_data.clone();
882+
883+
ir::ProgramEnvironment { trait_data, associated_ty_data, program_clauses }
884+
}
885+
}
886+
885887
impl ir::ImplDatum {
886888
/// Given `impl<T: Clone> Clone for Vec<T>`, generate:
887889
///

chalk-rust/src/solve/environment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl Environment {
2929
Arc::new(env)
3030
}
3131

32-
pub fn elaborated_clauses(&self, program: &Program) -> impl Iterator<Item = WhereClause> {
32+
pub fn elaborated_clauses(&self, program: &ProgramEnvironment) -> impl Iterator<Item = WhereClause> {
3333
let mut set = HashSet::new();
3434
set.extend(self.clauses.iter().cloned());
3535

chalk-rust/src/solve/fulfill.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl<'s> Fulfill<'s> {
2323
Fulfill { solver, infer, obligations: vec![], constraints: HashSet::new() }
2424
}
2525

26-
pub fn program(&self) -> Arc<Program> {
26+
pub fn program(&self) -> Arc<ProgramEnvironment> {
2727
self.solver.program.clone()
2828
}
2929

chalk-rust/src/solve/solver/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ use std::sync::Arc;
1313
use super::*;
1414

1515
pub struct Solver {
16-
pub(super) program: Arc<Program>,
16+
pub(super) program: Arc<ProgramEnvironment>,
1717
overflow_depth: usize,
1818
stack: Vec<Query<InEnvironment<WhereClauseGoal>>>,
1919
}
2020

2121
impl Solver {
22-
pub fn new(program: &Arc<Program>, overflow_depth: usize) -> Self {
22+
pub fn new(program: &Arc<ProgramEnvironment>, overflow_depth: usize) -> Self {
2323
Solver { program: program.clone(), stack: vec![], overflow_depth, }
2424
}
2525

chalk-rust/src/solve/test.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ fn solve_goal(program_text: &str,
2727
assert!(program_text.starts_with("{"));
2828
assert!(program_text.ends_with("}"));
2929
let program = Arc::new(parse_and_lower_program(&program_text[1..program_text.len()-1]).unwrap());
30+
let env = Arc::new(program.environment());
3031
ir::set_current_program(&program, || {
3132
for (goal_text, expected) in goals {
3233
println!("----------------------------------------------------------------------");
@@ -39,7 +40,7 @@ fn solve_goal(program_text: &str,
3940
// tests don't require a higher one.
4041
let overflow_depth = 3;
4142

42-
let mut solver = Solver::new(&program, overflow_depth);
43+
let mut solver = Solver::new(&env, overflow_depth);
4344
let result = match Prove::new(&mut solver, goal).solve() {
4445
Ok(v) => format!("{:#?}", v),
4546
Err(e) => format!("{}", e),

0 commit comments

Comments
 (0)