Skip to content

Commit 3bd2744

Browse files
committed
Handle const generic pattern types
1 parent a2bdb99 commit 3bd2744

File tree

5 files changed

+28
-18
lines changed

5 files changed

+28
-18
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,21 @@ 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.type_of(def_id).instantiate_identity();
2234+
let item_def_id = tcx.parent(def_id);
2235+
let generics = tcx.generics_of(item_def_id);
2236+
let index = generics.param_def_id_to_index[&def_id];
2237+
let name = tcx.item_name(def_id);
2238+
ty::Const::new_param(tcx, ty::ParamConst::new(index, name), ty)
2239+
}
2240+
22262241
_ => {
22272242
let err = tcx
22282243
.dcx()

compiler/rustc_middle/src/ty/layout.rs

+5
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,11 @@ impl<'tcx> SizeSkeleton<'tcx> {
312312
) -> Result<SizeSkeleton<'tcx>, &'tcx LayoutError<'tcx>> {
313313
debug_assert!(!ty.has_non_region_infer());
314314

315+
let ty = match *ty.kind() {
316+
ty::Pat(inner_ty, pat) if matches!(*pat, ty::PatternKind::Range { .. }) => inner_ty,
317+
_ => ty,
318+
};
319+
315320
// First try computing a static layout.
316321
let err = match tcx.layout_of(param_env.and(ty)) {
317322
Ok(layout) => {

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
@@ -1,3 +1,5 @@
1+
//@ check-pass
2+
13
#![feature(pattern_types)]
24
#![feature(core_pattern_types)]
35
#![feature(core_pattern_type)]
@@ -7,7 +9,5 @@ use std::pat::pattern_type;
79
trait Foo {}
810

911
impl<const START: u32, const END: u32> Foo for pattern_type!(u32 is START..=END) {}
10-
//~^ ERROR: range patterns must have constant range start and end
11-
//~| ERROR: range patterns must have constant range start and end
1212

1313
fn main() {}

tests/ui/type/pattern_types/const_generics.stderr

-14
This file was deleted.

0 commit comments

Comments
 (0)