Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 77ab568

Browse files
Preserve all spans for closure captures, not just one
This is important for the "convert closure to fn" assist, as it needs to find and modify the places the captures are used.
1 parent 12faedd commit 77ab568

File tree

4 files changed

+456
-179
lines changed

4 files changed

+456
-179
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/infer.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ use crate::{
5959
generics::Generics,
6060
infer::{coerce::CoerceMany, unify::InferenceTable},
6161
lower::ImplTraitLoweringMode,
62+
mir::MirSpan,
6263
to_assoc_type_id,
6364
traits::FnTrait,
6465
utils::{InTypeConstIdMetadata, UnevaluatedConstEvaluatorFolder},
@@ -553,6 +554,12 @@ pub(crate) struct InferenceContext<'a> {
553554

554555
// fields related to closure capture
555556
current_captures: Vec<CapturedItemWithoutTy>,
557+
/// A stack that has an entry for each projection in the current capture.
558+
///
559+
/// For example, in `a.b.c`, we capture the spans of `a`, `a.b`, and `a.b.c`.
560+
/// We do that because sometimes we truncate projections (when a closure captures
561+
/// both `a.b` and `a.b.c`), and we want to provide accurate spans in this case.
562+
current_capture_span_stack: Vec<MirSpan>,
556563
current_closure: Option<ClosureId>,
557564
/// Stores the list of closure ids that need to be analyzed before this closure. See the
558565
/// comment on `InferenceContext::sort_closures`
@@ -634,6 +641,7 @@ impl<'a> InferenceContext<'a> {
634641
breakables: Vec::new(),
635642
deferred_cast_checks: Vec::new(),
636643
current_captures: Vec::new(),
644+
current_capture_span_stack: Vec::new(),
637645
current_closure: None,
638646
deferred_closures: FxHashMap::default(),
639647
closure_dependencies: FxHashMap::default(),

0 commit comments

Comments
 (0)