Skip to content

Commit 761b127

Browse files
committed
Auto merge of #13806 - WaffleLapkin:typed_blåhaj, r=Veykril
fix: Skip adjustment hints if the adjustment is identity (`T` -> `T`) Supersedes #13765
2 parents 927d56a + 874ff2b commit 761b127

File tree

3 files changed

+62
-19
lines changed

3 files changed

+62
-19
lines changed

crates/hir/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -3691,6 +3691,13 @@ impl From<ItemInNs> for ScopeDef {
36913691
}
36923692
}
36933693

3694+
#[derive(Clone, Debug, PartialEq, Eq)]
3695+
pub struct Adjustment {
3696+
pub source: Type,
3697+
pub target: Type,
3698+
pub kind: Adjust,
3699+
}
3700+
36943701
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
36953702
pub enum Adjust {
36963703
/// Go from ! to any type.

crates/hir/src/semantics.rs

+34-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
mod source_to_def;
44

5-
use std::{cell::RefCell, fmt, iter, ops};
5+
use std::{cell::RefCell, fmt, iter, mem, ops};
66

77
use base_db::{FileId, FileRange};
88
use hir_def::{
@@ -29,7 +29,7 @@ use crate::{
2929
db::HirDatabase,
3030
semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx},
3131
source_analyzer::{resolve_hir_path, SourceAnalyzer},
32-
Access, Adjust, AutoBorrow, BindingMode, BuiltinAttr, Callable, ConstParam, Crate,
32+
Access, Adjust, Adjustment, AutoBorrow, BindingMode, BuiltinAttr, Callable, ConstParam, Crate,
3333
DeriveHelper, Field, Function, HasSource, HirFileId, Impl, InFile, Label, LifetimeParam, Local,
3434
Macro, Module, ModuleDef, Name, OverloadedDeref, Path, ScopeDef, ToolModule, Trait, Type,
3535
TypeAlias, TypeParam, VariantDef,
@@ -334,7 +334,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
334334
self.imp.resolve_trait(trait_)
335335
}
336336

337-
pub fn expr_adjustments(&self, expr: &ast::Expr) -> Option<Vec<Adjust>> {
337+
pub fn expr_adjustments(&self, expr: &ast::Expr) -> Option<Vec<Adjustment>> {
338338
self.imp.expr_adjustments(expr)
339339
}
340340

@@ -1067,26 +1067,42 @@ impl<'db> SemanticsImpl<'db> {
10671067
}
10681068
}
10691069

1070-
fn expr_adjustments(&self, expr: &ast::Expr) -> Option<Vec<Adjust>> {
1070+
fn expr_adjustments(&self, expr: &ast::Expr) -> Option<Vec<Adjustment>> {
10711071
let mutability = |m| match m {
10721072
hir_ty::Mutability::Not => Mutability::Shared,
10731073
hir_ty::Mutability::Mut => Mutability::Mut,
10741074
};
1075-
self.analyze(expr.syntax())?.expr_adjustments(self.db, expr).map(|it| {
1075+
1076+
let analyzer = self.analyze(expr.syntax())?;
1077+
1078+
let (mut source_ty, _) = analyzer.type_of_expr(self.db, expr)?;
1079+
1080+
analyzer.expr_adjustments(self.db, expr).map(|it| {
10761081
it.iter()
1077-
.map(|adjust| match adjust.kind {
1078-
hir_ty::Adjust::NeverToAny => Adjust::NeverToAny,
1079-
hir_ty::Adjust::Deref(Some(hir_ty::OverloadedDeref(m))) => {
1080-
Adjust::Deref(Some(OverloadedDeref(mutability(m))))
1081-
}
1082-
hir_ty::Adjust::Deref(None) => Adjust::Deref(None),
1083-
hir_ty::Adjust::Borrow(hir_ty::AutoBorrow::RawPtr(m)) => {
1084-
Adjust::Borrow(AutoBorrow::RawPtr(mutability(m)))
1085-
}
1086-
hir_ty::Adjust::Borrow(hir_ty::AutoBorrow::Ref(m)) => {
1087-
Adjust::Borrow(AutoBorrow::Ref(mutability(m)))
1088-
}
1089-
hir_ty::Adjust::Pointer(pc) => Adjust::Pointer(pc),
1082+
.map(|adjust| {
1083+
let target =
1084+
Type::new_with_resolver(self.db, &analyzer.resolver, adjust.target.clone());
1085+
let kind = match adjust.kind {
1086+
hir_ty::Adjust::NeverToAny => Adjust::NeverToAny,
1087+
hir_ty::Adjust::Deref(Some(hir_ty::OverloadedDeref(m))) => {
1088+
Adjust::Deref(Some(OverloadedDeref(mutability(m))))
1089+
}
1090+
hir_ty::Adjust::Deref(None) => Adjust::Deref(None),
1091+
hir_ty::Adjust::Borrow(hir_ty::AutoBorrow::RawPtr(m)) => {
1092+
Adjust::Borrow(AutoBorrow::RawPtr(mutability(m)))
1093+
}
1094+
hir_ty::Adjust::Borrow(hir_ty::AutoBorrow::Ref(m)) => {
1095+
Adjust::Borrow(AutoBorrow::Ref(mutability(m)))
1096+
}
1097+
hir_ty::Adjust::Pointer(pc) => Adjust::Pointer(pc),
1098+
};
1099+
1100+
// Update `source_ty` for the next adjustment
1101+
let source = mem::replace(&mut source_ty, target.clone());
1102+
1103+
let adjustment = Adjustment { source, target, kind };
1104+
1105+
adjustment
10901106
})
10911107
.collect()
10921108
})

crates/ide/src/inlay_hints/adjustment.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ pub(super) fn hints(
5959
});
6060
}
6161
for adjustment in adjustments.into_iter().rev() {
62+
if adjustment.source == adjustment.target {
63+
continue;
64+
}
65+
6266
// FIXME: Add some nicer tooltips to each of these
63-
let text = match adjustment {
67+
let text = match adjustment.kind {
6468
Adjust::NeverToAny if config.adjustment_hints == AdjustmentHints::Always => {
6569
"<never-to-any>"
6670
}
@@ -213,4 +217,20 @@ impl Trait for Struct {}
213217
"#,
214218
)
215219
}
220+
221+
#[test]
222+
fn never_to_never_is_never_shown() {
223+
check_with_config(
224+
InlayHintsConfig { adjustment_hints: AdjustmentHints::Always, ..DISABLED_CONFIG },
225+
r#"
226+
fn never() -> ! {
227+
return loop {};
228+
}
229+
230+
fn or_else() {
231+
let () = () else { return };
232+
}
233+
"#,
234+
)
235+
}
216236
}

0 commit comments

Comments
 (0)