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

Commit 3eba6d3

Browse files
committed
Auto merge of rust-lang#15349 - lowr:fix/fn-def-display-source-code, r=lnicola
Show anonymous fn def type as a fn pointer in source code Fixes rust-lang#15346 The second commit is an unrelated change. I can remove it if not desired.
2 parents 037844c + 104d707 commit 3eba6d3

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

crates/hir-ty/src/display.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,15 @@ use crate::{
4848
};
4949

5050
pub trait HirWrite: fmt::Write {
51-
fn start_location_link(&mut self, location: ModuleDefId);
52-
fn end_location_link(&mut self);
51+
fn start_location_link(&mut self, _location: ModuleDefId) {}
52+
fn end_location_link(&mut self) {}
5353
}
5454

5555
// String will ignore link metadata
56-
impl HirWrite for String {
57-
fn start_location_link(&mut self, _: ModuleDefId) {}
58-
59-
fn end_location_link(&mut self) {}
60-
}
56+
impl HirWrite for String {}
6157

6258
// `core::Formatter` will ignore metadata
63-
impl HirWrite for fmt::Formatter<'_> {
64-
fn start_location_link(&mut self, _: ModuleDefId) {}
65-
fn end_location_link(&mut self) {}
66-
}
59+
impl HirWrite for fmt::Formatter<'_> {}
6760

6861
pub struct HirFormatter<'a> {
6962
pub db: &'a dyn HirDatabase,
@@ -885,6 +878,13 @@ impl HirDisplay for Ty {
885878
TyKind::FnDef(def, parameters) => {
886879
let def = from_chalk(db, *def);
887880
let sig = db.callable_item_signature(def).substitute(Interner, parameters);
881+
882+
if f.display_target.is_source_code() {
883+
// `FnDef` is anonymous and there's no surface syntax for it. Show it as a
884+
// function pointer type.
885+
return sig.hir_fmt(f);
886+
}
887+
888888
f.start_location_link(def.into());
889889
match def {
890890
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)