Skip to content

Commit 31dbec7

Browse files
authored
Merge pull request #19655 from Veykril/push-kunlloxnyksr
refactor: Fold hygiene map into bindings themselves
2 parents 34e7d60 + cb6ddbe commit 31dbec7

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

crates/hir-def/src/expr_store.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,6 @@ pub struct ExpressionStore {
9898
/// Block expressions in this store that may contain inner items.
9999
block_scopes: Box<[BlockId]>,
100100

101-
/// A map from binding to its hygiene ID.
102-
///
103-
/// Bindings that don't come from macro expansion are not allocated to save space, so not all bindings appear here.
104-
/// If a binding does not appear here it has `SyntaxContextId::ROOT`.
105-
///
106-
/// Note that this may not be the direct `SyntaxContextId` of the binding's expansion, because transparent
107-
/// expansions are attributed to their parent expansion (recursively).
108-
binding_hygiene: FxHashMap<BindingId, HygieneId>,
109101
/// A map from an variable usages to their hygiene ID.
110102
///
111103
/// Expressions (and destructuing patterns) that can be recorded here are single segment path, although not all single segments path refer
@@ -155,7 +147,6 @@ pub struct ExpressionStoreBuilder {
155147
pub binding_owners: FxHashMap<BindingId, ExprId>,
156148
pub types: Arena<TypeRef>,
157149
block_scopes: Vec<BlockId>,
158-
binding_hygiene: FxHashMap<BindingId, HygieneId>,
159150
ident_hygiene: FxHashMap<ExprOrPatId, HygieneId>,
160151
}
161152

@@ -192,7 +183,6 @@ impl ExpressionStoreBuilder {
192183
mut pats,
193184
mut bindings,
194185
mut binding_owners,
195-
mut binding_hygiene,
196186
mut ident_hygiene,
197187
mut types,
198188
} = self;
@@ -201,7 +191,6 @@ impl ExpressionStoreBuilder {
201191
pats.shrink_to_fit();
202192
bindings.shrink_to_fit();
203193
binding_owners.shrink_to_fit();
204-
binding_hygiene.shrink_to_fit();
205194
ident_hygiene.shrink_to_fit();
206195
types.shrink_to_fit();
207196

@@ -213,7 +202,6 @@ impl ExpressionStoreBuilder {
213202
binding_owners,
214203
types,
215204
block_scopes: block_scopes.into_boxed_slice(),
216-
binding_hygiene,
217205
ident_hygiene,
218206
}
219207
}
@@ -556,7 +544,7 @@ impl ExpressionStore {
556544
}
557545

558546
fn binding_hygiene(&self, binding: BindingId) -> HygieneId {
559-
self.binding_hygiene.get(&binding).copied().unwrap_or(HygieneId::ROOT)
547+
self.bindings[binding].hygiene
560548
}
561549

562550
pub fn expr_path_hygiene(&self, expr: ExprId) -> HygieneId {

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

+30-20
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,14 @@ pub(super) fn lower_body(
104104
{
105105
let is_mutable =
106106
self_param_syn.mut_token().is_some() && self_param_syn.amp_token().is_none();
107+
let hygiene = self_param_syn
108+
.name()
109+
.map(|name| collector.hygiene_id_for(name.syntax().text_range()))
110+
.unwrap_or(HygieneId::ROOT);
107111
let binding_id: la_arena::Idx<Binding> = collector.alloc_binding(
108112
Name::new_symbol_root(sym::self_),
109113
BindingAnnotation::new(is_mutable, false),
114+
hygiene,
110115
);
111116
self_param = Some(binding_id);
112117
source_map_self_param =
@@ -136,17 +141,15 @@ pub(super) fn lower_body(
136141
{
137142
let is_mutable =
138143
self_param_syn.mut_token().is_some() && self_param_syn.amp_token().is_none();
139-
let binding_id: la_arena::Idx<Binding> = collector.alloc_binding(
140-
Name::new_symbol_root(sym::self_),
141-
BindingAnnotation::new(is_mutable, false),
142-
);
143144
let hygiene = self_param_syn
144145
.name()
145146
.map(|name| collector.hygiene_id_for(name.syntax().text_range()))
146147
.unwrap_or(HygieneId::ROOT);
147-
if !hygiene.is_root() {
148-
collector.store.binding_hygiene.insert(binding_id, hygiene);
149-
}
148+
let binding_id: la_arena::Idx<Binding> = collector.alloc_binding(
149+
Name::new_symbol_root(sym::self_),
150+
BindingAnnotation::new(is_mutable, false),
151+
hygiene,
152+
);
150153
self_param = Some(binding_id);
151154
source_map_self_param = Some(collector.expander.in_file(AstPtr::new(&self_param_syn)));
152155
}
@@ -486,13 +489,10 @@ impl BindingList {
486489
hygiene: HygieneId,
487490
mode: BindingAnnotation,
488491
) -> BindingId {
489-
let id = *self.map.entry((name, hygiene)).or_insert_with_key(|(name, _)| {
490-
let id = ec.alloc_binding(name.clone(), mode);
491-
if !hygiene.is_root() {
492-
ec.store.binding_hygiene.insert(id, hygiene);
493-
}
494-
id
495-
});
492+
let id = *self
493+
.map
494+
.entry((name, hygiene))
495+
.or_insert_with_key(|(name, hygiene)| ec.alloc_binding(name.clone(), mode, *hygiene));
496496
if ec.store.bindings[id].mode != mode {
497497
ec.store.bindings[id].problems = Some(BindingProblems::BoundInconsistently);
498498
}
@@ -1770,7 +1770,8 @@ impl ExprCollector<'_> {
17701770
);
17711771
let loop_outer = self
17721772
.alloc_expr(Expr::Loop { body: loop_inner, label: label.map(|it| it.1) }, syntax_ptr);
1773-
let iter_binding = self.alloc_binding(iter_name, BindingAnnotation::Mutable);
1773+
let iter_binding =
1774+
self.alloc_binding(iter_name, BindingAnnotation::Mutable, HygieneId::ROOT);
17741775
let iter_pat = self.alloc_pat_desugared(Pat::Bind { id: iter_binding, subpat: None });
17751776
self.add_definition_to_binding(iter_binding, iter_pat);
17761777
self.alloc_expr(
@@ -1803,8 +1804,11 @@ impl ExprCollector<'_> {
18031804
let expr = self
18041805
.alloc_expr(Expr::Call { callee: try_branch, args: Box::new([operand]) }, syntax_ptr);
18051806
let continue_name = Name::generate_new_name(self.store.bindings.len());
1806-
let continue_binding =
1807-
self.alloc_binding(continue_name.clone(), BindingAnnotation::Unannotated);
1807+
let continue_binding = self.alloc_binding(
1808+
continue_name.clone(),
1809+
BindingAnnotation::Unannotated,
1810+
HygieneId::ROOT,
1811+
);
18081812
let continue_bpat =
18091813
self.alloc_pat_desugared(Pat::Bind { id: continue_binding, subpat: None });
18101814
self.add_definition_to_binding(continue_binding, continue_bpat);
@@ -1818,7 +1822,8 @@ impl ExprCollector<'_> {
18181822
expr: self.alloc_expr(Expr::Path(Path::from(continue_name)), syntax_ptr),
18191823
};
18201824
let break_name = Name::generate_new_name(self.store.bindings.len());
1821-
let break_binding = self.alloc_binding(break_name.clone(), BindingAnnotation::Unannotated);
1825+
let break_binding =
1826+
self.alloc_binding(break_name.clone(), BindingAnnotation::Unannotated, HygieneId::ROOT);
18221827
let break_bpat = self.alloc_pat_desugared(Pat::Bind { id: break_binding, subpat: None });
18231828
self.add_definition_to_binding(break_binding, break_bpat);
18241829
let break_arm = MatchArm {
@@ -3137,8 +3142,13 @@ impl ExprCollector<'_> {
31373142
self.alloc_expr_desugared(Expr::Missing)
31383143
}
31393144

3140-
fn alloc_binding(&mut self, name: Name, mode: BindingAnnotation) -> BindingId {
3141-
let binding = self.store.bindings.alloc(Binding { name, mode, problems: None });
3145+
fn alloc_binding(
3146+
&mut self,
3147+
name: Name,
3148+
mode: BindingAnnotation,
3149+
hygiene: HygieneId,
3150+
) -> BindingId {
3151+
let binding = self.store.bindings.alloc(Binding { name, mode, problems: None, hygiene });
31423152
if let Some(owner) = self.current_binding_owner {
31433153
self.store.binding_owners.insert(binding, owner);
31443154
}

crates/hir-def/src/hir.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ use type_ref::TypeRefId;
2828
use crate::{
2929
BlockId,
3030
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
31-
expr_store::path::{GenericArgs, Path},
31+
expr_store::{
32+
HygieneId,
33+
path::{GenericArgs, Path},
34+
},
3235
type_ref::{Mutability, Rawness},
3336
};
3437

@@ -552,6 +555,9 @@ pub struct Binding {
552555
pub name: Name,
553556
pub mode: BindingAnnotation,
554557
pub problems: Option<BindingProblems>,
558+
/// Note that this may not be the direct `SyntaxContextId` of the binding's expansion, because transparent
559+
/// expansions are attributed to their parent expansion (recursively).
560+
pub hygiene: HygieneId,
555561
}
556562

557563
#[derive(Debug, Clone, Eq, PartialEq)]

0 commit comments

Comments
 (0)