diff options
author | Florian Diebold <[email protected]> | 2019-11-16 11:53:13 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2019-11-16 12:25:54 +0000 |
commit | 351c29d859d74f7a61e654bdbcad634bfb136225 (patch) | |
tree | 39289f3a5fd0d828780ffa3128317afc8a0598e4 /crates/ra_hir/src/ty | |
parent | 9c2a9a9a0635e53466749fdedcdc5a371e658cde (diff) |
Fix handling of the binders in dyn/impl Trait
We need to be more careful now when substituting bound variables (previously, we
didn't have anything that used bound variables except Chalk, so it was not a
problem).
This is obviously quite ad-hoc; Chalk has more infrastructure for handling this
in a principled way, which we maybe should adopt.
Diffstat (limited to 'crates/ra_hir/src/ty')
-rw-r--r-- | crates/ra_hir/src/ty/infer/unify.rs | 22 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 43 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/traits.rs | 7 |
3 files changed, 59 insertions, 13 deletions
diff --git a/crates/ra_hir/src/ty/infer/unify.rs b/crates/ra_hir/src/ty/infer/unify.rs index ca33cc7f8..64d9394cf 100644 --- a/crates/ra_hir/src/ty/infer/unify.rs +++ b/crates/ra_hir/src/ty/infer/unify.rs | |||
@@ -134,17 +134,19 @@ where | |||
134 | } | 134 | } |
135 | 135 | ||
136 | impl<T> Canonicalized<T> { | 136 | impl<T> Canonicalized<T> { |
137 | pub fn decanonicalize_ty(&self, ty: Ty) -> Ty { | 137 | pub fn decanonicalize_ty(&self, mut ty: Ty) -> Ty { |
138 | ty.fold(&mut |ty| match ty { | 138 | ty.walk_mut_binders( |
139 | Ty::Bound(idx) => { | 139 | &mut |ty, binders| match ty { |
140 | if (idx as usize) < self.free_vars.len() { | 140 | &mut Ty::Bound(idx) => { |
141 | Ty::Infer(self.free_vars[idx as usize]) | 141 | if idx as usize >= binders && (idx as usize - binders) < self.free_vars.len() { |
142 | } else { | 142 | *ty = Ty::Infer(self.free_vars[idx as usize - binders]); |
143 | Ty::Bound(idx) | 143 | } |
144 | } | 144 | } |
145 | } | 145 | _ => {} |
146 | ty => ty, | 146 | }, |
147 | }) | 147 | 0, |
148 | ); | ||
149 | ty | ||
148 | } | 150 | } |
149 | 151 | ||
150 | pub fn apply_solution( | 152 | pub fn apply_solution( |
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 838cb4d23..ca1693679 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -4185,6 +4185,49 @@ fn test<T: Trait<Type = u32>>(x: T, y: impl Trait<Type = i64>) { | |||
4185 | } | 4185 | } |
4186 | 4186 | ||
4187 | #[test] | 4187 | #[test] |
4188 | fn impl_trait_assoc_binding_projection_bug() { | ||
4189 | let (db, pos) = TestDB::with_position( | ||
4190 | r#" | ||
4191 | //- /main.rs crate:main deps:std | ||
4192 | pub trait Language { | ||
4193 | type Kind; | ||
4194 | } | ||
4195 | pub enum RustLanguage {} | ||
4196 | impl Language for RustLanguage { | ||
4197 | type Kind = SyntaxKind; | ||
4198 | } | ||
4199 | struct SyntaxNode<L> {} | ||
4200 | fn foo() -> impl Iterator<Item = SyntaxNode<RustLanguage>> {} | ||
4201 | |||
4202 | trait Clone { | ||
4203 | fn clone(&self) -> Self; | ||
4204 | } | ||
4205 | |||
4206 | fn api_walkthrough() { | ||
4207 | for node in foo() { | ||
4208 | node.clone()<|>; | ||
4209 | } | ||
4210 | } | ||
4211 | |||
4212 | //- /std.rs crate:std | ||
4213 | #[prelude_import] use iter::*; | ||
4214 | mod iter { | ||
4215 | trait IntoIterator { | ||
4216 | type Item; | ||
4217 | } | ||
4218 | trait Iterator { | ||
4219 | type Item; | ||
4220 | } | ||
4221 | impl<T: Iterator> IntoIterator for T { | ||
4222 | type Item = <T as Iterator>::Item; | ||
4223 | } | ||
4224 | } | ||
4225 | "#, | ||
4226 | ); | ||
4227 | assert_eq!("{unknown}", type_at_pos(&db, pos)); | ||
4228 | } | ||
4229 | |||
4230 | #[test] | ||
4188 | fn projection_eq_within_chalk() { | 4231 | fn projection_eq_within_chalk() { |
4189 | // std::env::set_var("CHALK_DEBUG", "1"); | 4232 | // std::env::set_var("CHALK_DEBUG", "1"); |
4190 | assert_snapshot!( | 4233 | assert_snapshot!( |
diff --git a/crates/ra_hir/src/ty/traits.rs b/crates/ra_hir/src/ty/traits.rs index 771525deb..99dbab99e 100644 --- a/crates/ra_hir/src/ty/traits.rs +++ b/crates/ra_hir/src/ty/traits.rs | |||
@@ -165,9 +165,9 @@ impl TypeWalk for ProjectionPredicate { | |||
165 | self.ty.walk(f); | 165 | self.ty.walk(f); |
166 | } | 166 | } |
167 | 167 | ||
168 | fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) { | 168 | fn walk_mut_binders(&mut self, f: &mut impl FnMut(&mut Ty, usize), binders: usize) { |
169 | self.projection_ty.walk_mut(f); | 169 | self.projection_ty.walk_mut_binders(f, binders); |
170 | self.ty.walk_mut(f); | 170 | self.ty.walk_mut_binders(f, binders); |
171 | } | 171 | } |
172 | } | 172 | } |
173 | 173 | ||
@@ -188,6 +188,7 @@ pub(crate) fn trait_solve_query( | |||
188 | } | 188 | } |
189 | 189 | ||
190 | let canonical = goal.to_chalk(db).cast(); | 190 | let canonical = goal.to_chalk(db).cast(); |
191 | |||
191 | // We currently don't deal with universes (I think / hope they're not yet | 192 | // We currently don't deal with universes (I think / hope they're not yet |
192 | // relevant for our use cases?) | 193 | // relevant for our use cases?) |
193 | let u_canonical = chalk_ir::UCanonical { canonical, universes: 1 }; | 194 | let u_canonical = chalk_ir::UCanonical { canonical, universes: 1 }; |