Skip to content

Commit

Permalink
Use PatKind::Error instead of PatKind::Wild to report errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Oct 13, 2023
1 parent 31e2640 commit 5eca57c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,12 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {

#[instrument(level = "trace", skip(self))]
fn check_irrefutable(&mut self, pat: &Pat<'tcx>, origin: &str, sp: Option<Span>) {
// If we got errors while lowering, don't emit anything more.
if let Err(err) = pat.pat_error_reported() {
self.error = Err(err);
return;
}

let mut cx = self.new_cx(self.lint_level, false);

let pattern = self.lower_pattern(&mut cx, pat);
Expand Down
41 changes: 19 additions & 22 deletions compiler/rustc_mir_build/src/thir/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {

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

hir::PatKind::Path(ref qpath) => {
Expand Down Expand Up @@ -423,9 +421,9 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
if adt_def.is_enum() {
let args = match ty.kind() {
ty::Adt(_, args) | ty::FnDef(_, args) => args,
ty::Error(_) => {
ty::Error(e) => {
// Avoid ICE (#50585)
return PatKind::Wild;
return PatKind::Error(*e);
}
_ => bug!("inappropriate type for def: {:?}", ty),
};
Expand All @@ -452,7 +450,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
| Res::SelfTyAlias { .. }
| Res::SelfCtor(..) => PatKind::Leaf { subpatterns },
_ => {
match res {
let e = match res {
Res::Def(DefKind::ConstParam, _) => {
self.tcx.sess.emit_err(ConstParamInPattern { span })
}
Expand All @@ -461,7 +459,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
}
_ => self.tcx.sess.emit_err(NonConstPath { span }),
};
PatKind::Wild
PatKind::Error(e)
}
};

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

self.tcx.sess.emit_err(AssocConstInPattern { span });

return pat_from_kind(PatKind::Wild);
let e = self.tcx.sess.emit_err(AssocConstInPattern { span });
return pat_from_kind(PatKind::Error(e));
}

Err(_) => {
self.tcx.sess.emit_err(CouldNotEvalConstPattern { span });
return pat_from_kind(PatKind::Wild);
let e = self.tcx.sess.emit_err(CouldNotEvalConstPattern { span });
return pat_from_kind(PatKind::Error(e));
}
};

Expand Down Expand Up @@ -574,12 +571,12 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
Err(ErrorHandled::TooGeneric(_)) => {
// While `Reported | Linted` cases will have diagnostics emitted already
// it is not true for TooGeneric case, so we need to give user more information.
self.tcx.sess.emit_err(ConstPatternDependsOnGenericParameter { span });
pat_from_kind(PatKind::Wild)
let e = self.tcx.sess.emit_err(ConstPatternDependsOnGenericParameter { span });
pat_from_kind(PatKind::Error(e))
}
Err(_) => {
self.tcx.sess.emit_err(CouldNotEvalConstPattern { span });
pat_from_kind(PatKind::Wild)
let e = self.tcx.sess.emit_err(CouldNotEvalConstPattern { span });
pat_from_kind(PatKind::Error(e))
}
}
}
Expand Down Expand Up @@ -629,7 +626,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
let uneval = mir::UnevaluatedConst { def: def_id.to_def_id(), args, promoted: None };
debug_assert!(!args.has_free_regions());

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

0 comments on commit 5eca57c

Please sign in to comment.