about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2024-11-06 21:10:31 +0000
committerEsteban Küber <esteban@kuber.com.ar>2024-11-17 23:39:59 +0000
commitc25b44bee96e4489dab8f44409ba347bfeb328b9 (patch)
treedbba1316368f803b9edf0887e0842631bcab66d1
parentff2f7a7a834843ea74b1e7d6511eb4ad06f43981 (diff)
downloadrust-c25b44bee96e4489dab8f44409ba347bfeb328b9.tar.gz
rust-c25b44bee96e4489dab8f44409ba347bfeb328b9.zip
Fold `PatKind::NamedConstant` into `PatKind::Constant`
-rw-r--r--compiler/rustc_middle/src/thir.rs9
-rw-r--r--compiler/rustc_middle/src/thir/visit.rs2
-rw-r--r--compiler/rustc_mir_build/src/build/custom/parse/instruction.rs4
-rw-r--r--compiler/rustc_mir_build/src/build/matches/match_pair.rs4
-rw-r--r--compiler/rustc_mir_build/src/build/matches/mod.rs1
-rw-r--r--compiler/rustc_mir_build/src/check_unsafety.rs1
-rw-r--r--compiler/rustc_mir_build/src/thir/pattern/check_match.rs5
-rw-r--r--compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs7
-rw-r--r--compiler/rustc_mir_build/src/thir/pattern/mod.rs13
-rw-r--r--compiler/rustc_mir_build/src/thir/print.rs2
-rw-r--r--compiler/rustc_pattern_analysis/src/rustc.rs2
-rw-r--r--compiler/rustc_ty_utils/src/consts.rs4
12 files changed, 21 insertions, 33 deletions
diff --git a/compiler/rustc_middle/src/thir.rs b/compiler/rustc_middle/src/thir.rs
index 821f8c99704..84f5f3a4611 100644
--- a/compiler/rustc_middle/src/thir.rs
+++ b/compiler/rustc_middle/src/thir.rs
@@ -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.
diff --git a/compiler/rustc_middle/src/thir/visit.rs b/compiler/rustc_middle/src/thir/visit.rs
index 759ed77dbcb..92c0add65ba 100644
--- a/compiler/rustc_middle/src/thir/visit.rs
+++ b/compiler/rustc_middle/src/thir/visit.rs
@@ -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 } => {
diff --git a/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs b/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs
index 049586fd6a0..60624855fea 100644
--- a/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs
+++ b/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs
@@ -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),
diff --git a/compiler/rustc_mir_build/src/build/matches/match_pair.rs b/compiler/rustc_mir_build/src/build/matches/match_pair.rs
index 83a1e021484..df3cf53eb17 100644
--- a/compiler/rustc_mir_build/src/build/matches/match_pair.rs
+++ b/compiler/rustc_mir_build/src/build/matches/match_pair.rs
@@ -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 },
diff --git a/compiler/rustc_mir_build/src/build/matches/mod.rs b/compiler/rustc_mir_build/src/build/matches/mod.rs
index ac5be665654..a62d4e9d873 100644
--- a/compiler/rustc_mir_build/src/build/matches/mod.rs
+++ b/compiler/rustc_mir_build/src/build/matches/mod.rs
@@ -882,7 +882,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
             }
 
             PatKind::Constant { .. }
-            | PatKind::NamedConstant { .. }
             | PatKind::Range { .. }
             | PatKind::Wild
             | PatKind::Never
diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs
index d073cbdd877..33e194fa246 100644
--- a/compiler/rustc_mir_build/src/check_unsafety.rs
+++ b/compiler/rustc_mir_build/src/check_unsafety.rs
@@ -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 { .. }
diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs
index b54e1c2b552..f3cfc8b1a22 100644
--- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs
+++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs
@@ -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 =
diff --git a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs
index 82632350af5..06b13274efc 100644
--- a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs
+++ b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs
@@ -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!(
diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs
index 260ace55fba..11d06e5a1cc 100644
--- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs
+++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs
@@ -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;
diff --git a/compiler/rustc_mir_build/src/thir/print.rs b/compiler/rustc_mir_build/src/thir/print.rs
index 3fe75439339..43bca812bbd 100644
--- a/compiler/rustc_mir_build/src/thir/print.rs
+++ b/compiler/rustc_mir_build/src/thir/print.rs
@@ -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);
diff --git a/compiler/rustc_pattern_analysis/src/rustc.rs b/compiler/rustc_pattern_analysis/src/rustc.rs
index 6496d9fd73d..ec671150c40 100644
--- a/compiler/rustc_pattern_analysis/src/rustc.rs
+++ b/compiler/rustc_pattern_analysis/src/rustc.rs
@@ -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) {
diff --git a/compiler/rustc_ty_utils/src/consts.rs b/compiler/rustc_ty_utils/src/consts.rs
index 3a0eb8143cd..12f169d718c 100644
--- a/compiler/rustc_ty_utils/src/consts.rs
+++ b/compiler/rustc_ty_utils/src/consts.rs
@@ -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()
             }