mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-15 06:33:58 +00:00
Auto merge of #15349 - lowr:fix/fn-def-display-source-code, r=lnicola
Show anonymous fn def type as a fn pointer in source code Fixes #15346 The second commit is an unrelated change. I can remove it if not desired.
This commit is contained in:
commit
3eba6d30ad
3 changed files with 31 additions and 13 deletions
|
@ -48,22 +48,15 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait HirWrite: fmt::Write {
|
pub trait HirWrite: fmt::Write {
|
||||||
fn start_location_link(&mut self, location: ModuleDefId);
|
fn start_location_link(&mut self, _location: ModuleDefId) {}
|
||||||
fn end_location_link(&mut self);
|
fn end_location_link(&mut self) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// String will ignore link metadata
|
// String will ignore link metadata
|
||||||
impl HirWrite for String {
|
impl HirWrite for String {}
|
||||||
fn start_location_link(&mut self, _: ModuleDefId) {}
|
|
||||||
|
|
||||||
fn end_location_link(&mut self) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// `core::Formatter` will ignore metadata
|
// `core::Formatter` will ignore metadata
|
||||||
impl HirWrite for fmt::Formatter<'_> {
|
impl HirWrite for fmt::Formatter<'_> {}
|
||||||
fn start_location_link(&mut self, _: ModuleDefId) {}
|
|
||||||
fn end_location_link(&mut self) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct HirFormatter<'a> {
|
pub struct HirFormatter<'a> {
|
||||||
pub db: &'a dyn HirDatabase,
|
pub db: &'a dyn HirDatabase,
|
||||||
|
@ -885,6 +878,13 @@ impl HirDisplay for Ty {
|
||||||
TyKind::FnDef(def, parameters) => {
|
TyKind::FnDef(def, parameters) => {
|
||||||
let def = from_chalk(db, *def);
|
let def = from_chalk(db, *def);
|
||||||
let sig = db.callable_item_signature(def).substitute(Interner, parameters);
|
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());
|
f.start_location_link(def.into());
|
||||||
match def {
|
match def {
|
||||||
CallableDefId::FunctionId(ff) => {
|
CallableDefId::FunctionId(ff) => {
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
@ -1878,7 +1878,6 @@ where
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn add_function_with_fn_arg() {
|
fn add_function_with_fn_arg() {
|
||||||
// FIXME: The argument in `bar` is wrong.
|
|
||||||
check_assist(
|
check_assist(
|
||||||
generate_function,
|
generate_function,
|
||||||
r"
|
r"
|
||||||
|
@ -1899,7 +1898,7 @@ fn foo() {
|
||||||
bar(Baz::new);
|
bar(Baz::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bar(new: fn) ${0:-> _} {
|
fn bar(new: fn() -> Baz) ${0:-> _} {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
",
|
",
|
||||||
|
|
Loading…
Reference in a new issue