Skip to content

Commit 3f7ae68

Browse files
Rollup merge of #123689 - spastorino:pattern_types_const_generics, r=oli-obk
Add const generics support for pattern types r? `@oli-obk`
2 parents 2b4c581 + 30c546a commit 3f7ae68

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,24 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
22232223
Err(LitToConstError::TypeError) => todo!(),
22242224
}
22252225
}
2226+
2227+
hir::ExprKind::Path(hir::QPath::Resolved(
2228+
_,
2229+
&hir::Path {
2230+
res: Res::Def(DefKind::ConstParam, def_id), ..
2231+
},
2232+
)) => {
2233+
let ty = tcx
2234+
.type_of(def_id)
2235+
.no_bound_vars()
2236+
.expect("const parameter types cannot be generic");
2237+
let item_def_id = tcx.parent(def_id);
2238+
let generics = tcx.generics_of(item_def_id);
2239+
let index = generics.param_def_id_to_index[&def_id];
2240+
let name = tcx.item_name(def_id);
2241+
ty::Const::new_param(tcx, ty::ParamConst::new(index, name), ty)
2242+
}
2243+
22262244
_ => {
22272245
let err = tcx
22282246
.dcx()

compiler/rustc_ty_utils/src/layout.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,14 @@ fn layout_of_uncached<'tcx>(
133133
ty::PatternKind::Range { start, end, include_end } => {
134134
if let Abi::Scalar(scalar) | Abi::ScalarPair(scalar, _) = &mut layout.abi {
135135
if let Some(start) = start {
136-
scalar.valid_range_mut().start = start.eval_bits(tcx, param_env);
136+
scalar.valid_range_mut().start = start
137+
.try_eval_bits(tcx, param_env)
138+
.ok_or_else(|| error(cx, LayoutError::Unknown(ty)))?;
137139
}
138140
if let Some(end) = end {
139-
let mut end = end.eval_bits(tcx, param_env);
141+
let mut end = end
142+
.try_eval_bits(tcx, param_env)
143+
.ok_or_else(|| error(cx, LayoutError::Unknown(ty)))?;
140144
if !include_end {
141145
end = end.wrapping_sub(1);
142146
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ check-pass
2+
3+
#![feature(pattern_types)]
4+
#![feature(core_pattern_types)]
5+
#![feature(core_pattern_type)]
6+
7+
use std::pat::pattern_type;
8+
9+
trait Foo {}
10+
11+
impl<const START: u32, const END: u32> Foo for pattern_type!(u32 is START..=END) {}
12+
13+
fn main() {}

0 commit comments

Comments
 (0)