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(..) | Range(..)
| Binding { subpattern: None, .. } | Binding { subpattern: None, .. }
| Constant { .. } | Constant { .. }
| NamedConstant { .. }
| Error(_) => {} | Error(_) => {}
AscribeUserType { subpattern, .. } AscribeUserType { subpattern, .. }
| Binding { subpattern: Some(subpattern), .. } | Binding { subpattern: Some(subpattern), .. }
@ -787,12 +786,8 @@ pub enum PatKind<'tcx> {
/// * `String`, if `string_deref_patterns` is enabled. /// * `String`, if `string_deref_patterns` is enabled.
Constant { Constant {
value: mir::Const<'tcx>, value: mir::Const<'tcx>,
}, /// The `const` item this constant came from, if any.
opt_def: Option<DefId>,
/// Same as `Constant`, but that came from a `const` that we can point at in diagnostics.
NamedConstant {
value: mir::Const<'tcx>,
span: Span,
}, },
/// Inline constant found while lowering a pattern. /// 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); visitor.visit_pat(&subpattern.pattern);
} }
} }
Constant { value: _ } | NamedConstant { value: _, span: _ } => {} Constant { value: _, opt_def: _ } => {}
InlineConstant { def: _, subpattern } => visitor.visit_pat(subpattern), InlineConstant { def: _, subpattern } => visitor.visit_pat(subpattern),
Range(_) => {} Range(_) => {}
Slice { prefix, slice, suffix } | Array { prefix, slice, suffix } => { 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(); let mut targets = Vec::new();
for arm in rest { for arm in rest {
let arm = &self.thir[*arm]; let arm = &self.thir[*arm];
let (PatKind::Constant { value } | PatKind::NamedConstant { value, span: _ }) = let PatKind::Constant { value, opt_def: _ } = arm.pattern.kind else {
arm.pattern.kind
else {
return Err(ParseError { return Err(ParseError {
span: arm.pattern.span, span: arm.pattern.span,
item_description: format!("{:?}", arm.pattern.kind), 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: _ } => { PatKind::Constant { value, opt_def: _ } => TestCase::Constant { value },
TestCase::Constant { value }
}
PatKind::AscribeUserType { PatKind::AscribeUserType {
ascription: thir::Ascription { ref annotation, variance }, ascription: thir::Ascription { ref annotation, variance },

View File

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

View File

@ -316,7 +316,6 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
PatKind::Binding { .. } PatKind::Binding { .. }
// match is conditional on having this value // match is conditional on having this value
| PatKind::Constant { .. } | PatKind::Constant { .. }
| PatKind::NamedConstant { .. }
| PatKind::Variant { .. } | PatKind::Variant { .. }
| PatKind::Leaf { .. } | PatKind::Leaf { .. }
| PatKind::Deref { .. } | 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 = None;
let mut interpreted_as_const_sugg = None; let mut interpreted_as_const_sugg = None;
if let PatKind::NamedConstant { span, .. } if let PatKind::Constant { opt_def: Some(def_id), .. }
| PatKind::AscribeUserType { | PatKind::AscribeUserType {
subpattern: box Pat { kind: PatKind::NamedConstant { span, .. }, .. }, subpattern: box Pat { kind: PatKind::Constant { opt_def: Some(def_id), .. }, .. },
.. ..
} = pat.kind } = pat.kind
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span) && 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. // When we encounter a constant as the binding name, point at the `const` definition.
interpreted_as_const = Some(span); interpreted_as_const = Some(span);
interpreted_as_const_sugg = interpreted_as_const_sugg =

View File

@ -266,6 +266,7 @@ impl<'tcx> ConstToPat<'tcx> {
// optimization for now. // optimization for now.
ty::Str => PatKind::Constant { ty::Str => PatKind::Constant {
value: mir::Const::Ty(ty, ty::Const::new_value(tcx, cv, ty)), 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 // 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 // convert the dereferenced constant to a pattern that is the sub-pattern of the
@ -311,13 +312,17 @@ impl<'tcx> ConstToPat<'tcx> {
} else { } else {
PatKind::Constant { PatKind::Constant {
value: mir::Const::Ty(ty, ty::Const::new_value(tcx, cv, ty)), 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(..) => { 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 // The raw pointers we see here have been "vetted" by valtree construction to be
// just integers, so we simply allow them. // 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(..) => { ty::FnPtr(..) => {
unreachable!( unreachable!(

View File

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

View File

@ -702,7 +702,7 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
self.print_pat(subpattern, depth_lvl + 2); self.print_pat(subpattern, depth_lvl + 2);
print_indented!(self, "}", depth_lvl + 1); 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, "Constant {", depth_lvl + 1);
print_indented!(self, format!("value: {:?}", value), depth_lvl + 2); print_indented!(self, format!("value: {:?}", value), depth_lvl + 2);
print_indented!(self, "}", depth_lvl + 1); 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() { match ty.kind() {
ty::Bool => { ty::Bool => {
ctor = match value.try_eval_bool(cx.tcx, cx.param_env) { 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 { match pat.kind {
thir::PatKind::Constant { value } | thir::PatKind::NamedConstant { value, span: _ } => { thir::PatKind::Constant { value, opt_def: _ } => value.has_non_region_param(),
value.has_non_region_param()
}
thir::PatKind::Range(box thir::PatRange { lo, hi, .. }) => { thir::PatKind::Range(box thir::PatRange { lo, hi, .. }) => {
lo.has_non_region_param() || hi.has_non_region_param() lo.has_non_region_param() || hi.has_non_region_param()
} }