Skip to content

Commit 9527146

Browse files
committed
Upgrade Chalk again
The big change here is counting binders, not variables (rust-lang/chalk#360). We have to adapt to the same scheme for our `Ty::Bound`. It's mostly fine though, even makes some things more clear.
1 parent 3659502 commit 9527146

File tree

12 files changed

+198
-115
lines changed

12 files changed

+198
-115
lines changed

Cargo.lock

Lines changed: 6 additions & 6 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
@@ -23,9 +23,9 @@ ra_prof = { path = "../ra_prof" }
2323
ra_syntax = { path = "../ra_syntax" }
2424
test_utils = { path = "../test_utils" }
2525

26-
chalk-solve = { git = "https://github.com/rust-lang/chalk.git", rev = "d383af7333cc6014e9d9e3e77668b5d5b0a5b40e" }
27-
chalk-rust-ir = { git = "https://github.com/rust-lang/chalk.git", rev = "d383af7333cc6014e9d9e3e77668b5d5b0a5b40e" }
28-
chalk-ir = { git = "https://github.com/rust-lang/chalk.git", rev = "d383af7333cc6014e9d9e3e77668b5d5b0a5b40e" }
26+
chalk-solve = { git = "https://github.com/rust-lang/chalk.git", rev = "039fc904a05f8cb3d0c682c9a57a63dda7a35356" }
27+
chalk-rust-ir = { git = "https://github.com/rust-lang/chalk.git", rev = "039fc904a05f8cb3d0c682c9a57a63dda7a35356" }
28+
chalk-ir = { git = "https://github.com/rust-lang/chalk.git", rev = "039fc904a05f8cb3d0c682c9a57a63dda7a35356" }
2929

3030
[dev-dependencies]
3131
insta = "0.15.0"

crates/ra_hir_ty/src/autoderef.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
db::HirDatabase,
1515
traits::{InEnvironment, Solution},
1616
utils::generics,
17-
Canonical, Substs, Ty, TypeWalk,
17+
BoundVar, Canonical, DebruijnIndex, Substs, Ty,
1818
};
1919

2020
const AUTODEREF_RECURSION_LIMIT: usize = 10;
@@ -61,14 +61,13 @@ fn deref_by_trait(
6161
return None;
6262
}
6363

64-
// FIXME make the Canonical handling nicer
64+
// FIXME make the Canonical / bound var handling nicer
6565

66-
let parameters = Substs::build_for_generics(&generic_params)
67-
.push(ty.value.value.clone().shift_bound_vars(1))
68-
.build();
66+
let parameters =
67+
Substs::build_for_generics(&generic_params).push(ty.value.value.clone()).build();
6968

7069
let projection = super::traits::ProjectionPredicate {
71-
ty: Ty::Bound(0),
70+
ty: Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, ty.value.num_vars)),
7271
projection_ty: super::ProjectionTy { associated_ty: target, parameters },
7372
};
7473

@@ -93,12 +92,16 @@ fn deref_by_trait(
9392
// we have `impl<T> Deref for Foo<T> { Target = T }`, that should be
9493
// the case.
9594
for i in 1..vars.0.num_vars {
96-
if vars.0.value[i] != Ty::Bound((i - 1) as u32) {
95+
if vars.0.value[i - 1] != Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, i - 1))
96+
{
9797
warn!("complex solution for derefing {:?}: {:?}, ignoring", ty.value, solution);
9898
return None;
9999
}
100100
}
101-
Some(Canonical { value: vars.0.value[0].clone(), num_vars: vars.0.num_vars })
101+
Some(Canonical {
102+
value: vars.0.value[vars.0.value.len() - 1].clone(),
103+
num_vars: vars.0.num_vars,
104+
})
102105
}
103106
Solution::Ambig(_) => {
104107
info!("Ambiguous solution for derefing {:?}: {:?}", ty.value, solution);

crates/ra_hir_ty/src/display.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl HirDisplay for Ty {
303303
}
304304
}
305305
}
306-
Ty::Bound(idx) => write!(f, "?{}", idx)?,
306+
Ty::Bound(idx) => write!(f, "?{}.{}", idx.debruijn.depth(), idx.index)?,
307307
Ty::Dyn(predicates) | Ty::Opaque(predicates) => {
308308
match self {
309309
Ty::Dyn(_) => write!(f, "dyn ")?,

crates/ra_hir_ty/src/infer/unify.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue};
77
use test_utils::tested_by;
88

99
use super::{InferenceContext, Obligation};
10-
use crate::{Canonical, InEnvironment, InferTy, Substs, Ty, TypeCtor, TypeWalk};
10+
use crate::{
11+
BoundVar, Canonical, DebruijnIndex, InEnvironment, InferTy, Substs, Ty, TypeCtor, TypeWalk,
12+
};
1113

1214
impl<'a> InferenceContext<'a> {
1315
pub(super) fn canonicalizer<'b>(&'b mut self) -> Canonicalizer<'a, 'b>
@@ -47,7 +49,7 @@ where
4749
})
4850
}
4951

