From 72002b401dcc0f9fb14ebf2d8d255f051bfdc513 Mon Sep 17 00:00:00 2001 From: Dawer <7803845+iDawer@users.noreply.github.com> Date: Wed, 9 Jun 2021 11:49:12 +0500 Subject: [PATCH] internal: allow ambiguous unsize coercion. --- crates/hir_ty/src/infer/coerce.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs index d2e999a730f..0e6b474486a 100644 --- a/crates/hir_ty/src/infer/coerce.rs +++ b/crates/hir_ty/src/infer/coerce.rs @@ -17,8 +17,8 @@ Adjust, Adjustment, AutoBorrow, InferOk, InferResult, InferenceContext, OverloadedDeref, PointerCast, TypeError, TypeMismatch, }, - static_lifetime, Canonical, DomainGoal, FnPointer, FnSig, InEnvironment, Interner, Solution, - Substitution, Ty, TyBuilder, TyExt, TyKind, + static_lifetime, Canonical, DomainGoal, FnPointer, FnSig, Guidance, InEnvironment, Interner, + Solution, Substitution, Ty, TyBuilder, TyExt, TyKind, }; pub(crate) type CoerceResult = Result, Ty)>, TypeError>; @@ -541,7 +541,7 @@ fn try_coerce_unsized(&mut self, from_ty: &Ty, to_ty: &Ty) -> CoerceResult { _ => return Err(TypeError), }; - let trait_ref = { + let coerce_unsized_tref = { let b = TyBuilder::trait_ref(self.db, coerce_unsized_trait); if b.remaining() != 2 { // The CoerceUnsized trait should have two generic params: Self and T. @@ -551,7 +551,7 @@ fn try_coerce_unsized(&mut self, from_ty: &Ty, to_ty: &Ty) -> CoerceResult { }; let goal: InEnvironment = - InEnvironment::new(&self.trait_env.env, trait_ref.cast(&Interner)); + InEnvironment::new(&self.trait_env.env, coerce_unsized_tref.cast(&Interner)); let canonicalized = self.canonicalize(goal); @@ -575,8 +575,11 @@ fn try_coerce_unsized(&mut self, from_ty: &Ty, to_ty: &Ty) -> CoerceResult { }, ); } - // FIXME: should we accept ambiguous results here? - _ => return Err(TypeError), + Solution::Ambig(guidance) => { + if let Guidance::Definite(subst) = guidance { + canonicalized.apply_solution(&mut self.table, subst); + } + } }; let unsize = Adjustment { kind: Adjust::Pointer(PointerCast::Unsize), target: to_ty.clone() };