Skip to content

Commit c4efc25

Browse files
committed
Thread pattern types through the HIR
1 parent c340e67 commit c4efc25

File tree

15 files changed

+48
-5
lines changed

15 files changed

+48
-5
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14631463
}
14641464
}
14651465
}
1466+
TyKind::Pat(ty, pat) => hir::TyKind::Pat(self.lower_ty(ty, itctx), self.lower_pat(pat)),
14661467
TyKind::MacCall(_) => {
14671468
span_bug!(t.span, "`TyKind::MacCall` should have been expanded by now")
14681469
}
@@ -1473,7 +1474,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14731474
);
14741475
hir::TyKind::Err(guar)
14751476
}
1476-
TyKind::Pat(..) => span_bug!(t.span, "pattern types are unimplemented"),
14771477
TyKind::Dummy => panic!("`TyKind::Dummy` should never be lowered"),
14781478
};
14791479

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,6 +2624,8 @@ pub enum TyKind<'hir> {
26242624
Infer,
26252625
/// Placeholder for a type that has failed to be defined.
26262626
Err(rustc_span::ErrorGuaranteed),
2627+
/// Pattern types (`u32 as 1..`)
2628+
Pat(&'hir Ty<'hir>, &'hir Pat<'hir>),
26272629
}
26282630

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

compiler/rustc_hir/src/intravisit.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,10 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) -> V::Resul
882882
TyKind::AnonAdt(item_id) => {
883883
try_visit!(visitor.visit_nested_item(item_id));
884884
}
885+
TyKind::Pat(ty, pat) => {
886+
try_visit!(visitor.visit_ty(ty));
887+
try_visit!(visitor.visit_pat(pat));
888+
}
885889
}
886890
V::Result::output()
887891
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
21952195
// handled specially and will not descend into this routine.
21962196
self.ty_infer(None, hir_ty.span)
21972197
}
2198+
hir::TyKind::Pat(..) => span_bug!(hir_ty.span, "{hir_ty:#?}"),
21982199
hir::TyKind::Err(guar) => Ty::new_error(tcx, *guar),
21992200
};
22002201

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,11 @@ impl<'a> State<'a> {
330330
self.word("_");
331331
}
332332
hir::TyKind::AnonAdt(..) => self.word("/* anonymous adt */"),
333+
hir::TyKind::Pat(ty, pat) => {
334+
self.print_type(ty);
335+
self.word(" is ");
336+
self.print_pat(pat);
337+
}
333338
}
334339
self.end()
335340
}

compiler/rustc_lint/src/non_local_def.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
157157
| TyKind::Never
158158
| TyKind::Tup(_)
159159
| TyKind::Path(_)
160+
| TyKind::Pat(..)
160161
| TyKind::AnonAdt(_)
161162
| TyKind::OpaqueDef(_, _, _)
162163
| TyKind::Typeof(_)

