rustc_middle/ty/
pattern.rs

1use std::fmt;
2
3use rustc_data_structures::intern::Interned;
4use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
5
6use crate::ty;
7
8#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable)]
9#[rustc_pass_by_value]
10pub struct Pattern<'tcx>(pub Interned<'tcx, PatternKind<'tcx>>);
11
12impl<'tcx> std::ops::Deref for Pattern<'tcx> {
13    type Target = PatternKind<'tcx>;
14
15    fn deref(&self) -> &Self::Target {
16        &*self.0
17    }
18}
19
20impl<'tcx> fmt::Debug for Pattern<'tcx> {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        write!(f, "{:?}", **self)
23    }
24}
25
26impl<'tcx> fmt::Debug for PatternKind<'tcx> {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match *self {
29            PatternKind::Range { start, end } => {
30                write!(f, "{start}")?;
31
32                if let Some(c) = end.try_to_value() {
33                    let end = c.valtree.unwrap_leaf();
34                    let size = end.size();
35                    let max = match c.ty.kind() {
36                        ty::Int(_) => {
37                            Some(ty::ScalarInt::truncate_from_int(size.signed_int_max(), size))
38                        }
39                        ty::Uint(_) => {
40                            Some(ty::ScalarInt::truncate_from_uint(size.unsigned_int_max(), size))
41                        }
42                        ty::Char => Some(ty::ScalarInt::truncate_from_uint(char::MAX, size)),
43                        _ => None,
44                    };
45                    if let Some((max, _)) = max
46                        && end == max
47                    {
48                        return write!(f, "..");
49                    }
50                }
51
52                write!(f, "..={end}")
53            }
54        }
55    }
56}
57
58#[derive(Clone, PartialEq, Eq, Hash)]
59#[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)]
60pub enum PatternKind<'tcx> {
61    Range { start: ty::Const<'tcx>, end: ty::Const<'tcx> },
62}