Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b517aee

Browse files
committed
Show TyKind::FnDef as a fn pointer in source code
1 parent 037844c commit b517aee

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

crates/hir-ty/src/display.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,13 @@ impl HirDisplay for Ty {
885885
TyKind::FnDef(def, parameters) => {
886886
let def = from_chalk(db, *def);
887887
let sig = db.callable_item_signature(def).substitute(Interner, parameters);
888+
889+
if f.display_target.is_source_code() {
890+
// `FnDef` is anonymous and there's no surface syntax for it. Show it as a
891+
// function pointer type.
892+
return sig.hir_fmt(f);
893+
}
894+
888895
f.start_location_link(def.into());
889896
match def {
890897
CallableDefId::FunctionId(ff) => {

crates/hir-ty/src/tests/display_source_code.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,22 @@ fn f(a: impl Foo<i8, Assoc<i16> = i32>) {
227227
"#,
228228
);
229229
}
230+
231+
#[test]
232+
fn fn_def_is_shown_as_fn_ptr() {
233+
check_types_source_code(
234+
r#"
235+
fn foo(_: i32) -> i64 { 42 }
236+
struct S<T>(T);
237+
enum E { A(usize) }
238+
fn test() {
239+
let f = foo;
240+
//^ fn(i32) -> i64
241+
let f = S::<i8>;
242+
//^ fn(i8) -> S<i8>
243+
let f = E::A;
244+
//^ fn(usize) -> E
245+
}
246+
"#,
247+
);
248+
}

crates/ide-assists/src/handlers/generate_function.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,7 +1878,6 @@ where
18781878

18791879
#[test]
18801880
fn add_function_with_fn_arg() {
1881-
// FIXME: The argument in `bar` is wrong.
18821881
check_assist(
18831882
generate_function,
18841883
r"
@@ -1899,7 +1898,7 @@ fn foo() {
18991898
bar(Baz::new);
19001899
}
19011900
1902-
fn bar(new: fn) ${0:-> _} {
1901+
fn bar(new: fn() -> Baz) ${0:-> _} {
19031902
todo!()
19041903
}
19051904
",

0 commit comments

Comments
 (0)