Skip to content

Commit dcd3538

Browse files
committed
Upgrade Chalk
1 parent d1d91df commit dcd3538

File tree

4 files changed

+87
-73
lines changed

4 files changed

+87
-73
lines changed

Cargo.lock

Lines changed: 28 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_hir_ty/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ ra_prof = { path = "../ra_prof" }
2121
ra_syntax = { path = "../ra_syntax" }
2222
test_utils = { path = "../test_utils" }
2323

24-
chalk-solve = { git = "https://github.com/rust-lang/chalk.git", rev = "ff65b5ac9860f3c36bd892c865ab23d5ff0bbae5" }
25-
chalk-rust-ir = { git = "https://github.com/rust-lang/chalk.git", rev = "ff65b5ac9860f3c36bd892c865ab23d5ff0bbae5" }
26-
chalk-ir = { git = "https://github.com/rust-lang/chalk.git", rev = "ff65b5ac9860f3c36bd892c865ab23d5ff0bbae5" }
24+
chalk-solve = { git = "https://github.com/rust-lang/chalk.git" }
25+
chalk-rust-ir = { git = "https://github.com/rust-lang/chalk.git" }
26+
chalk-ir = { git = "https://github.com/rust-lang/chalk.git" }
2727

2828
lalrpop-intern = "0.15.1"
2929

crates/ra_hir_ty/src/traits.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,19 @@ impl TraitSolver {
5050
Err(_) => ra_db::Canceled::throw(),
5151
};
5252

53+
let fuel = std::cell::Cell::new(CHALK_SOLVER_FUEL);
54+
5355
let solution = panic::catch_unwind({
5456
let solver = panic::AssertUnwindSafe(&mut solver);
5557
let context = panic::AssertUnwindSafe(&context);
56-
move || solver.0.solve(context.0, goal)
58+
move || {
59+
solver.0.solve_limited(context.0, goal, || {
60+
context.0.db.check_canceled();
61+
let remaining = fuel.get();
62+
fuel.set(remaining - 1);
63+
remaining > 0
64+
})
65+
}
5766
});
5867

