From 55d1d5dd8bc5a9aea1786ffddcd98c07a87a08bb Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 25 Jan 2025 15:52:52 +0100 Subject: [PATCH] Split out `ExpressionStore` from `Body` --- crates/hir-def/src/db.rs | 2 +- crates/hir-def/src/{body.rs => expr_store.rs} | 226 ++-------- crates/hir-def/src/expr_store/body.rs | 175 ++++++++ .../hir-def/src/{body => expr_store}/lower.rs | 387 ++++++++++-------- .../src/{body => expr_store}/lower/asm.rs | 2 +- .../src/{body => expr_store}/pretty.rs | 44 +- .../hir-def/src/{body => expr_store}/scope.rs | 54 +-- .../hir-def/src/{body => expr_store}/tests.rs | 1 + .../src/{body => expr_store}/tests/block.rs | 0 crates/hir-def/src/lib.rs | 12 +- crates/hir-def/src/resolver.rs | 8 +- crates/hir-ty/src/consteval.rs | 2 +- crates/hir-ty/src/diagnostics/expr.rs | 2 +- crates/hir-ty/src/diagnostics/match_check.rs | 3 +- crates/hir-ty/src/diagnostics/unsafe_check.rs | 2 +- crates/hir-ty/src/infer.rs | 2 +- crates/hir-ty/src/infer/diagnostics.rs | 2 +- crates/hir-ty/src/infer/pat.rs | 4 +- crates/hir-ty/src/lower.rs | 2 +- crates/hir-ty/src/mir.rs | 2 +- crates/hir-ty/src/mir/eval.rs | 2 +- crates/hir-ty/src/mir/lower.rs | 2 +- crates/hir-ty/src/mir/pretty.rs | 2 +- crates/hir-ty/src/tests.rs | 2 +- crates/hir/src/diagnostics.rs | 4 +- crates/hir/src/lib.rs | 26 +- crates/hir/src/source_analyzer.rs | 2 +- .../rust-analyzer/src/cli/analysis_stats.rs | 2 +- 28 files changed, 530 insertions(+), 444 deletions(-) rename crates/hir-def/src/{body.rs => expr_store.rs} (78%) create mode 100644 crates/hir-def/src/expr_store/body.rs rename crates/hir-def/src/{body => expr_store}/lower.rs (91%) rename crates/hir-def/src/{body => expr_store}/lower/asm.rs (99%) rename crates/hir-def/src/{body => expr_store}/pretty.rs (95%) rename crates/hir-def/src/{body => expr_store}/scope.rs (90%) rename crates/hir-def/src/{body => expr_store}/tests.rs (99%) rename crates/hir-def/src/{body => expr_store}/tests/block.rs (100%) diff --git a/crates/hir-def/src/db.rs b/crates/hir-def/src/db.rs index bf6cc1dcad..598a850898 100644 --- a/crates/hir-def/src/db.rs +++ b/crates/hir-def/src/db.rs @@ -10,12 +10,12 @@ use triomphe::Arc; use crate::{ attr::{Attrs, AttrsWithOwner}, - body::{scope::ExprScopes, Body, BodySourceMap}, data::{ adt::{EnumData, EnumVariantData, StructData, VariantData}, ConstData, ExternCrateDeclData, FunctionData, ImplData, Macro2Data, MacroRulesData, ProcMacroData, StaticData, TraitAliasData, TraitData, TypeAliasData, }, + expr_store::{scope::ExprScopes, Body, BodySourceMap}, generics::GenericParams, import_map::ImportMap, item_tree::{AttrOwner, ItemTree, ItemTreeSourceMaps}, diff --git a/crates/hir-def/src/body.rs b/crates/hir-def/src/expr_store.rs similarity index 78% rename from crates/hir-def/src/body.rs rename to crates/hir-def/src/expr_store.rs index 73c3f421aa..9df6eaade7 100644 --- a/crates/hir-def/src/body.rs +++ b/crates/hir-def/src/expr_store.rs @@ -1,5 +1,6 @@ -//! Defines `Body`: a lowered representation of bodies of functions, statics and +//! Defines `ExpressionStore`: a lowered representation of functions, statics and //! consts. +mod body; mod lower; mod pretty; pub mod scope; @@ -9,11 +10,10 @@ mod tests; use std::ops::{Deref, Index}; -use base_db::CrateId; use cfg::{CfgExpr, CfgOptions}; use either::Either; use hir_expand::{name::Name, ExpandError, InFile}; -use la_arena::{Arena, ArenaMap, Idx, RawIdx}; +use la_arena::{Arena, ArenaMap}; use rustc_hash::FxHashMap; use smallvec::SmallVec; use span::{Edition, MacroFileId, SyntaxContextData}; @@ -23,19 +23,18 @@ use tt::TextRange; use crate::{ db::DefDatabase, - expander::Expander, hir::{ Array, AsmOperand, Binding, BindingId, Expr, ExprId, ExprOrPatId, Label, LabelId, Pat, PatId, RecordFieldPat, Statement, }, - item_tree::AttrOwner, nameres::DefMap, path::{ModPath, Path}, - src::HasSource, type_ref::{TypeRef, TypeRefId, TypesMap, TypesSourceMap}, - BlockId, DefWithBodyId, HasModule, Lookup, SyntheticSyntax, + BlockId, DefWithBodyId, Lookup, SyntheticSyntax, }; +pub use self::body::{Body, BodySourceMap}; + /// A wrapper around [`span::SyntaxContextId`] that is intended only for comparisons. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct HygieneId(span::SyntaxContextId); @@ -80,9 +79,8 @@ pub type ExprOrPatSource = InFile; pub type SelfParamPtr = AstPtr; pub type MacroCallPtr = AstPtr; -/// The body of an item (function, const etc.). #[derive(Debug, Eq, PartialEq)] -pub struct Body { +pub struct ExpressionStore { pub exprs: Arena, pub pats: Arena, pub bindings: Arena, @@ -90,18 +88,8 @@ pub struct Body { /// Id of the closure/coroutine that owns the corresponding binding. If a binding is owned by the /// top level expression, it will not be listed in here. pub binding_owners: FxHashMap, - /// The patterns for the function's parameters. While the parameter types are - /// part of the function signature, the patterns are not (they don't change - /// the external type of the function). - /// - /// If this `Body` is for the body of a constant, this will just be - /// empty. - pub params: Box<[PatId]>, - pub self_param: Option, - /// The `ExprId` of the actual body expression. - pub body_expr: ExprId, pub types: TypesMap, - /// Block expressions in this body that may contain inner items. + /// Block expressions in this store that may contain inner items. block_scopes: Box<[BlockId]>, /// A map from binding to its hygiene ID. @@ -119,33 +107,8 @@ pub struct Body { ident_hygiene: FxHashMap, } -/// The body of an item (function, const etc.). #[derive(Debug, Eq, PartialEq, Default)] -pub struct BodyCollector { - pub exprs: Arena, - pub pats: Arena, - pub bindings: Arena, - pub labels: Arena