Skip to content

Commit 9286166

Browse files
committed
Allow range pattern types to allow the entire range (heh) of possible ranges
1 parent d1720e2 commit 9286166

File tree

6 files changed

+34
-20
lines changed

6 files changed

+34
-20
lines changed

compiler/rustc_hir_analysis/src/variance/constraints.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,13 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
233233

234234
ty::Pat(typ, pat) => {
235235
match *pat {
236-
ty::PatternKind::Range { start, end } => {
237-
self.add_constraints_from_const(current, start, variance);
238-
self.add_constraints_from_const(current, end, variance);
236+
ty::PatternKind::Range { start, end, include_end: _ } => {
237+
if let Some(start) = start {
238+
self.add_constraints_from_const(current, start, variance);
239+
}
240+
if let Some(end) = end {
241+
self.add_constraints_from_const(current, end, variance);
242+
}
239243
}
240244
}
241245
self.add_constraints_from_ty(current, typ, variance);

compiler/rustc_middle/src/ty/pattern.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ impl<'tcx> fmt::Debug for PatternKind<'tcx> {
4444
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
4545
#[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)]
4646
pub enum PatternKind<'tcx> {
47-
Range { start: ty::Const<'tcx>, end: ty::Const<'tcx> },
47+
Range { start: Option<ty::Const<'tcx>>, end: Option<ty::Const<'tcx>>, include_end: bool },
4848
}

compiler/rustc_middle/src/ty/walk.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
153153

154154
ty::Pat(ty, pat) => {
155155
match *pat {
156-
ty::PatternKind::Range { start, end } => {
157-
stack.push(start.into());
158-
stack.push(end.into());
156+
ty::PatternKind::Range { start, end, include_end: _ } => {
157+
stack.extend(start.map(Into::into));
158+
stack.extend(end.map(Into::into));
159159
}
160160
}
161161
stack.push(ty.into());

compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -525,12 +525,7 @@ fn encode_ty<'tcx>(
525525
// u3patI<element-type><pattern>E as vendor extended type
526526
let mut s = String::from("u3patI");
527527
s.push_str(&encode_ty(tcx, *ty0, dict, options));
528-
match **pat {
529-
ty::PatternKind::Range { start, end } => {
530-
s.push_str(&encode_const(tcx, start, dict, options));
531-
s.push_str(&encode_const(tcx, end, dict, options));
532-
}
533-
}
528+
write!(s, "{:?}", **pat).unwrap();
534529
s.push('E');
535530
compress(dict, DictKey::Ty(ty, TyQ::None), &mut s);
536531
typeid.push_str(&s);

compiler/rustc_symbol_mangling/src/v0.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,16 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
423423
self.push("T");
424424
self = ty.print(self)?;
425425
match *pat {
426-
ty::PatternKind::Range { start, end } => {
427-
self = self.print_const(start)?;
428-
self = self.print_const(end)?;
426+
ty::PatternKind::Range { start, end, include_end } => {
427+
if let Some(start) = start {
428+
self = self.print_const(start)?;
429+
}
430+
let _ = write!(self.out, "_");
431+
if let Some(end) = end {
432+
self = self.print_const(end)?;
433+
}
434+
435+
let _ = write!(self.out, "{:x}_", include_end as u8);
429436
}
430437
}
431438
self.push("E");

compiler/rustc_ty_utils/src/layout.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,19 @@ fn layout_of_uncached<'tcx>(
116116
let layout = cx.layout_of(ty)?.layout;
117117
let mut abi = layout.abi();
118118
match *pat {
119-
ty::PatternKind::Range { start, end } => {
119+
ty::PatternKind::Range { start, end, include_end } => {
120120
if let Abi::Scalar(scalar) | Abi::ScalarPair(scalar, _) = &mut abi {
121-
scalar.valid_range_mut().start =
122-
start.eval_bits(tcx, param_env, start.ty());
123-
scalar.valid_range_mut().end = end.eval_bits(tcx, param_env, end.ty());
121+
if let Some(start) = start {
122+
scalar.valid_range_mut().start =
123+
start.eval_bits(tcx, param_env, start.ty());
124+
}
125+
if let Some(end) = end {
126+
let mut end = end.eval_bits(tcx, param_env, end.ty());
127+
if !include_end {
128+
end = end.wrapping_sub(1);
129+
}
130+
scalar.valid_range_mut().end = end;
131+
}
124132

125133
tcx.intern_layout(LayoutS { abi, ..LayoutS::clone(&layout.0) })
126134
} else {

0 commit comments

Comments
 (0)