Show TyKind::FnDef as a fn pointer in source code

This commit is contained in:
Ryo Yoshida 2023-07-28 19:09:38 +09:00
parent 037844c8a0
commit b517aeeca5
No known key found for this signature in database
GPG Key ID: E25698A930586171
3 changed files with 27 additions and 2 deletions

View File

@ -885,6 +885,13 @@ impl HirDisplay for Ty {
TyKind::FnDef(def, parameters) => {
let def = from_chalk(db, *def);
let sig = db.callable_item_signature(def).substitute(Interner, parameters);
if f.display_target.is_source_code() {
// `FnDef` is anonymous and there's no surface syntax for it. Show it as a
// function pointer type.
return sig.hir_fmt(f);
}
f.start_location_link(def.into());
match def {
CallableDefId::FunctionId(ff) => {

View File

@ -227,3 +227,22 @@ fn f(a: impl Foo<i8, Assoc<i16> = i32>) {
"#,
);
}
#[test]
fn fn_def_is_shown_as_fn_ptr() {
check_types_source_code(
r#"
fn foo(_: i32) -> i64 { 42 }
struct S<T>(T);
enum E { A(usize) }
fn test() {
let f = foo;
//^ fn(i32) -> i64
let f = S::<i8>;
//^ fn(i8) -> S<i8>
let f = E::A;
//^ fn(usize) -> E
}
"#,
);
}

View File

@ -1878,7 +1878,6 @@ where
#[test]
fn add_function_with_fn_arg() {
// FIXME: The argument in `bar` is wrong.
check_assist(
generate_function,
r"
@ -1899,7 +1898,7 @@ fn foo() {
bar(Baz::new);
}
fn bar(new: fn) ${0:-> _} {
fn bar(new: fn() -> Baz) ${0:-> _} {
todo!()
}
",