Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add const generics support for pattern types #123689

Merged
merged 2 commits into from Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Expand Up @@ -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), ..
},
)) => {
Copy link
Member

@fmease fmease Apr 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that we're missing a call to prohibit_generic_args here. Right now, we permit:

pattern_type!(u32 is START::<(), i32, 2>..=END::<_>)

where START and END are u32s like in the UI test.

While we could add this check, this just reinforces oli's point: #123689 (comment), Zulip discussion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed this while reviewing an unrelated PR.

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()
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_ty_utils/src/layout.rs
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
13 changes: 13 additions & 0 deletions 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() {}