compiler/rustc_passes/src/hir_stats.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
352352
TraitObject,
353353
Typeof,
354354
Infer,
355+
Pat,
355356
Err
356357
]
357358
);

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,6 +1782,7 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
17821782
BorrowedRef { lifetime, mutability: m.mutbl, type_: Box::new(clean_ty(m.ty, cx)) }
17831783
}
17841784
TyKind::Slice(ty) => Slice(Box::new(clean_ty(ty, cx))),
1785+
TyKind::Pat(ty, pat) => Type::Pat(Box::new(clean_ty(ty, cx)), format!("{pat:?}").into()),
17851786
TyKind::Array(ty, ref length) => {
17861787
let length = match length {
17871788
hir::ArrayLen::Infer(..) => "_".to_string(),

src/librustdoc/clean/types.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,9 @@ pub(crate) enum Type {
14831483
///
14841484
/// This is mostly Rustdoc's version of [`hir::Path`].
14851485
/// It has to be different because Rustdoc's [`PathSegment`] can contain cleaned generics.
1486-
Path { path: Path },
1486+
Path {
1487+
path: Path,
1488+
},
14871489
/// A `dyn Trait` object: `dyn for<'a> Trait<'a> + Send + 'static`
14881490
DynTrait(Vec<PolyTrait>, Option<Lifetime>),
14891491
/// A type parameter.
@@ -1500,10 +1502,15 @@ pub(crate) enum Type {
15001502
///
15011503
/// The `String` field is a stringified version of the array's length parameter.
15021504
Array(Box<Type>, Box<str>),
1505+
Pat(Box<Type>, Box<str>),
15031506
/// A raw pointer type: `*const i32`, `*mut i32`
15041507
RawPointer(Mutability, Box<Type>),
15051508
/// A reference type: `&i32`, `&'a mut Foo`
1506-
BorrowedRef { lifetime: Option<Lifetime>, mutability: Mutability, type_: Box<Type> },
1509+
BorrowedRef {
1510+
lifetime: Option<Lifetime>,
1511+
mutability: Mutability,
1512+
type_: Box<Type>,
1513+
},
15071514

15081515
/// A qualified path to an associated item: `<Type as Trait>::Name`
15091516
QPath(Box<QPathData>),
@@ -1700,6 +1707,7 @@ impl Type {
17001707
BareFunction(..) => PrimitiveType::Fn,
17011708
Slice(..) => PrimitiveType::Slice,
17021709
Array(..) => PrimitiveType::Array,
1710+
Type::Pat(..) => PrimitiveType::Pat,
17031711
RawPointer(..) => PrimitiveType::RawPointer,
17041712
QPath(box QPathData { ref self_type, .. }) => return self_type.inner_def_id(cache),
17051713
Generic(_) | Infer | ImplTrait(_) => return None,
@@ -1753,6 +1761,7 @@ pub(crate) enum PrimitiveType {
17531761
Str,
17541762
Slice,
17551763
Array,
1764+
Pat,
17561765
Tuple,
17571766
Unit,
17581767
RawPointer,
@@ -1907,6 +1916,7 @@ impl PrimitiveType {
19071916
Bool => sym::bool,
19081917
Char => sym::char,
19091918
Array => sym::array,
1919+
Pat => sym::pat,
19101920
Slice => sym::slice,
19111921
Tuple => sym::tuple,
19121922
Unit => sym::unit,

src/librustdoc/html/format.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,10 @@ fn fmt_type<'cx>(
10711071
write!(f, "]")
10721072
}
10731073
},
1074+
clean::Type::Pat(ref t, ref pat) => {
1075+
fmt::Display::fmt(&t.print(cx), f)?;
1076+
write!(f, " is {pat}")
1077+
}
10741078
clean::Array(ref t, ref n) => match **t {
10751079
clean::Generic(name) if !f.alternate() => primitive_link(
10761080
f,

src/librustdoc/html/render/search_index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ fn get_index_type_id(
668668
}
669669
}
670670
// Not supported yet
671-
clean::Generic(_) | clean::ImplTrait(_) | clean::Infer => None,
671+
clean::Type::Pat(..) | clean::Generic(_) | clean::ImplTrait(_) | clean::Infer => None,
672672
}
673673
}
674674

src/librustdoc/json/conversions.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,9 @@ impl FromWithTcx<clean::Type> for Type {
573573
Tuple(t) => Type::Tuple(t.into_tcx(tcx)),
574574
Slice(t) => Type::Slice(Box::new((*t).into_tcx(tcx))),
575575
Array(t, s) => Type::Array { type_: Box::new((*t).into_tcx(tcx)), len: s.to_string() },
576+
clean::Type::Pat(t, p) => {
577+
Type::Pat { type_: Box::new((*t).into_tcx(tcx)), pat: p.to_string() }
578+
}
576579
ImplTrait(g) => Type::ImplTrait(g.into_tcx(tcx)),
577580
Infer => Type::Infer,
578581
RawPointer(mutability, type_) => Type::RawPointer {

src/rustdoc-json-types/lib.rs

Lines changed: 7 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,7 @@ impl TyCoercionStability {
821821
| TyKind::Array(..)
822822
| TyKind::Ptr(_)
823823
| TyKind::BareFn(_)
824+
| TyKind::Pat(..)
824825
| TyKind::Never
825826
| TyKind::Tup(_)
826827
| TyKind::Path(_) => Self::Deref,

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

Lines changed: 4 additions & 0 deletions
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)