Skip to content

Commit ef58843

Browse files
committed
Auto merge of #15721 - Veykril:shrink-pat-ptr, r=Veykril
Shrink PatPtr by swapping its AstPtr and Either wrap order Will have neglible perf results I imagine, but it cleans up some code
2 parents a158670 + 88a00bf commit ef58843

File tree

15 files changed

+164
-190
lines changed

15 files changed

+164
-190
lines changed

Diff for: crates/hir-def/src/body.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub struct Body {
5757
pub type ExprPtr = AstPtr<ast::Expr>;
5858
pub type ExprSource = InFile<ExprPtr>;
5959

60-
pub type PatPtr = Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>>;
60+
pub type PatPtr = AstPtr<Either<ast::Pat, ast::SelfParam>>;
6161
pub type PatSource = InFile<PatPtr>;
6262

6363
pub type LabelPtr = AstPtr<ast::Label>;
@@ -356,12 +356,12 @@ impl BodySourceMap {
356356
}
357357

358358
pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option<PatId> {
359-
let src = node.map(|it| Either::Left(AstPtr::new(it)));
359+
let src = node.map(|it| AstPtr::new(it).wrap_left());
360360
self.pat_map.get(&src).cloned()
361361
}
362362

363363
pub fn node_self_param(&self, node: InFile<&ast::SelfParam>) -> Option<PatId> {
364-
let src = node.map(|it| Either::Right(AstPtr::new(it)));
364+
let src = node.map(|it| AstPtr::new(it).wrap_right());
365365
self.pat_map.get(&src).cloned()
366366
}
367367

Diff for: crates/hir-def/src/body/lower.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,12 @@ impl ExprCollector<'_> {
196196
if let Some(self_param) =
197197
param_list.self_param().filter(|_| attr_enabled.next().unwrap_or(false))
198198
{
199-
let ptr = AstPtr::new(&self_param);
200-
let binding_id: la_arena::Idx<Binding> = self.alloc_binding(
201-
name![self],
202-
BindingAnnotation::new(
203-
self_param.mut_token().is_some() && self_param.amp_token().is_none(),
204-
false,
205-
),
206-
);
207-
let param_pat =
208-
self.alloc_pat(Pat::Bind { id: binding_id, subpat: None }, Either::Right(ptr));
199+
let is_mutable =
200+
self_param.mut_token().is_some() && self_param.amp_token().is_none();
201+
let ptr = AstPtr::new(&Either::Right(self_param));
202+
let binding_id: la_arena::Idx<Binding> =
203+
self.alloc_binding(name![self], BindingAnnotation::new(is_mutable, false));
204+
let param_pat = self.alloc_pat(Pat::Bind { id: binding_id, subpat: None }, ptr);
209205
self.add_definition_to_binding(binding_id, param_pat);
210206
self.body.params.push(param_pat);
211207
}
@@ -1260,8 +1256,8 @@ impl ExprCollector<'_> {
12601256
(Some(id), Pat::Bind { id, subpat })
12611257
};
12621258

