Skip to content

Commit 8646afb

Browse files
committed
Use PatKind::Error instead of PatKind::Wild to report errors
1 parent aab3b93 commit 8646afb

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,12 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {
426426

427427
#[instrument(level = "trace", skip(self))]
428428
fn check_irrefutable(&mut self, pat: &Pat<'tcx>, origin: &str, sp: Option<Span>) {
429+
// If we got errors while lowering, don't emit anything more.
430+
if let Err(err) = pat.pat_error_reported() {
431+
self.error = Err(err);
432+
return;
433+
}
434+
429435
let mut cx = self.new_cx(self.lint_level, false);
430436

431437
let pattern = self.lower_pattern(&mut cx, pat);

compiler/rustc_mir_build/src/thir/pattern/mod.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
252252

253253
hir::PatKind::Range(ref lo_expr, ref hi_expr, end) => {
254254
let (lo_expr, hi_expr) = (lo_expr.as_deref(), hi_expr.as_deref());
255-
// FIXME?: returning `_` can cause inaccurate "unreachable" warnings. This can be
256-
// fixed by returning `PatKind::Const(ConstKind::Error(...))` if #115937 gets
257-
// merged.
258-
self.lower_pattern_range(lo_expr, hi_expr, end, ty, span).unwrap_or(PatKind::Wild)
255+
self.lower_pattern_range(lo_expr, hi_expr, end, ty, span)
256+
.unwrap_or_else(PatKind::Error)
259257
}
260258

261259
hir::PatKind::Path(ref qpath) => {
@@ -423,9 +421,9 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
423421
if adt_def.is_enum() {
424422
let args = match ty.kind() {
425423
ty::Adt(_, args) | ty::FnDef(_, args) => args,
426-
ty::Error(_) => {
424+
ty::Error(e) => {
427425
// Avoid ICE (#50585)
428-
return PatKind::Wild;
426+
return PatKind::Error(*e);
429427
}
430428
_ => bug!("inappropriate type for def: {:?}", ty),
431429
};
@@ -452,7 +450,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
452450
| Res::SelfTyAlias { .. }
453451
| Res::SelfCtor(..) => PatKind::Leaf { subpatterns },
454452
_ => {
455-
match res {
453+
let e = match res {
456454
Res::Def(DefKind::ConstParam, _) => {
457455
self.tcx.sess.emit_err(ConstParamInPattern { span })
458456
}
@@ -461,7 +459,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
461459
}
462460
_ => self.tcx.sess.emit_err(NonConstPath { span }),
463461
};
464-
PatKind::Wild
462+
PatKind::Error(e)
465463
}
466464
};
467465

@@ -513,14 +511,13 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
513511
// It should be assoc consts if there's no error but we cannot resolve it.
514512
debug_assert!(is_associated_const);
515513

516-
self.tcx.sess.emit_err(AssocConstInPattern { span });
517-
518-
return pat_from_kind(PatKind::Wild);
514+
let e = self.tcx.sess.emit_err(AssocConstInPattern { span });
515+
return pat_from_kind(PatKind::Error(e));
519516
}
520517

521518
Err(_) => {
522-
self.tcx.sess.emit_err(CouldNotEvalConstPattern { span });
523-
return pat_from_kind(PatKind::Wild);
519+
let e = self.tcx.sess.emit_err(CouldNotEvalConstPattern { span });
520+
return pat_from_kind(PatKind::Error(e));
524521
}
525522
};
526523

@@ -574,12 +571,12 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
574571
Err(ErrorHandled::TooGeneric(_)) => {
575572
// While `Reported | Linted` cases will have diagnostics emitted already
576573
// it is not true for TooGeneric case, so we need to give user more information.
577-
self.tcx.sess.emit_err(ConstPatternDependsOnGenericParameter { span });
578-
pat_from_kind(PatKind::Wild)
574+
let e = self.tcx.sess.emit_err(ConstPatternDependsOnGenericParameter { span });
575+
pat_from_kind(PatKind::Error(e))
579576
}
580577
Err(_) => {
581-
self.tcx.sess.emit_err(CouldNotEvalConstPattern { span });
582-
pat_from_kind(PatKind::Wild)
578+
let e = self.tcx.sess.emit_err(CouldNotEvalConstPattern { span });
579+
pat_from_kind(PatKind::Error(e))
583580
}
584581
}
585582
}
@@ -629,7 +626,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
629626
let uneval = mir::UnevaluatedConst { def: def_id.to_def_id(), args, promoted: None };
630627
debug_assert!(!args.has_free_regions());
631628

632-
let ct = ty::UnevaluatedConst { def: def_id.to_def_id(), args: args };
629+
let ct = ty::UnevaluatedConst { def: def_id.to_def_id(), args };
633630
// First try using a valtree in order to destructure the constant into a pattern.
634631
// FIXME: replace "try to do a thing, then fall back to another thing"
635632
// but something more principled, like a trait query checking whether this can be turned into a valtree.
@@ -649,10 +646,10 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
649646
Ok(val) => self.const_to_pat(mir::Const::Val(val, ty), id, span, None).kind,
650647
Err(ErrorHandled::TooGeneric(_)) => {
651648
// If we land here it means the const can't be evaluated because it's `TooGeneric`.
652-
self.tcx.sess.emit_err(ConstPatternDependsOnGenericParameter { span });
653-
PatKind::Wild
649+
let e = self.tcx.sess.emit_err(ConstPatternDependsOnGenericParameter { span });
650+
PatKind::Error(e)
654651
}
655-
Err(ErrorHandled::Reported(..)) => PatKind::Wild,
652+
Err(ErrorHandled::Reported(err, ..)) => PatKind::Error(err.into()),
656653
}
657654
}
658655
}
@@ -685,7 +682,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
685682
Ok(constant) => {
686683
self.const_to_pat(Const::Ty(constant), expr.hir_id, lit.span, None).kind
687684
}
688-
Err(LitToConstError::Reported(_)) => PatKind::Wild,
685+
Err(LitToConstError::Reported(e)) => PatKind::Error(e),
689686
Err(LitToConstError::TypeError) => bug!("lower_lit: had type error"),
690687
}
691688
}

0 commit comments

Comments
 (0)