Skip to content

Commit db6d23c

Browse files
committed
Cleanup body ast ptr defs
1 parent 724455b commit db6d23c

File tree

1 file changed

+74
-75
lines changed

1 file changed

+74
-75
lines changed

crates/hir-def/src/body.rs

+74-75
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@ impl HygieneId {
5959
}
6060
}
6161

62+
pub type ExprPtr = AstPtr<ast::Expr>;
63+
pub type ExprSource = InFile<ExprPtr>;
64+
65+
pub type PatPtr = AstPtr<ast::Pat>;
66+
pub type PatSource = InFile<PatPtr>;
67+
68+
pub type LabelPtr = AstPtr<ast::Label>;
69+
pub type LabelSource = InFile<LabelPtr>;
70+
71+
pub type FieldPtr = AstPtr<ast::RecordExprField>;
72+
pub type FieldSource = InFile<FieldPtr>;
73+
74+
pub type PatFieldPtr = AstPtr<Either<ast::RecordExprField, ast::RecordPatField>>;
75+
pub type PatFieldSource = InFile<PatFieldPtr>;
76+
77+
pub type ExprOrPatPtr = AstPtr<Either<ast::Expr, ast::Pat>>;
78+
pub type ExprOrPatSource = InFile<ExprOrPatPtr>;
79+
80+
pub type SelfParamPtr = AstPtr<ast::SelfParam>;
81+
pub type MacroCallPtr = AstPtr<ast::MacroCall>;
82+
6283
/// The body of an item (function, const etc.).
6384
#[derive(Debug, Eq, PartialEq)]
6485
pub struct Body {
@@ -112,68 +133,6 @@ pub struct BodyCollector {
112133
ident_hygiene: FxHashMap<ExprOrPatId, HygieneId>,
113134
}
114135

115-
impl BodyCollector {
116-
fn finish(
117-
self,
118-
body_expr: ExprId,
119-
self_param: Option<BindingId>,
120-
params: Box<[PatId]>,
121-
) -> Body {
122-
let Self {
123-
block_scopes,
124-
mut exprs,
125-
mut labels,
126-
mut pats,
127-
mut bindings,
128-
mut binding_owners,
129-
mut binding_hygiene,
130-
mut ident_hygiene,
131-
mut types,
132-
} = self;
133-
exprs.shrink_to_fit();
134-
labels.shrink_to_fit();
135-
pats.shrink_to_fit();
136-
bindings.shrink_to_fit();
137-
binding_owners.shrink_to_fit();
138-
binding_hygiene.shrink_to_fit();
139-
ident_hygiene.shrink_to_fit();
140-
types.shrink_to_fit();
141-
142-
Body {
143-
exprs,
144-
pats,
145-
bindings,
146-
labels,
147-
binding_owners,
148-
params,
149-
self_param,
150-
body_expr,
151-
types,
152-
block_scopes: block_scopes.into_boxed_slice(),
153-
binding_hygiene,
154-
ident_hygiene,
155-
}
156-
}
157-
}
158-
159-
pub type ExprPtr = AstPtr<ast::Expr>;
160-
pub type ExprSource = InFile<ExprPtr>;
161-
162-
pub type PatPtr = AstPtr<ast::Pat>;
163-
pub type PatSource = InFile<PatPtr>;
164-
165-
pub type LabelPtr = AstPtr<ast::Label>;
166-
pub type LabelSource = InFile<LabelPtr>;
167-
168-
pub type FieldPtr = AstPtr<ast::RecordExprField>;
169-
pub type FieldSource = InFile<FieldPtr>;
170-
171-
pub type PatFieldPtr = AstPtr<Either<ast::RecordExprField, ast::RecordPatField>>;
172-
pub type PatFieldSource = InFile<PatFieldPtr>;
173-
174-
pub type ExprOrPatPtr = AstPtr<Either<ast::Expr, ast::Pat>>;
175-
pub type ExprOrPatSource = InFile<ExprOrPatPtr>;
176-
177136
/// An item body together with the mapping from syntax nodes to HIR expression
178137
/// IDs. This is needed to go from e.g. a position in a file to the HIR
179138
/// expression containing it; but for type inference etc., we want to operate on
@@ -198,7 +157,7 @@ pub struct BodySourceMap {
198157
label_map: FxHashMap<LabelSource, LabelId>,
199158
label_map_back: ArenaMap<LabelId, LabelSource>,
200159

201-
self_param: Option<InFile<AstPtr<ast::SelfParam>>>,
160+
self_param: Option<InFile<SelfParamPtr>>,
202161
binding_definitions: FxHashMap<BindingId, SmallVec<[PatId; 4]>>,
203162

204163
/// We don't create explicit nodes for record fields (`S { record_field: 92 }`).
@@ -210,7 +169,7 @@ pub struct BodySourceMap {
210169

211170
template_map: Option<Box<FormatTemplate>>,
212171

213-
expansions: FxHashMap<InFile<AstPtr<ast::MacroCall>>, MacroFileId>,
172+
expansions: FxHashMap<InFile<MacroCallPtr>, MacroFileId>,
214173

215174
/// Diagnostics accumulated during body lowering. These contain `AstPtr`s and so are stored in
216175
/// the source map (since they're just as volatile).
@@ -228,19 +187,63 @@ struct FormatTemplate {
228187
/// The value stored for each capture is its template literal and offset inside it. The template literal
229188
/// is from the `format_args[_nl]!()` macro and so needs to be mapped up once to go to the user-written
230189
/// template.
231-
implicit_capture_to_source: FxHashMap<ExprId, InFile<(AstPtr<ast::Expr>, TextRange)>>,
190+
implicit_capture_to_source: FxHashMap<ExprId, InFile<(ExprPtr, TextRange)>>,
232191
}
233192

234193
#[derive(Debug, Eq, PartialEq)]
235194
pub enum BodyDiagnostic {
236195
InactiveCode { node: InFile<SyntaxNodePtr>, cfg: CfgExpr, opts: CfgOptions },
237-
MacroError { node: InFile<AstPtr<ast::MacroCall>>, err: ExpandError },
238-
UnresolvedMacroCall { node: InFile<AstPtr<ast::MacroCall>>, path: ModPath },
196+
MacroError { node: InFile<MacroCallPtr>, err: ExpandError },
197+
UnresolvedMacroCall { node: InFile<MacroCallPtr>, path: ModPath },
239198
UnreachableLabel { node: InFile<AstPtr<ast::Lifetime>>, name: Name },
240199
AwaitOutsideOfAsync { node: InFile<AstPtr<ast::AwaitExpr>>, location: String },
241200
UndeclaredLabel { node: InFile<AstPtr<ast::Lifetime>>, name: Name },
242201
}
243202

203+
impl BodyCollector {
204+
fn finish(
205+
self,
206+
body_expr: ExprId,
207+
self_param: Option<BindingId>,
208+
params: Box<[PatId]>,
209+
) -> Body {
210+
let Self {
211+
block_scopes,
212+
mut exprs,
213+
mut labels,
214+
mut pats,
215+
mut bindings,
216+
mut binding_owners,
217+
mut binding_hygiene,
218+
mut ident_hygiene,
219+
mut types,
220+
} = self;
221+
exprs.shrink_to_fit();
222+
labels.shrink_to_fit();
223+
pats.shrink_to_fit();
224+
bindings.shrink_to_fit();
225+
binding_owners.shrink_to_fit();
226+
binding_hygiene.shrink_to_fit();
227+
ident_hygiene.shrink_to_fit();
228+
types.shrink_to_fit();
229+
230+
Body {
231+
exprs,
232+
pats,
233+
bindings,
234+
labels,
235+
binding_owners,
236+
params,
237+
self_param,
238+
body_expr,
239+
types,
240+
block_scopes: block_scopes.into_boxed_slice(),
241+
binding_hygiene,
242+
ident_hygiene,
243+
}
244+
}
245+
}
246+
244247
impl Body {
245248
pub(crate) fn body_with_source_map_query(
246249
db: &dyn DefDatabase,
@@ -765,17 +768,15 @@ impl BodySourceMap {
765768
self.expansions.get(&src).cloned()
766769
}
767770

768-
pub fn macro_calls(
769-
&self,
770-
) -> impl Iterator<Item = (InFile<AstPtr<ast::MacroCall>>, MacroFileId)> + '_ {
771+
pub fn macro_calls(&self) -> impl Iterator<Item = (InFile<MacroCallPtr>, MacroFileId)> + '_ {
771772
self.expansions.iter().map(|(&a, &b)| (a, b))
772773
}
773774

774775
pub fn pat_syntax(&self, pat: PatId) -> Result<ExprOrPatSource, SyntheticSyntax> {
775776
self.pat_map_back.get(pat).cloned().ok_or(SyntheticSyntax)
776777
}
777778

778-
pub fn self_param_syntax(&self) -> Option<InFile<AstPtr<ast::SelfParam>>> {
779+
pub fn self_param_syntax(&self) -> Option<InFile<SelfParamPtr>> {
779780
self.self_param
780781
}
781782

@@ -809,9 +810,7 @@ impl BodySourceMap {
809810
self.expr_map.get(&src).copied()
810811
}
811812

812-
pub fn expansions(
813-
&self,
814-
) -> impl Iterator<Item = (&InFile<AstPtr<ast::MacroCall>>, &MacroFileId)> {
813+
pub fn expansions(&self) -> impl Iterator<Item = (&InFile<MacroCallPtr>, &MacroFileId)> {
815814
self.expansions.iter()
816815
}
817816

@@ -831,7 +830,7 @@ impl BodySourceMap {
831830
pub fn format_args_implicit_capture(
832831
&self,
833832
capture_expr: ExprId,
834-
) -> Option<InFile<(AstPtr<ast::Expr>, TextRange)>> {
833+
) -> Option<InFile<(ExprPtr, TextRange)>> {
835834
self.template_map.as_ref()?.implicit_capture_to_source.get(&capture_expr).copied()
836835
}
837836

0 commit comments

Comments
 (0)