diff --git a/crates/hir-ty/src/infer.rs b/crates/hir-ty/src/infer.rs index 31e56dec625..53ea14504b6 100644 --- a/crates/hir-ty/src/infer.rs +++ b/crates/hir-ty/src/infer.rs @@ -1020,7 +1020,7 @@ fn from_option(ty: Option) -> Self { /// The primary use case is where the expected type is a fat pointer, /// like `&[isize]`. For example, consider the following statement: /// - /// let x: &[isize] = &[1, 2, 3]; + /// let x: &[isize] = &[1, 2, 3]; /// /// In this case, the expected type for the `&[1, 2, 3]` expression is /// `&[isize]`. If however we were to say that `[1, 2, 3]` has the diff --git a/crates/hir-ty/src/lib.rs b/crates/hir-ty/src/lib.rs index 42c3b58d5ad..2b5989c6c6f 100644 --- a/crates/hir-ty/src/lib.rs +++ b/crates/hir-ty/src/lib.rs @@ -38,10 +38,12 @@ macro_rules! eprintln { use chalk_ir::{ fold::{Shift, TypeFoldable}, interner::HasInterner, - NoSolution, + NoSolution, UniverseIndex, }; use hir_def::{expr::ExprId, type_ref::Rawness, TypeOrConstParamId}; +use hir_expand::name; use itertools::Either; +use traits::FnTrait; use utils::Generics; use crate::{consteval::unknown_const, db::HirDatabase, utils::generics}; @@ -508,3 +510,68 @@ fn try_fold_free_var_lifetime( }); Canonical { value, binders: chalk_ir::CanonicalVarKinds::from_iter(Interner, kinds) } } + +pub fn callable_sig_from_fnonce( + self_ty: &Canonical, + env: Arc, + db: &dyn HirDatabase, +) -> Option { + let krate = env.krate; + let fn_once_trait = FnTrait::FnOnce.get_id(db, krate)?; + let output_assoc_type = db.trait_data(fn_once_trait).associated_type_by_name(&name![Output])?; + + let mut kinds = self_ty.binders.interned().to_vec(); + let b = TyBuilder::trait_ref(db, fn_once_trait); + if b.remaining() != 2 { + return None; + } + let fn_once = b + .push(self_ty.value.clone()) + .fill_with_bound_vars(DebruijnIndex::INNERMOST, kinds.len()) + .build(); + kinds.extend(fn_once.substitution.iter(Interner).skip(1).map(|x| { + let vk = match x.data(Interner) { + chalk_ir::GenericArgData::Ty(_) => { + chalk_ir::VariableKind::Ty(chalk_ir::TyVariableKind::General) + } + chalk_ir::GenericArgData::Lifetime(_) => chalk_ir::VariableKind::Lifetime, + chalk_ir::GenericArgData::Const(c) => { + chalk_ir::VariableKind::Const(c.data(Interner).ty.clone()) + } + }; + chalk_ir::WithKind::new(vk, UniverseIndex::ROOT) + })); + + // FIXME: chalk refuses to solve `>::Output == ^0.1`, so we first solve + // `>` and then replace `^0.0` with the concrete argument tuple. + let trait_env = env.env.clone(); + let obligation = InEnvironment { goal: fn_once.cast(Interner), environment: trait_env }; + let canonical = + Canonical { binders: CanonicalVarKinds::from_iter(Interner, kinds), value: obligation }; + let subst = match db.trait_solve(krate, canonical) { + Some(Solution::Unique(vars)) => vars.value.subst, + _ => return None, + }; + let args = subst.at(Interner, self_ty.binders.interned().len()).ty(Interner)?; + let params = match args.kind(Interner) { + chalk_ir::TyKind::Tuple(_, subst) => { + subst.iter(Interner).filter_map(|arg| arg.ty(Interner).cloned()).collect::>() + } + _ => return None, + }; + if params.iter().any(|ty| ty.is_unknown()) { + return None; + } + + let fn_once = TyBuilder::trait_ref(db, fn_once_trait) + .push(self_ty.value.clone()) + .push(args.clone()) + .build(); + let projection = + TyBuilder::assoc_type_projection(db, output_assoc_type, Some(fn_once.substitution.clone())) + .build(); + + let ret_ty = db.normalize_projection(projection, env); + + Some(CallableSig::from_params_and_return(params, ret_ty.clone(), false)) +} diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index f5324208c9a..cbd9bf32a54 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -2995,7 +2995,17 @@ pub fn as_callable(&self, db: &dyn HirDatabase) -> Option { let callee = match self.ty.kind(Interner) { TyKind::Closure(id, _) => Callee::Closure(*id), TyKind::Function(_) => Callee::FnPtr, - _ => Callee::Def(self.ty.callable_def(db)?), + TyKind::FnDef(..) => Callee::Def(self.ty.callable_def(db)?), + _ => { + let ty = hir_ty::replace_errors_with_variables(&self.ty); + let sig = hir_ty::callable_sig_from_fnonce(&ty, self.env.clone(), db)?; + return Some(Callable { + ty: self.clone(), + sig, + callee: Callee::Other, + is_bound_method: false, + }); + } }; let sig = self.ty.callable_sig(db)?; @@ -3464,6 +3474,7 @@ enum Callee { Def(CallableDefId), Closure(ClosureId), FnPtr, + Other, } pub enum CallableKind { @@ -3472,6 +3483,8 @@ pub enum CallableKind { TupleEnumVariant(Variant), Closure, FnPtr, + /// Some other type that implements `FnOnce`. + Other, } impl Callable { @@ -3483,6 +3496,7 @@ pub fn kind(&self) -> CallableKind { Def(CallableDefId::EnumVariantId(it)) => CallableKind::TupleEnumVariant(it.into()), Closure(_) => CallableKind::Closure, FnPtr => CallableKind::FnPtr, + Other => CallableKind::Other, } } pub fn receiver_param(&self, db: &dyn HirDatabase) -> Option { diff --git a/crates/ide/src/signature_help.rs b/crates/ide/src/signature_help.rs index fedc1a43582..7486b20293a 100644 --- a/crates/ide/src/signature_help.rs +++ b/crates/ide/src/signature_help.rs @@ -149,7 +149,7 @@ fn signature_help_for_call( variant.name(db) ); } - hir::CallableKind::Closure | hir::CallableKind::FnPtr => (), + hir::CallableKind::Closure | hir::CallableKind::FnPtr | hir::CallableKind::Other => (), } res.signature.push('('); @@ -189,9 +189,10 @@ fn signature_help_for_call( hir::CallableKind::Function(func) if callable.return_type().contains_unknown() => { render(func.ret_type(db)) } - hir::CallableKind::Function(_) | hir::CallableKind::Closure | hir::CallableKind::FnPtr => { - render(callable.return_type()) - } + hir::CallableKind::Function(_) + | hir::CallableKind::Closure + | hir::CallableKind::FnPtr + | hir::CallableKind::Other => render(callable.return_type()), hir::CallableKind::TupleStruct(_) | hir::CallableKind::TupleEnumVariant(_) => {} } Some(res) @@ -387,10 +388,9 @@ pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) { } fn check(ra_fixture: &str, expect: Expect) { - // Implicitly add `Sized` to avoid noisy `T: ?Sized` in the results. let fixture = format!( r#" -#[lang = "sized"] trait Sized {{}} +//- minicore: sized, fn {ra_fixture} "# ); @@ -1331,4 +1331,19 @@ fn foo(self: &Self, other: Self) "#]], ); } + + #[test] + fn help_for_generic_call() { + check( + r#" +fn f i32>(f: F) { + f($0) +} +"#, + expect![[r#" + (u8, u16) -> i32 + ^^ --- + "#]], + ); + } }