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

Commit 09b9a92

Browse files
committed
fix: Don't invalid body query results when generating desugared names
1 parent c04c0dd commit 09b9a92

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,8 @@ impl ExprCollector<'_> {
703703
let Some(try_from_output) = LangItem::TryTraitFromOutput.path(self.db, self.krate) else {
704704
return self.collect_block(e);
705705
};
706-
let label = self.alloc_label_desugared(Label { name: Name::generate_new_name() });
706+
let label = self
707+
.alloc_label_desugared(Label { name: Name::generate_new_name(self.body.labels.len()) });
707708
let old_label = self.current_try_block_label.replace(label);
708709

709710
let (btail, expr_id) = self.with_labeled_rib(label, |this| {
@@ -840,7 +841,7 @@ impl ExprCollector<'_> {
840841
this.collect_expr_opt(e.loop_body().map(|it| it.into()))
841842
}),
842843
};
843-
let iter_name = Name::generate_new_name();
844+
let iter_name = Name::generate_new_name(self.body.exprs.len());
844845
let iter_expr = self.alloc_expr(Expr::Path(Path::from(iter_name.clone())), syntax_ptr);
845846
let iter_expr_mut = self.alloc_expr(
846847
Expr::Ref { expr: iter_expr, rawness: Rawness::Ref, mutability: Mutability::Mut },
@@ -901,7 +902,7 @@ impl ExprCollector<'_> {
901902
Expr::Call { callee: try_branch, args: Box::new([operand]), is_assignee_expr: false },
902903
syntax_ptr,
903904
);
904-
let continue_name = Name::generate_new_name();
905+
let continue_name = Name::generate_new_name(self.body.bindings.len());
905906
let continue_binding =
906907
self.alloc_binding(continue_name.clone(), BindingAnnotation::Unannotated);
907908
let continue_bpat =
@@ -916,7 +917,7 @@ impl ExprCollector<'_> {
916917
guard: None,
917918
expr: self.alloc_expr(Expr::Path(Path::from(continue_name)), syntax_ptr),
918919
};
919-
let break_name = Name::generate_new_name();
920+
let break_name = Name::generate_new_name(self.body.bindings.len());
920921
let break_binding = self.alloc_binding(break_name.clone(), BindingAnnotation::Unannotated);
921922
let break_bpat = self.alloc_pat_desugared(Pat::Bind { id: break_binding, subpat: None });
922923
self.add_definition_to_binding(break_binding, break_bpat);

crates/hir-expand/src/name.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,11 @@ impl Name {
111111
self == &Name::missing()
112112
}
113113

114-
/// Generates a new name which is only equal to itself, by incrementing a counter. Due
115-
/// its implementation, it should not be used in things that salsa considers, like
116-
/// type names or field names, and it should be only used in names of local variables
117-
/// and labels and similar things.
118-
pub fn generate_new_name() -> Name {
119-
use std::sync::atomic::{AtomicUsize, Ordering};
120-
static CNT: AtomicUsize = AtomicUsize::new(0);
121-
let c = CNT.fetch_add(1, Ordering::Relaxed);
122-
Name::new_text(format_smolstr!("<ra@gennew>{c}"))
114+
/// Generates a new name that attempts to be unique. Should only be used when body lowering and
115+
/// creating desugared locals and labels. The caller is responsible for picking an index
116+
/// that is stable across re-executions
117+
pub fn generate_new_name(idx: usize) -> Name {
118+
Name::new_text(format_smolstr!("<ra@gennew>{idx}"))
123119
}
124120

125121
/// Returns the tuple index this name represents if it is a tuple field.

0 commit comments

Comments
 (0)