Skip to content

Commit c25b44b

Browse files
committed
Fold PatKind::NamedConstant into PatKind::Constant
1 parent ff2f7a7 commit c25b44b

File tree

12 files changed

+21
-33
lines changed

12 files changed

+21
-33
lines changed

compiler/rustc_middle/src/thir.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,6 @@ impl<'tcx> Pat<'tcx> {
640640
| Range(..)
641641
| Binding { subpattern: None, .. }
642642
| Constant { .. }
643-
| NamedConstant { .. }
644643
| Error(_) => {}
645644
AscribeUserType { subpattern, .. }
646645
| Binding { subpattern: Some(subpattern), .. }
@@ -787,12 +786,8 @@ pub enum PatKind<'tcx> {
787786
/// * `String`, if `string_deref_patterns` is enabled.
788787
Constant {
789788
value: mir::Const<'tcx>,
790-
},
791-
792-
/// Same as `Constant`, but that came from a `const` that we can point at in diagnostics.
793-
NamedConstant {
794-
value: mir::Const<'tcx>,
795-
span: Span,
789+
/// The `const` item this constant came from, if any.
790+
opt_def: Option<DefId>,
796791
},
797792

798793
/// Inline constant found while lowering a pattern.

compiler/rustc_middle/src/thir/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ pub fn walk_pat<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
246246
visitor.visit_pat(&subpattern.pattern);
247247
}
248248
}
249-
Constant { value: _ } | NamedConstant { value: _, span: _ } => {}
249+
Constant { value: _, opt_def: _ } => {}
250250
InlineConstant { def: _, subpattern } => visitor.visit_pat(subpattern),
251251
Range(_) => {}
252252
Slice { prefix, slice, suffix } | Array { prefix, slice, suffix } => {

compiler/rustc_mir_build/src/build/custom/parse/instruction.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,7 @@ impl<'a, 'tcx> ParseCtxt<'a, 'tcx> {
144144
let mut targets = Vec::new();
145145
for arm in rest {
146146
let arm = &self.thir[*arm];
147-
let (PatKind::Constant { value } | PatKind::NamedConstant { value, span: _ }) =
148-
arm.pattern.kind
149-
else {
147+
let PatKind::Constant { value, opt_def: _ } = arm.pattern.kind else {
150148
return Err(ParseError {
151149
span: arm.pattern.span,
152150
item_description: format!("{:?}", arm.pattern.kind),

compiler/rustc_mir_build/src/build/matches/match_pair.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,7 @@ impl<'pat, 'tcx> MatchPairTree<'pat, 'tcx> {
129129
}
130130
}
131131

132-
PatKind::Constant { value } | PatKind::NamedConstant { value, span: _ } => {
133-
TestCase::Constant { value }
134-
}
132+
PatKind::Constant { value, opt_def: _ } => TestCase::Constant { value },
135133

136134
PatKind::AscribeUserType {
137135
ascription: thir::Ascription { ref annotation, variance },

compiler/rustc_mir_build/src/build/matches/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
882882
}
883883

884884
PatKind::Constant { .. }
885-
| PatKind::NamedConstant { .. }
886885
| PatKind::Range { .. }
887886
| PatKind::Wild
888887
| PatKind::Never

compiler/rustc_mir_build/src/check_unsafety.rs

-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
316316
PatKind::Binding { .. }
317317
// match is conditional on having this value
318318
| PatKind::Constant { .. }
319-
| PatKind::NamedConstant { .. }
320319
| PatKind::Variant { .. }
321320
| PatKind::Leaf { .. }
322321
| PatKind::Deref { .. }

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -670,13 +670,14 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
670670
let mut interpreted_as_const = None;
671671
let mut interpreted_as_const_sugg = None;
672672

673-
if let PatKind::NamedConstant { span, .. }
673+
if let PatKind::Constant { opt_def: Some(def_id), .. }
674674
| PatKind::AscribeUserType {
675-
subpattern: box Pat { kind: PatKind::NamedConstant { span, .. }, .. },
675+
subpattern: box Pat { kind: PatKind::Constant { opt_def: Some(def_id), .. }, .. },
676676
..
677677
} = pat.kind
678678
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span)
679679
{
680+
let span = self.tcx.def_span(def_id);
680681
// When we encounter a constant as the binding name, point at the `const` definition.
681682
interpreted_as_const = Some(span);
682683
interpreted_as_const_sugg =

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ impl<'tcx> ConstToPat<'tcx> {
266266
// optimization for now.
267267
ty::Str => PatKind::Constant {
268268
value: mir::Const::Ty(ty, ty::Const::new_value(tcx, cv, ty)),
269+
opt_def: None,
269270
},
270271
// All other references are converted into deref patterns and then recursively
271272
// convert the dereferenced constant to a pattern that is the sub-pattern of the
@@ -311,13 +312,17 @@ impl<'tcx> ConstToPat<'tcx> {
311312
} else {
312313
PatKind::Constant {
313314
value: mir::Const::Ty(ty, ty::Const::new_value(tcx, cv, ty)),
315+
opt_def: None,
314316
}
315317
}
316318
}
317319
ty::Pat(..) | ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::RawPtr(..) => {
318320
// The raw pointers we see here have been "vetted" by valtree construction to be
319321
// just integers, so we simply allow them.
320-
PatKind::Constant { value: mir::Const::Ty(ty, ty::Const::new_value(tcx, cv, ty)) }
322+
PatKind::Constant {
323+
value: mir::Const::Ty(ty, ty::Const::new_value(tcx, cv, ty)),
324+
opt_def: None,
325+
}
321326
}
322327
ty::FnPtr(..) => {
323328
unreachable!(

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

+4-9
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
157157
}
158158
kind => (kind, None, None),
159159
};
160-
let value = if let PatKind::Constant { value }
161-
| PatKind::NamedConstant { value, span: _ } = kind
162-
{
160+
let value = if let PatKind::Constant { value, opt_def: _ } = kind {
163161
value
164162
} else {
165163
let msg = format!(
@@ -253,7 +251,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
253251
(RangeEnd::Included, Some(Ordering::Less)) => {}
254252
// `x..=y` where `x == y` and `x` and `y` are finite.
255253
(RangeEnd::Included, Some(Ordering::Equal)) if lo.is_finite() && hi.is_finite() => {
256-
kind = PatKind::Constant { value: lo.as_finite().unwrap() };
254+
kind = PatKind::Constant { value: lo.as_finite().unwrap(), opt_def: None };
257255
}
258256
// `..=x` where `x == ty::MIN`.
259257
(RangeEnd::Included, Some(Ordering::Equal)) if !lo.is_finite() => {}
@@ -562,15 +560,12 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
562560
_ => return pat_from_kind(self.lower_variant_or_leaf(res, id, span, ty, vec![])),
563561
};
564562

565-
// HERE
566563
let args = self.typeck_results.node_args(id);
567564
let c = ty::Const::new_unevaluated(self.tcx, ty::UnevaluatedConst { def: def_id, args });
568-
let def_span = self.tcx.def_span(def_id);
569565
let mut pattern = self.const_to_pat(c, ty, id, span);
570-
if let PatKind::Constant { value } = pattern.kind {
571-
pattern.kind = PatKind::NamedConstant { value, span: def_span };
566+
if let PatKind::Constant { value, opt_def: None } = pattern.kind {
567+
pattern.kind = PatKind::Constant { value, opt_def: Some(def_id) };
572568
}
573-
tracing::info!("pattern {pattern:#?} {c:?} {ty:?} {id:?}");
574569

575570
if !is_associated_const {
576571
return pattern;

compiler/rustc_mir_build/src/thir/print.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
702702
self.print_pat(subpattern, depth_lvl + 2);
703703
print_indented!(self, "}", depth_lvl + 1);
704704
}
705-
PatKind::Constant { value } | PatKind::NamedConstant { value, span: _ } => {
705+
PatKind::Constant { value, opt_def: _ } => {
706706
print_indented!(self, "Constant {", depth_lvl + 1);
707707
print_indented!(self, format!("value: {:?}", value), depth_lvl + 2);
708708
print_indented!(self, "}", depth_lvl + 1);

compiler/rustc_pattern_analysis/src/rustc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
536536
),
537537
}
538538
}
539-
PatKind::Constant { value } | PatKind::NamedConstant { value, span: _ } => {
539+
PatKind::Constant { value, opt_def: _ } => {
540540
match ty.kind() {
541541
ty::Bool => {
542542
ctor = match value.try_eval_bool(cx.tcx, cx.param_env) {

compiler/rustc_ty_utils/src/consts.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,7 @@ impl<'a, 'tcx> IsThirPolymorphic<'a, 'tcx> {
370370
}
371371

372372
match pat.kind {
373-
thir::PatKind::Constant { value } | thir::PatKind::NamedConstant { value, span: _ } => {
374-
value.has_non_region_param()
375-
}
373+
thir::PatKind::Constant { value, opt_def: _ } => value.has_non_region_param(),
376374
thir::PatKind::Range(box thir::PatRange { lo, hi, .. }) => {
377375
lo.has_non_region_param() || hi.has_non_region_param()
378376
}

0 commit comments

Comments
 (0)