Fold PatKind::NamedConstant into PatKind::Constant

This commit is contained in:
Esteban Küber 2024-11-06 21:10:31 +00:00
parent ff2f7a7a83
commit c25b44bee9
12 changed files with 21 additions and 33 deletions

View File

@ -640,7 +640,6 @@ impl<'tcx> Pat<'tcx> {
| Range(..)
| Binding { subpattern: None, .. }
| Constant { .. }
| NamedConstant { .. }
| Error(_) => {}
AscribeUserType { subpattern, .. }
| Binding { subpattern: Some(subpattern), .. }
@ -787,12 +786,8 @@ pub enum PatKind<'tcx> {
/// * `String`, if `string_deref_patterns` is enabled.
Constant {
value: mir::Const<'tcx>,
},
/// Same as `Constant`, but that came from a `const` that we can point at in diagnostics.
NamedConstant {
value: mir::Const<'tcx>,
span: Span,
/// The `const` item this constant came from, if any.
opt_def: Option<DefId>,
},
/// Inline constant found while lowering a pattern.

View File

@ -246,7 +246,7 @@ pub fn walk_pat<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
visitor.visit_pat(&subpattern.pattern);
}
}
Constant { value: _ } | NamedConstant { value: _, span: _ } => {}
Constant { value: _, opt_def: _ } => {}
InlineConstant { def: _, subpattern } => visitor.visit_pat(subpattern),
Range(_) => {}
Slice { prefix, slice, suffix } | Array { prefix, slice, suffix } => {

View File

@ -144,9 +144,7 @@ impl<'a, 'tcx> ParseCtxt<'a, 'tcx> {
let mut targets = Vec::new();
for arm in rest {
let arm = &self.thir[*arm];
let (PatKind::Constant { value } | PatKind::NamedConstant { value, span: _ }) =
arm.pattern.kind
else {
let PatKind::Constant { value, opt_def: _ } = arm.pattern.kind else {
return Err(ParseError {
span: arm.pattern.span,
item_description: format!("{:?}", arm.pattern.kind),

View File

@ -129,9 +129,7 @@ impl<'pat, 'tcx> MatchPairTree<'pat, 'tcx> {
}
}
PatKind::Constant { value } | PatKind::NamedConstant { value, span: _ } => {
TestCase::Constant { value }
}
PatKind::Constant { value, opt_def: _ } => TestCase::Constant { value },
PatKind::AscribeUserType {
ascription: thir::Ascription { ref annotation, variance },

View File

@ -882,7 +882,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
PatKind::Constant { .. }
| PatKind::NamedConstant { .. }
| PatKind::Range { .. }
| PatKind::Wild
| PatKind::Never

View File

@ -316,7 +316,6 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
PatKind::Binding { .. }
// match is conditional on having this value
| PatKind::Constant { .. }
| PatKind::NamedConstant { .. }
| PatKind::Variant { .. }
| PatKind::Leaf { .. }
| PatKind::Deref { .. }

View File

@ -670,13 +670,14 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
let mut interpreted_as_const = None;
let mut interpreted_as_const_sugg = None;
if let PatKind::NamedConstant { span, .. }
if let PatKind::Constant { opt_def: Some(def_id), .. }
| PatKind::AscribeUserType {
subpattern: box Pat { kind: PatKind::NamedConstant { span, .. }, .. },
subpattern: box Pat { kind: PatKind::Constant { opt_def: Some(def_id), .. }, .. },
..
} = pat.kind
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span)
{
let span = self.tcx.def_span(def_id);
// When we encounter a constant as the binding name, point at the `const` definition.
interpreted_as_const = Some(span);
interpreted_as_const_sugg =

View File

@ -266,6 +266,7 @@ impl<'tcx> ConstToPat<'tcx> {
// optimization for now.
ty::Str => PatKind::Constant {
value: mir::Const::Ty(ty, ty::Const::new_value(tcx, cv, ty)),
opt_def: None,
},
// All other references are converted into deref patterns and then recursively
// convert the dereferenced constant to a pattern that is the sub-pattern of the
@ -311,13 +312,17 @@ impl<'tcx> ConstToPat<'tcx> {
} else {
PatKind::Constant {
value: mir::Const::Ty(ty, ty::Const::new_value(tcx, cv, ty)),
opt_def: None,
}
}
}
ty::Pat(..) | ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::RawPtr(..) => {
// The raw pointers we see here have been "vetted" by valtree construction to be
// just integers, so we simply allow them.
PatKind::Constant { value: mir::Const::Ty(ty, ty::Const::new_value(tcx, cv, ty)) }
PatKind::Constant {
value: mir::Const::Ty(ty, ty::Const::new_value(tcx, cv, ty)),
opt_def: None,
}
}
ty::FnPtr(..) => {
unreachable!(

View File

@ -157,9 +157,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
}
kind => (kind, None, None),
};
let value = if let PatKind::Constant { value }
| PatKind::NamedConstant { value, span: _ } = kind
{
let value = if let PatKind::Constant { value, opt_def: _ } = kind {
value
} else {
let msg = format!(
@ -253,7 +251,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
(RangeEnd::Included, Some(Ordering::Less)) => {}
// `x..=y` where `x == y` and `x` and `y` are finite.
(RangeEnd::Included, Some(Ordering::Equal)) if lo.is_finite() && hi.is_finite() => {
kind = PatKind::Constant { value: lo.as_finite().unwrap() };
kind = PatKind::Constant { value: lo.as_finite().unwrap(), opt_def: None };
}
// `..=x` where `x == ty::MIN`.
(RangeEnd::Included, Some(Ordering::Equal)) if !lo.is_finite() => {}
@ -562,15 +560,12 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
_ => return pat_from_kind(self.lower_variant_or_leaf(res, id, span, ty, vec![])),
};
// HERE
let args = self.typeck_results.node_args(id);
let c = ty::Const::new_unevaluated(self.tcx, ty::UnevaluatedConst { def: def_id, args });
let def_span = self.tcx.def_span(def_id);
let mut pattern = self.const_to_pat(c, ty, id, span);
if let PatKind::Constant { value } = pattern.kind {
pattern.kind = PatKind::NamedConstant { value, span: def_span };
if let PatKind::Constant { value, opt_def: None } = pattern.kind {
pattern.kind = PatKind::Constant { value, opt_def: Some(def_id) };
}
tracing::info!("pattern {pattern:#?} {c:?} {ty:?} {id:?}");
if !is_associated_const {
return pattern;

View File

@ -702,7 +702,7 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
self.print_pat(subpattern, depth_lvl + 2);
print_indented!(self, "}", depth_lvl + 1);
}
PatKind::Constant { value } | PatKind::NamedConstant { value, span: _ } => {
PatKind::Constant { value, opt_def: _ } => {
print_indented!(self, "Constant {", depth_lvl + 1);
print_indented!(self, format!("value: {:?}", value), depth_lvl + 2);
print_indented!(self, "}", depth_lvl + 1);

View File

@ -536,7 +536,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
),
}
}
PatKind::Constant { value } | PatKind::NamedConstant { value, span: _ } => {
PatKind::Constant { value, opt_def: _ } => {
match ty.kind() {
ty::Bool => {
ctor = match value.try_eval_bool(cx.tcx, cx.param_env) {

View File

@ -370,9 +370,7 @@ impl<'a, 'tcx> IsThirPolymorphic<'a, 'tcx> {
}
match pat.kind {
thir::PatKind::Constant { value } | thir::PatKind::NamedConstant { value, span: _ } => {
value.has_non_region_param()
}
thir::PatKind::Constant { value, opt_def: _ } => value.has_non_region_param(),
thir::PatKind::Range(box thir::PatRange { lo, hi, .. }) => {
lo.has_non_region_param() || hi.has_non_region_param()
}