Skip to content

Commit 83d7724

Browse files
committed
fix: Add binding definition for for-expr iterator desugared binding
1 parent fcfc6af commit 83d7724

File tree

3 files changed

+42
-35
lines changed

3 files changed

+42
-35
lines changed

crates/hir-def/src/body/lower.rs

+21-25
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl ExprCollector<'_> {
188188
param_list.self_param().filter(|_| attr_enabled.next().unwrap_or(false))
189189
{
190190
let ptr = AstPtr::new(&self_param);
191-
let binding_id = self.alloc_binding(
191+
let binding_id: la_arena::Idx<Binding> = self.alloc_binding(
192192
name![self],
193193
BindingAnnotation::new(
194194
self_param.mut_token().is_some() && self_param.amp_token().is_none(),
@@ -745,16 +745,14 @@ impl ExprCollector<'_> {
745745
/// }
746746
/// ```
747747
fn collect_for_loop(&mut self, syntax_ptr: AstPtr<ast::Expr>, e: ast::ForExpr) -> ExprId {
748-
let (into_iter_fn, iter_next_fn, option_some, option_none) = 'if_chain: {
749-
if let Some(into_iter_fn) = LangItem::IntoIterIntoIter.path(self.db, self.krate) {
750-
if let Some(iter_next_fn) = LangItem::IteratorNext.path(self.db, self.krate) {
751-
if let Some(option_some) = LangItem::OptionSome.path(self.db, self.krate) {
752-
if let Some(option_none) = LangItem::OptionNone.path(self.db, self.krate) {
753-
break 'if_chain (into_iter_fn, iter_next_fn, option_some, option_none);
754-
}
755-
}
756-
}
757-
}
748+
let Some((into_iter_fn, iter_next_fn, option_some, option_none)) = (|| {
749+
Some((
750+
LangItem::IntoIterIntoIter.path(self.db, self.krate)?,
751+
LangItem::IteratorNext.path(self.db, self.krate)?,
752+
LangItem::OptionSome.path(self.db, self.krate)?,
753+
LangItem::OptionNone.path(self.db, self.krate)?,
754+
))
755+
})() else {
758756
// Some of the needed lang items are missing, so we can't desugar
759757
return self.alloc_expr(Expr::Missing, syntax_ptr);
760758
};
@@ -787,8 +785,8 @@ impl ExprCollector<'_> {
787785
}),
788786
};
789787
let iter_name = Name::generate_new_name();
790-
let iter_binding = self.alloc_binding(iter_name.clone(), BindingAnnotation::Mutable);
791-
let iter_expr = self.alloc_expr(Expr::Path(Path::from(iter_name)), syntax_ptr.clone());
788+
let iter_expr =
789+
self.alloc_expr(Expr::Path(Path::from(iter_name.clone())), syntax_ptr.clone());
792790
let iter_expr_mut = self.alloc_expr(
793791
Expr::Ref { expr: iter_expr, rawness: Rawness::Ref, mutability: Mutability::Mut },
794792
syntax_ptr.clone(),
@@ -808,7 +806,9 @@ impl ExprCollector<'_> {
808806
);
809807
let loop_outer =
810808
self.alloc_expr(Expr::Loop { body: loop_inner, label }, syntax_ptr.clone());
809+
let iter_binding = self.alloc_binding(iter_name, BindingAnnotation::Mutable);
811810
let iter_pat = self.alloc_pat_desugared(Pat::Bind { id: iter_binding, subpat: None });
811+
self.add_definition_to_binding(iter_binding, iter_pat);
812812
self.alloc_expr(
813813
Expr::Match {
814814
expr: iterator,
@@ -830,18 +830,14 @@ impl ExprCollector<'_> {
830830
/// }
831831
/// ```
832832
fn collect_try_operator(&mut self, syntax_ptr: AstPtr<ast::Expr>, e: ast::TryExpr) -> ExprId {
833-
let (try_branch, cf_continue, cf_break, try_from_residual) = 'if_chain: {
834-
if let Some(try_branch) = LangItem::TryTraitBranch.path(self.db, self.krate) {
835-
if let Some(cf_continue) = LangItem::ControlFlowContinue.path(self.db, self.krate) {
836-
if let Some(cf_break) = LangItem::ControlFlowBreak.path(self.db, self.krate) {
837-
if let Some(try_from_residual) =
838-
LangItem::TryTraitFromResidual.path(self.db, self.krate)
839-
{
840-
break 'if_chain (try_branch, cf_continue, cf_break, try_from_residual);
841-
}
842-
}
843-
}
844-
}
833+
let Some((try_branch, cf_continue, cf_break, try_from_residual)) = (|| {
834+
Some((
835+
LangItem::TryTraitBranch.path(self.db, self.krate)?,
836+
LangItem::ControlFlowContinue.path(self.db, self.krate)?,
837+
LangItem::ControlFlowBreak.path(self.db, self.krate)?,
838+
LangItem::TryTraitFromResidual.path(self.db, self.krate)?,
839+
))
840+
})() else {
845841
// Some of the needed lang items are missing, so we can't desugar
846842
return self.alloc_expr(Expr::Missing, syntax_ptr);
847843
};

crates/hir/src/lib.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -2800,6 +2800,22 @@ impl Local {
28002800
/// All definitions for this local. Example: `let (a$0, _) | (_, a$0) = x;`
28012801
pub fn sources(self, db: &dyn HirDatabase) -> Vec<LocalSource> {
28022802
let (body, source_map) = db.body_with_source_map(self.parent);
2803+
self.sources_(db, &body, &source_map).collect()
2804+
}
2805+
2806+
/// The leftmost definition for this local. Example: `let (a$0, _) | (_, a) = x;`
2807+
pub fn primary_source(self, db: &dyn HirDatabase) -> LocalSource {
2808+
let (body, source_map) = db.body_with_source_map(self.parent);
2809+
let src = self.sources_(db, &body, &source_map).next().unwrap();
2810+
src
2811+
}
2812+
2813+
fn sources_<'a>(
2814+
self,
2815+
db: &'a dyn HirDatabase,
2816+
body: &'a hir_def::body::Body,
2817+
source_map: &'a hir_def::body::BodySourceMap,
2818+
) -> impl Iterator<Item = LocalSource> + 'a {
28032819
body[self.binding_id]
28042820
.definitions
28052821
.iter()
@@ -2812,14 +2828,7 @@ impl Local {
28122828
Either::Right(it) => Either::Right(it.to_node(&root)),
28132829
})
28142830
})
2815-
.map(|source| LocalSource { local: self, source })
2816-
.collect()
2817-
}
2818-
2819-
/// The leftmost definition for this local. Example: `let (a$0, _) | (_, a) = x;`
2820-
pub fn primary_source(self, db: &dyn HirDatabase) -> LocalSource {
2821-
let all_sources = self.sources(db);
2822-
all_sources.into_iter().next().unwrap()
2831+
.map(move |source| LocalSource { local: self, source })
28232832
}
28242833
}
28252834

crates/rust-analyzer/src/cli/analysis_stats.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,11 @@ impl flags::AnalysisStats {
165165
}
166166
}
167167
eprintln!(
168-
", mods: {}, decls: {num_decls}, bodies: {}",
168+
", mods: {}, decls: {num_decls}, bodies: {}, adts: {}, consts: {}",
169169
visited_modules.len(),
170-
bodies.len()
170+
bodies.len(),
171+
adts.len(),
172+
consts.len(),
171173
);
172174
eprintln!("{:<20} {}", "Item Collection:", analysis_sw.elapsed());
173175

0 commit comments

Comments
 (0)