Unnamed repository; edit this file 'description' to name the repository.
Cleanup body ast ptr defs
| -rw-r--r-- | crates/hir-def/src/body.rs | 149 |
1 files changed, 74 insertions, 75 deletions
diff --git a/crates/hir-def/src/body.rs b/crates/hir-def/src/body.rs index 334177751e..73c3f421aa 100644 --- a/crates/hir-def/src/body.rs +++ b/crates/hir-def/src/body.rs @@ -59,6 +59,27 @@ impl HygieneId { } } +pub type ExprPtr = AstPtr<ast::Expr>; +pub type ExprSource = InFile<ExprPtr>; + +pub type PatPtr = AstPtr<ast::Pat>; +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>; + +pub type PatFieldPtr = AstPtr<Either<ast::RecordExprField, ast::RecordPatField>>; +pub type PatFieldSource = InFile<PatFieldPtr>; + +pub type ExprOrPatPtr = AstPtr<Either<ast::Expr, ast::Pat>>; +pub type ExprOrPatSource = InFile<ExprOrPatPtr>; + +pub type SelfParamPtr = AstPtr<ast::SelfParam>; +pub type MacroCallPtr = AstPtr<ast::MacroCall>; + /// The body of an item (function, const etc.). #[derive(Debug, Eq, PartialEq)] pub struct Body { @@ -112,68 +133,6 @@ pub struct BodyCollector { ident_hygiene: FxHashMap<ExprOrPatId, HygieneId>, } -impl BodyCollector { - fn finish( - self, - body_expr: ExprId, - self_param: Option<BindingId>, - params: Box<[PatId]>, - ) -> Body { - let Self { - block_scopes, - mut exprs, - mut labels, - mut pats, - mut bindings, - mut binding_owners, - mut binding_hygiene, - mut ident_hygiene, - mut types, - } = self; - exprs.shrink_to_fit(); - labels.shrink_to_fit(); - pats.shrink_to_fit(); - bindings.shrink_to_fit(); - binding_owners.shrink_to_fit(); - binding_hygiene.shrink_to_fit(); - ident_hygiene.shrink_to_fit(); - types.shrink_to_fit(); - - Body { - exprs, - pats, - bindings, - labels, - binding_owners, - params, - self_param, - body_expr, - types, - block_scopes: block_scopes.into_boxed_slice(), - binding_hygiene, - ident_hygiene, - } - } -} - -pub type ExprPtr = AstPtr<ast::Expr>; -pub type ExprSource = InFile<ExprPtr>; - -pub type PatPtr = AstPtr<ast::Pat>; -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>; - -pub type PatFieldPtr = AstPtr<Either<ast::RecordExprField, ast::RecordPatField>>; -pub type PatFieldSource = InFile<PatFieldPtr>; - -pub type ExprOrPatPtr = AstPtr<Either<ast::Expr, ast::Pat>>; -pub type ExprOrPatSource = InFile<ExprOrPatPtr>; - /// 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 @@ -198,7 +157,7 @@ pub struct BodySourceMap { label_map: FxHashMap<LabelSource, LabelId>, label_map_back: ArenaMap<LabelId, LabelSource>, - self_param: Option<InFile<AstPtr<ast::SelfParam>>>, + self_param: Option<InFile<SelfParamPtr>>, binding_definitions: FxHashMap<BindingId, SmallVec<[PatId; 4]>>, /// We don't create explicit nodes for record fields (`S { record_field: 92 }`). @@ -210,7 +169,7 @@ pub struct BodySourceMap { template_map: Option<Box<FormatTemplate>>, - expansions: FxHashMap<InFile<AstPtr<ast::MacroCall>>, MacroFileId>, + expansions: FxHashMap<InFile<MacroCallPtr>, MacroFileId>, /// Diagnostics accumulated during body lowering. These contain `AstPtr`s and so are stored in /// the source map (since they're just as volatile). @@ -228,19 +187,63 @@ struct FormatTemplate { /// The value stored for each capture is its template literal and offset inside it. The template literal /// is from the `format_args[_nl]!()` macro and so needs to be mapped up once to go to the user-written /// template. - implicit_capture_to_source: FxHashMap<ExprId, InFile<(AstPtr<ast::Expr>, TextRange)>>, + implicit_capture_to_source: FxHashMap<ExprId, InFile<(ExprPtr, TextRange)>>, } #[derive(Debug, Eq, PartialEq)] pub enum BodyDiagnostic { InactiveCode { node: InFile<SyntaxNodePtr>, cfg: CfgExpr, opts: CfgOptions }, - MacroError { node: InFile<AstPtr<ast::MacroCall>>, err: ExpandError }, - UnresolvedMacroCall { node: InFile<AstPtr<ast::MacroCall>>, path: ModPath }, + MacroError { node: InFile<MacroCallPtr>, err: ExpandError }, + UnresolvedMacroCall { node: InFile<MacroCallPtr>, path: ModPath }, UnreachableLabel { node: InFile<AstPtr<ast::Lifetime>>, name: Name }, AwaitOutsideOfAsync { node: InFile<AstPtr<ast::AwaitExpr>>, location: String }, UndeclaredLabel { node: InFile<AstPtr<ast::Lifetime>>, name: Name }, } +impl BodyCollector { + fn finish( + self, + body_expr: ExprId, + self_param: Option<BindingId>, + params: Box<[PatId]>, + ) -> Body { + let Self { + block_scopes, + mut exprs, + mut labels, + mut pats, + mut bindings, + mut binding_owners, + mut binding_hygiene, + mut ident_hygiene, + mut types, + } = self; + exprs.shrink_to_fit(); + labels.shrink_to_fit(); + pats.shrink_to_fit(); + bindings.shrink_to_fit(); + binding_owners.shrink_to_fit(); + binding_hygiene.shrink_to_fit(); + ident_hygiene.shrink_to_fit(); + types.shrink_to_fit(); + + Body { + exprs, + pats, + bindings, + labels, + binding_owners, + params, + self_param, + body_expr, + types, + block_scopes: block_scopes.into_boxed_slice(), + binding_hygiene, + ident_hygiene, + } + } +} + impl Body { pub(crate) fn body_with_source_map_query( db: &dyn DefDatabase, @@ -765,9 +768,7 @@ impl BodySourceMap { self.expansions.get(&src).cloned() } - pub fn macro_calls( - &self, - ) -> impl Iterator<Item = (InFile<AstPtr<ast::MacroCall>>, MacroFileId)> + '_ { + pub fn macro_calls(&self) -> impl Iterator<Item = (InFile<MacroCallPtr>, MacroFileId)> + '_ { self.expansions.iter().map(|(&a, &b)| (a, b)) } @@ -775,7 +776,7 @@ impl BodySourceMap { self.pat_map_back.get(pat).cloned().ok_or(SyntheticSyntax) } - pub fn self_param_syntax(&self) -> Option<InFile<AstPtr<ast::SelfParam>>> { + pub fn self_param_syntax(&self) -> Option<InFile<SelfParamPtr>> { self.self_param } @@ -809,9 +810,7 @@ impl BodySourceMap { self.expr_map.get(&src).copied() } - pub fn expansions( - &self, - ) -> impl Iterator<Item = (&InFile<AstPtr<ast::MacroCall>>, &MacroFileId)> { + pub fn expansions(&self) -> impl Iterator<Item = (&InFile<MacroCallPtr>, &MacroFileId)> { self.expansions.iter() } @@ -831,7 +830,7 @@ impl BodySourceMap { pub fn format_args_implicit_capture( &self, capture_expr: ExprId, - ) -> Option<InFile<(AstPtr<ast::Expr>, TextRange)>> { + ) -> Option<InFile<(ExprPtr, TextRange)>> { self.template_map.as_ref()?.implicit_capture_to_source.get(&capture_expr).copied() } |