Skip to content

Commit 50e6224

Browse files
authored
Rollup merge of #150498 - Zalathar:no-const-pat, r=BoxyUwU
mir_build: Remove several remnants of `#![feature(inline_const_pat)]` This PR cleans up some THIR-related code that was only needed for inline `const { .. }` blocks in patterns. The `inline_const_pat` feature was removed in #138492 due to implementation concerns. I considered retaining the code for preserving `ExpandedConstant` for range endpoints, but ultimately decided to remove that too, because I found it very awkward to document an edge case that is currently not needed by any subsequent code. With this PR, `is_const_pat_that_looks_like_binding` is the only function that meaningfully consumes `thir::PatKind::ExpandedConst`.
2 parents de21e04 + 89f7d70 commit 50e6224

5 files changed

Lines changed: 43 additions & 43 deletions

File tree

compiler/rustc_middle/src/thir.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -832,18 +832,18 @@ pub enum PatKind<'tcx> {
832832
value: ty::Value<'tcx>,
833833
},
834834

835-
/// Pattern obtained by converting a constant (inline or named) to its pattern
836-
/// representation using `const_to_pat`. This is used for unsafety checking.
835+
/// Wrapper node representing a named constant that was lowered to a pattern
836+
/// using `const_to_pat`.
837+
///
838+
/// This is used by some diagnostics for non-exhaustive matches, to map
839+
/// the pattern node back to the `DefId` of its original constant.
840+
///
841+
/// FIXME(#150498): Can we make this an `Option<DefId>` field on `Pat`
842+
/// instead, so that non-diagnostic code can ignore it more easily?
837843
ExpandedConstant {
838844
/// [DefId] of the constant item.
839845
def_id: DefId,
840846
/// The pattern that the constant lowered to.
841-
///
842-
/// HACK: we need to keep the `DefId` of inline constants around for unsafety checking;
843-
/// therefore when a range pattern contains inline constants, we re-wrap the range pattern
844-
/// with the `ExpandedConstant` nodes that correspond to the range endpoints. Hence
845-
/// `subpattern` may actually be a range pattern, and `def_id` be the constant for one of
846-
/// its endpoints.
847847
subpattern: Box<Pat<'tcx>>,
848848
},
849849

compiler/rustc_mir_build/src/check_unsafety.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,6 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
410410
visit::walk_pat(self, pat);
411411
self.inside_adt = old_inside_adt;
412412
}
413-
PatKind::ExpandedConstant { def_id, .. } => {
414-
if let Some(def) = def_id.as_local()
415-
&& matches!(self.tcx.def_kind(def_id), DefKind::InlineConst)
416-
{
417-
self.visit_inner_body(def);
418-
}
419-
visit::walk_pat(self, pat);
420-
}
421413
_ => {
422414
visit::walk_pat(self, pat);
423415
}

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
55
use rustc_errors::codes::*;
66
use rustc_errors::{Applicability, ErrorGuaranteed, MultiSpan, struct_span_code_err};
77
use rustc_hir::def::*;
8-
use rustc_hir::def_id::LocalDefId;
8+
use rustc_hir::def_id::{DefId, LocalDefId};
99
use rustc_hir::{self as hir, BindingMode, ByRef, HirId, MatchSource};
1010
use rustc_infer::infer::TyCtxtInferExt;
1111
use rustc_lint::Level;
@@ -687,12 +687,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
687687
unpeeled_pat = subpattern;
688688
}
689689

