Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
- Remove check for "how many path segments is the pattern"
- Check before suggesting if the path has multiple path segments
  • Loading branch information
estebank committed Nov 13, 2024
1 parent 83bf37e commit d9cb691
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
9 changes: 8 additions & 1 deletion compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,9 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
..
} = pat.kind
&& let DefKind::Const = self.tcx.def_kind(def_id)
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span)
// We filter out paths with multiple path::segments.
&& snippet.chars().all(|c| c.is_alphanumeric() || c == '_')
{
let span = self.tcx.def_span(def_id);
let variable = self.tcx.item_name(def_id).to_string();
Expand Down Expand Up @@ -1143,7 +1146,11 @@ fn report_non_exhaustive_match<'p, 'tcx>(

for &arm in arms {
let arm = &thir.arms[arm];
if let PatKind::ExpandedConstant { def_id, is_inline: false, .. } = arm.pattern.kind {
if let PatKind::ExpandedConstant { def_id, is_inline: false, .. } = arm.pattern.kind
&& let Ok(snippet) = cx.tcx.sess.source_map().span_to_snippet(arm.pattern.span)
// We filter out paths with multiple path::segments.
&& snippet.chars().all(|c| c.is_alphanumeric() || c == '_')
{
let const_name = cx.tcx.item_name(def_id);
err.span_label(
arm.pattern.span,
Expand Down
40 changes: 20 additions & 20 deletions compiler/rustc_mir_build/src/thir/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,19 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
}
kind => (kind, None, None),
};
let value = if let PatKind::Constant { value } = kind {
value
} else {
let msg = format!(
"found bad range pattern endpoint `{expr:?}` outside of error recovery"
);
return Err(self.tcx.dcx().span_delayed_bug(expr.span, msg));
let value = match kind {
PatKind::Constant { value } => value,
PatKind::ExpandedConstant { subpattern, .. }
if let PatKind::Constant { value } = subpattern.kind =>
{
value
}
_ => {
let msg = format!(
"found bad range pattern endpoint `{expr:?}` outside of error recovery"
);
return Err(self.tcx.dcx().span_delayed_bug(expr.span, msg));
}
};
Ok((Some(PatRangeBoundary::Finite(value)), ascr, inline_const))
}
Expand Down Expand Up @@ -570,19 +576,13 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
let args = self.typeck_results.node_args(id);
let c = ty::Const::new_unevaluated(self.tcx, ty::UnevaluatedConst { def: def_id, args });
let subpattern = self.const_to_pat(c, ty, id, span);
let pattern = if let hir::QPath::Resolved(None, path) = qpath
&& path.segments.len() == 1
{
// We only want to mark constants when referenced as bare names that could have been
// new bindings if the `const` didn't exist.
Box::new(Pat {
span,
ty,
kind: PatKind::ExpandedConstant { subpattern, def_id, is_inline: false },
})
} else {
subpattern
};
// We only want to mark constants when referenced as bare names that could have been
// new bindings if the `const` didn't exist.
let pattern = Box::new(Pat {
span,
ty,
kind: PatKind::ExpandedConstant { subpattern, def_id, is_inline: false },
});

if !is_associated_const {
return pattern;
Expand Down

0 comments on commit d9cb691

Please sign in to comment.