Skip to content

Commit 6480b76

Browse files
committed
review comments
- Remove check for "how many path segments is the pattern" - Check before suggesting if the path has multiple path segments
1 parent bb37e5d commit 6480b76

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,9 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
677677
..
678678
} = pat.kind
679679
&& let DefKind::Const = self.tcx.def_kind(def_id)
680+
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span)
681+
// We filter out paths with multiple path::segments.
682+
&& snippet.chars().all(|c| c.is_alphanumeric() || c == '_')
680683
{
681684
let span = self.tcx.def_span(def_id);
682685
let variable = self.tcx.item_name(def_id).to_string();
@@ -1143,7 +1146,11 @@ fn report_non_exhaustive_match<'p, 'tcx>(
11431146

11441147
for &arm in arms {
11451148
let arm = &thir.arms[arm];
1146-
if let PatKind::ExpandedConstant { def_id, is_inline: false, .. } = arm.pattern.kind {
1149+
if let PatKind::ExpandedConstant { def_id, is_inline: false, .. } = arm.pattern.kind
1150+
&& let Ok(snippet) = cx.tcx.sess.source_map().span_to_snippet(arm.pattern.span)
1151+
// We filter out paths with multiple path::segments.
1152+
&& snippet.chars().all(|c| c.is_alphanumeric() || c == '_')
1153+
{
11471154
let const_name = cx.tcx.item_name(def_id);
11481155
err.span_label(
11491156
arm.pattern.span,

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

+18-20
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,19 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
160160
}
161161
kind => (kind, None, None),
162162
};
163-
let value = if let PatKind::Constant { value } = kind {
164-
value
165-
} else {
166-
let msg = format!(
167-
"found bad range pattern endpoint `{expr:?}` outside of error recovery"
168-
);
169-
return Err(self.tcx.dcx().span_delayed_bug(expr.span, msg));
163+
let value = match kind {
164+
PatKind::Constant { value } => value,
165+
PatKind::ExpandedConstant { subpattern, .. }
166+
if let PatKind::Constant { value } = subpattern.kind =>
167+
{
168+
value
169+
}
170+
_ => {
171+
let msg = format!(
172+
"found bad range pattern endpoint `{expr:?}` outside of error recovery"
173+
);
174+
return Err(self.tcx.dcx().span_delayed_bug(expr.span, msg));
175+
}
170176
};
171177
Ok((Some(PatRangeBoundary::Finite(value)), ascr, inline_const))
172178
}
@@ -570,19 +576,11 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
570576
let args = self.typeck_results.node_args(id);
571577
let c = ty::Const::new_unevaluated(self.tcx, ty::UnevaluatedConst { def: def_id, args });
572578
let subpattern = self.const_to_pat(c, ty, id, span);
573-
let pattern = if let hir::QPath::Resolved(None, path) = qpath
574-
&& path.segments.len() == 1
575-
{
576-
// We only want to mark constants when referenced as bare names that could have been
577-
// new bindings if the `const` didn't exist.
578-
Box::new(Pat {
579-
span,
580-
ty,
581-
kind: PatKind::ExpandedConstant { subpattern, def_id, is_inline: false },
582-
})
583-
} else {
584-
subpattern
585-
};
579+
let pattern = Box::new(Pat {
580+
span,
581+
ty,
582+
kind: PatKind::ExpandedConstant { subpattern, def_id, is_inline: false },
583+
});
586584

587585
if !is_associated_const {
588586
return pattern;

0 commit comments

Comments
 (0)