Skip to content

Commit 4adefa4

Browse files
authored
Rollup merge of rust-lang#128471 - camelid:rustdoc-self, r=notriddle
rustdoc: Fix handling of `Self` type in search index and refactor its representation ### Summary - Add enum variant `clean::Type::SelfTy` and use it instead of `clean::Type::Generic(kw::SelfUpper)`. - Stop treating `Self` as a generic in the search index. - Remove struct formerly known as `clean::SelfTy` (constructed as representation of function receiver type). We're better off without it. ### Before ![image](https://github.com/user-attachments/assets/d257bdd8-3a62-4c71-84a5-9c950f2e4f00) ### After ![image](https://github.com/user-attachments/assets/8f6d3f22-92c1-41e3-9ab8-a881b66816c0) r? ```@notriddle``` cc rust-lang#127589 (comment)
2 parents 02c3837 + dac7f20 commit 4adefa4

File tree

11 files changed

+326
-306
lines changed

11 files changed

+326
-306
lines changed

src/librustdoc/clean/inline.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_middle::ty::fast_reject::SimplifiedType;
1212
use rustc_middle::ty::{self, TyCtxt};
1313
use rustc_span::def_id::LOCAL_CRATE;
1414
use rustc_span::hygiene::MacroKind;
15-
use rustc_span::symbol::{kw, sym, Symbol};
15+
use rustc_span::symbol::{sym, Symbol};
1616
use thin_vec::{thin_vec, ThinVec};
1717
use {rustc_ast as ast, rustc_hir as hir};
1818

