Skip to content
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

Show anonymous fn def type as a fn pointer in source code #15349

Merged
merged 2 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions crates/hir-ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,15 @@ use crate::{
};

pub trait HirWrite: fmt::Write {
fn start_location_link(&mut self, location: ModuleDefId);
fn end_location_link(&mut self);
fn start_location_link(&mut self, _location: ModuleDefId) {}
fn end_location_link(&mut self) {}
}

// String will ignore link metadata
impl HirWrite for String {
fn start_location_link(&mut self, _: ModuleDefId) {}

fn end_location_link(&mut self) {}
}
impl HirWrite for String {}

// `core::Formatter` will ignore metadata
impl HirWrite for fmt::Formatter<'_> {
fn start_location_link(&mut self, _: ModuleDefId) {}
fn end_location_link(&mut self) {}
}
impl HirWrite for fmt::Formatter<'_> {}

pub struct HirFormatter<'a> {
pub db: &'a dyn HirDatabase,
Expand Down Expand Up @@ -885,6 +878,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) => {
Expand Down
19 changes: 19 additions & 0 deletions crates/hir-ty/src/tests/display_source_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
"#,
);
}
3 changes: 1 addition & 2 deletions crates/ide-assists/src/handlers/generate_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1878,7 +1878,6 @@ where

#[test]
fn add_function_with_fn_arg() {
// FIXME: The argument in `bar` is wrong.
check_assist(
generate_function,
r"
Expand All @@ -1899,7 +1898,7 @@ fn foo() {
bar(Baz::new);
}

fn bar(new: fn) ${0:-> _} {
fn bar(new: fn() -> Baz) ${0:-> _} {
todo!()
}
",
Expand Down