Skip to content

Fs fix 2880 #4483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions crates/ra_hir_ty/src/tests/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,35 @@ fn test<F: FnOnce(u32, u64) -> u128>(f: F) {
);
}

#[test]
fn fn_pointer() {
assert_snapshot!(
infer(r#"
#[lang = "fn_once"]
trait FnOnce<Args> {
type Output;

fn call_once(self, args: Args) -> <Self as FnOnce<Args>>::Output;
}

fn test(f: fn(u32, u32) -> u128) {
f.call_once((1, 2));
}
"#),
@r###"
77..81 'self': Self
83..87 'args': Args
141..142 'f': fn(u32, u32) -> u128
166..194 '{ ...2)); }': ()
172..173 'f': fn(u32, u32) -> u128
172..191 'f.call...1, 2))': u128
184..190 '(1, 2)': (u32, u32)
185..186 '1': u32
188..189 '2': u32
"###
);
}

#[test]
fn closure_1() {
assert_snapshot!(
Expand Down
10 changes: 10 additions & 0 deletions crates/ra_hir_ty/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ pub struct ClosureFnTraitImplData {
fn_trait: FnTrait,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PointerFnTraitImplData {
num_args: u16,
fn_trait: FnTrait,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UnsizeToSuperTraitObjectData {
trait_: TraitId,
Expand All @@ -327,6 +333,8 @@ pub enum Impl {
UnsizeToTraitObject(TraitId),
/// dyn Trait: Unsize<dyn SuperTrait> if Trait: SuperTrait
UnsizeToSuperTraitObject(UnsizeToSuperTraitObjectData),
/// Function pointer types implement the Fn traits synthetically.
PointerFnTraitImpl(PointerFnTraitImplData),
}
/// This exists just for Chalk, because our ImplIds are only unique per module.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand All @@ -342,6 +350,8 @@ pub enum AssocTyValue {
TypeAlias(TypeAliasId),
/// The output type of the Fn trait implementation.
ClosureFnTraitImplOutput(ClosureFnTraitImplData),
/// The output type of the Fn trait implementation.
PointerFnTraitImplOutput(PointerFnTraitImplData),
}
/// This exists just for Chalk, because it needs a unique ID for each associated
/// type value in an impl (even synthetic ones).
Expand Down
103 changes: 103 additions & 0 deletions crates/ra_hir_ty/src/traits/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ pub(super) fn get_builtin_impls(
}
}

if let Ty::Apply(ApplicationTy { ctor: TypeCtor::FnPtr { num_args }, .. }) = ty {
let fn_trait = super::FnTrait::FnOnce;
if let Some(actual_trait) = get_fn_trait(db, krate, fn_trait) {
if trait_ == actual_trait {
let impl_ = super::PointerFnTraitImplData { num_args: *num_args, fn_trait };
if check_fn_trait_impl_prerequisites(db, krate, impl_) {
callback(Impl::PointerFnTraitImpl(impl_));
}
}
}
}

let unsize_trait = get_unsize_trait(db, krate);
if let Some(actual_trait) = unsize_trait {
if trait_ == actual_trait {
Expand Down Expand Up @@ -104,6 +116,7 @@ pub(super) fn impl_datum(db: &dyn HirDatabase, krate: CrateId, impl_: Impl) -> B
Impl::UnsizeToSuperTraitObject(data) => {
super_trait_object_unsize_impl_datum(db, krate, data)
}
Impl::PointerFnTraitImpl(data) => pointer_fn_impl_datum(db, krate, data),
}
}

Expand All @@ -117,6 +130,9 @@ pub(super) fn associated_ty_value(
AssocTyValue::ClosureFnTraitImplOutput(data) => {
closure_fn_trait_output_assoc_ty_value(db, krate, data)
}
AssocTyValue::PointerFnTraitImplOutput(data) => {
pointer_fn_trait_output_assoc_ty_value(db, krate, data)
}
}
}

Expand All @@ -143,6 +159,66 @@ fn check_closure_fn_trait_impl_prerequisites(
db.trait_data(fn_once_trait).associated_type_by_name(&name![Output]).is_some()
}

fn check_fn_trait_impl_prerequisites(
db: &dyn HirDatabase,
krate: CrateId,
data: super::PointerFnTraitImplData,
) -> bool {
// the respective Fn/FnOnce/FnMut trait needs to exist
if get_fn_trait(db, krate, data.fn_trait).is_none() {
return false;
}

// FIXME: there are more assumptions that we should probably check here:
// the traits having no type params, FnOnce being a supertrait

// the FnOnce trait needs to exist and have an assoc type named Output
let fn_ptr_trait = match get_fn_trait(db, krate, super::FnTrait::FnOnce) {
Some(t) => t,
None => return false,
};
db.trait_data(fn_ptr_trait).associated_type_by_name(&name![Output]).is_some()
}

fn pointer_fn_impl_datum(
db: &dyn HirDatabase,
krate: CrateId,
data: super::PointerFnTraitImplData,
) -> BuiltinImplData {
// for some function pointer fn(X, Y) -> Z;
// impl<T, U, V> Fn<(T, U)> for fn(T, U) -> V { Output = V }
let trait_ = get_fn_trait(db, krate, data.fn_trait).expect("fn trait for fn pointer missing");
let num_args = data.num_args;

let arg_ty = Ty::apply(
TypeCtor::Tuple { cardinality: num_args },
Substs::builder(num_args as usize)
.fill_with_bound_vars(DebruijnIndex::INNERMOST, 0)
.build(),
);

let self_ty = Ty::apply(
TypeCtor::FnPtr { num_args },
Substs::builder(num_args as usize + 1)
.fill_with_bound_vars(DebruijnIndex::INNERMOST, 0)
.build(),
);

let trait_ref = TraitRef {
trait_,
substs: Substs::build_for_def(db, trait_).push(self_ty).push(arg_ty).build(),
};

let output_ty_id = AssocTyValue::PointerFnTraitImplOutput(data);

BuiltinImplData {
num_vars: num_args as usize + 1,
trait_ref,
where_clauses: Vec::new(),
assoc_ty_values: vec![output_ty_id],
}
}

fn closure_fn_trait_impl_datum(
db: &dyn HirDatabase,
krate: CrateId,
Expand Down Expand Up @@ -193,6 +269,33 @@ fn closure_fn_trait_impl_datum(
}
}

fn pointer_fn_trait_output_assoc_ty_value(
db: &dyn HirDatabase,
krate: CrateId,
data: super::PointerFnTraitImplData,
) -> BuiltinImplAssocTyValueData {
let impl_ = Impl::PointerFnTraitImpl(data);

let num_args = data.num_args;

let output_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, num_args.into()));

let fn_trait =
get_fn_trait(db, krate, super::FnTrait::FnOnce).expect("assoc ty value should not exist");

let output_ty_id = db
.trait_data(fn_trait)
.associated_type_by_name(&name![Output])
.expect("assoc ty value should not exist");

BuiltinImplAssocTyValueData {
impl_,
assoc_ty_id: output_ty_id,
num_vars: num_args as usize + 1,
value: output_ty,
}
}

fn closure_fn_trait_output_assoc_ty_value(
db: &dyn HirDatabase,
krate: CrateId,
Expand Down