50-
fn do_canonicalize<T: TypeWalk>(&mut self, t: T, binders: usize) -> T {
52+
fn do_canonicalize<T: TypeWalk>(&mut self, t: T, binders: DebruijnIndex) -> T {
5153
t.fold_binders(
5254
&mut |ty, binders| match ty {
5355
Ty::Infer(tv) => {
@@ -72,7 +74,7 @@ where
7274
InferTy::MaybeNeverTypeVar(_) => InferTy::MaybeNeverTypeVar(root),
7375
};
7476
let position = self.add(free_var);
75-
Ty::Bound((position + binders) as u32)
77+
Ty::Bound(BoundVar::new(binders, position))
7678
}
7779
}
7880
_ => ty,
@@ -89,7 +91,7 @@ where
8991
}
9092

9193
pub(crate) fn canonicalize_ty(mut self, ty: Ty) -> Canonicalized<Ty> {
92-
let result = self.do_canonicalize(ty, 0);
94+
let result = self.do_canonicalize(ty, DebruijnIndex::INNERMOST);
9395
self.into_canonicalized(result)
9496
}
9597

@@ -98,8 +100,12 @@ where
98100
obligation: InEnvironment<Obligation>,
99101
) -> Canonicalized<InEnvironment<Obligation>> {
100102
let result = match obligation.value {
101-
Obligation::Trait(tr) => Obligation::Trait(self.do_canonicalize(tr, 0)),
102-
Obligation::Projection(pr) => Obligation::Projection(self.do_canonicalize(pr, 0)),
103+
Obligation::Trait(tr) => {
104+
Obligation::Trait(self.do_canonicalize(tr, DebruijnIndex::INNERMOST))
105+
}
106+
Obligation::Projection(pr) => {
107+
Obligation::Projection(self.do_canonicalize(pr, DebruijnIndex::INNERMOST))
108+
}
103109
};
104110
self.into_canonicalized(InEnvironment {
105111
value: result,
@@ -112,13 +118,13 @@ impl<T> Canonicalized<T> {
112118
pub fn decanonicalize_ty(&self, mut ty: Ty) -> Ty {
113119
ty.walk_mut_binders(
114120
&mut |ty, binders| {
115-
if let &mut Ty::Bound(idx) = ty {
116-
if idx as usize >= binders && (idx as usize - binders) < self.free_vars.len() {
117-
*ty = Ty::Infer(self.free_vars[idx as usize - binders]);
121+
if let &mut Ty::Bound(bound) = ty {
122+
if bound.debruijn >= binders {
123+
*ty = Ty::Infer(self.free_vars[bound.index]);
118124
}
119125
}
120126
},
121-
0,
127+
DebruijnIndex::INNERMOST,
122128
);
123129
ty
124130
}
@@ -150,7 +156,7 @@ pub fn unify(ty1: &Canonical<Ty>, ty2: &Canonical<Ty>) -> Option<Substs> {
150156
// (kind of hacky)
151157
for (i, var) in vars.iter().enumerate() {
152158
if &*table.resolve_ty_shallow(var) == var {
153-
table.unify(var, &Ty::Bound(i as u32));
159+
table.unify(var, &Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, i)));
154160
}
155161
}
156162
Some(

0 commit comments

Comments
 (0)