Handle const generic pattern types

This commit is contained in:
Santiago Pastorino 2024-02-20 12:32:28 -03:00
parent a2bdb994d3
commit 30c546aee1
No known key found for this signature in database
GPG key ID: 8131A24E0C79EFAF
4 changed files with 26 additions and 18 deletions

View file

@ -2223,6 +2223,24 @@ fn lower_ty_common(&self, hir_ty: &hir::Ty<'tcx>, borrowed: bool, in_path: bool)
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()

View file

@ -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);
}

View file

@ -1,3 +1,5 @@
//@ check-pass
#![feature(pattern_types)]
#![feature(core_pattern_types)]
#![feature(core_pattern_type)]
@ -7,7 +9,5 @@
trait Foo {}
impl<const START: u32, const END: u32> Foo for pattern_type!(u32 is START..=END) {}
//~^ ERROR: range patterns must have constant range start and end
//~| ERROR: range patterns must have constant range start and end
fn main() {}

View file

@ -1,14 +0,0 @@
error: "range patterns must have constant range start and end"
--> $DIR/const_generics.rs:9:69
|
LL | impl<const START: u32, const END: u32> Foo for pattern_type!(u32 is START..=END) {}
| ^^^^^
error: "range patterns must have constant range start and end"
--> $DIR/const_generics.rs:9:77
|
LL | impl<const START: u32, const END: u32> Foo for pattern_type!(u32 is START..=END) {}
| ^^^
error: aborting due to 2 previous errors