Skip to content

Commit 9fbd202

Browse files
committed
Render places in capture inlay hints
1 parent abcdb4b commit 9fbd202

File tree

4 files changed

+21
-30
lines changed

4 files changed

+21
-30
lines changed

crates/hir-ty/src/infer/closure.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -170,23 +170,7 @@ impl CapturedItem {
170170
self.kind
171171
}
172172

173-
pub fn display_kind(&self) -> &'static str {
174-
match self.kind {
175-
CaptureKind::ByRef(k) => match k {
176-
BorrowKind::Shared => "immutable borrow",
177-
BorrowKind::Shallow => {
178-
never!("shallow borrow should not happen in closure captures");
179-
"shallow borrow"
180-
},
181-
BorrowKind::Unique => "unique immutable borrow ([read more](https://doc.rust-lang.org/stable/reference/types/closure.html#unique-immutable-borrows-in-captures))",
182-
BorrowKind::Mut { .. } => "mutable borrow",
183-
},
184-
CaptureKind::ByValue => "move",
185-
}
186-
}
187-
188-
pub fn display_place(&self, owner: ClosureId, db: &dyn HirDatabase) -> String {
189-
let owner = db.lookup_intern_closure(owner.into()).0;
173+
pub fn display_place(&self, owner: DefWithBodyId, db: &dyn HirDatabase) -> String {
190174
let body = db.body(owner);
191175
let mut result = body[self.place.local].name.to_string();
192176
let mut field_need_paren = false;

crates/hir/src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3214,7 +3214,11 @@ impl Closure {
32143214
let owner = db.lookup_intern_closure((self.id).into()).0;
32153215
let infer = &db.infer(owner);
32163216
let info = infer.closure_info(&self.id);
3217-
info.0.iter().cloned().map(|capture| ClosureCapture { owner, capture }).collect()
3217+
info.0
3218+
.iter()
3219+
.cloned()
3220+
.map(|capture| ClosureCapture { owner, closure: self.id, capture })
3221+
.collect()
32183222
}
32193223

32203224
pub fn fn_trait(&self, db: &dyn HirDatabase) -> FnTrait {
@@ -3228,6 +3232,7 @@ impl Closure {
32283232
#[derive(Clone, Debug, PartialEq, Eq)]
32293233
pub struct ClosureCapture {
32303234
owner: DefWithBodyId,
3235+
closure: ClosureId,
32313236
capture: hir_ty::CapturedItem,
32323237
}
32333238

@@ -3251,12 +3256,8 @@ impl ClosureCapture {
32513256
}
32523257
}
32533258

3254-
pub fn display_kind(&self) -> &'static str {
3255-
self.capture.display_kind()
3256-
}
3257-
3258-
pub fn display_place(&self, owner: ClosureId, db: &dyn HirDatabase) -> String {
3259-
self.capture.display_place(owner, db)
3259+
pub fn display_place(&self, db: &dyn HirDatabase) -> String {
3260+
self.capture.display_place(self.owner, db)
32603261
}
32613262
}
32623263

crates/ide/src/hover/render.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use std::fmt::Display;
33

44
use either::Either;
55
use hir::{
6-
Adt, AsAssocItem, AttributeTemplate, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo,
6+
Adt, AsAssocItem, AttributeTemplate, CaptureKind, HasAttrs, HasSource, HirDisplay, Semantics,
7+
TypeInfo,
78
};
89
use ide_db::{
910
base_db::SourceDatabase,
@@ -54,8 +55,14 @@ pub(super) fn closure_expr(
5455
let mut captures = c
5556
.captured_items(sema.db)
5657
.into_iter()
57-
.map(|x| {
58-
format!("* `{}` by {}", x.display_place(c.clone().into(), sema.db), x.display_kind())
58+
.map(|it| {
59+
let borrow_kind= match it.kind() {
60+
CaptureKind::SharedRef => "immutable borrow",
61+
CaptureKind::UniqueSharedRef => "unique immutable borrow ([read more](https://doc.rust-lang.org/stable/reference/types/closure.html#unique-immutable-borrows-in-captures))",
62+
CaptureKind::MutableRef => "mutable borrow",
63+
CaptureKind::Move => "move",
64+
};
65+
format!("* `{}` by {}", it.display_place(sema.db), borrow_kind)
5966
})
6067
.join("\n");
6168
if captures.trim().is_empty() {

crates/ide/src/inlay_hints/closure_captures.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub(super) fn hints(
6565
hir::CaptureKind::MutableRef => "&mut ",
6666
hir::CaptureKind::Move => "",
6767
},
68-
local.name(sema.db)
68+
capture.display_place(sema.db)
6969
),
7070
None,
7171
source.name().and_then(|name| sema.original_range_opt(name.syntax())),
@@ -156,13 +156,12 @@ fn main() {
156156
// ^ )
157157
&mut baz;
158158
};
159-
// FIXME: &mut qux should be &unique qux
160159
|| {
161160
// ^ move
162161
// ^ (
163162
// ^ &mut baz
164163
// ^ , $
165-
// ^ &mut qux
164+
// ^ &unique *qux
166165
// ^ )
167166
baz = NonCopy;
168167
*qux = NonCopy;

0 commit comments

Comments
 (0)