From 4c5fd19ee5734992344827b11c0d0e864b020b25 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 7 May 2023 09:42:50 +0200 Subject: [PATCH] Render places in capture inlay hints --- crates/hir-ty/src/display.rs | 1 - crates/hir-ty/src/infer/closure.rs | 18 +----------------- crates/hir/src/lib.rs | 15 ++++++++------- crates/ide/src/hover/render.rs | 13 ++++++++++--- crates/ide/src/inlay_hints/closure_captures.rs | 5 ++--- 5 files changed, 21 insertions(+), 31 deletions(-) diff --git a/crates/hir-ty/src/display.rs b/crates/hir-ty/src/display.rs index 22326990299c..3cfe78141d43 100644 --- a/crates/hir-ty/src/display.rs +++ b/crates/hir-ty/src/display.rs @@ -304,7 +304,6 @@ pub struct HirDisplayWrapper<'a, T> { pub enum ClosureStyle { /// `impl FnX(i32, i32) -> i32`, where `FnX` is the most special trait between `Fn`, `FnMut`, `FnOnce` that the /// closure implements. This is the default. - // FIXME: Allow rendering non capturing closures as plain function pointers? ImplFn, /// `|i32, i32| -> i32` RANotation, diff --git a/crates/hir-ty/src/infer/closure.rs b/crates/hir-ty/src/infer/closure.rs index a2c72e5751df..6d03c76eb6ab 100644 --- a/crates/hir-ty/src/infer/closure.rs +++ b/crates/hir-ty/src/infer/closure.rs @@ -170,23 +170,7 @@ impl CapturedItem { self.kind } - pub fn display_kind(&self) -> &'static str { - match self.kind { - CaptureKind::ByRef(k) => match k { - BorrowKind::Shared => "immutable borrow", - BorrowKind::Shallow => { - never!("shallow borrow should not happen in closure captures"); - "shallow borrow" - }, - BorrowKind::Unique => "unique immutable borrow ([read more](https://doc.rust-lang.org/stable/reference/types/closure.html#unique-immutable-borrows-in-captures))", - BorrowKind::Mut { .. } => "mutable borrow", - }, - CaptureKind::ByValue => "move", - } - } - - pub fn display_place(&self, owner: ClosureId, db: &dyn HirDatabase) -> String { - let owner = db.lookup_intern_closure(owner.into()).0; + pub fn display_place(&self, owner: DefWithBodyId, db: &dyn HirDatabase) -> String { let body = db.body(owner); let mut result = body[self.place.local].name.to_string(); let mut field_need_paren = false; diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 84608777054f..1fac95ae5e37 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -3214,7 +3214,11 @@ impl Closure { let owner = db.lookup_intern_closure((self.id).into()).0; let infer = &db.infer(owner); let info = infer.closure_info(&self.id); - info.0.iter().cloned().map(|capture| ClosureCapture { owner, capture }).collect() + info.0 + .iter() + .cloned() + .map(|capture| ClosureCapture { owner, closure: self.id, capture }) + .collect() } pub fn fn_trait(&self, db: &dyn HirDatabase) -> FnTrait { @@ -3228,6 +3232,7 @@ impl Closure { #[derive(Clone, Debug, PartialEq, Eq)] pub struct ClosureCapture { owner: DefWithBodyId, + closure: ClosureId, capture: hir_ty::CapturedItem, } @@ -3251,12 +3256,8 @@ impl ClosureCapture { } } - pub fn display_kind(&self) -> &'static str { - self.capture.display_kind() - } - - pub fn display_place(&self, owner: ClosureId, db: &dyn HirDatabase) -> String { - self.capture.display_place(owner, db) + pub fn display_place(&self, db: &dyn HirDatabase) -> String { + self.capture.display_place(self.owner, db) } } diff --git a/crates/ide/src/hover/render.rs b/crates/ide/src/hover/render.rs index 8f8cdcce515c..9bbedec44a2d 100644 --- a/crates/ide/src/hover/render.rs +++ b/crates/ide/src/hover/render.rs @@ -3,7 +3,8 @@ use std::fmt::Display; use either::Either; use hir::{ - Adt, AsAssocItem, AttributeTemplate, HasAttrs, HasSource, HirDisplay, Semantics, TypeInfo, + Adt, AsAssocItem, AttributeTemplate, CaptureKind, HasAttrs, HasSource, HirDisplay, Semantics, + TypeInfo, }; use ide_db::{ base_db::SourceDatabase, @@ -54,8 +55,14 @@ pub(super) fn closure_expr( let mut captures = c .captured_items(sema.db) .into_iter() - .map(|x| { - format!("* `{}` by {}", x.display_place(c.clone().into(), sema.db), x.display_kind()) + .map(|it| { + let borrow_kind= match it.kind() { + CaptureKind::SharedRef => "immutable borrow", + CaptureKind::UniqueSharedRef => "unique immutable borrow ([read more](https://doc.rust-lang.org/stable/reference/types/closure.html#unique-immutable-borrows-in-captures))", + CaptureKind::MutableRef => "mutable borrow", + CaptureKind::Move => "move", + }; + format!("* `{}` by {}", it.display_place(sema.db), borrow_kind) }) .join("\n"); if captures.trim().is_empty() { diff --git a/crates/ide/src/inlay_hints/closure_captures.rs b/crates/ide/src/inlay_hints/closure_captures.rs index d7e5332c41da..60c4fe411f4f 100644 --- a/crates/ide/src/inlay_hints/closure_captures.rs +++ b/crates/ide/src/inlay_hints/closure_captures.rs @@ -65,7 +65,7 @@ pub(super) fn hints( hir::CaptureKind::MutableRef => "&mut ", hir::CaptureKind::Move => "", }, - local.name(sema.db) + capture.display_place(sema.db) ), None, source.name().and_then(|name| sema.original_range_opt(name.syntax())), @@ -156,13 +156,12 @@ fn main() { // ^ ) &mut baz; }; - // FIXME: &mut qux should be &unique qux || { // ^ move // ^ ( // ^ &mut baz // ^ , $ -// ^ &mut qux +// ^ &mut *qux // ^ ) baz = NonCopy; *qux = NonCopy;