From 0ad7317b24dc90c3787482f9ec563e7830d499fc Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 1 May 2019 17:57:56 +0200 Subject: Canonicalize before doing method resolution --- crates/ra_hir/src/ty/infer/unify.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'crates/ra_hir/src/ty/infer') diff --git a/crates/ra_hir/src/ty/infer/unify.rs b/crates/ra_hir/src/ty/infer/unify.rs index 5edb95c31..820a64789 100644 --- a/crates/ra_hir/src/ty/infer/unify.rs +++ b/crates/ra_hir/src/ty/infer/unify.rs @@ -1,6 +1,8 @@ //! Unification and canonicalization logic. -use super::{InferenceContext, Ty, TraitRef, InferTy, HirDatabase}; +use crate::db::HirDatabase; +use crate::ty::{Ty, Canonical, TraitRef, InferTy}; +use super::InferenceContext; impl<'a, D: HirDatabase> InferenceContext<'a, D> { pub(super) fn canonicalizer<'b>(&'b mut self) -> Canonicalizer<'a, 'b, D> @@ -13,13 +15,6 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { // TODO improve the interface of this -// TODO move further up? -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub(crate) struct Canonical { - pub value: T, - pub num_vars: usize, -} - pub(super) struct Canonicalizer<'a, 'b, D: HirDatabase> where 'a: 'b, @@ -68,6 +63,19 @@ where Canonical { value, num_vars: self.free_vars.len() } } + pub fn decanonicalize_ty(&self, ty: Ty) -> Ty { + ty.fold(&mut |ty| match ty { + Ty::Bound(idx) => { + if (idx as usize) < self.free_vars.len() { + Ty::Infer(self.free_vars[idx as usize].clone()) + } else { + Ty::Bound(idx) + } + } + ty => ty, + }) + } + pub fn apply_solution(&mut self, solution: Canonical>) { // the solution may contain new variables, which we need to convert to new inference vars let new_vars = -- cgit v1.2.3