Skip to content

Commit 70e10de

Browse files
committed
Auto merge of #14326 - iDawer:refactor, r=Veykril
internal: Rename `hir::diagnostics::MissingMatchArms.match_expr` field `hir::diagnostics::MissingMatchArms.match_expr` had confusing name: it is pointing to scrutinee expression. Renamed to `scrutinee_expr` and used better fitting type for it. Also small refactorings/cleanup.
2 parents 6d42e75 + 17b9d35 commit 70e10de

File tree

4 files changed

+21
-23
lines changed

4 files changed

+21
-23
lines changed

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

+14-15
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl ExprValidator {
8484

8585
match expr {
8686
Expr::Match { expr, arms } => {
87-
self.validate_match(id, *expr, arms, db, self.infer.clone());
87+
self.validate_match(id, *expr, arms, db);
8888
}
8989
Expr::Call { .. } | Expr::MethodCall { .. } => {
9090
self.validate_call(db, id, expr, &mut filter_map_next_checker);
@@ -147,16 +147,15 @@ impl ExprValidator {
147147

148148
fn validate_match(
149149
&mut self,
150-
id: ExprId,
151150
match_expr: ExprId,
151+
scrutinee_expr: ExprId,
152152
arms: &[MatchArm],
153153
db: &dyn HirDatabase,
154-
infer: Arc<InferenceResult>,
155154
) {
156155
let body = db.body(self.owner);
157156

158-
let match_expr_ty = &infer[match_expr];
159-
if match_expr_ty.is_unknown() {
157+
let scrut_ty = &self.infer[scrutinee_expr];
158+
if scrut_ty.is_unknown() {
160159
return;
161160
}
162161

@@ -166,23 +165,23 @@ impl ExprValidator {
166165
let mut m_arms = Vec::with_capacity(arms.len());
167166
let mut has_lowering_errors = false;
168167
for arm in arms {
169-
if let Some(pat_ty) = infer.type_of_pat.get(arm.pat) {
168+
if let Some(pat_ty) = self.infer.type_of_pat.get(arm.pat) {
170169
// We only include patterns whose type matches the type
171-
// of the match expression. If we had an InvalidMatchArmPattern
170+
// of the scrutinee expression. If we had an InvalidMatchArmPattern
172171
// diagnostic or similar we could raise that in an else
173172
// block here.
174173
//
175174
// When comparing the types, we also have to consider that rustc
176-
// will automatically de-reference the match expression type if
175+
// will automatically de-reference the scrutinee expression type if
177176
// necessary.
178177
//
179178
// FIXME we should use the type checker for this.
180-
if (pat_ty == match_expr_ty
181-
|| match_expr_ty
179+
if (pat_ty == scrut_ty
180+
|| scrut_ty
182181
.as_reference()
183182
.map(|(match_expr_ty, ..)| match_expr_ty == pat_ty)
184183
.unwrap_or(false))
185-
&& types_of_subpatterns_do_match(arm.pat, &body, &infer)
184+
&& types_of_subpatterns_do_match(arm.pat, &body, &self.infer)
186185
{
187186
// If we had a NotUsefulMatchArm diagnostic, we could
188187
// check the usefulness of each pattern as we added it
@@ -206,16 +205,16 @@ impl ExprValidator {
206205
return;
207206
}
208207

209-
let report = compute_match_usefulness(&cx, &m_arms, match_expr_ty);
208+
let report = compute_match_usefulness(&cx, &m_arms, scrut_ty);
210209

211210
// FIXME Report unreacheble arms
212211
// https://github.com/rust-lang/rust/blob/f31622a50/compiler/rustc_mir_build/src/thir/pattern/check_match.rs#L200
213212

214213
let witnesses = report.non_exhaustiveness_witnesses;
215214
if !witnesses.is_empty() {
216215
self.diagnostics.push(BodyValidationDiagnostic::MissingMatchArms {
217-
match_expr: id,
218-
uncovered_patterns: missing_match_arms(&cx, match_expr_ty, witnesses, arms),
216+
match_expr,
217+
uncovered_patterns: missing_match_arms(&cx, scrut_ty, witnesses, arms),
219218
});
220219
}
221220
}
@@ -379,7 +378,7 @@ fn missing_match_arms<'p>(
379378
arms: &[MatchArm],
380379
) -> String {
381380
struct DisplayWitness<'a, 'p>(&'a DeconstructedPat<'p>, &'a MatchCheckCtx<'a, 'p>);
382-
impl<'a, 'p> fmt::Display for DisplayWitness<'a, 'p> {
381+
impl fmt::Display for DisplayWitness<'_, '_> {
383382
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
384383
let DisplayWitness(witness, cx) = *self;
385384
let pat = witness.to_pat(cx);

crates/hir/src/diagnostics.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ pub struct MismatchedArgCount {
199199

200200
#[derive(Debug)]
201201
pub struct MissingMatchArms {
202-
pub file: HirFileId,
203-
pub match_expr: AstPtr<ast::Expr>,
202+
pub scrutinee_expr: InFile<AstPtr<ast::Expr>>,
204203
pub uncovered_patterns: String,
205204
}
206205

crates/hir/src/lib.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1628,11 +1628,13 @@ impl DefWithBody {
16281628
if let ast::Expr::MatchExpr(match_expr) =
16291629
&source_ptr.value.to_node(&root)
16301630
{
1631-
if let Some(match_expr) = match_expr.expr() {
1631+
if let Some(scrut_expr) = match_expr.expr() {
16321632
acc.push(
16331633
MissingMatchArms {
1634-
file: source_ptr.file_id,
1635-
match_expr: AstPtr::new(&match_expr),
1634+
scrutinee_expr: InFile::new(
1635+
source_ptr.file_id,
1636+
AstPtr::new(&scrut_expr),
1637+
),
16361638
uncovered_patterns,
16371639
}
16381640
.into(),

crates/ide-diagnostics/src/handlers/missing_match_arms.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use hir::InFile;
2-
31
use crate::{Diagnostic, DiagnosticsContext};
42

53
// Diagnostic: missing-match-arm
@@ -12,7 +10,7 @@ pub(crate) fn missing_match_arms(
1210
Diagnostic::new(
1311
"missing-match-arm",
1412
format!("missing match arm: {}", d.uncovered_patterns),
15-
ctx.sema.diagnostics_display_range(InFile::new(d.file, d.match_expr.clone().into())).range,
13+
ctx.sema.diagnostics_display_range(d.scrutinee_expr.clone().map(Into::into)).range,
1614
)
1715
}
1816

0 commit comments

Comments
 (0)