Skip to content

Commit 27e4e32

Browse files
committed
Thread pattern types through the HIR
1 parent bc5f3ff commit 27e4e32

File tree

14 files changed

+51
-5
lines changed

14 files changed

+51
-5
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15311531
);
15321532
hir::TyKind::Err(guar)
15331533
}
1534-
TyKind::Pat(..) => span_bug!(t.span, "pattern types are unimplemented"),
1534+
TyKind::Pat(ty, pat) => hir::TyKind::Pat(self.lower_ty(ty, itctx), self.lower_pat(pat)),
15351535
};
15361536

15371537
hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.lower_node_id(t.id) }

compiler/rustc_hir/src/hir.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2621,6 +2621,8 @@ pub enum TyKind<'hir> {
26212621
Infer,
26222622
/// Placeholder for a type that has failed to be defined.
26232623
Err(rustc_span::ErrorGuaranteed),
2624+
/// Pattern types (`u32 as 1..`)
2625+
Pat(&'hir Ty<'hir>, &'hir Pat<'hir>),
26242626
}
26252627

26262628
#[derive(Debug, Clone, Copy, HashStable_Generic)]

compiler/rustc_hir/src/intravisit.rs

+4
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,10 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) {
857857
}
858858
TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
859859
TyKind::Infer | TyKind::InferDelegation(..) | TyKind::Err(_) => {}
860+
TyKind::Pat(ty, pat) => {
861+
visitor.visit_ty(ty);
862+
visitor.visit_pat(pat)
863+
}
860864
}
861865
}
862866

compiler/rustc_hir_analysis/src/astconv/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2559,6 +2559,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
25592559
// handled specially and will not descend into this routine.
25602560
self.ty_infer(None, ast_ty.span)
25612561
}
2562+
hir::TyKind::Pat(..) => span_bug!(ast_ty.span, "{ast_ty:#?}"),
25622563
hir::TyKind::Err(guar) => Ty::new_error(tcx, *guar),
25632564
};
25642565

compiler/rustc_hir_pretty/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ impl<'a> State<'a> {
323323
hir::TyKind::Infer | hir::TyKind::InferDelegation(..) => {
324324
self.word("_");
325325
}
326+
hir::TyKind::Pat(ty, pat) => {
327+
self.print_type(ty);
328+
self.word(" is ");
329+
self.print_pat(pat);
330+
}
326331
}
327332
self.end()
328333
}

compiler/rustc_passes/src/hir_stats.rs

+1
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
355355
TraitObject,
356356
Typeof,
357357
Infer,
358+
Pat,
358359
Err
359360
]
360361
);

