Skip to content

Worked issue #10446 hide "obvious" inlays #10864

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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 54 additions & 4 deletions crates/ide/src/inlay_hints.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use either::Either;
use hir::{known, Callable, HasVisibility, HirDisplay, Semantics, TypeInfo};
use hir::{known, Callable, CallableKind, HasVisibility, HirDisplay, Semantics, TypeInfo};
use ide_db::RootDatabase;
use ide_db::{base_db::FileRange, helpers::FamousDefs};
use itertools::Itertools;
Expand Down Expand Up @@ -374,7 +374,7 @@ fn should_not_display_type_hint(
for node in bind_pat.syntax().ancestors() {
match_ast! {
match node {
ast::LetStmt(it) => return it.ty().is_some(),
ast::LetStmt(it) => return is_unambigious_type(sema, it).is_some(),
ast::Param(it) => return it.ty().is_some(),
ast::MatchArm(_it) => return pat_is_enum_variant(db, bind_pat, pat_ty),
ast::IfExpr(it) => {
Expand All @@ -401,6 +401,35 @@ fn should_not_display_type_hint(
false
}

fn is_unambigious_type(sema: &Semantics<RootDatabase>, let_stmt: ast::LetStmt) -> Option<()> {
if let_stmt.ty().is_some() {
return Some(());
}
let initializer = let_stmt.initializer()?;
let (callable, _arg_list) = get_callable(sema, &initializer)?;
match callable.kind() {
CallableKind::TupleEnumVariant(_) => Some(()),
CallableKind::TupleStruct(_) => {
if let ast::Expr::CallExpr(call) = initializer {
if let ast::Expr::PathExpr(path_expr) = call.expr()? {
let args = path_expr.path()?.segments().find_map(|s| s.generic_arg_list())?;
let unambigious = args
.generic_args()
.filter_map(|a| match a {
ast::GenericArg::TypeArg(type_arg) => type_arg.ty(),
ast::GenericArg::AssocTypeArg(assoc_type_arg) => assoc_type_arg.ty(),
_ => None,
})
.all(|ty| !matches!(ty, ast::Type::InferType(_)));
return if unambigious { Some(()) } else { None };
}
}
None
}
_ => None,
}
}

fn should_hide_param_name_hint(
sema: &Semantics<RootDatabase>,
callable: &hir::Callable,
Expand Down Expand Up @@ -921,6 +950,29 @@ fn main() {
);
}

#[test]
fn hide_type_hints_tuple_generics() {
check_types(
r#"
struct Struct<T, U>(T, U);

enum Enum {
Variant(u32)
}

fn main() {
let foo = Struct::<u32, u32>(0, 0);
let foo = Enum::Variant(0);
let foo = Struct(1, 2);
//^^^ Struct<u32, u32>
bar(foo);
}

fn bar(foo: Struct<u32, u32>) {}
"#,
)
}

#[test]
fn type_hints_bindings_after_at() {
check_types(
Expand Down Expand Up @@ -1154,7 +1206,6 @@ struct Test { a: Option<u32>, b: u8 }

fn main() {
let test = Some(Test { a: Some(3), b: 1 });
//^^^^ Option<Test>
Comment on lines 1208 to -1157
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are undesirable, the idea behind this "hiding" is to only hide info if it is redundant. In this case here specifically, only Some is mentioned, so the fact that this local is of the type Option<Test> is not redundant and as such should always be shown.

if let None = &test {};
if let test = &test {};
//^^^^ &Option<Test>
Expand Down Expand Up @@ -1184,7 +1235,6 @@ struct Test { a: Option<u32>, b: u8 }

fn main() {
let test = Some(Test { a: Some(3), b: 1 });
//^^^^ Option<Test>
while let Some(Test { a: Some(x), b: y }) = &test {};
//^ &u32 ^ &u8
}"#,
Expand Down