Skip to content

Commit d220902

Browse files
committed
Thread pattern types through the HIR
1 parent e3dd737 commit d220902

File tree

15 files changed

+51
-6
lines changed

15 files changed

+51
-6
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13821382
);
13831383
hir::TyKind::Err
13841384
}
1385-
TyKind::Pat(..) => span_bug!(t.span, "pattern types are unimplemented"),
1385+
TyKind::Pat(ty, pat) => hir::TyKind::Pat(self.lower_ty(ty, itctx), self.lower_pat(pat)),
13861386
};
13871387

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

compiler/rustc_hir/src/hir.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2682,6 +2682,8 @@ pub enum TyKind<'hir> {
26822682
Infer,
26832683
/// Placeholder for a type that has failed to be defined.
26842684
Err,
2685+
/// Pattern types (`u32 as 1..`)
2686+
Pat(&'hir Ty<'hir>, &'hir Pat<'hir>),
26852687
}
26862688

26872689
#[derive(Debug, HashStable_Generic)]

compiler/rustc_hir/src/intravisit.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,10 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty<'v>) {
845845
}
846846
TyKind::Typeof(ref expression) => visitor.visit_anon_const(expression),
847847
TyKind::Infer | TyKind::Err => {}
848+
TyKind::Pat(ty, pat) => {
849+
visitor.visit_ty(ty);
850+
visitor.visit_pat(pat)
851+
}
848852
}
849853
}
850854

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2961,6 +2961,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
29612961
// handled specially and will not descend into this routine.
29622962
self.ty_infer(None, ast_ty.span)
29632963
}
2964+
hir::TyKind::Pat(..) => span_bug!(ast_ty.span, "{ast_ty:#?}"),
29642965
hir::TyKind::Err => tcx.ty_error(),
29652966
};
29662967

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,11 @@ impl<'a> State<'a> {
366366
hir::TyKind::Infer => {
367367
self.word("_");
368368
}
369+
hir::TyKind::Pat(ty, pat) => {
370+
self.print_type(ty);
371+
self.word(" is ");
372+
self.print_pat(pat);
373+
}
369374
}
370375
self.end()
371376
}

compiler/rustc_passes/src/hir_stats.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
334334
TraitObject,
335335
Typeof,
336336
Infer,
337+
Pat,
337338
Err
338339
]
339340
);

compiler/rustc_save_analysis/src/sig.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,10 @@ impl<'hir> Sig for hir::Ty<'hir> {
322322
let item = scx.tcx.hir().item(item_id);
323323
item.make(offset, Some(item_id.hir_id()), scx)
324324
}
325-
hir::TyKind::Typeof(_) | hir::TyKind::Infer | hir::TyKind::Err => Err("Ty"),
325+
hir::TyKind::Pat(..)
326+
| hir::TyKind::Typeof(_)
327+
| hir::TyKind::Infer
328+
| hir::TyKind::Err => Err("Ty"),
326329
}
327330
}
328331
}

src/librustdoc/clean/mod.rs

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

src/librustdoc/clean/types.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,7 +1602,9 @@ pub(crate) enum Type {
16021602
///
16031603
/// This is mostly Rustdoc's version of [`hir::Path`].
16041604
/// It has to be different because Rustdoc's [`PathSegment`] can contain cleaned generics.
1605-
Path { path: Path },
1605+
Path {
1606+
path: Path,
1607+
},
16061608
/// A `dyn Trait` object: `dyn for<'a> Trait<'a> + Send + 'static`
16071609
DynTrait(Vec<PolyTrait>, Option<Lifetime>),
16081610
/// A type parameter.
@@ -1619,10 +1621,15 @@ pub(crate) enum Type {
16191621
///
16201622
/// The `String` field is a stringified version of the array's length parameter.
16211623
Array(Box<Type>, Box<str>),
1624+
Pat(Box<Type>, Box<str>),
16221625
/// A raw pointer type: `*const i32`, `*mut i32`
16231626
RawPointer(Mutability, Box<Type>),
16241627
/// A reference type: `&i32`, `&'a mut Foo`
1625-
BorrowedRef { lifetime: Option<Lifetime>, mutability: Mutability, type_: Box<Type> },
1628+
BorrowedRef {
1629+
lifetime: Option<Lifetime>,
1630+
mutability: Mutability,
1631+
type_: Box<Type>,
1632+
},
16261633

16271634
/// A qualified path to an associated item: `<Type as Trait>::Name`
16281635
QPath(Box<QPathData>),
@@ -1747,6 +1754,7 @@ impl Type {
17471754
BareFunction(..) => PrimitiveType::Fn,
17481755
Slice(..) => PrimitiveType::Slice,
17491756
Array(..) => PrimitiveType::Array,
1757+
Type::Pat(..) => PrimitiveType::Pat,
17501758
RawPointer(..) => PrimitiveType::RawPointer,
17511759
QPath(box QPathData { ref self_type, .. }) => return self_type.inner_def_id(cache),
17521760
Generic(_) | Infer | ImplTrait(_) => return None,
@@ -1798,6 +1806,7 @@ pub(crate) enum PrimitiveType {
17981806
Str,
17991807
Slice,
18001808
Array,
1809+
Pat,
18011810
Tuple,
18021811
Unit,
18031812
RawPointer,
@@ -1945,6 +1954,7 @@ impl PrimitiveType {
19451954
Bool => sym::bool,
19461955
Char => sym::char,
19471956
Array => sym::array,
1957+
Pat => sym::pat,
19481958
Slice => sym::slice,
19491959
Tuple => sym::tuple,
19501960
Unit => sym::unit,

src/librustdoc/html/format.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,10 @@ fn fmt_type<'cx>(
10121012
write!(f, "]")
10131013
}
10141014
},
1015+
clean::Type::Pat(ref t, ref pat) => {
1016+
fmt::Display::fmt(&t.print(cx), f)?;
1017+
write!(f, " is {pat}")
1018+
}
10151019
clean::Array(ref t, ref n) => match **t {
10161020
clean::Generic(name) if !f.alternate() => primitive_link(
10171021
f,

0 commit comments

Comments
 (0)