Skip to content

Commit

Permalink
Handle const generic pattern types
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed Apr 9, 2024
1 parent a2bdb99 commit 3bd2744
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
15 changes: 15 additions & 0 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2223,6 +2223,21 @@ 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).instantiate_identity();
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()
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ impl<'tcx> SizeSkeleton<'tcx> {
) -> Result<SizeSkeleton<'tcx>, &'tcx LayoutError<'tcx>> {
debug_assert!(!ty.has_non_region_infer());

let ty = match *ty.kind() {
ty::Pat(inner_ty, pat) if matches!(*pat, ty::PatternKind::Range { .. }) => inner_ty,
_ => ty,
};

// First try computing a static layout.
let err = match tcx.layout_of(param_env.and(ty)) {
Ok(layout) => {
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_ty_utils/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/type/pattern_types/const_generics.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//@ check-pass

#![feature(pattern_types)]
#![feature(core_pattern_types)]
#![feature(core_pattern_type)]
Expand All @@ -7,7 +9,5 @@ use std::pat::pattern_type;
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() {}
14 changes: 0 additions & 14 deletions tests/ui/type/pattern_types/const_generics.stderr

This file was deleted.

0 comments on commit 3bd2744

Please sign in to comment.