From 693582946fae1813627ad59f60a31c9237e98744 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 1 May 2021 21:53:10 +0200 Subject: Rewrite coercion using the new unification --- crates/hir_ty/src/lib.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'crates/hir_ty/src/lib.rs') diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 15b61bedc..179a27763 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -203,6 +203,17 @@ impl CallableSig { } } + pub fn to_fn_ptr(&self) -> FnPointer { + FnPointer { + num_binders: 0, + sig: FnSig { abi: (), safety: Safety::Safe, variadic: self.is_varargs }, + substitution: FnSubst(Substitution::from_iter( + &Interner, + self.params_and_return.iter().cloned(), + )), + } + } + pub fn params(&self) -> &[Ty] { &self.params_and_return[0..self.params_and_return.len() - 1] } -- cgit v1.2.3 From a09079f27aa631b011f6c0703200862d28af81f4 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 15 May 2021 16:00:24 +0200 Subject: Fix coercion of two closures to a function pointer Fixes #8604. --- crates/hir_ty/src/lib.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'crates/hir_ty/src/lib.rs') diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 179a27763..06d5cd0b6 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -167,6 +167,7 @@ pub fn make_canonical>( Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) } } +// FIXME: get rid of this, just replace it by FnPointer /// A function signature as seen by type inference: Several parameter types and /// one return type. #[derive(Clone, PartialEq, Eq, Debug)] -- cgit v1.2.3 From 8397734cfe26793d3e9f9ec5f8392655a4b8e106 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 15 May 2021 20:28:07 +0200 Subject: Fix HIR expecting errors to unify with anything --- crates/hir_ty/src/lib.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'crates/hir_ty/src/lib.rs') diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 06d5cd0b6..56f60c46b 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -43,6 +43,7 @@ use hir_def::{ type_ref::{ConstScalar, Rawness}, TypeParamId, }; +use stdx::always; use crate::{db::HirDatabase, display::HirDisplay, utils::generics}; @@ -326,3 +327,58 @@ pub(crate) fn fold_tys + Fold>( } t.fold_with(&mut TyFolder(f), binders).expect("fold failed unexpectedly") } + +pub fn replace_errors_with_variables(t: T) -> Canonical +where + T: HasInterner + Fold, + T::Result: HasInterner, +{ + use chalk_ir::{ + fold::{Folder, SuperFold}, + Fallible, + }; + struct ErrorReplacer { + vars: usize, + } + impl<'i> Folder<'i, Interner> for ErrorReplacer { + fn as_dyn(&mut self) -> &mut dyn Folder<'i, Interner> { + self + } + + fn interner(&self) -> &'i Interner { + &Interner + } + + fn fold_ty(&mut self, ty: Ty, outer_binder: DebruijnIndex) -> Fallible { + if let TyKind::Error = ty.kind(&Interner) { + let index = self.vars; + self.vars += 1; + Ok(TyKind::BoundVar(BoundVar::new(outer_binder, index)).intern(&Interner)) + } else { + let ty = ty.super_fold_with(self.as_dyn(), outer_binder)?; + Ok(ty) + } + } + + fn fold_inference_ty( + &mut self, + var: InferenceVar, + kind: TyVariableKind, + _outer_binder: DebruijnIndex, + ) -> Fallible { + always!(false); + Ok(TyKind::InferenceVar(var, kind).intern(&Interner)) + } + } + let mut error_replacer = ErrorReplacer { vars: 0 }; + let value = t + .fold_with(&mut error_replacer, DebruijnIndex::INNERMOST) + .expect("fold failed unexpectedly"); + let kinds = (0..error_replacer.vars).map(|_| { + chalk_ir::CanonicalVarKind::new( + chalk_ir::VariableKind::Ty(TyVariableKind::General), + chalk_ir::UniverseIndex::ROOT, + ) + }); + Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(&Interner, kinds) } +} -- cgit v1.2.3 From 1250ddc5cf58ff0a6bbf7c07e5bd9f7cc7db5a09 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 16 May 2021 15:50:28 +0200 Subject: Rework obligation handling We can't do the easy hack that we did before anymore, where we kept track of whether any inference variables changed since the last time we rechecked obligations. Instead, we store the obligations in canonicalized form; that way we can easily check the inference variables to see whether they have changed since the goal was canonicalized. --- crates/hir_ty/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'crates/hir_ty/src/lib.rs') diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 56f60c46b..72093d75a 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -45,7 +45,7 @@ use hir_def::{ }; use stdx::always; -use crate::{db::HirDatabase, display::HirDisplay, utils::generics}; +use crate::{db::HirDatabase, utils::generics}; pub use autoderef::autoderef; pub use builder::TyBuilder; @@ -114,6 +114,7 @@ pub type FnSig = chalk_ir::FnSig; pub type InEnvironment = chalk_ir::InEnvironment; pub type DomainGoal = chalk_ir::DomainGoal; +pub type Goal = chalk_ir::Goal; pub type AliasEq = chalk_ir::AliasEq; pub type Solution = chalk_solve::Solution; pub type ConstrainedSubst = chalk_ir::ConstrainedSubst; -- cgit v1.2.3