about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2024-04-10 16:15:24 +0200
committerGitHub <noreply@github.com>2024-04-10 16:15:24 +0200
commit3f7ae6803b8953bf1e1e8596edb4bc1513334e37 (patch)
tree8e6c6f97607716f6cf3628bec2f0fd3c0e888117
parent2b4c581ef91c5bfe8fdcdf95a1e64b715416fcf3 (diff)
parent30c546aee1165405b525be84913eaff86a46b99b (diff)
downloadrust-3f7ae6803b8953bf1e1e8596edb4bc1513334e37.tar.gz
rust-3f7ae6803b8953bf1e1e8596edb4bc1513334e37.zip
Rollup merge of #123689 - spastorino:pattern_types_const_generics, r=oli-obk
Add const generics support for pattern types

r? `@oli-obk`
-rw-r--r--compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs18
-rw-r--r--compiler/rustc_ty_utils/src/layout.rs8
-rw-r--r--tests/ui/type/pattern_types/const_generics.rs13
3 files changed, 37 insertions, 2 deletions
diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
index 59f0fac5aa7..9fb8b4ac40e 100644
--- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
+++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
@@ -2223,6 +2223,24 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
                                         Err(LitToConstError::TypeError) => todo!(),
                                     }
                                 }
+
+                                hir::ExprKind::Path(hir::QPath::Resolved(
+                                    _,
+                                    &hir::Path {
+                                        res: Res::Def(DefKind::ConstParam, def_id), ..
+                                    },
+                                )) => {
+                                    let ty = tcx
+                                        .type_of(def_id)
+                                        .no_bound_vars()
+                                        .expect("const parameter types cannot be generic");
+                                    let item_def_id = tcx.parent(def_id);
+                                    let generics = tcx.generics_of(item_def_id);
+                                    let index = generics.param_def_id_to_index[&def_id];
+                                    let name = tcx.item_name(def_id);
+                                    ty::Const::new_param(tcx, ty::ParamConst::new(index, name), ty)
+                                }
+
                                 _ => {
                                     let err = tcx
                                         .dcx()
diff --git a/compiler/rustc_ty_utils/src/layout.rs b/compiler/rustc_ty_utils/src/layout.rs
index 77cd4662ae5..902b76e8c1e 100644
--- a/compiler/rustc_ty_utils/src/layout.rs
+++ b/compiler/rustc_ty_utils/src/layout.rs
@@ -133,10 +133,14 @@ fn layout_of_uncached<'tcx>(
                 ty::PatternKind::Range { start, end, include_end } => {
                     if let Abi::Scalar(scalar) | Abi::ScalarPair(scalar, _) = &mut layout.abi {
                         if let Some(start) = start {
-                            scalar.valid_range_mut().start = start.eval_bits(tcx, param_env);
+                            scalar.valid_range_mut().start = start
+                                .try_eval_bits(tcx, param_env)
+                                .ok_or_else(|| error(cx, LayoutError::Unknown(ty)))?;
                         }
                         if let Some(end) = end {
-                            let mut end = end.eval_bits(tcx, param_env);
+                            let mut end = end
+                                .try_eval_bits(tcx, param_env)
+                                .ok_or_else(|| error(cx, LayoutError::Unknown(ty)))?;
                             if !include_end {
                                 end = end.wrapping_sub(1);
                             }
diff --git a/tests/ui/type/pattern_types/const_generics.rs b/tests/ui/type/pattern_types/const_generics.rs
new file mode 100644
index 00000000000..5bc6fd54e0f
--- /dev/null
+++ b/tests/ui/type/pattern_types/const_generics.rs
@@ -0,0 +1,13 @@
+//@ check-pass
+
+#![feature(pattern_types)]
+#![feature(core_pattern_types)]
+#![feature(core_pattern_type)]
+
+use std::pat::pattern_type;
+
+trait Foo {}
+
+impl<const START: u32, const END: u32> Foo for pattern_type!(u32 is START..=END) {}
+
+fn main() {}