1263-
let ptr = AstPtr::new(&pat);
1264-
let pat = self.alloc_pat(pattern, Either::Left(ptr));
1259+
let ptr = AstPtr::new(&Either::Left(pat));
1260+
let pat = self.alloc_pat(pattern, ptr);
12651261
if let Some(binding_id) = binding {
12661262
self.add_definition_to_binding(binding_id, pat);
12671263
}
@@ -1395,7 +1391,7 @@ impl ExprCollector<'_> {
13951391
ast::Pat::MacroPat(mac) => match mac.macro_call() {
13961392
Some(call) => {
13971393
let macro_ptr = AstPtr::new(&call);
1398-
let src = self.expander.to_source(Either::Left(AstPtr::new(&pat)));
1394+
let src = self.expander.to_source(AstPtr::new(&Either::Left(pat)));
13991395
let pat =
14001396
self.collect_macro_call(call, macro_ptr, true, |this, expanded_pat| {
14011397
this.collect_pat_opt(expanded_pat, binding_list)
@@ -1430,8 +1426,8 @@ impl ExprCollector<'_> {
14301426
Pat::Range { start, end }
14311427
}
14321428
};
1433-
let ptr = AstPtr::new(&pat);
1434-
self.alloc_pat(pattern, Either::Left(ptr))
1429+
let ptr = AstPtr::new(&Either::Left(pat));
1430+
self.alloc_pat(pattern, ptr)
14351431
}
14361432

14371433
fn collect_pat_opt(&mut self, pat: Option<ast::Pat>, binding_list: &mut BindingList) -> PatId {

Diff for: crates/hir-def/src/body/scope.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,7 @@ fn foo() {
475475
.pat_syntax(*body.bindings[resolved.binding()].definitions.first().unwrap())
476476
.unwrap();
477477

478-
let local_name = pat_src.value.either(
479-
|it| it.syntax_node_ptr().to_node(file.syntax()),
480-
|it| it.syntax_node_ptr().to_node(file.syntax()),
481-
);
478+
let local_name = pat_src.value.syntax_node_ptr().to_node(file.syntax());
482479
assert_eq!(local_name.text_range(), expected_name.syntax().text_range());
483480
}
484481

Diff for: crates/hir-ty/src/diagnostics/decl_check.rs

+34-38
Original file line numberDiff line numberDiff line change
@@ -336,48 +336,44 @@ impl<'a> DeclValidator<'a> {
336336

337337
for (id, replacement) in pats_replacements {
338338
if let Ok(source_ptr) = source_map.pat_syntax(id) {
339-
if let Some(expr) = source_ptr.value.as_ref().left() {
339+
if let Some(ptr) = source_ptr.value.clone().cast::<ast::IdentPat>() {
340340
let root = source_ptr.file_syntax(self.db.upcast());
341-
if let ast::Pat::IdentPat(ident_pat) = expr.to_node(&root) {
342-
let parent = match ident_pat.syntax().parent() {
343-
Some(parent) => parent,
344-
None => continue,
345-
};
346-
let name_ast = match ident_pat.name() {
347-
Some(name_ast) => name_ast,
348-
None => continue,
349-
};
341+
let ident_pat = ptr.to_node(&root);
342+
let parent = match ident_pat.syntax().parent() {
343+
Some(parent) => parent,
344+
None => continue,
345+
};
346+
let name_ast = match ident_pat.name() {
347+
Some(name_ast) => name_ast,
348+
None => continue,
349+
};
350+
351+
let is_param = ast::Param::can_cast(parent.kind());
352+
353+
// We have to check that it's either `let var = ...` or `var @ Variant(_)` statement,
354+
// because e.g. match arms are patterns as well.
355+
// In other words, we check that it's a named variable binding.
356+
let is_binding = ast::LetStmt::can_cast(parent.kind())
357+
|| (ast::MatchArm::can_cast(parent.kind())
358+
&& ident_pat.at_token().is_some());
359+
if !(is_param || is_binding) {
360+
// This pattern is not an actual variable declaration, e.g. `Some(val) => {..}` match arm.
361+
continue;
362+
}
350363

351-
let is_param = ast::Param::can_cast(parent.kind());
352-
353-
// We have to check that it's either `let var = ...` or `var @ Variant(_)` statement,
354-
// because e.g. match arms are patterns as well.
355-
// In other words, we check that it's a named variable binding.
356-
let is_binding = ast::LetStmt::can_cast(parent.kind())
357-
|| (ast::MatchArm::can_cast(parent.kind())
358-
&& ident_pat.at_token().is_some());
359-
if !(is_param || is_binding) {
360-
// This pattern is not an actual variable declaration, e.g. `Some(val) => {..}` match arm.
361-
continue;
362-
}
364+
let ident_type =
365+
if is_param { IdentType::Parameter } else { IdentType::Variable };
363366

364-
let ident_type =
365-
if is_param { IdentType::Parameter } else { IdentType::Variable };
366-
367-
let diagnostic = IncorrectCase {
368-
file: source_ptr.file_id,
369-
ident_type,
370-
ident: AstPtr::new(&name_ast),
371-
expected_case: replacement.expected_case,
372-
ident_text: replacement
373-
.current_name
374-
.display(self.db.upcast())
375-
.to_string(),
376-
suggested_text: replacement.suggested_text,
377-
};
367+
let diagnostic = IncorrectCase {
368+
file: source_ptr.file_id,
369+
ident_type,
370+
ident: AstPtr::new(&name_ast),
371+
expected_case: replacement.expected_case,
372+
ident_text: replacement.current_name.display(self.db.upcast()).to_string(),
373+
suggested_text: replacement.suggested_text,
374+
};
378375

379-
self.sink.push(diagnostic);
380-
}
376+
self.sink.push(diagnostic);
381377
}
382378
}
383379
}

Diff for: crates/hir-ty/src/mir/eval.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,7 @@ impl MirEvalError {
375375
Err(_) => continue,
376376
},
377377
MirSpan::PatId(p) => match source_map.pat_syntax(*p) {
378-
Ok(s) => s.map(|it| match it {
379-
Either::Left(e) => e.into(),
380-
Either::Right(e) => e.into(),
381-
}),
378+
Ok(s) => s.map(|it| it.syntax_node_ptr()),
382379
Err(_) => continue,
383380
},
384381
MirSpan::Unknown => continue,

Diff for: crates/hir-ty/src/tests.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,7 @@ fn pat_node(
269269
Some(match body_source_map.pat_syntax(pat) {
270270
Ok(sp) => {
271271
let root = db.parse_or_expand(sp.file_id);
272-
sp.map(|ptr| {
273-
ptr.either(
274-
|it| it.to_node(&root).syntax().clone(),
275-
|it| it.to_node(&root).syntax().clone(),
276-
)
277-
})
272+
sp.map(|ptr| ptr.to_node(&root).syntax().clone())
278273
}
279274
Err(SyntheticSyntax) => return None,
280275
})
@@ -303,12 +298,7 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
303298
let syntax_ptr = match body_source_map.pat_syntax(pat) {
304299
Ok(sp) => {
305300
let root = db.parse_or_expand(sp.file_id);
306-
sp.map(|ptr| {
307-
ptr.either(
308-
|it| it.to_node(&root).syntax().clone(),
309-
|it| it.to_node(&root).syntax().clone(),
310-
)
311-
})
301+
sp.map(|ptr| ptr.to_node(&root).syntax().clone())
312302
}
313303
Err(SyntheticSyntax) => continue,
314304
};

Diff for: crates/hir/src/diagnostics.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,19 @@ pub struct MalformedDerive {
174174

175175
#[derive(Debug)]
176176
pub struct NoSuchField {
177-
pub field: InFile<Either<AstPtr<ast::RecordExprField>, AstPtr<ast::RecordPatField>>>,
177+
pub field: InFile<AstPtr<Either<ast::RecordExprField, ast::RecordPatField>>>,
178178
pub private: bool,
179179
}
180180

181181
#[derive(Debug)]
182182
pub struct PrivateAssocItem {
183-
pub expr_or_pat:
184-
InFile<Either<AstPtr<ast::Expr>, Either<AstPtr<ast::Pat>, AstPtr<ast::SelfParam>>>>,
183+
pub expr_or_pat: InFile<AstPtr<Either<ast::Expr, Either<ast::Pat, ast::SelfParam>>>>,
185184
pub item: AssocItem,
186185
}
187186

188187
#[derive(Debug)]
189188
pub struct MismatchedTupleStructPatArgCount {
190-
pub expr_or_pat: InFile<Either<AstPtr<ast::Expr>, AstPtr<ast::Pat>>>,
189+
pub expr_or_pat: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
191190
pub expected: usize,
192191
pub found: usize,
193192
}
@@ -228,7 +227,7 @@ pub struct MissingUnsafe {
228227
#[derive(Debug)]
229228
pub struct MissingFields {
230229
pub file: HirFileId,
231-
pub field_list_parent: Either<AstPtr<ast::RecordExpr>, AstPtr<ast::RecordPat>>,
230+
pub field_list_parent: AstPtr<Either<ast::RecordExpr, ast::RecordPat>>,
232231
pub field_list_parent_path: Option<AstPtr<ast::Path>>,
233232
pub missed_fields: Vec<Name>,
234233
}
@@ -255,7 +254,7 @@ pub struct MissingMatchArms {
255254

256255
#[derive(Debug)]
257256
pub struct TypeMismatch {
258-
pub expr_or_pat: Either<InFile<AstPtr<ast::Expr>>, InFile<AstPtr<ast::Pat>>>,
257+
pub expr_or_pat: InFile<AstPtr<Either<ast::Expr, ast::Pat>>>,
259258
pub expected: Type,
260259
pub actual: Type,
261260
}

0 commit comments

Comments
 (0)