From 73e49493bd32ff12dbb70cde7d3fee9cf1dec7cd Mon Sep 17 00:00:00 2001 From: Jeroen Vannevel Date: Tue, 15 Feb 2022 14:39:22 +0000 Subject: [PATCH 01/12] rough, but appears to work --- Cargo.toml | 2 +- crates/hir/src/display.rs | 4 +++ crates/hir_def/src/item_tree/pretty.rs | 6 ++--- crates/hir_def/src/type_ref.rs | 18 +++++++++---- crates/hir_ty/src/display.rs | 26 +++++++++++++++---- crates/hir_ty/src/lower.rs | 2 +- crates/ide/src/hover.rs | 1 + crates/ide/src/hover/render.rs | 1 + crates/ide/src/hover/tests.rs | 36 ++++++++++++++++++++++++++ 9 files changed, 81 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3f83041bec..1b322acdc6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ exclude = ["crates/proc_macro_test/imp"] [profile.dev] # Disabling debug info speeds up builds a bunch, # and we don't rely on it for debugging that much. -debug = 0 +debug = 2 [profile.dev.package] # These speed up local tests. diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index 1e949771ea..19402c1c25 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs @@ -22,7 +22,9 @@ use crate::{ impl HirDisplay for Function { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { + println!("Formatting for Function"); let data = f.db.function_data(self.id); + println!("data: {:?}", &data); write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; if data.is_default() { write!(f, "default ")?; @@ -461,6 +463,7 @@ impl HirDisplay for Trait { impl HirDisplay for TypeAlias { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { + println!("Formatting for TypeAlias"); write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; let data = f.db.type_alias_data(self.id); write!(f, "type {}", data.name)?; @@ -468,6 +471,7 @@ impl HirDisplay for TypeAlias { write!(f, ": ")?; f.write_joined(&data.bounds, " + ")?; } + println!("type_ref: {:?}", &data.type_ref); if let Some(ty) = &data.type_ref { write!(f, " = ")?; ty.hir_fmt(f)?; diff --git a/crates/hir_def/src/item_tree/pretty.rs b/crates/hir_def/src/item_tree/pretty.rs index eaaff5a21f..4d57e484c1 100644 --- a/crates/hir_def/src/item_tree/pretty.rs +++ b/crates/hir_def/src/item_tree/pretty.rs @@ -496,11 +496,11 @@ impl<'a> Printer<'a> { let (ret, args) = args_and_ret.split_last().expect("TypeRef::Fn is missing return type"); w!(self, "fn("); - for (i, arg) in args.iter().enumerate() { + for (i, (name, typeref)) in args.iter().enumerate() { if i != 0 { w!(self, ", "); } - self.print_type_ref(arg); + self.print_type_ref(&typeref); } if *varargs { if !args.is_empty() { @@ -509,7 +509,7 @@ impl<'a> Printer<'a> { w!(self, "..."); } w!(self, ") -> "); - self.print_type_ref(ret); + self.print_type_ref(&ret.1); } TypeRef::Macro(_ast_id) => { w!(self, ""); diff --git a/crates/hir_def/src/type_ref.rs b/crates/hir_def/src/type_ref.rs index cfc69feccc..3c2e036ab9 100644 --- a/crates/hir_def/src/type_ref.rs +++ b/crates/hir_def/src/type_ref.rs @@ -3,7 +3,7 @@ use hir_expand::{name::Name, AstId, InFile}; use std::convert::TryInto; -use syntax::ast; +use syntax::{ast, AstNode}; use crate::{body::LowerCtx, intern::Interned, path::Path}; @@ -89,7 +89,7 @@ pub enum TypeRef { Array(Box, ConstScalar), Slice(Box), /// A fn pointer. Last element of the vector is the return type. - Fn(Vec, bool /*varargs*/), + Fn(Vec<(Option, TypeRef)>, bool /*varargs*/), // For ImplTrait(Vec>), DynTrait(Vec>), @@ -188,11 +188,16 @@ impl TypeRef { is_varargs = param.dotdotdot_token().is_some(); } - pl.params().map(|p| p.ty()).map(|it| TypeRef::from_ast_opt(ctx, it)).collect() + pl.params().map(|p| (p.pat(), p.ty())).map(|it| { + println!("{it:?}"); + let type_ref = TypeRef::from_ast_opt(ctx, it.1); + let name = it.0.unwrap().syntax().text().to_string(); + (Some(name), type_ref) + }).collect() } else { Vec::new() }; - params.push(ret_ty); + params.push((None, ret_ty)); TypeRef::Fn(params, is_varargs) } // for types are close enough for our purposes to the inner type for now... @@ -230,7 +235,10 @@ impl TypeRef { fn go(type_ref: &TypeRef, f: &mut impl FnMut(&TypeRef)) { f(type_ref); match type_ref { - TypeRef::Fn(types, _) | TypeRef::Tuple(types) => { + TypeRef::Fn(types, _) => { + types.iter().for_each(|t| go(&t.1, f)) + } + TypeRef::Tuple(types) => { types.iter().for_each(|t| go(t, f)) } TypeRef::RawPtr(type_ref, _) diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index f02f4ac024..bca628a5c8 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -239,6 +239,7 @@ where T: HirDisplay, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + println!("formatting.."); match self.t.hir_fmt(&mut HirFormatter { db: self.db, fmt: f, @@ -341,6 +342,9 @@ impl HirDisplay for Ty { return write!(f, "{}", TYPE_HINT_TRUNCATION); } + let interner_kind = self.kind(Interner); + println!("interner kind: {interner_kind:?}"); + match self.kind(Interner) { TyKind::Never => write!(f, "!")?, TyKind::Str => write!(f, "str")?, @@ -1094,15 +1098,27 @@ impl HirDisplay for TypeRef { inner.hir_fmt(f)?; write!(f, "]")?; } - TypeRef::Fn(tys, is_varargs) => { - // FIXME: Function pointer qualifiers. + TypeRef::Fn(parameters, is_varargs) => { write!(f, "fn(")?; - f.write_joined(&tys[..tys.len() - 1], ", ")?; + for index in 0..parameters.len() - 1 { + let (param_name,param_type) = ¶meters[index]; + match param_name { + Some(name) => { + write!(f, "{}: ", name)?; + param_type.hir_fmt(f)?; + }, + None => write!(f, " : {:?}", param_type)?, + }; + + if index != parameters.len() - 2 { + write!(f, ", ")?; + } + } if *is_varargs { - write!(f, "{}...", if tys.len() == 1 { "" } else { ", " })?; + write!(f, "{}...", if parameters.len() == 1 { "" } else { ", " })?; } write!(f, ")")?; - let ret_ty = tys.last().unwrap(); + let ret_ty = ¶meters.last().unwrap().1; match ret_ty { TypeRef::Tuple(tup) if tup.is_empty() => {} _ => { diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 55b1a67ea7..4873693e8c 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -201,7 +201,7 @@ impl<'a> TyLoweringContext<'a> { TypeRef::Placeholder => TyKind::Error.intern(Interner), TypeRef::Fn(params, is_varargs) => { let substs = self.with_shifted_in(DebruijnIndex::ONE, |ctx| { - Substitution::from_iter(Interner, params.iter().map(|tr| ctx.lower_ty(tr))) + Substitution::from_iter(Interner, params.iter().map(|tr| ctx.lower_ty(&tr.1))) }); TyKind::Function(FnPointer { num_binders: 0, // FIXME lower `for<'a> fn()` correctly diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 0eba0b09ba..550e1c1543 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -172,6 +172,7 @@ pub(crate) fn hover_for_definition( Definition::BuiltinType(_) => Some(FamousDefs(sema, sema.scope(node).krate())), _ => None, }; + println!("definition: {definition:?}"); if let Some(markup) = render::definition(sema.db, definition, famous_defs.as_ref(), config) { let mut res = HoverResult::default(); res.markup = render::process_markup(sema.db, definition, &markup, config); diff --git a/crates/ide/src/hover/render.rs b/crates/ide/src/hover/render.rs index f94348ec58..0a5c335661 100644 --- a/crates/ide/src/hover/render.rs +++ b/crates/ide/src/hover/render.rs @@ -411,6 +411,7 @@ where D: HasAttrs + HirDisplay, { let label = def.display(db).to_string(); + println!("label: {label:?}"); let docs = def.attrs(db).docs(); (label, docs) } diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index ed76c84ab4..ed9f6d507c 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -1310,6 +1310,42 @@ fn test_hover_function_show_qualifiers() { ); } +#[test] +fn test_hover_function_show_types() { + check( + r#"fn foo$0(a: i32, b:i32) -> i32 { 0 }"#, + expect![[r#" + *foo* + + ```rust + test + ``` + + ```rust + fn foo(a: i32, b: i32) -> i32 + ``` + "#]], + ); +} + +#[test] +fn test_hover_function_pointer_show_types() { + check( + r#"type foo$0 = fn(a: i32, b: i32) -> i32;"#, + expect![[r#" + *foo* + + ```rust + test + ``` + + ```rust + type foo = fn(a: i32, b: i32) -> i32 + ``` + "#]], + ); +} + #[test] fn test_hover_trait_show_qualifiers() { check_actions( From c450d0ce411acaf48ff7aabe393020df6e60f378 Mon Sep 17 00:00:00 2001 From: Jeroen Vannevel Date: Tue, 15 Feb 2022 14:47:23 +0000 Subject: [PATCH 02/12] cleanup --- Cargo.toml | 2 +- crates/hir/src/display.rs | 4 ---- crates/hir_def/src/item_tree/pretty.rs | 6 +++--- crates/hir_def/src/type_ref.rs | 5 ++--- crates/hir_ty/src/display.rs | 6 +----- crates/ide/src/hover.rs | 1 - crates/ide/src/hover/render.rs | 1 - 7 files changed, 7 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1b322acdc6..3f83041bec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ exclude = ["crates/proc_macro_test/imp"] [profile.dev] # Disabling debug info speeds up builds a bunch, # and we don't rely on it for debugging that much. -debug = 2 +debug = 0 [profile.dev.package] # These speed up local tests. diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs index 19402c1c25..1e949771ea 100644 --- a/crates/hir/src/display.rs +++ b/crates/hir/src/display.rs @@ -22,9 +22,7 @@ use crate::{ impl HirDisplay for Function { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { - println!("Formatting for Function"); let data = f.db.function_data(self.id); - println!("data: {:?}", &data); write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; if data.is_default() { write!(f, "default ")?; @@ -463,7 +461,6 @@ impl HirDisplay for Trait { impl HirDisplay for TypeAlias { fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { - println!("Formatting for TypeAlias"); write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; let data = f.db.type_alias_data(self.id); write!(f, "type {}", data.name)?; @@ -471,7 +468,6 @@ impl HirDisplay for TypeAlias { write!(f, ": ")?; f.write_joined(&data.bounds, " + ")?; } - println!("type_ref: {:?}", &data.type_ref); if let Some(ty) = &data.type_ref { write!(f, " = ")?; ty.hir_fmt(f)?; diff --git a/crates/hir_def/src/item_tree/pretty.rs b/crates/hir_def/src/item_tree/pretty.rs index 4d57e484c1..0df6e97dd4 100644 --- a/crates/hir_def/src/item_tree/pretty.rs +++ b/crates/hir_def/src/item_tree/pretty.rs @@ -493,10 +493,10 @@ impl<'a> Printer<'a> { w!(self, "]"); } TypeRef::Fn(args_and_ret, varargs) => { - let (ret, args) = + let ((_, return_type), args) = args_and_ret.split_last().expect("TypeRef::Fn is missing return type"); w!(self, "fn("); - for (i, (name, typeref)) in args.iter().enumerate() { + for (i, (_, typeref)) in args.iter().enumerate() { if i != 0 { w!(self, ", "); } @@ -509,7 +509,7 @@ impl<'a> Printer<'a> { w!(self, "..."); } w!(self, ") -> "); - self.print_type_ref(&ret.1); + self.print_type_ref(&return_type); } TypeRef::Macro(_ast_id) => { w!(self, ""); diff --git a/crates/hir_def/src/type_ref.rs b/crates/hir_def/src/type_ref.rs index 3c2e036ab9..8667e0a7d6 100644 --- a/crates/hir_def/src/type_ref.rs +++ b/crates/hir_def/src/type_ref.rs @@ -189,10 +189,9 @@ impl TypeRef { } pl.params().map(|p| (p.pat(), p.ty())).map(|it| { - println!("{it:?}"); let type_ref = TypeRef::from_ast_opt(ctx, it.1); - let name = it.0.unwrap().syntax().text().to_string(); - (Some(name), type_ref) + let name = if it.0.is_some() { Some(it.0.unwrap().syntax().text().to_string()) } else { None }; + (name, type_ref) }).collect() } else { Vec::new() diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index bca628a5c8..ad330d6261 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -239,7 +239,6 @@ where T: HirDisplay, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - println!("formatting.."); match self.t.hir_fmt(&mut HirFormatter { db: self.db, fmt: f, @@ -341,10 +340,7 @@ impl HirDisplay for Ty { if f.should_truncate() { return write!(f, "{}", TYPE_HINT_TRUNCATION); } - - let interner_kind = self.kind(Interner); - println!("interner kind: {interner_kind:?}"); - + match self.kind(Interner) { TyKind::Never => write!(f, "!")?, TyKind::Str => write!(f, "str")?, diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 550e1c1543..0eba0b09ba 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -172,7 +172,6 @@ pub(crate) fn hover_for_definition( Definition::BuiltinType(_) => Some(FamousDefs(sema, sema.scope(node).krate())), _ => None, }; - println!("definition: {definition:?}"); if let Some(markup) = render::definition(sema.db, definition, famous_defs.as_ref(), config) { let mut res = HoverResult::default(); res.markup = render::process_markup(sema.db, definition, &markup, config); diff --git a/crates/ide/src/hover/render.rs b/crates/ide/src/hover/render.rs index 0a5c335661..f94348ec58 100644 --- a/crates/ide/src/hover/render.rs +++ b/crates/ide/src/hover/render.rs @@ -411,7 +411,6 @@ where D: HasAttrs + HirDisplay, { let label = def.display(db).to_string(); - println!("label: {label:?}"); let docs = def.attrs(db).docs(); (label, docs) } From 3bba811e925e8eb5d53a58b7cb8a10fa29f71caa Mon Sep 17 00:00:00 2001 From: Jeroen Vannevel Date: Tue, 15 Feb 2022 14:47:51 +0000 Subject: [PATCH 03/12] fmt --- crates/hir_def/src/type_ref.rs | 25 ++++++++++++++----------- crates/hir_ty/src/display.rs | 6 +++--- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/crates/hir_def/src/type_ref.rs b/crates/hir_def/src/type_ref.rs index 8667e0a7d6..dcc43c1fad 100644 --- a/crates/hir_def/src/type_ref.rs +++ b/crates/hir_def/src/type_ref.rs @@ -188,11 +188,18 @@ impl TypeRef { is_varargs = param.dotdotdot_token().is_some(); } - pl.params().map(|p| (p.pat(), p.ty())).map(|it| { - let type_ref = TypeRef::from_ast_opt(ctx, it.1); - let name = if it.0.is_some() { Some(it.0.unwrap().syntax().text().to_string()) } else { None }; - (name, type_ref) - }).collect() + pl.params() + .map(|p| (p.pat(), p.ty())) + .map(|it| { + let type_ref = TypeRef::from_ast_opt(ctx, it.1); + let name = if it.0.is_some() { + Some(it.0.unwrap().syntax().text().to_string()) + } else { + None + }; + (name, type_ref) + }) + .collect() } else { Vec::new() }; @@ -234,12 +241,8 @@ impl TypeRef { fn go(type_ref: &TypeRef, f: &mut impl FnMut(&TypeRef)) { f(type_ref); match type_ref { - TypeRef::Fn(types, _) => { - types.iter().for_each(|t| go(&t.1, f)) - } - TypeRef::Tuple(types) => { - types.iter().for_each(|t| go(t, f)) - } + TypeRef::Fn(types, _) => types.iter().for_each(|t| go(&t.1, f)), + TypeRef::Tuple(types) => types.iter().for_each(|t| go(t, f)), TypeRef::RawPtr(type_ref, _) | TypeRef::Reference(type_ref, ..) | TypeRef::Array(type_ref, _) diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index ad330d6261..4b077c2c8a 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -340,7 +340,7 @@ impl HirDisplay for Ty { if f.should_truncate() { return write!(f, "{}", TYPE_HINT_TRUNCATION); } - + match self.kind(Interner) { TyKind::Never => write!(f, "!")?, TyKind::Str => write!(f, "str")?, @@ -1097,12 +1097,12 @@ impl HirDisplay for TypeRef { TypeRef::Fn(parameters, is_varargs) => { write!(f, "fn(")?; for index in 0..parameters.len() - 1 { - let (param_name,param_type) = ¶meters[index]; + let (param_name, param_type) = ¶meters[index]; match param_name { Some(name) => { write!(f, "{}: ", name)?; param_type.hir_fmt(f)?; - }, + } None => write!(f, " : {:?}", param_type)?, }; From 0a80cc82b1220eddcfd00327f5debcbb7dce975c Mon Sep 17 00:00:00 2001 From: Jeroen Vannevel Date: Tue, 15 Feb 2022 14:55:21 +0000 Subject: [PATCH 04/12] cleaning --- crates/hir_def/src/type_ref.rs | 4 +++- crates/hir_ty/src/display.rs | 3 ++- crates/hir_ty/src/lower.rs | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/hir_def/src/type_ref.rs b/crates/hir_def/src/type_ref.rs index dcc43c1fad..01aca8f1de 100644 --- a/crates/hir_def/src/type_ref.rs +++ b/crates/hir_def/src/type_ref.rs @@ -241,7 +241,9 @@ impl TypeRef { fn go(type_ref: &TypeRef, f: &mut impl FnMut(&TypeRef)) { f(type_ref); match type_ref { - TypeRef::Fn(types, _) => types.iter().for_each(|t| go(&t.1, f)), + TypeRef::Fn(params, _) => { + params.iter().for_each(|(_, param_type)| go(¶m_type, f)) + } TypeRef::Tuple(types) => types.iter().for_each(|t| go(t, f)), TypeRef::RawPtr(type_ref, _) | TypeRef::Reference(type_ref, ..) diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 4b077c2c8a..377821b376 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -1103,9 +1103,10 @@ impl HirDisplay for TypeRef { write!(f, "{}: ", name)?; param_type.hir_fmt(f)?; } - None => write!(f, " : {:?}", param_type)?, + None => param_type.hir_fmt(f)?, }; + // Last index contains the return type so we stop writing commas on the second-to-last index if index != parameters.len() - 2 { write!(f, ", ")?; } diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 4873693e8c..a140dd4057 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -201,7 +201,7 @@ impl<'a> TyLoweringContext<'a> { TypeRef::Placeholder => TyKind::Error.intern(Interner), TypeRef::Fn(params, is_varargs) => { let substs = self.with_shifted_in(DebruijnIndex::ONE, |ctx| { - Substitution::from_iter(Interner, params.iter().map(|tr| ctx.lower_ty(&tr.1))) + Substitution::from_iter(Interner, params.iter().map(|(_, tr)| ctx.lower_ty(tr))) }); TyKind::Function(FnPointer { num_binders: 0, // FIXME lower `for<'a> fn()` correctly From e1df78820ee5f4c3981035e13c6d6ec305974822 Mon Sep 17 00:00:00 2001 From: Jeroen Vannevel Date: Tue, 15 Feb 2022 14:58:06 +0000 Subject: [PATCH 05/12] removed unwrap --- crates/hir_ty/src/display.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 377821b376..3644bbd30d 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -1115,12 +1115,13 @@ impl HirDisplay for TypeRef { write!(f, "{}...", if parameters.len() == 1 { "" } else { ", " })?; } write!(f, ")")?; - let ret_ty = ¶meters.last().unwrap().1; - match ret_ty { - TypeRef::Tuple(tup) if tup.is_empty() => {} - _ => { - write!(f, " -> ")?; - ret_ty.hir_fmt(f)?; + if let Some((_, ret_ty)) = ¶meters.last() { + match ret_ty { + TypeRef::Tuple(tup) if tup.is_empty() => {} + _ => { + write!(f, " -> ")?; + ret_ty.hir_fmt(f)?; + } } } } From c8cd7a68b3ee3bc2c7de4d4287f554f78c63846a Mon Sep 17 00:00:00 2001 From: Jeroen Vannevel Date: Tue, 15 Feb 2022 19:12:23 +0000 Subject: [PATCH 06/12] use Name instead of String --- crates/hir_def/src/type_ref.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/crates/hir_def/src/type_ref.rs b/crates/hir_def/src/type_ref.rs index 01aca8f1de..05d139219c 100644 --- a/crates/hir_def/src/type_ref.rs +++ b/crates/hir_def/src/type_ref.rs @@ -1,9 +1,12 @@ //! HIR for references to types. Paths in these are not yet resolved. They can //! be directly created from an ast::TypeRef, without further queries. -use hir_expand::{name::Name, AstId, InFile}; +use hir_expand::{ + name::{AsName, Name}, + AstId, InFile, +}; use std::convert::TryInto; -use syntax::{ast, AstNode}; +use syntax::ast::{self, HasName}; use crate::{body::LowerCtx, intern::Interned, path::Path}; @@ -89,7 +92,7 @@ pub enum TypeRef { Array(Box, ConstScalar), Slice(Box), /// A fn pointer. Last element of the vector is the return type. - Fn(Vec<(Option, TypeRef)>, bool /*varargs*/), + Fn(Vec<(Option, TypeRef)>, bool /*varargs*/), // For ImplTrait(Vec>), DynTrait(Vec>), @@ -192,10 +195,11 @@ impl TypeRef { .map(|p| (p.pat(), p.ty())) .map(|it| { let type_ref = TypeRef::from_ast_opt(ctx, it.1); - let name = if it.0.is_some() { - Some(it.0.unwrap().syntax().text().to_string()) - } else { - None + let name = match it.0 { + Some(ast::Pat::IdentPat(it)) => Some( + it.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing), + ), + _ => None, }; (name, type_ref) }) From d985394ce2e5a7689e16fe1b940fc03adfb65805 Mon Sep 17 00:00:00 2001 From: Jeroen Vannevel Date: Tue, 15 Feb 2022 19:20:50 +0000 Subject: [PATCH 07/12] add test for function pointer without identifier --- crates/ide/src/hover/tests.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index ed9f6d507c..85fe4cc964 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -1346,6 +1346,24 @@ fn test_hover_function_pointer_show_types() { ); } +#[test] +fn test_hover_function_pointer_no_identifier_show_types() { + check( + r#"type foo$0 = fn(i32, _: i32) -> i32;"#, + expect![[r#" + *foo* + + ```rust + test + ``` + + ```rust + type foo = fn(i32, i32) -> i32 + ``` + "#]], + ); +} + #[test] fn test_hover_trait_show_qualifiers() { check_actions( From 842ffde43d2c4e4dff94b9f07675708f1ac4ca8c Mon Sep 17 00:00:00 2001 From: Jeroen Vannevel Date: Tue, 15 Feb 2022 19:22:36 +0000 Subject: [PATCH 08/12] test names --- crates/ide/src/hover/tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/ide/src/hover/tests.rs b/crates/ide/src/hover/tests.rs index 85fe4cc964..c5c531c30b 100644 --- a/crates/ide/src/hover/tests.rs +++ b/crates/ide/src/hover/tests.rs @@ -1329,7 +1329,7 @@ fn test_hover_function_show_types() { } #[test] -fn test_hover_function_pointer_show_types() { +fn test_hover_function_pointer_show_identifiers() { check( r#"type foo$0 = fn(a: i32, b: i32) -> i32;"#, expect![[r#" @@ -1347,7 +1347,7 @@ fn test_hover_function_pointer_show_types() { } #[test] -fn test_hover_function_pointer_no_identifier_show_types() { +fn test_hover_function_pointer_no_identifier() { check( r#"type foo$0 = fn(i32, _: i32) -> i32;"#, expect![[r#" From f083c868906eb863c61a751a7cd75559be4891cf Mon Sep 17 00:00:00 2001 From: Jeroen Vannevel Date: Tue, 15 Feb 2022 19:27:56 +0000 Subject: [PATCH 09/12] simplified write --- crates/hir_ty/src/display.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 3644bbd30d..57439bd75e 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -1098,13 +1098,11 @@ impl HirDisplay for TypeRef { write!(f, "fn(")?; for index in 0..parameters.len() - 1 { let (param_name, param_type) = ¶meters[index]; - match param_name { - Some(name) => { - write!(f, "{}: ", name)?; - param_type.hir_fmt(f)?; - } - None => param_type.hir_fmt(f)?, - }; + if let Some(name) = param_name { + write!(f, "{}: ", name)?; + } + + param_type.hir_fmt(f)?; // Last index contains the return type so we stop writing commas on the second-to-last index if index != parameters.len() - 2 { From 794105c18b490fbcbb81b8cf3689b2313438f19e Mon Sep 17 00:00:00 2001 From: Jeroen Vannevel Date: Tue, 15 Feb 2022 19:37:24 +0000 Subject: [PATCH 10/12] removed double map --- crates/hir_def/src/type_ref.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/hir_def/src/type_ref.rs b/crates/hir_def/src/type_ref.rs index 05d139219c..ee8ef6caa3 100644 --- a/crates/hir_def/src/type_ref.rs +++ b/crates/hir_def/src/type_ref.rs @@ -192,10 +192,9 @@ impl TypeRef { } pl.params() - .map(|p| (p.pat(), p.ty())) .map(|it| { - let type_ref = TypeRef::from_ast_opt(ctx, it.1); - let name = match it.0 { + let type_ref = TypeRef::from_ast_opt(ctx, it.ty()); + let name = match it.pat() { Some(ast::Pat::IdentPat(it)) => Some( it.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing), ), From d1fc208c9ceb959d616fa790fca5d282bc8d820d Mon Sep 17 00:00:00 2001 From: Jeroen Vannevel Date: Fri, 18 Feb 2022 09:12:52 +0000 Subject: [PATCH 11/12] re-added FIXME --- crates/hir_ty/src/display.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 57439bd75e..2ee4f5cf41 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -1095,6 +1095,7 @@ impl HirDisplay for TypeRef { write!(f, "]")?; } TypeRef::Fn(parameters, is_varargs) => { + // FIXME: Function pointer qualifiers. write!(f, "fn(")?; for index in 0..parameters.len() - 1 { let (param_name, param_type) = ¶meters[index]; From 9c6542f2097df1cfcc9491036ec607c6a2842070 Mon Sep 17 00:00:00 2001 From: Jeroen Vannevel Date: Mon, 21 Feb 2022 10:29:38 +0000 Subject: [PATCH 12/12] parameters.split_last() --- crates/hir_ty/src/display.rs | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 2ee4f5cf41..0e75ddeabc 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -1097,29 +1097,28 @@ impl HirDisplay for TypeRef { TypeRef::Fn(parameters, is_varargs) => { // FIXME: Function pointer qualifiers. write!(f, "fn(")?; - for index in 0..parameters.len() - 1 { - let (param_name, param_type) = ¶meters[index]; - if let Some(name) = param_name { - write!(f, "{}: ", name)?; - } + if let Some(((_, return_type), function_parameters)) = parameters.split_last() { + for index in 0..function_parameters.len() { + let (param_name, param_type) = &function_parameters[index]; + if let Some(name) = param_name { + write!(f, "{}: ", name)?; + } - param_type.hir_fmt(f)?; + param_type.hir_fmt(f)?; - // Last index contains the return type so we stop writing commas on the second-to-last index - if index != parameters.len() - 2 { - write!(f, ", ")?; + if index != function_parameters.len() - 1 { + write!(f, ", ")?; + } } - } - if *is_varargs { - write!(f, "{}...", if parameters.len() == 1 { "" } else { ", " })?; - } - write!(f, ")")?; - if let Some((_, ret_ty)) = ¶meters.last() { - match ret_ty { + if *is_varargs { + write!(f, "{}...", if parameters.len() == 1 { "" } else { ", " })?; + } + write!(f, ")")?; + match &return_type { TypeRef::Tuple(tup) if tup.is_empty() => {} _ => { write!(f, " -> ")?; - ret_ty.hir_fmt(f)?; + return_type.hir_fmt(f)?; } } }