src/librustdoc/clean/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,7 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
18331833
BorrowedRef { lifetime, mutability: m.mutbl, type_: Box::new(clean_ty(m.ty, cx)) }
18341834
}
18351835
TyKind::Slice(ty) => Slice(Box::new(clean_ty(ty, cx))),
1836+
TyKind::Pat(ty, pat) => Type::Pat(Box::new(clean_ty(ty, cx)), format!("{pat:?}").into()),
18361837
TyKind::Array(ty, ref length) => {
18371838
let length = match length {
18381839
hir::ArrayLen::Infer(_, _) => "_".to_string(),

src/librustdoc/clean/types.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,9 @@ pub(crate) enum Type {
14811481
///
14821482
/// This is mostly Rustdoc's version of [`hir::Path`].
14831483
/// It has to be different because Rustdoc's [`PathSegment`] can contain cleaned generics.
1484-
Path { path: Path },
1484+
Path {
1485+
path: Path,
1486+
},
14851487
/// A `dyn Trait` object: `dyn for<'a> Trait<'a> + Send + 'static`
14861488
DynTrait(Vec<PolyTrait>, Option<Lifetime>),
14871489
/// A type parameter.
@@ -1498,10 +1500,15 @@ pub(crate) enum Type {
14981500
///
14991501
/// The `String` field is a stringified version of the array's length parameter.
15001502
Array(Box<Type>, Box<str>),
1503+
Pat(Box<Type>, Box<str>),
15011504
/// A raw pointer type: `*const i32`, `*mut i32`
15021505
RawPointer(Mutability, Box<Type>),
15031506
/// A reference type: `&i32`, `&'a mut Foo`
1504-
BorrowedRef { lifetime: Option<Lifetime>, mutability: Mutability, type_: Box<Type> },
1507+
BorrowedRef {
1508+
lifetime: Option<Lifetime>,
1509+
mutability: Mutability,
1510+
type_: Box<Type>,
1511+
},
15051512

15061513
/// A qualified path to an associated item: `<Type as Trait>::Name`
15071514
QPath(Box<QPathData>),
@@ -1698,6 +1705,7 @@ impl Type {
16981705
BareFunction(..) => PrimitiveType::Fn,
16991706
Slice(..) => PrimitiveType::Slice,
17001707
Array(..) => PrimitiveType::Array,
1708+
Type::Pat(..) => PrimitiveType::Pat,
17011709
RawPointer(..) => PrimitiveType::RawPointer,
17021710
QPath(box QPathData { ref self_type, .. }) => return self_type.inner_def_id(cache),
17031711
Generic(_) | Infer | ImplTrait(_) => return None,
@@ -1749,6 +1757,7 @@ pub(crate) enum PrimitiveType {
17491757
Str,
17501758
Slice,
17511759
Array,
1760+
Pat,
17521761
Tuple,
17531762
Unit,
17541763
RawPointer,
@@ -1895,6 +1904,7 @@ impl PrimitiveType {
18951904
Bool => sym::bool,
18961905
Char => sym::char,
18971906
Array => sym::array,
1907+
Pat => sym::pat,
18981908
Slice => sym::slice,
18991909
Tuple => sym::tuple,
19001910
Unit => sym::unit,

src/librustdoc/html/format.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,10 @@ fn fmt_type<'cx>(
10601060
write!(f, "]")
10611061
}
10621062
},
1063+
clean::Type::Pat(ref t, ref pat) => {
1064+
fmt::Display::fmt(&t.print(cx), f)?;
1065+
write!(f, " is {pat}")
1066+
}
10631067
clean::Array(ref t, ref n) => match **t {
10641068
clean::Generic(name) if !f.alternate() => primitive_link(
10651069
f,

src/librustdoc/html/render/search_index.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,11 @@ fn get_index_type_id(
584584
}
585585
}
586586
// Not supported yet
587-
clean::BareFunction(_) | clean::Generic(_) | clean::ImplTrait(_) | clean::Infer => None,
587+
clean::Type::Pat(..)
588+
| clean::BareFunction(_)
589+
| clean::Generic(_)
590+
| clean::ImplTrait(_)
591+
| clean::Infer => None,
588592
}
589593
}
590594

src/librustdoc/json/conversions.rs

+3
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,9 @@ impl FromWithTcx<clean::Type> for Type {
576576
Tuple(t) => Type::Tuple(t.into_tcx(tcx)),
577577
Slice(t) => Type::Slice(Box::new((*t).into_tcx(tcx))),
578578
Array(t, s) => Type::Array { type_: Box::new((*t).into_tcx(tcx)), len: s.to_string() },
579+
clean::Type::Pat(t, p) => {
580+
Type::Pat { type_: Box::new((*t).into_tcx(tcx)), pat: p.to_string() }
581+
}
579582
ImplTrait(g) => Type::ImplTrait(g.into_tcx(tcx)),
580583
Infer => Type::Infer,
581584
RawPointer(mutability, type_) => Type::RawPointer {

src/rustdoc-json-types/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
88
use std::path::PathBuf;
99

1010
/// rustdoc format-version.
11-
pub const FORMAT_VERSION: u32 = 28;
11+
pub const FORMAT_VERSION: u32 = 29;
1212

1313
/// A `Crate` is the root of the emitted JSON blob. It contains all type/documentation information
1414
/// about the language items in the local crate, as well as info about external items to allow
@@ -562,6 +562,12 @@ pub enum Type {
562562
type_: Box<Type>,
563563
len: String,
564564
},
565+
/// `u32 is 1..`
566+
Pat {
567+
#[serde(rename = "type")]
568+
type_: Box<Type>,
569+
pat: String,
570+
},
565571
/// `impl TraitA + TraitB + ...`
566572
ImplTrait(Vec<GenericBound>),
567573
/// `_`

src/tools/clippy/clippy_lints/src/dereference.rs

+1
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ impl TyCoercionStability {
823823
| TyKind::Array(..)
824824
| TyKind::Ptr(_)
825825
| TyKind::BareFn(_)
826+
| TyKind::Pat(..)
826827
| TyKind::Never
827828
| TyKind::Tup(_)
828829
| TyKind::Path(_) => Self::Deref,

src/tools/clippy/clippy_utils/src/hir_utils.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,10 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
10681068
self.hash_ty(ty);
10691069
self.hash_array_length(len);
10701070
},
1071+
TyKind::Pat(ty, pat) => {
1072+
self.hash_ty(ty);
1073+
self.hash_pat(pat);
1074+
},
10711075
TyKind::Ptr(ref mut_ty) => {
10721076
self.hash_ty(mut_ty.ty);
10731077
mut_ty.mutbl.hash(&mut self.s);

0 commit comments

Comments
 (0)