Skip to content

Commit d2d653c

Browse files
internal: reduce body lookups in expr diagnostics
1 parent 9d8889c commit d2d653c

File tree

1 file changed

+8
-12
lines changed
  • crates/hir-ty/src/diagnostics

1 file changed

+8
-12
lines changed

crates/hir-ty/src/diagnostics/expr.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,25 @@ impl BodyValidationDiagnostic {
5050
pub fn collect(db: &dyn HirDatabase, owner: DefWithBodyId) -> Vec<BodyValidationDiagnostic> {
5151
let _p = profile::span("BodyValidationDiagnostic::collect");
5252
let infer = db.infer(owner);
53-
let mut validator = ExprValidator::new(owner, infer);
53+
let body = db.body(owner);
54+
let mut validator = ExprValidator { owner, body, infer, diagnostics: Vec::new() };
5455
validator.validate_body(db);
5556
validator.diagnostics
5657
}
5758
}
5859

5960
struct ExprValidator {
6061
owner: DefWithBodyId,
62+
body: Arc<Body>,
6163
infer: Arc<InferenceResult>,
6264
pub(super) diagnostics: Vec<BodyValidationDiagnostic>,
6365
}
6466

6567
impl ExprValidator {
66-
fn new(owner: DefWithBodyId, infer: Arc<InferenceResult>) -> ExprValidator {
67-
ExprValidator { owner, infer, diagnostics: Vec::new() }
68-
}
69-
7068
fn validate_body(&mut self, db: &dyn HirDatabase) {
71-
let body = db.body(self.owner);
7269
let mut filter_map_next_checker = None;
70+
// we'll pass &mut self while iterating over body.exprs, so they need to be disjoint
71+
let body = Arc::clone(&self.body);
7372

7473
for (id, expr) in body.exprs.iter() {
7574
if let Some((variant, missed_fields, true)) =
@@ -152,8 +151,6 @@ impl ExprValidator {
152151
arms: &[MatchArm],
153152
db: &dyn HirDatabase,
154153
) {
155-
let body = db.body(self.owner);
156-
157154
let scrut_ty = &self.infer[scrutinee_expr];
158155
if scrut_ty.is_unknown() {
159156
return;
@@ -181,13 +178,13 @@ impl ExprValidator {
181178
.as_reference()
182179
.map(|(match_expr_ty, ..)| match_expr_ty == pat_ty)
183180
.unwrap_or(false))
184-
&& types_of_subpatterns_do_match(arm.pat, &body, &self.infer)
181+
&& types_of_subpatterns_do_match(arm.pat, &self.body, &self.infer)
185182
{
186183
// If we had a NotUsefulMatchArm diagnostic, we could
187184
// check the usefulness of each pattern as we added it
188185
// to the matrix here.
189186
let m_arm = match_check::MatchArm {
190-
pat: self.lower_pattern(&cx, arm.pat, db, &body, &mut has_lowering_errors),
187+
pat: self.lower_pattern(&cx, arm.pat, db, &mut has_lowering_errors),
191188
has_guard: arm.guard.is_some(),
192189
};
193190
m_arms.push(m_arm);
@@ -224,10 +221,9 @@ impl ExprValidator {
224221
cx: &MatchCheckCtx<'_, 'p>,
225222
pat: PatId,
226223
db: &dyn HirDatabase,
227-
body: &Body,
228224
have_errors: &mut bool,
229225
) -> &'p DeconstructedPat<'p> {
230-
let mut patcx = match_check::PatCtxt::new(db, &self.infer, body);
226+
let mut patcx = match_check::PatCtxt::new(db, &self.infer, &self.body);
231227
let pattern = patcx.lower_pattern(pat);
232228
let pattern = cx.pattern_arena.alloc(DeconstructedPat::from_pat(cx, &pattern));
233229
if !patcx.errors.is_empty() {

0 commit comments

Comments
 (0)