]> git.lizzy.rs Git - rust.git/blobdiff - crates/hir-ty/src/lib.rs
Auto merge of #13223 - lowr:fix/hir-proj-normalization, r=flodiebold
[rust.git] / crates / hir-ty / src / lib.rs
index 85d8ccc07720f4278263b9ed8f1e32a2323e6f82..de4a5446e57f00e948bee718ae6013dcddbb99b7 100644 (file)
@@ -1,6 +1,8 @@
 //! The type system. We currently use this to infer types for completion, hover
 //! information and various assists.
 
+#![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
+
 #[allow(unused)]
 macro_rules! eprintln {
     ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
@@ -12,6 +14,7 @@ macro_rules! eprintln {
 mod chalk_ext;
 pub mod consteval;
 mod infer;
+mod inhabitedness;
 mod interner;
 mod lower;
 mod mapping;
@@ -33,7 +36,7 @@ macro_rules! eprintln {
 use std::sync::Arc;
 
 use chalk_ir::{
-    fold::{Fold, Shift},
+    fold::{Shift, TypeFoldable},
     interner::HasInterner,
     NoSolution,
 };
@@ -47,12 +50,13 @@ macro_rules! eprintln {
 pub use builder::{ParamKind, TyBuilder};
 pub use chalk_ext::*;
 pub use infer::{
-    could_coerce, could_unify, Adjust, Adjustment, AutoBorrow, InferenceDiagnostic, InferenceResult,
+    could_coerce, could_unify, Adjust, Adjustment, AutoBorrow, BindingMode, InferenceDiagnostic,
+    InferenceResult,
 };
 pub use interner::Interner;
 pub use lower::{
-    associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode,
-    TyDefId, TyLoweringContext, ValueTyDefId,
+    associated_type_shorthand_candidates, CallableDefId, ImplTraitLoweringMode, TyDefId,
+    TyLoweringContext, ValueTyDefId,
 };
 pub use mapping::{
     from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id, from_placeholder_idx,
@@ -135,7 +139,7 @@ pub fn param_idx(db: &dyn HirDatabase, id: TypeOrConstParamId) -> Option<usize>
 
 pub(crate) fn wrap_empty_binders<T>(value: T) -> Binders<T>
 where
-    T: Fold<Interner, Result = T> + HasInterner<Interner = Interner>,
+    T: TypeFoldable<Interner> + HasInterner<Interner = Interner>,
 {
     Binders::empty(Interner, value.shifted_in_from(Interner, DebruijnIndex::ONE))
 }
@@ -192,20 +196,6 @@ pub(crate) fn make_binders<T: HasInterner<Interner = Interner>>(
     make_binders_with_count(db, usize::MAX, generics, value)
 }
 
-// FIXME: get rid of this
-pub fn make_canonical<T: HasInterner<Interner = Interner>>(
-    value: T,
-    kinds: impl IntoIterator<Item = TyVariableKind>,
-) -> Canonical<T> {
-    let kinds = kinds.into_iter().map(|tk| {
-        chalk_ir::CanonicalVarKind::new(
-            chalk_ir::VariableKind::Ty(tk),
-            chalk_ir::UniverseIndex::ROOT,
-        )
-    });
-    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.
@@ -263,14 +253,12 @@ pub fn ret(&self) -> &Ty {
     }
 }
 
-impl Fold<Interner> for CallableSig {
-    type Result = CallableSig;
-
+impl TypeFoldable<Interner> for CallableSig {
     fn fold_with<E>(
         self,
-        folder: &mut dyn chalk_ir::fold::Folder<Interner, Error = E>,
+        folder: &mut dyn chalk_ir::fold::TypeFolder<Interner, Error = E>,
         outer_binder: DebruijnIndex,
-    ) -> Result<Self::Result, E> {
+    ) -> Result<Self, E> {
         let vec = self.params_and_return.to_vec();
         let folded = vec.fold_with(folder, outer_binder)?;
         Ok(CallableSig { params_and_return: folded.into(), is_varargs: self.is_varargs })
@@ -299,22 +287,22 @@ pub fn static_lifetime() -> Lifetime {
     LifetimeData::Static.intern(Interner)
 }
 
-pub(crate) fn fold_free_vars<T: HasInterner<Interner = Interner> + Fold<Interner>>(
+pub(crate) fn fold_free_vars<T: HasInterner<Interner = Interner> + TypeFoldable<Interner>>(
     t: T,
     for_ty: impl FnMut(BoundVar, DebruijnIndex) -> Ty,
     for_const: impl FnMut(Ty, BoundVar, DebruijnIndex) -> Const,
-) -> T::Result {
-    use chalk_ir::{fold::Folder, Fallible};
+) -> T {
+    use chalk_ir::{fold::TypeFolder, Fallible};
     struct FreeVarFolder<F1, F2>(F1, F2);
     impl<
             'i,
             F1: FnMut(BoundVar, DebruijnIndex) -> Ty + 'i,
             F2: FnMut(Ty, BoundVar, DebruijnIndex) -> Const + 'i,
-        > Folder<Interner> for FreeVarFolder<F1, F2>
+        > TypeFolder<Interner> for FreeVarFolder<F1, F2>
     {
         type Error = NoSolution;
 
-        fn as_dyn(&mut self) -> &mut dyn Folder<Interner, Error = Self::Error> {
+        fn as_dyn(&mut self) -> &mut dyn TypeFolder<Interner, Error = Self::Error> {
             self
         }
 
@@ -343,11 +331,11 @@ fn fold_free_var_const(
         .expect("fold failed unexpectedly")
 }
 
-pub(crate) fn fold_tys<T: HasInterner<Interner = Interner> + Fold<Interner>>(
+pub(crate) fn fold_tys<T: HasInterner<Interner = Interner> + TypeFoldable<Interner>>(
     t: T,
     mut for_ty: impl FnMut(Ty, DebruijnIndex) -> Ty,
     binders: DebruijnIndex,
-) -> T::Result {
+) -> T {
     fold_tys_and_consts(
         t,
         |x, d| match x {
@@ -358,22 +346,22 @@ pub(crate) fn fold_tys<T: HasInterner<Interner = Interner> + Fold<Interner>>(
     )
 }
 
-pub(crate) fn fold_tys_and_consts<T: HasInterner<Interner = Interner> + Fold<Interner>>(
+pub(crate) fn fold_tys_and_consts<T: HasInterner<Interner = Interner> + TypeFoldable<Interner>>(
     t: T,
     f: impl FnMut(Either<Ty, Const>, DebruijnIndex) -> Either<Ty, Const>,
     binders: DebruijnIndex,
-) -> T::Result {
+) -> T {
     use chalk_ir::{
-        fold::{Folder, SuperFold},
+        fold::{TypeFolder, TypeSuperFoldable},
         Fallible,
     };
     struct TyFolder<F>(F);
-    impl<'i, F: FnMut(Either<Ty, Const>, DebruijnIndex) -> Either<Ty, Const> + 'i> Folder<Interner>
-        for TyFolder<F>
+    impl<'i, F: FnMut(Either<Ty, Const>, DebruijnIndex) -> Either<Ty, Const> + 'i>
+        TypeFolder<Interner> for TyFolder<F>
     {
         type Error = NoSolution;
 
-        fn as_dyn(&mut self) -> &mut dyn Folder<Interner, Error = Self::Error> {
+        fn as_dyn(&mut self) -> &mut dyn TypeFolder<Interner, Error = Self::Error> {
             self
         }
 
@@ -396,22 +384,22 @@ fn fold_const(&mut self, c: Const, outer_binder: DebruijnIndex) -> Fallible<Cons
 /// 'Canonicalizes' the `t` by replacing any errors with new variables. Also
 /// ensures there are no unbound variables or inference variables anywhere in
 /// the `t`.
-pub fn replace_errors_with_variables<T>(t: &T) -> Canonical<T::Result>
+pub fn replace_errors_with_variables<T>(t: &T) -> Canonical<T>
 where
-    T: HasInterner<Interner = Interner> + Fold<Interner> + Clone,
-    T::Result: HasInterner<Interner = Interner>,
+    T: HasInterner<Interner = Interner> + TypeFoldable<Interner> + Clone,
+    T: HasInterner<Interner = Interner>,
 {
     use chalk_ir::{
-        fold::{Folder, SuperFold},
+        fold::{TypeFolder, TypeSuperFoldable},
         Fallible,
     };
     struct ErrorReplacer {
         vars: usize,
     }
-    impl Folder<Interner> for ErrorReplacer {
+    impl TypeFolder<Interner> for ErrorReplacer {
         type Error = NoSolution;
 
-        fn as_dyn(&mut self) -> &mut dyn Folder<Interner, Error = Self::Error> {
+        fn as_dyn(&mut self) -> &mut dyn TypeFolder<Interner, Error = Self::Error> {
             self
         }