Skip to content

Commit d80e731

Browse files
bors[bot]Veykril
andauthored
Merge #10441
10441: feat: Hide type inlay hints for constructors r=Veykril a=Veykril Fixes #3022 bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents e28aa19 + fdfe191 commit d80e731

File tree

2 files changed

+140
-8
lines changed

2 files changed

+140
-8
lines changed

crates/ide/src/inlay_hints.rs

Lines changed: 129 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use either::Either;
22
use hir::{known, Callable, HasVisibility, HirDisplay, Semantics, TypeInfo};
33
use ide_db::RootDatabase;
44
use ide_db::{base_db::FileRange, helpers::FamousDefs};
5+
use itertools::Itertools;
56
use stdx::to_lower_snake_case;
67
use syntax::{
78
ast::{self, AstNode, HasArgList, HasName},
@@ -198,28 +199,99 @@ fn get_bind_pat_hints(
198199

199200
let descended = sema.descend_node_into_attributes(pat.clone()).pop();
200201
let desc_pat = descended.as_ref().unwrap_or(pat);
201-
let krate = sema.scope(desc_pat.syntax()).module().map(|it| it.krate());
202-
let famous_defs = FamousDefs(sema, krate);
203-
204202
let ty = sema.type_of_pat(&desc_pat.clone().into())?.original;
205203

206204
if should_not_display_type_hint(sema, &pat, &ty) {
207205
return None;
208206
}
209207

208+
let krate = sema.scope(desc_pat.syntax()).module().map(|it| it.krate());
209+
let famous_defs = FamousDefs(sema, krate);
210+
let label = hint_iterator(sema, &famous_defs, config, &ty);
211+
212+
let label = match label {
213+
Some(label) => label,
214+
None => {
215+
let ty_name = ty.display_truncated(sema.db, config.max_length).to_string();
216+
if is_named_constructor(sema, pat, &ty_name).is_some() {
217+
return None;
218+
}
219+
ty_name.into()
220+
}
221+
};
222+
210223
acc.push(InlayHint {
211224
range: match pat.name() {
212225
Some(name) => name.syntax().text_range(),
213226
None => pat.syntax().text_range(),
214227
},
215228
kind: InlayKind::TypeHint,
216-
label: hint_iterator(sema, &famous_defs, config, &ty)
217-
.unwrap_or_else(|| ty.display_truncated(sema.db, config.max_length).to_string().into()),
229+
label,
218230
});
219231

220232
Some(())
221233
}
222234

235+
fn is_named_constructor(
236+
sema: &Semantics<RootDatabase>,
237+
pat: &ast::IdentPat,
238+
ty_name: &str,
239+
) -> Option<()> {
240+
let it = pat.syntax().parent()?;
241+
let expr = match_ast! {
242+
match it {
243+
ast::LetStmt(it) => it.initializer(),
244+
ast::Condition(it) => it.expr(),
245+
_ => None,
246+
}
247+
};
248+
249+
if let Some(expr) = expr {
250+
let expr = sema.descend_node_into_attributes(expr.clone()).pop().unwrap_or(expr);
251+
let expr = match expr {
252+
ast::Expr::TryExpr(it) => it.expr(),
253+
ast::Expr::AwaitExpr(it) => it.expr(),
254+
expr => Some(expr),
255+
}?;
256+
let expr = match expr {
257+
ast::Expr::CallExpr(call) => match call.expr()? {
258+
ast::Expr::PathExpr(p) => p,
259+
_ => return None,
260+
},
261+
_ => return None,
262+
};
263+
let path = expr.path()?;
264+
265+
let callable = sema.type_of_expr(&ast::Expr::PathExpr(expr))?.original.as_callable(sema.db);
266+
let callable_kind = callable.map(|it| it.kind());
267+
if let Some(hir::CallableKind::TupleStruct(_) | hir::CallableKind::TupleEnumVariant(_)) =
268+
callable_kind
269+
{
270+
if let Some(seg) = path.segment() {
271+
if &seg.to_string() == ty_name {
272+
return Some(());
273+
}
274+
}
275+
}
276+
277+
let qual_seg = path.qualifier()?.segment()?;
278+
let name = match qual_seg.kind()? {
279+
ast::PathSegmentKind::Name(name_ref) => {
280+
match qual_seg.generic_arg_list().map(|it| it.generic_args()) {
281+
Some(generics) => format!("{}<{}>", name_ref, generics.format(", ")),
282+
None => name_ref.to_string(),
283+
}
284+
}
285+
ast::PathSegmentKind::Type { type_ref: Some(ty), trait_ref: None } => ty.to_string(),
286+
_ => return None,
287+
};
288+
if &name == ty_name {
289+
return Some(());
290+
}
291+
}
292+
None
293+
}
294+
223295
/// Checks if the type is an Iterator from std::iter and replaces its hint with an `impl Iterator<Item = Ty>`.
224296
fn hint_iterator(
225297
sema: &Semantics<RootDatabase>,
@@ -470,10 +542,12 @@ mod tests {
470542
max_length: None,
471543
};
472544

545+
#[track_caller]
473546
fn check(ra_fixture: &str) {
474547
check_with_config(TEST_CONFIG, ra_fixture);
475548
}
476549

550+
#[track_caller]
477551
fn check_params(ra_fixture: &str) {
478552
check_with_config(
479553
InlayHintsConfig {
@@ -486,6 +560,7 @@ mod tests {
486560
);
487561
}
488562

563+
#[track_caller]
489564
fn check_types(ra_fixture: &str) {
490565
check_with_config(
491566
InlayHintsConfig {
@@ -498,6 +573,7 @@ mod tests {
498573
);
499574
}
500575

576+
#[track_caller]
501577
fn check_chains(ra_fixture: &str) {
502578
check_with_config(
503579
InlayHintsConfig {
@@ -510,6 +586,7 @@ mod tests {
510586
);
511587
}
512588

589+
#[track_caller]
513590
fn check_with_config(config: InlayHintsConfig, ra_fixture: &str) {
514591
let (analysis, file_id) = fixture::file(&ra_fixture);
515592
let expected = extract_annotations(&*analysis.file_text(file_id).unwrap());
@@ -519,6 +596,7 @@ mod tests {
519596
assert_eq!(expected, actual, "\nExpected:\n{:#?}\n\nActual:\n{:#?}", expected, actual);
520597
}
521598

599+
#[track_caller]
522600
fn check_expect(config: InlayHintsConfig, ra_fixture: &str, expect: Expect) {
523601
let (analysis, file_id) = fixture::file(&ra_fixture);
524602
let inlay_hints = analysis.inlay_hints(&config, file_id).unwrap();
@@ -1191,11 +1269,12 @@ trait Display {}
11911269
trait Sync {}
11921270
11931271
fn main() {
1194-
let _v = Vec::<Box<&(dyn Display + Sync)>>::new();
1272+
// The block expression wrapping disables the constructor hint hiding logic
1273+
let _v = { Vec::<Box<&(dyn Display + Sync)>>::new() };
11951274
//^^ Vec<Box<&(dyn Display + Sync)>>
1196-
let _v = Vec::<Box<*const (dyn Display + Sync)>>::new();
1275+
let _v = { Vec::<Box<*const (dyn Display + Sync)>>::new() };
11971276
//^^ Vec<Box<*const (dyn Display + Sync)>>
1198-
let _v = Vec::<Box<dyn Display + Sync>>::new();
1277+
let _v = { Vec::<Box<dyn Display + Sync>>::new() };
11991278
//^^ Vec<Box<dyn Display + Sync>>
12001279
}
12011280
"#,
@@ -1234,6 +1313,48 @@ fn main() {
12341313
);
12351314
}
12361315

1316+
#[test]
1317+
fn skip_constructor_type_hints() {
1318+
check_types(
1319+
r#"
1320+
//- minicore: try
1321+
use core::ops::ControlFlow;
1322+
1323+
struct Struct;
1324+
struct TupleStruct();
1325+
1326+
impl Struct {
1327+
fn new() -> Self {
1328+
Struct
1329+
}
1330+
fn try_new() -> ControlFlow<(), Self> {
1331+
ControlFlow::Continue(Struct)
1332+
}
1333+
}
1334+
1335+
struct Generic<T>(T);
1336+
impl Generic<i32> {
1337+
fn new() -> Self {
1338+
Generic(0)
1339+
}
1340+
}
1341+
1342+
fn main() {
1343+
let strukt = Struct::new();
1344+
let tuple_struct = TupleStruct();
1345+
let generic0 = Generic::new();
1346+
// ^^^^^^^^ Generic<i32>
1347+
let generic1 = Generic::<i32>::new();
1348+
let generic2 = <Generic<i32>>::new();
1349+
}
1350+
1351+
fn fallible() -> ControlFlow<()> {
1352+
let strukt = Struct::try_new()?;
1353+
}
1354+
"#,
1355+
);
1356+
}
1357+
12371358
#[test]
12381359
fn closures() {
12391360
check(

crates/test_utils/src/minicore.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,17 @@ pub mod ops {
300300
#[lang = "branch"]
301301
fn branch(self) -> ControlFlow<Self::Residual, Self::Output>;
302302
}
303+
304+
impl<B, C> Try for ControlFlow<B, C> {
305+
type Output = C;
306+
type Residual = ControlFlow<B, convert::Infallible>;
307+
fn from_output(output: Self::Output) -> Self {}
308+
fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {}
309+
}
310+
311+
impl<B, C> FromResidual for ControlFlow<B, C> {
312+
fn from_residual(residual: ControlFlow<B, convert::Infallible>) -> Self {}
313+
}
303314
}
304315
pub use self::try_::{ControlFlow, FromResidual, Try};
305316
// endregion:try

0 commit comments

Comments
 (0)