Skip to content

Commit 5f0c038

Browse files
committed
Manually create goal
1 parent f706742 commit 5f0c038

File tree

2 files changed

+70
-77
lines changed

2 files changed

+70
-77
lines changed

tests/integration/panic.rs

Lines changed: 66 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
use chalk_integration::db::ChalkDatabase;
2-
use chalk_integration::query::LoweringDatabase;
31
use chalk_ir::interner::ChalkIr;
42
use chalk_ir::AssocTypeId;
5-
use chalk_ir::Goal;
63
use chalk_ir::ImplId;
7-
use chalk_ir::InEnvironment;
84
use chalk_ir::OpaqueTyId;
95
use chalk_ir::Parameter;
106
use chalk_ir::ProgramClause;
117
use chalk_ir::StructId;
128
use chalk_ir::TraitId;
139
use chalk_ir::TypeName;
14-
use chalk_ir::UCanonical;
1510
use chalk_rust_ir::AssociatedTyDatum;
1611
use chalk_rust_ir::AssociatedTyValue;
1712
use chalk_rust_ir::AssociatedTyValueId;
@@ -21,96 +16,96 @@ use chalk_rust_ir::StructDatum;
2116
use chalk_rust_ir::TraitDatum;
2217
use chalk_rust_ir::WellKnownTrait;
2318
use chalk_solve::RustIrDatabase;
24-
use chalk_solve::Solution;
2519
use chalk_solve::SolverChoice;
2620
use std::sync::Arc;
2721

2822
#[derive(Debug, Default)]
2923
struct MockDatabase {
30-
chalk_db: ChalkDatabase,
3124
panic: bool,
3225
}
3326

