diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 401feffff42..8dfceb35aff 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -198,28 +198,69 @@ fn get_bind_pat_hints( let descended = sema.descend_node_into_attributes(pat.clone()).pop(); let desc_pat = descended.as_ref().unwrap_or(pat); - let krate = sema.scope(desc_pat.syntax()).module().map(|it| it.krate()); - let famous_defs = FamousDefs(sema, krate); - let ty = sema.type_of_pat(&desc_pat.clone().into())?.original; if should_not_display_type_hint(sema, &pat, &ty) { return None; } + let krate = sema.scope(desc_pat.syntax()).module().map(|it| it.krate()); + let famous_defs = FamousDefs(sema, krate); + let label = hint_iterator(sema, &famous_defs, config, &ty); + + let label = match label { + Some(label) => label, + None => { + let ty = ty.display_truncated(sema.db, config.max_length).to_string(); + if Some(&*ty) == get_constructor_name(sema, pat).as_deref() { + return None; + } + ty.into() + } + }; + acc.push(InlayHint { range: match pat.name() { Some(name) => name.syntax().text_range(), None => pat.syntax().text_range(), }, kind: InlayKind::TypeHint, - label: hint_iterator(sema, &famous_defs, config, &ty) - .unwrap_or_else(|| ty.display_truncated(sema.db, config.max_length).to_string().into()), + label, }); Some(()) } +fn get_constructor_name(sema: &Semantics, pat: &ast::IdentPat) -> Option { + let it = pat.syntax().parent()?; + let expr = match_ast! { + match it { + ast::LetStmt(it) => it.initializer(), + ast::Condition(it) => it.expr(), + _ => None, + } + }; + + if let Some(expr) = expr { + let expr = sema.descend_node_into_attributes(expr.clone()).pop().unwrap_or(expr); + let expr = match expr { + ast::Expr::TryExpr(it) => it.expr(), + ast::Expr::AwaitExpr(it) => it.expr(), + expr => Some(expr), + }?; + let path = match expr { + ast::Expr::CallExpr(call) => match call.expr()? { + ast::Expr::PathExpr(p) => p.path(), + _ => None, + }, + _ => None, + }?; + let seg = path.qualifier()?.segment()?; + return Some(seg.to_string()); + } + None +} + /// Checks if the type is an Iterator from std::iter and replaces its hint with an `impl Iterator`. fn hint_iterator( sema: &Semantics, @@ -1234,6 +1275,51 @@ fn generic(t: T) { ); } + #[test] + fn skip_constructor_type_hints() { + check_types( + r#" +//- minicore: try +use core::ops::ControlFlow; + +struct Struct; +struct TupleStruct(); + +impl Struct { + fn new() -> Self { + Struct + } + fn try_new() -> ControlFlow<(), Self> { + ControlFlow::Continue(Struct) + } +} + +struct Generic(T); +impl Generic { + fn new() -> Self { + Generic(0) + } +} + +fn main() { + let strukt = Struct::new(); + let tuple_struct = TupleStruct(); + // ^^^^^^^^^^^^ TupleStruct + let generic0 = Generic::new(); + // ^^^^^^^^ Generic + let generic1 = Generic::::new(); + // ^^^^^^^^ Generic + let generic2 = >::new(); + // ^^^^^^^^ Generic +} + +fn fallible() -> ControlFlow<()> { + let strukt = Struct::try_new()?; +} +"#, + ); + } + #[test] fn closures() { check( diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs index 5e17047a407..045b4898e50 100644 --- a/crates/test_utils/src/minicore.rs +++ b/crates/test_utils/src/minicore.rs @@ -300,6 +300,17 @@ pub trait Try: FromResidual { #[lang = "branch"] fn branch(self) -> ControlFlow; } + + impl Try for ControlFlow { + type Output = C; + type Residual = ControlFlow; + fn from_output(output: Self::Output) -> Self {} + fn branch(self) -> ControlFlow {} + } + + impl FromResidual for ControlFlow { + fn from_residual(residual: ControlFlow) -> Self {} + } } pub use self::try_::{ControlFlow, FromResidual, Try}; // endregion:try