690-
if let PatKind::ExpandedConstant { def_id, .. } = unpeeled_pat.kind
691-
&& let DefKind::Const = self.tcx.def_kind(def_id)
692-
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span)
693-
// We filter out paths with multiple path::segments.
694-
&& snippet.chars().all(|c| c.is_alphanumeric() || c == '_')
695-
{
690+
if let Some(def_id) = is_const_pat_that_looks_like_binding(self.tcx, unpeeled_pat) {
696691
let span = self.tcx.def_span(def_id);
697692
let variable = self.tcx.item_name(def_id).to_string();
698693
// When we encounter a constant as the binding name, point at the `const` definition.
@@ -1209,6 +1204,26 @@ fn pat_is_catchall(pat: &DeconstructedPat<'_, '_>) -> bool {
12091204
}
12101205
}
12111206

1207+
/// If the given pattern is a named constant that looks like it could have been
1208+
/// intended to be a binding, returns the `DefId` of the named constant.
1209+
///
1210+
/// Diagnostics use this to give more detailed suggestions for non-exhaustive
1211+
/// matches.
1212+
fn is_const_pat_that_looks_like_binding<'tcx>(tcx: TyCtxt<'tcx>, pat: &Pat<'tcx>) -> Option<DefId> {
1213+
// The pattern must be a named constant, and the name that appears in
1214+
// the pattern's source text must resemble a plain identifier without any
1215+
// `::` namespace separators or other non-identifier characters.
1216+
if let PatKind::ExpandedConstant { def_id, .. } = pat.kind
1217+
&& matches!(tcx.def_kind(def_id), DefKind::Const)
1218+
&& let Ok(snippet) = tcx.sess.source_map().span_to_snippet(pat.span)
1219+
&& snippet.chars().all(|c| c.is_alphanumeric() || c == '_')
1220+
{
1221+
Some(def_id)
1222+
} else {
1223+
None
1224+
}
1225+
}
1226+
12121227
/// Report that a match is not exhaustive.
12131228
fn report_non_exhaustive_match<'p, 'tcx>(
12141229
cx: &PatCtxt<'p, 'tcx>,
@@ -1303,12 +1318,7 @@ fn report_non_exhaustive_match<'p, 'tcx>(
13031318

13041319
for &arm in arms {
13051320
let arm = &thir.arms[arm];
1306-
if let PatKind::ExpandedConstant { def_id, .. } = arm.pattern.kind
1307-
&& !matches!(cx.tcx.def_kind(def_id), DefKind::InlineConst)
1308-
&& let Ok(snippet) = cx.tcx.sess.source_map().span_to_snippet(arm.pattern.span)
1309-
// We filter out paths with multiple path::segments.
1310-
&& snippet.chars().all(|c| c.is_alphanumeric() || c == '_')
1311-
{
1321+
if let Some(def_id) = is_const_pat_that_looks_like_binding(cx.tcx, &arm.pattern) {
13121322
let const_name = cx.tcx.item_name(def_id);
13131323
err.span_label(
13141324
arm.pattern.span,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl<'tcx> ConstToPat<'tcx> {
187187
}
188188

189189
// Wrap the pattern in a marker node to indicate that it is the result of lowering a
190-
// constant. This is used for diagnostics, and for unsafety checking of inline const blocks.
190+
// constant. This is used for diagnostics.
191191
let kind = PatKind::ExpandedConstant { subpattern: inlined_const_as_pat, def_id: uv.def };
192192
Box::new(Pat { kind, ty, span: self.span })
193193
}

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use rustc_middle::ty::adjustment::{PatAdjust, PatAdjustment};
2121
use rustc_middle::ty::layout::IntegerExt;
2222
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt};
2323
use rustc_middle::{bug, span_bug};
24-
use rustc_span::def_id::DefId;
2524
use rustc_span::{ErrorGuaranteed, Span};
2625
use tracing::{debug, instrument};
2726

@@ -131,9 +130,8 @@ impl<'tcx> PatCtxt<'tcx> {
131130
fn lower_pattern_range_endpoint(
132131
&mut self,
133132
expr: Option<&'tcx hir::PatExpr<'tcx>>,
134-
// Out-parameters collecting extra data to be reapplied by the caller
133+
// Out-parameter collecting extra data to be reapplied by the caller
135134
ascriptions: &mut Vec<Ascription<'tcx>>,
136-
expanded_consts: &mut Vec<DefId>,
137135
) -> Result<Option<PatRangeBoundary<'tcx>>, ErrorGuaranteed> {
138136
let Some(expr) = expr else { return Ok(None) };
139137

@@ -148,8 +146,10 @@ impl<'tcx> PatCtxt<'tcx> {
148146
ascriptions.push(ascription);
149147
kind = subpattern.kind;
150148
}
151-
PatKind::ExpandedConstant { def_id, subpattern } => {
152-
expanded_consts.push(def_id);
149+
PatKind::ExpandedConstant { def_id: _, subpattern } => {
150+
// Expanded-constant nodes are currently only needed by
151+
// diagnostics that don't apply to range patterns, so we
152+
// can just discard them here.
153153
kind = subpattern.kind;
154154
}
155155
_ => break,
@@ -227,10 +227,7 @@ impl<'tcx> PatCtxt<'tcx> {
227227

228228
// Collect extra data while lowering the endpoints, to be reapplied later.
229229
let mut ascriptions = vec![];
230-
let mut expanded_consts = vec![];
231-
232-
let mut lower_endpoint =
233-
|expr| self.lower_pattern_range_endpoint(expr, &mut ascriptions, &mut expanded_consts);
230+
let mut lower_endpoint = |expr| self.lower_pattern_range_endpoint(expr, &mut ascriptions);
234231

235232
let lo = lower_endpoint(lo_expr)?.unwrap_or(PatRangeBoundary::NegInfinity);
236233
let hi = lower_endpoint(hi_expr)?.unwrap_or(PatRangeBoundary::PosInfinity);
@@ -282,10 +279,11 @@ impl<'tcx> PatCtxt<'tcx> {
282279
let subpattern = Box::new(Pat { span, ty, kind });
283280
kind = PatKind::AscribeUserType { ascription, subpattern };
284281
}
285-
for def_id in expanded_consts {
286-
let subpattern = Box::new(Pat { span, ty, kind });
287-
kind = PatKind::ExpandedConstant { def_id, subpattern };
288-
}
282+
// `PatKind::ExpandedConstant` wrappers from range endpoints used to
283+
// also be preserved here, but that was only needed for unsafeck of
284+
// inline `const { .. }` patterns, which were removed by
285+
// <https://github.com/rust-lang/rust/pull/138492>.
286+
289287
Ok(kind)
290288
}
291289

0 commit comments

Comments
 (0)