34-
impl MockDatabase {
35-
pub fn with(program_text: &str, solver_choice: SolverChoice) -> Self {
36-
Self {
37-
chalk_db: ChalkDatabase::with(program_text, solver_choice),
38-
panic: false,
39-
}
40-
}
41-
42-
pub fn solve(
43-
&self,
44-
goal: &UCanonical<InEnvironment<Goal<ChalkIr>>>,
45-
) -> Option<Solution<ChalkIr>> {
46-
let solver = self.chalk_db.solver();
47-
let solution = solver.lock().unwrap().solve(self, goal);
48-
solution
49-
}
50-
}
51-
27+
#[allow(unused_variables)]
5228
impl RustIrDatabase<ChalkIr> for MockDatabase {
5329
fn custom_clauses(&self) -> Vec<ProgramClause<ChalkIr>> {
5430
if self.panic {
55-
unimplemented!()
31+
panic!("test panic");
5632
} else {
57-
self.chalk_db.custom_clauses()
33+
vec![]
5834
}
5935
}
6036

6137
fn associated_ty_data(&self, ty: AssocTypeId<ChalkIr>) -> Arc<AssociatedTyDatum<ChalkIr>> {
62-
self.chalk_db.associated_ty_data(ty)
38+
unimplemented!()
6339
}
6440

6541
fn trait_datum(&self, id: TraitId<ChalkIr>) -> Arc<TraitDatum<ChalkIr>> {
66-
self.chalk_db.trait_datum(id)
42+
assert_eq!(id.0.index, 0);
43+
return Arc::new(chalk_rust_ir::TraitDatum {
44+
id,
45+
binders: chalk_ir::Binders::new(
46+
chalk_ir::ParameterKinds::new(&ChalkIr),
47+
chalk_rust_ir::TraitDatumBound {
48+
where_clauses: vec![],
49+
},
50+
),
51+
flags: chalk_rust_ir::TraitFlags {
52+
auto: false,
53+
marker: false,
54+
upstream: false,
55+
fundamental: false,
56+
non_enumerable: false,
57+
coinductive: false,
58+
},
59+
associated_ty_ids: vec![],
60+
well_known: None,
61+
});
6762
}
6863

6964
fn impl_datum(&self, id: ImplId<ChalkIr>) -> Arc<ImplDatum<ChalkIr>> {
70-
self.chalk_db.impl_datum(id)
65+
unimplemented!()
7166
}
7267

7368
fn associated_ty_value(
7469
&self,
7570
id: AssociatedTyValueId<ChalkIr>,
7671
) -> Arc<AssociatedTyValue<ChalkIr>> {
77-
self.chalk_db.associated_ty_value(id)
72+
unimplemented!()
7873
}
7974

8075
fn opaque_ty_data(&self, id: OpaqueTyId<ChalkIr>) -> Arc<OpaqueTyDatum<ChalkIr>> {
81-
self.chalk_db.opaque_ty_data(id)
76+
unimplemented!()
8277
}
8378

8479
fn struct_datum(&self, id: StructId<ChalkIr>) -> Arc<StructDatum<ChalkIr>> {
85-
self.chalk_db.struct_datum(id)
80+
unimplemented!()
8681
}
8782

8883
fn as_struct_id(&self, type_name: &TypeName<ChalkIr>) -> Option<StructId<ChalkIr>> {
89-
self.chalk_db.as_struct_id(type_name)
84+
unimplemented!()
9085
}
9186

9287
fn impls_for_trait(
9388
&self,
9489
trait_id: TraitId<ChalkIr>,
9590
parameters: &[Parameter<ChalkIr>],
9691
) -> Vec<ImplId<ChalkIr>> {
97-
self.chalk_db.impls_for_trait(trait_id, parameters)
92+
unimplemented!()
9893
}
9994

10095
fn local_impls_to_coherence_check(&self, trait_id: TraitId<ChalkIr>) -> Vec<ImplId<ChalkIr>> {
101-
self.chalk_db.local_impls_to_coherence_check(trait_id)
96+
unimplemented!()
10297
}
10398

10499
fn impl_provided_for(
105100
&self,
106101
auto_trait_id: TraitId<ChalkIr>,
107102
struct_id: StructId<ChalkIr>,
108103
) -> bool {
109-
self.chalk_db.impl_provided_for(auto_trait_id, struct_id)
104+
unimplemented!()
110105
}
111106

112107
fn well_known_trait_id(&self, well_known_trait: WellKnownTrait) -> Option<TraitId<ChalkIr>> {
113-
self.chalk_db.well_known_trait_id(well_known_trait)
108+
unimplemented!()
114109
}
115110

116111
fn interner(&self) -> &ChalkIr {
@@ -121,12 +116,11 @@ impl RustIrDatabase<ChalkIr> for MockDatabase {
121116
#[test]
122117
fn unwind_safety() {
123118
use self::MockDatabase;
124-
use chalk_integration::lowering::LowerGoal;
125-
use chalk_integration::query::LoweringDatabase;
126-
use chalk_solve::ext::GoalExt;
119+
use chalk_ir::*;
127120
use std::panic;
128121

129122
// lower program
123+
/*
130124
let mut db = lower_program_with_db! {
131125
program {
132126
struct Foo { }
@@ -137,24 +131,43 @@ fn unwind_safety() {
137131
};
138132
139133
let program = db.chalk_db.checked_program().unwrap();
140-
141-
// lower goal
142-
let goal = lower_goal! {
143-
goal {
144-
Foo: Bar
145-
}
146-
program &*program
134+
*/
135+
let mut db = MockDatabase { panic: false };
136+
137+
let peeled_goal: UCanonical<InEnvironment<Goal<ChalkIr>>> = UCanonical {
138+
canonical: Canonical {
139+
binders: CanonicalVarKinds::new(&ChalkIr),
140+
value: InEnvironment {
141+
environment: Environment::new(&ChalkIr),
142+
goal: GoalData::DomainGoal(DomainGoal::Holds(WhereClause::Implemented(TraitRef {
143+
trait_id: TraitId(interner::RawId { index: 0 }),
144+
substitution: Substitution::from1(
145+
&ChalkIr,
146+
ParameterKind::Ty(
147+
TyData::Apply(ApplicationTy {
148+
name: TypeName::Struct(StructId(interner::RawId { index: 1 })),
149+
substitution: Substitution::empty(&ChalkIr),
150+
})
151+
.intern(&ChalkIr),
152+
)
153+
.intern(&ChalkIr),
154+
),
155+
})))
156+
.intern(&ChalkIr),
157+
},
158+
},
159+
universes: 1,
147160
};
148-
let peeled_goal = goal.into_peeled_goal(db.interner());
149161

162+
let mut solver = SolverChoice::slg_default().into_solver();
150163
// solve goal but this will panic
151164
db.panic = true;
152-
let result = panic::catch_unwind(|| {
153-
db.solve(&peeled_goal);
154-
});
165+
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
166+
solver.solve(&db, &peeled_goal);
167+
}));
155168
assert!(result.is_err() == true);
156169

157170
// solve again but without panicking this time
158171
db.panic = false;
159-
assert!(db.solve(&peeled_goal).is_some());
172+
assert!(solver.solve(&db, &peeled_goal).is_some());
160173
}

tests/test_util.rs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,15 @@
11
#![allow(unused_macros)]
22

3-
macro_rules! lower_program_with_db {
4-
(program $program:tt database $database:ty) => {{
3+
macro_rules! lowering_success {
4+
(program $program:tt) => {
55
let program_text = stringify!($program);
66
assert!(program_text.starts_with("{"));
77
assert!(program_text.ends_with("}"));
8-
<$database>::with(
8+
let result = chalk_integration::db::ChalkDatabase::with(
99
&program_text[1..program_text.len() - 1],
1010
chalk_solve::SolverChoice::default(),
1111
)
12-
}};
13-
}
14-
15-
macro_rules! lower_goal {
16-
(goal $goal:tt program $program:expr) => {{
17-
let goal_text = stringify!($goal);
18-
assert!(goal_text.starts_with("{"));
19-
assert!(goal_text.ends_with("}"));
20-
chalk_parse::parse_goal(&goal_text[1..goal_text.len() - 1])
21-
.unwrap()
22-
.lower($program)
23-
.unwrap()
24-
}};
25-
}
26-
27-
macro_rules! lowering_success {
28-
(program $program:tt) => {
29-
let result = lower_program_with_db!(
30-
program $program
31-
database chalk_integration::db::ChalkDatabase
32-
).checked_program();
12+
.checked_program();
3313
if let Err(ref e) = result {
3414
println!("lowering error: {}", e);
3515
}

0 commit comments

Comments
 (0)