Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lift out the module scope into a field in the Resolver #13174

Merged
merged 1 commit into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions crates/hir-def/src/body.rs
Expand Up @@ -250,6 +250,10 @@ pub type PatSource = InFile<PatPtr>;

pub type LabelPtr = AstPtr<ast::Label>;
pub type LabelSource = InFile<LabelPtr>;

pub type FieldPtr = AstPtr<ast::RecordExprField>;
pub type FieldSource = InFile<FieldPtr>;

/// An item body together with the mapping from syntax nodes to HIR expression
/// IDs. This is needed to go from e.g. a position in a file to the HIR
/// expression containing it; but for type inference etc., we want to operate on
Expand All @@ -274,8 +278,8 @@ pub struct BodySourceMap {

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

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

Expand Down Expand Up @@ -456,9 +460,10 @@ impl BodySourceMap {
self.label_map.get(&src).cloned()
}

pub fn field_syntax(&self, expr: ExprId) -> InFile<AstPtr<ast::RecordExprField>> {
pub fn field_syntax(&self, expr: ExprId) -> FieldSource {
self.field_map_back[&expr].clone()
}

pub fn node_field(&self, node: InFile<&ast::RecordExprField>) -> Option<ExprId> {
let src = node.map(AstPtr::new);
self.field_map.get(&src).cloned()
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-def/src/body/lower.rs
Expand Up @@ -24,7 +24,7 @@ use syntax::{

use crate::{
adt::StructKind,
body::{Body, BodySourceMap, Expander, LabelSource, PatPtr},
body::{Body, BodySourceMap, Expander, ExprPtr, LabelPtr, LabelSource, PatPtr},
body::{BodyDiagnostic, ExprSource, PatSource},
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
db::DefDatabase,
Expand Down Expand Up @@ -150,7 +150,7 @@ impl ExprCollector<'_> {
LowerCtx::new(self.db, self.expander.current_file_id)
}

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

fn alloc_label(&mut self, label: Label, ptr: AstPtr<ast::Label>) -> LabelId {
fn alloc_label(&mut self, label: Label, ptr: LabelPtr) -> LabelId {
let src = self.expander.to_source(ptr);
let id = self.make_label(label, src.clone());
self.source_map.label_map.insert(src, id);
Expand Down
31 changes: 21 additions & 10 deletions crates/hir-def/src/body/scope.rs
Expand Up @@ -47,16 +47,9 @@ pub struct ScopeData {
impl ExprScopes {
pub(crate) fn expr_scopes_query(db: &dyn DefDatabase, def: DefWithBodyId) -> Arc<ExprScopes> {
let body = db.body(def);
Arc::new(ExprScopes::new(&*body))
}

fn new(body: &Body) -> ExprScopes {
let mut scopes =
ExprScopes { scopes: Arena::default(), scope_by_expr: FxHashMap::default() };
let mut root = scopes.root_scope();
scopes.add_params_bindings(body, root, &body.params);
compute_expr_scopes(body.body_expr, body, &mut scopes, &mut root);
scopes
let mut scopes = ExprScopes::new(&*body);
scopes.shrink_to_fit();
Arc::new(scopes)
}

pub fn entries(&self, scope: ScopeId) -> &[ScopeEntry] {
Expand Down Expand Up @@ -89,6 +82,17 @@ impl ExprScopes {
pub fn scope_by_expr(&self) -> &FxHashMap<ExprId, ScopeId> {
&self.scope_by_expr
}
}

impl ExprScopes {
fn new(body: &Body) -> ExprScopes {
let mut scopes =
ExprScopes { scopes: Arena::default(), scope_by_expr: FxHashMap::default() };
let mut root = scopes.root_scope();
scopes.add_params_bindings(body, root, &body.params);
compute_expr_scopes(body.body_expr, body, &mut scopes, &mut root);
scopes
}

fn root_scope(&mut self) -> ScopeId {
self.scopes.alloc(ScopeData { parent: None, block: None, label: None, entries: vec![] })
Expand Down Expand Up @@ -138,6 +142,13 @@ impl ExprScopes {
fn set_scope(&mut self, node: ExprId, scope: ScopeId) {
self.scope_by_expr.insert(node, scope);
}

fn shrink_to_fit(&mut self) {
let ExprScopes { scopes, scope_by_expr } = self;
scopes.shrink_to_fit();
scopes.values_mut().for_each(|it| it.entries.shrink_to_fit());
scope_by_expr.shrink_to_fit();
}
}

fn compute_block_scopes(
Expand Down