@@ -792,11 +792,7 @@ fn build_macro(
792792
fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean::Generics {
793793
for pred in &mut g.where_predicates {
794794
match *pred {
795-
clean::WherePredicate::BoundPredicate {
796-
ty: clean::Generic(ref s),
797-
ref mut bounds,
798-
..
799-
} if *s == kw::SelfUpper => {
795+
clean::WherePredicate::BoundPredicate { ty: clean::SelfTy, ref mut bounds, .. } => {
800796
bounds.retain(|bound| match bound {
801797
clean::GenericBound::TraitBound(clean::PolyTrait { trait_, .. }, _) => {
802798
trait_.def_id() != trait_did
@@ -812,13 +808,13 @@ fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean:
812808
clean::WherePredicate::BoundPredicate {
813809
ty:
814810
clean::QPath(box clean::QPathData {
815-
self_type: clean::Generic(ref s),
811+
self_type: clean::Generic(_),
816812
trait_: Some(trait_),
817813
..
818814
}),
819815
bounds,
820816
..
821-
} => !(bounds.is_empty() || *s == kw::SelfUpper && trait_.def_id() == trait_did),
817+
} => !bounds.is_empty() && trait_.def_id() != trait_did,
822818
_ => true,
823819
});
824820
g
@@ -832,9 +828,7 @@ fn separate_supertrait_bounds(
832828
) -> (clean::Generics, Vec<clean::GenericBound>) {
833829
let mut ty_bounds = Vec::new();
834830
g.where_predicates.retain(|pred| match *pred {
835-
clean::WherePredicate::BoundPredicate { ty: clean::Generic(ref s), ref bounds, .. }
836-
if *s == kw::SelfUpper =>
837-
{
831+
clean::WherePredicate::BoundPredicate { ty: clean::SelfTy, ref bounds, .. } => {
838832
ty_bounds.extend(bounds.iter().cloned());
839833
false
840834
}

src/librustdoc/clean/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1351,11 +1351,11 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
13511351
let self_arg_ty =
13521352
tcx.fn_sig(assoc_item.def_id).instantiate_identity().input(0).skip_binder();
13531353
if self_arg_ty == self_ty {
1354-
item.decl.inputs.values[0].type_ = Generic(kw::SelfUpper);
1354+
item.decl.inputs.values[0].type_ = SelfTy;
13551355
} else if let ty::Ref(_, ty, _) = *self_arg_ty.kind() {
13561356
if ty == self_ty {
13571357
match item.decl.inputs.values[0].type_ {
1358-
BorrowedRef { ref mut type_, .. } => **type_ = Generic(kw::SelfUpper),
1358+
BorrowedRef { ref mut type_, .. } => **type_ = SelfTy,
13591359
_ => unreachable!(),
13601360
}
13611361
}
@@ -1439,9 +1439,8 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
14391439
if trait_.def_id() != assoc_item.container_id(tcx) {
14401440
return true;
14411441
}
1442-
match *self_type {
1443-
Generic(ref s) if *s == kw::SelfUpper => {}
1444-
_ => return true,
1442+
if *self_type != SelfTy {
1443+
return true;
14451444
}
14461445
match &assoc.args {
14471446
GenericArgs::AngleBracketed { args, constraints } => {
@@ -2228,6 +2227,8 @@ pub(crate) fn clean_middle_ty<'tcx>(
22282227
ty::Param(ref p) => {
22292228
if let Some(bounds) = cx.impl_trait_bounds.remove(&p.index.into()) {
22302229
ImplTrait(bounds)
2230+
} else if p.name == kw::SelfUpper {
2231+
SelfTy
22312232
} else {
22322233
Generic(p.name)
22332234
}

src/librustdoc/clean/simplify.rs

-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ pub(crate) fn sized_bounds(cx: &mut DocContext<'_>, generics: &mut clean::Generi
145145
// should be handled when cleaning associated types.
146146
generics.where_predicates.retain(|pred| {
147147
if let WP::BoundPredicate { ty: clean::Generic(param), bounds, .. } = pred
148-
&& *param != rustc_span::symbol::kw::SelfUpper
149148
&& bounds.iter().any(|b| b.is_sized_bound(cx))
150149
{
151150
sized_params.insert(*param);

src/librustdoc/clean/types.rs

+11-26
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ use thin_vec::ThinVec;
3434
use {rustc_ast as ast, rustc_hir as hir};
3535

3636
pub(crate) use self::ItemKind::*;
37-
pub(crate) use self::SelfTy::*;
3837
pub(crate) use self::Type::{
3938
Array, BareFunction, BorrowedRef, DynTrait, Generic, ImplTrait, Infer, Primitive, QPath,
40-
RawPointer, Slice, Tuple,
39+
RawPointer, SelfTy, Slice, Tuple,
4140
};
4241
use crate::clean::cfg::Cfg;
4342
use crate::clean::clean_middle_path;
@@ -1384,8 +1383,8 @@ pub(crate) struct FnDecl {
13841383
}
13851384

13861385
impl FnDecl {
1387-
pub(crate) fn self_type(&self) -> Option<SelfTy> {
1388-
self.inputs.values.get(0).and_then(|v| v.to_self())
1386+
pub(crate) fn receiver_type(&self) -> Option<&Type> {
1387+
self.inputs.values.get(0).and_then(|v| v.to_receiver())
13891388
}
13901389
}
13911390

@@ -1403,27 +1402,9 @@ pub(crate) struct Argument {
14031402
pub(crate) is_const: bool,
14041403
}
14051404

1406-
#[derive(Clone, PartialEq, Debug)]
1407-
pub(crate) enum SelfTy {
1408-
SelfValue,
1409-
SelfBorrowed(Option<Lifetime>, Mutability),
1410-
SelfExplicit(Type),
1411-
}
1412-
14131405
impl Argument {
1414-
pub(crate) fn to_self(&self) -> Option<SelfTy> {
1415-
if self.name != kw::SelfLower {
1416-
return None;
1417-
}
1418-
if self.type_.is_self_type() {
1419-
return Some(SelfValue);
1420-
}
1421-
match self.type_ {
1422-
BorrowedRef { ref lifetime, mutability, ref type_ } if type_.is_self_type() => {
1423-
Some(SelfBorrowed(lifetime.clone(), mutability))
1424-
}
1425-
_ => Some(SelfExplicit(self.type_.clone())),
1426-
}
1406+
pub(crate) fn to_receiver(&self) -> Option<&Type> {
1407+
if self.name == kw::SelfLower { Some(&self.type_) } else { None }
14271408
}
14281409
}
14291410

@@ -1477,6 +1458,8 @@ pub(crate) enum Type {
14771458
DynTrait(Vec<PolyTrait>, Option<Lifetime>),
14781459
/// A type parameter.
14791460
Generic(Symbol),
1461+
/// The `Self` type.
1462+
SelfTy,
14801463
/// A primitive (aka, builtin) type.
14811464
Primitive(PrimitiveType),
14821465
/// A function pointer: `extern "ABI" fn(...) -> ...`
@@ -1571,6 +1554,8 @@ impl Type {
15711554
// If both sides are generic, this returns true.
15721555
(_, Type::Generic(_)) => true,
15731556
(Type::Generic(_), _) => false,
1557+
// `Self` only matches itself.
1558+
(Type::SelfTy, Type::SelfTy) => true,
15741559
// Paths account for both the path itself and its generics.
15751560
(Type::Path { path: a }, Type::Path { path: b }) => {
15761561
a.def_id() == b.def_id()
@@ -1642,7 +1627,7 @@ impl Type {
16421627

16431628
pub(crate) fn is_self_type(&self) -> bool {
16441629
match *self {
1645-
Generic(name) => name == kw::SelfUpper,
1630+
SelfTy => true,
16461631
_ => false,
16471632
}
16481633
}
@@ -1700,7 +1685,7 @@ impl Type {
17001685
Type::Pat(..) => PrimitiveType::Pat,
17011686
RawPointer(..) => PrimitiveType::RawPointer,
17021687
QPath(box QPathData { ref self_type, .. }) => return self_type.def_id(cache),
1703-
Generic(_) | Infer | ImplTrait(_) => return None,
1688+
Generic(_) | SelfTy | Infer | ImplTrait(_) => return None,
17041689
};
17051690
Primitive(t).def_id(cache)
17061691
}

src/librustdoc/clean/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ pub(crate) fn resolve_type(cx: &mut DocContext<'_>, path: Path) -> Type {
468468
match path.res {
469469
Res::PrimTy(p) => Primitive(PrimitiveType::from(p)),
470470
Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } if path.segments.len() == 1 => {
471-
Generic(kw::SelfUpper)
471+
Type::SelfTy
472472
}
473473
Res::Def(DefKind::TyParam, _) if path.segments.len() == 1 => Generic(path.segments[0].name),
474474
_ => {

src/librustdoc/html/format.rs

+12-18
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,7 @@ fn fmt_type<'cx>(
10061006

10071007
match *t {
10081008
clean::Generic(name) => f.write_str(name.as_str()),
1009+
clean::SelfTy => f.write_str("Self"),
10091010
clean::Type::Path { ref path } => {
10101011
// Paths like `T::Output` and `Self::Output` should be rendered with all segments.
10111012
let did = path.def_id();
@@ -1452,29 +1453,22 @@ impl clean::FnDecl {
14521453

14531454
let last_input_index = self.inputs.values.len().checked_sub(1);
14541455
for (i, input) in self.inputs.values.iter().enumerate() {
1455-
if let Some(selfty) = input.to_self() {
1456+
if let Some(selfty) = input.to_receiver() {
14561457
match selfty {
1457-
clean::SelfValue => {
1458+
clean::SelfTy => {
14581459
write!(f, "self")?;
14591460
}
1460-
clean::SelfBorrowed(Some(ref lt), mutability) => {
1461-
write!(
1462-
f,
1463-
"{amp}{lifetime} {mutability}self",
1464-
lifetime = lt.print(),
1465-
mutability = mutability.print_with_space(),
1466-
)?;
1467-
}
1468-
clean::SelfBorrowed(None, mutability) => {
1469-
write!(
1470-
f,
1471-
"{amp}{mutability}self",
1472-
mutability = mutability.print_with_space(),
1473-
)?;
1461+
clean::BorrowedRef { lifetime, mutability, type_: box clean::SelfTy } => {
1462+
write!(f, "{amp}")?;
1463+
match lifetime {
1464+
Some(lt) => write!(f, "{lt} ", lt = lt.print())?,
1465+
None => {}
1466+
}
1467+
write!(f, "{mutability}self", mutability = mutability.print_with_space())?;
14741468
}
1475-
clean::SelfExplicit(ref typ) => {
1469+
_ => {
14761470
write!(f, "self: ")?;
1477-
typ.print(cx).fmt(f)?;
1471+
selfty.print(cx).fmt(f)?;
14781472
}
14791473
}
14801474
} else {

src/librustdoc/html/render/mod.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use serde::{Serialize, Serializer};
5858

5959
pub(crate) use self::context::*;
6060
pub(crate) use self::span_map::{collect_spans_and_sources, LinkFromSrc};
61-
use crate::clean::{self, ItemId, RenderedLink, SelfTy};
61+
use crate::clean::{self, ItemId, RenderedLink};
6262
use crate::error::Error;
6363
use crate::formats::cache::Cache;
6464
use crate::formats::item_type::ItemType;
@@ -1372,21 +1372,20 @@ fn render_deref_methods(
13721372

13731373
fn should_render_item(item: &clean::Item, deref_mut_: bool, tcx: TyCtxt<'_>) -> bool {
13741374
let self_type_opt = match *item.kind {
1375-
clean::MethodItem(ref method, _) => method.decl.self_type(),
1376-
clean::TyMethodItem(ref method) => method.decl.self_type(),
1375+
clean::MethodItem(ref method, _) => method.decl.receiver_type(),
1376+
clean::TyMethodItem(ref method) => method.decl.receiver_type(),
13771377
_ => None,
13781378
};
13791379

13801380
if let Some(self_ty) = self_type_opt {
1381-
let (by_mut_ref, by_box, by_value) = match self_ty {
1382-
SelfTy::SelfBorrowed(_, mutability)
1383-
| SelfTy::SelfExplicit(clean::BorrowedRef { mutability, .. }) => {
1381+
let (by_mut_ref, by_box, by_value) = match *self_ty {
1382+
clean::Type::BorrowedRef { mutability, .. } => {
13841383
(mutability == Mutability::Mut, false, false)
13851384
}
1386-
SelfTy::SelfExplicit(clean::Type::Path { path }) => {
1385+
clean::Type::Path { ref path } => {
13871386
(false, Some(path.def_id()) == tcx.lang_items().owned_box(), false)
13881387
}
1389-
SelfTy::SelfValue => (false, false, true),
1388+
clean::Type::SelfTy => (false, false, true),
13901389
_ => (false, false, false),
13911390
};
13921391

0 commit comments

Comments
 (0)