5968
let solution = match solution {
@@ -79,6 +88,9 @@ impl TraitSolver {
7988
/// high, we can run into slow edge cases; if we set it too low, Chalk won't
8089
/// find some solutions.
8190
const CHALK_SOLVER_MAX_SIZE: usize = 4;
91+
/// This controls how much 'time' we give the Chalk solver before giving up.
92+
const CHALK_SOLVER_FUEL: i32 = 100;
93+
// TODO: tune both these values
8294

8395
#[derive(Debug, Copy, Clone)]
8496
struct ChalkContext<'a, DB> {
@@ -97,7 +109,8 @@ pub(crate) fn trait_solver_query(
97109
}
98110

99111
fn create_chalk_solver() -> chalk_solve::Solver<TypeFamily> {
100-
let solver_choice = chalk_solve::SolverChoice::SLG { max_size: CHALK_SOLVER_MAX_SIZE };
112+
let solver_choice =
113+
chalk_solve::SolverChoice::SLG { max_size: CHALK_SOLVER_MAX_SIZE, expected_answers: None };
101114
solver_choice.into_solver()
102115
}
103116

@@ -232,7 +245,6 @@ fn solution_from_chalk(
232245
let convert_subst = |subst: chalk_ir::Canonical<chalk_ir::Substitution<TypeFamily>>| {
233246
let value = subst
234247
.value
235-
.parameters
236248
.into_iter()
237249
.map(|p| {
238250
let ty = match p.ty() {

crates/ra_hir_ty/src/traits/chalk.rs

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{fmt, sync::Arc};
33

44
use log::debug;
55

6-
use chalk_ir::{cast::Cast, Parameter, PlaceholderIndex, TypeName, UniverseIndex};
6+
use chalk_ir::{cast::Cast, GoalData, Parameter, PlaceholderIndex, TypeName, UniverseIndex};
77

88
use hir_def::{AssocContainerId, AssocItemId, GenericDefId, HasModule, Lookup, TypeAliasId};
99
use ra_db::{
@@ -24,6 +24,8 @@ impl chalk_ir::family::TypeFamily for TypeFamily {
2424
type InternedType = Box<chalk_ir::TyData<Self>>;
2525
type InternedLifetime = chalk_ir::LifetimeData<Self>;
2626
type InternedParameter = chalk_ir::ParameterData<Self>;
27+
type InternedGoal = Arc<GoalData<Self>>;
28+
type InternedSubstitution = Vec<Parameter<Self>>;
2729
type DefId = InternId;
2830

2931
// FIXME: implement these
@@ -78,6 +80,24 @@ impl chalk_ir::family::TypeFamily for TypeFamily {
7880
fn parameter_data(parameter: &chalk_ir::ParameterData<Self>) -> &chalk_ir::ParameterData<Self> {
7981
parameter
8082
}
83+
84+
fn intern_goal(goal: GoalData<Self>) -> Arc<GoalData<Self>> {
85+
Arc::new(goal)
86+
}
87+
88+
fn goal_data(goal: &Arc<GoalData<Self>>) -> &GoalData<Self> {
89+
goal
90+
}
91+
92+
fn intern_substitution<E>(
93+
data: impl IntoIterator<Item = Result<Parameter<Self>, E>>,
94+
) -> Result<Vec<Parameter<Self>>, E> {
95+
data.into_iter().collect()
96+
}
97+
98+
fn substitution_data(substitution: &Vec<Parameter<Self>>) -> &[Parameter<Self>] {
99+
substitution
100+
}
81101
}
82102

83103
impl chalk_ir::family::HasTypeFamily for TypeFamily {
@@ -114,13 +134,13 @@ impl ToChalk for Ty {
114134
match self {
115135
Ty::Apply(apply_ty) => {
116136
let name = apply_ty.ctor.to_chalk(db);
117-
let parameters = apply_ty.parameters.to_chalk(db);
118-
chalk_ir::ApplicationTy { name, parameters }.cast().intern()
137+
let substitution = apply_ty.parameters.to_chalk(db);
138+
chalk_ir::ApplicationTy { name, substitution }.cast().intern()
119139
}
120140
Ty::Projection(proj_ty) => {
121141
let associated_ty_id = proj_ty.associated_ty.to_chalk(db);
122-
let parameters = proj_ty.parameters.to_chalk(db);
123-
chalk_ir::ProjectionTy { associated_ty_id, parameters }.cast().intern()
142+
let substitution = proj_ty.parameters.to_chalk(db);
143+
chalk_ir::ProjectionTy { associated_ty_id, substitution }.cast().intern()
124144
}
125145
Ty::Param { idx, .. } => {
126146
PlaceholderIndex { ui: UniverseIndex::ROOT, idx: idx as usize }
@@ -135,23 +155,14 @@ impl ToChalk for Ty {
135155
.cloned()
136156
.map(|p| p.to_chalk(db))
137157
.collect();
138-
let bounded_ty = chalk_ir::BoundedTy { bounds: make_binders(where_clauses, 1) };
158+
let bounded_ty = chalk_ir::DynTy { bounds: make_binders(where_clauses, 1) };
139159
chalk_ir::TyData::Dyn(bounded_ty).intern()
140160
}
141-
Ty::Opaque(predicates) => {
142-
let where_clauses = predicates
143-
.iter()
144-
.filter(|p| !p.is_error())
145-
.cloned()
146-
.map(|p| p.to_chalk(db))
147-
.collect();
148-
let bounded_ty = chalk_ir::BoundedTy { bounds: make_binders(where_clauses, 1) };
149-
chalk_ir::TyData::Opaque(bounded_ty).intern()
150-
}
161+
Ty::Opaque(_predicates) => unimplemented!(),
151162
Ty::Unknown => {
152-
let parameters = Vec::new();
163+
let substitution = chalk_ir::Substitution::empty();
153164
let name = TypeName::Error;
154-
chalk_ir::ApplicationTy { name, parameters }.cast().intern()
165+
chalk_ir::ApplicationTy { name, substitution }.cast().intern()
155166
}
156167
}
157168
}
@@ -161,7 +172,7 @@ impl ToChalk for Ty {
161172
TypeName::Error => Ty::Unknown,
162173
_ => {
163174
let ctor = from_chalk(db, apply_ty.name);
164-
let parameters = from_chalk(db, apply_ty.parameters);
175+
let parameters = from_chalk(db, apply_ty.substitution);
165176
Ty::Apply(ApplicationTy { ctor, parameters })
166177
}
167178
},
@@ -171,10 +182,10 @@ impl ToChalk for Ty {
171182
}
172183
chalk_ir::TyData::Projection(proj) => {
173184
let associated_ty = from_chalk(db, proj.associated_ty_id);
174-
let parameters = from_chalk(db, proj.parameters);
185+
let parameters = from_chalk(db, proj.substitution);
175186
Ty::Projection(ProjectionTy { associated_ty, parameters })
176187
}
177-
chalk_ir::TyData::ForAll(_) => unimplemented!(),
188+
chalk_ir::TyData::Function(_) => unimplemented!(),
178189
chalk_ir::TyData::BoundVar(idx) => Ty::Bound(idx as u32),
179190
chalk_ir::TyData::InferenceVar(_iv) => Ty::Unknown,
180191
chalk_ir::TyData::Dyn(where_clauses) => {
@@ -183,27 +194,18 @@ impl ToChalk for Ty {
183194
where_clauses.bounds.value.into_iter().map(|c| from_chalk(db, c)).collect();
184195
Ty::Dyn(predicates)
185196
}
186-
chalk_ir::TyData::Opaque(where_clauses) => {
187-
assert_eq!(where_clauses.bounds.binders.len(), 1);
188-
let predicates =
189-
where_clauses.bounds.value.into_iter().map(|c| from_chalk(db, c)).collect();
190-
Ty::Opaque(predicates)
191-
}
192197
}
193198
}
194199
}
195200

196201
impl ToChalk for Substs {
197-
type Chalk = Vec<chalk_ir::Parameter<TypeFamily>>;
202+
type Chalk = chalk_ir::Substitution<TypeFamily>;
198203

199-
fn to_chalk(self, db: &impl HirDatabase) -> Vec<Parameter<TypeFamily>> {
200-
self.iter().map(|ty| ty.clone().to_chalk(db).cast()).collect()
204+
fn to_chalk(self, db: &impl HirDatabase) -> chalk_ir::Substitution<TypeFamily> {
205+
chalk_ir::Substitution::from(self.iter().map(|ty| ty.clone().to_chalk(db)))
201206
}
202207

203-
fn from_chalk(
204-
db: &impl HirDatabase,
205-
parameters: Vec<chalk_ir::Parameter<TypeFamily>>,
206-
) -> Substs {
208+
fn from_chalk(db: &impl HirDatabase, parameters: chalk_ir::Substitution<TypeFamily>) -> Substs {
207209
let tys = parameters
208210
.into_iter()
209211
.map(|p| match p.ty() {
@@ -220,13 +222,13 @@ impl ToChalk for TraitRef {
220222

221223
fn to_chalk(self: TraitRef, db: &impl HirDatabase) -> chalk_ir::TraitRef<TypeFamily> {
222224
let trait_id = self.trait_.to_chalk(db);
223-
let parameters = self.substs.to_chalk(db);
224-
chalk_ir::TraitRef { trait_id, parameters }
225+
let substitution = self.substs.to_chalk(db);
226+
chalk_ir::TraitRef { trait_id, substitution }
225227
}
226228

227229
fn from_chalk(db: &impl HirDatabase, trait_ref: chalk_ir::TraitRef<TypeFamily>) -> Self {
228230
let trait_ = from_chalk(db, trait_ref.trait_id);
229-
let substs = from_chalk(db, trait_ref.parameters);
231+
let substs = from_chalk(db, trait_ref.substitution);
230232
TraitRef { trait_, substs }
231233
}
232234
}
@@ -350,7 +352,7 @@ impl ToChalk for ProjectionTy {
350352
fn to_chalk(self, db: &impl HirDatabase) -> chalk_ir::ProjectionTy<TypeFamily> {
351353
chalk_ir::ProjectionTy {
352354
associated_ty_id: self.associated_ty.to_chalk(db),
353-
parameters: self.parameters.to_chalk(db),
355+
substitution: self.parameters.to_chalk(db),
354356
}
355357
}
356358

@@ -360,7 +362,7 @@ impl ToChalk for ProjectionTy {
360362
) -> ProjectionTy {
361363
ProjectionTy {
362364
associated_ty: from_chalk(db, projection_ty.associated_ty_id),
363-
parameters: from_chalk(db, projection_ty.parameters),
365+
parameters: from_chalk(db, projection_ty.substitution),
364366
}
365367
}
366368
}

0 commit comments

Comments
 (0)