Skip to content

Commit a2d7d8c

Browse files
authored
Rollup merge of #154955 - cijiugechu:fix-rustdoc-pattern-type-implementors, r=notriddle
Fix pattern types rendering in rustdoc Closes #150889 . `core` was rendering local pattern-type impls through `clean_ty`, which formatted `rustc_hir::TyPat` with derived Debug, while inlined docs go through `clean_middle_ty` and get the pretty-printed `rustc_middle::ty::Pattern`. Lower local HIR pattern types before formatting so both paths share the same canonical pattern printer and stop exposing `TyPat { ... }` in implementors. #### Current result: <img width="1133" height="738" alt="截屏2026-04-07 22 47 26" src="https://github.com/user-attachments/assets/bb5f8942-30b1-400a-a0a0-f581da5bb59f" />
2 parents 9ebe418 + 854d2ab commit a2d7d8c

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/librustdoc/clean/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1838,7 +1838,15 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
18381838
BorrowedRef { lifetime, mutability: m.mutbl, type_: Box::new(clean_ty(m.ty, cx)) }
18391839
}
18401840
TyKind::Slice(ty) => Slice(Box::new(clean_ty(ty, cx))),
1841-
TyKind::Pat(ty, pat) => Type::Pat(Box::new(clean_ty(ty, cx)), format!("{pat:?}").into()),
1841+
TyKind::Pat(inner_ty, pat) => {
1842+
// Local HIR pattern types should print the same way as cross-crate inlined ones,
1843+
// so lower to the canonical `rustc_middle::ty::Pattern` representation first.
1844+
let pat = match lower_ty(cx.tcx, ty).kind() {
1845+
ty::Pat(_, pat) => format!("{pat:?}").into_boxed_str(),
1846+
_ => format!("{pat:?}").into(),
1847+
};
1848+
Type::Pat(Box::new(clean_ty(inner_ty, cx)), pat)
1849+
}
18421850
TyKind::FieldOf(ty, hir::TyFieldPath { variant, field }) => {
18431851
let field_str = if let Some(variant) = variant {
18441852
format!("{variant}.{field}")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(pattern_types, pattern_type_macro)]
2+
#![crate_name = "pattern_types_implementors"]
3+
4+
use std::pat::pattern_type;
5+
6+
pub trait MyTrait {}
7+
8+
impl MyTrait for pattern_type!(*const u8 is !null) {}
9+
10+
//@ has pattern_types_implementors/trait.MyTrait.html
11+
//@ has - '//*[@id="implementors-list"]/*[@class="impl"]' 'impl MyTrait for *const u8 is !null'
12+
//@ !has - '//*[@id="implementors-list"]/*[@class="impl"]' 'TyPat {'

0 commit comments

Comments
 (0)