Skip to content

Commit f27f98d

Browse files
committed
Auto merge of rust-lang#13174 - Veykril:resolver, r=Veykril
Lift out the module scope into a field in the Resolver A Resolver *always* has a module scope at the end of its scope stack, instead of encoding this as an invariant we can just lift this scope out into a field, allowing us to skip going through the scope vec indirection entirely.
2 parents 2bb6635 + 8828049 commit f27f98d

File tree

6 files changed

+211
-159
lines changed

6 files changed

+211
-159
lines changed

crates/hir-def/src/body.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ pub type PatSource = InFile<PatPtr>;
250250

251251
pub type LabelPtr = AstPtr<ast::Label>;
252252
pub type LabelSource = InFile<LabelPtr>;
253+
254+
pub type FieldPtr = AstPtr<ast::RecordExprField>;
255+
pub type FieldSource = InFile<FieldPtr>;
256+
253257
/// An item body together with the mapping from syntax nodes to HIR expression
254258
/// IDs. This is needed to go from e.g. a position in a file to the HIR
255259
/// expression containing it; but for type inference etc., we want to operate on
@@ -274,8 +278,8 @@ pub struct BodySourceMap {
274278

275279
/// We don't create explicit nodes for record fields (`S { record_field: 92 }`).
276280
/// Instead, we use id of expression (`92`) to identify the field.
277-
field_map: FxHashMap<InFile<AstPtr<ast::RecordExprField>>, ExprId>,
278-
field_map_back: FxHashMap<ExprId, InFile<AstPtr<ast::RecordExprField>>>,
281+
field_map: FxHashMap<FieldSource, ExprId>,
282+
field_map_back: FxHashMap<ExprId, FieldSource>,
279283

280284
expansions: FxHashMap<InFile<AstPtr<ast::MacroCall>>, HirFileId>,
281285

@@ -456,9 +460,10 @@ impl BodySourceMap {
456460
self.label_map.get(&src).cloned()
457461
}
458462

459-
pub fn field_syntax(&self, expr: ExprId) -> InFile<AstPtr<ast::RecordExprField>> {
463+
pub fn field_syntax(&self, expr: ExprId) -> FieldSource {
460464
self.field_map_back[&expr].clone()
461465
}
466+
462467
pub fn node_field(&self, node: InFile<&ast::RecordExprField>) -> Option<ExprId> {
463468
let src = node.map(AstPtr::new);
464469
self.field_map.get(&src).cloned()

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use syntax::{
2424

2525
use crate::{
2626
adt::StructKind,
27-
body::{Body, BodySourceMap, Expander, LabelSource, PatPtr},
27+
body::{Body, BodySourceMap, Expander, ExprPtr, LabelPtr, LabelSource, PatPtr},
2828
body::{BodyDiagnostic, ExprSource, PatSource},
2929
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
3030
db::DefDatabase,
@@ -150,7 +150,7 @@ impl ExprCollector<'_> {
150150
LowerCtx::new(self.db, self.expander.current_file_id)
151151
}
152152

153-
fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
153+
fn alloc_expr(&mut self, expr: Expr, ptr: ExprPtr) -> ExprId {
154154
let src = self.expander.to_source(ptr);
155155
let id = self.make_expr(expr, src.clone());
156156
self.source_map.expr_map.insert(src, id);
@@ -185,7 +185,7 @@ impl ExprCollector<'_> {
185185
id
186186
}
187187

188-
fn alloc_label(&mut self, label: Label, ptr: AstPtr<ast::Label>) -> LabelId {
188+
fn alloc_label(&mut self, label: Label, ptr: LabelPtr) -> LabelId {
189189
let src = self.expander.to_source(ptr);
190190
let id = self.make_label(label, src.clone());
191191
self.source_map.label_map.insert(src, id);

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,9 @@ pub struct ScopeData {
4747
impl ExprScopes {
4848
pub(crate) fn expr_scopes_query(db: &dyn DefDatabase, def: DefWithBodyId) -> Arc<ExprScopes> {
4949
let body = db.body(def);
50-
Arc::new(ExprScopes::new(&*body))
51-
}
52-
53-
fn new(body: &Body) -> ExprScopes {
54-
let mut scopes =
55-
ExprScopes { scopes: Arena::default(), scope_by_expr: FxHashMap::default() };
56-
let mut root = scopes.root_scope();
57-
scopes.add_params_bindings(body, root, &body.params);
58-
compute_expr_scopes(body.body_expr, body, &mut scopes, &mut root);
59-
scopes
50+
let mut scopes = ExprScopes::new(&*body);
51+
scopes.shrink_to_fit();
52+
Arc::new(scopes)
6053
}
6154

6255
pub fn entries(&self, scope: ScopeId) -> &[ScopeEntry] {
@@ -89,6 +82,17 @@ impl ExprScopes {
8982
pub fn scope_by_expr(&self) -> &FxHashMap<ExprId, ScopeId> {
9083
&self.scope_by_expr
9184
}
85+
}
86+
87+
impl ExprScopes {
88+
fn new(body: &Body) -> ExprScopes {
89+
let mut scopes =
90+
ExprScopes { scopes: Arena::default(), scope_by_expr: FxHashMap::default() };
91+
let mut root = scopes.root_scope();
92+
scopes.add_params_bindings(body, root, &body.params);
93+
compute_expr_scopes(body.body_expr, body, &mut scopes, &mut root);
94+
scopes
95+
}
9296

9397
fn root_scope(&mut self) -> ScopeId {
9498
self.scopes.alloc(ScopeData { parent: None, block: None, label: None, entries: vec![] })
@@ -138,6 +142,13 @@ impl ExprScopes {
138142
fn set_scope(&mut self, node: ExprId, scope: ScopeId) {
139143
self.scope_by_expr.insert(node, scope);
140144
}
145+
146+
fn shrink_to_fit(&mut self) {
147+
let ExprScopes { scopes, scope_by_expr } = self;
148+
scopes.shrink_to_fit();
149+
scopes.values_mut().for_each(|it| it.entries.shrink_to_fit());
150+
scope_by_expr.shrink_to_fit();
151+
}
141152
}
142153

143154
fn compute_block_scopes(

0 commit comments

Comments
 (0)