Skip to content

Commit 772c662

Browse files
Remove UrlFragment::render method to unify clean::types::links and anchor
1 parent 15b3a13 commit 772c662

File tree

6 files changed

+52
-83
lines changed

6 files changed

+52
-83
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ pub(crate) fn clean_trait_ref_with_constraints<'tcx>(
249249
trait_ref: ty::PolyTraitRef<'tcx>,
250250
constraints: ThinVec<AssocItemConstraint>,
251251
) -> Path {
252-
let kind = cx.tcx.def_kind(trait_ref.def_id()).into();
252+
let kind = ItemType::from_def_id(trait_ref.def_id(), cx.tcx);
253253
if !matches!(kind, ItemType::Trait | ItemType::TraitAlias) {
254254
span_bug!(cx.tcx.def_span(trait_ref.def_id()), "`TraitRef` had unexpected kind {kind:?}");
255255
}

src/librustdoc/clean/types.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,15 @@ impl Item {
522522
debug!(?id);
523523
if let Ok(HrefInfo { mut url, .. }) = href(*id, cx) {
524524
debug!(?url);
525-
if let Some(ref fragment) = *fragment {
526-
fragment.render(&mut url, cx.tcx())
525+
match fragment {
526+
Some(UrlFragment::Item(def_id)) => {
527+
url.push_str(&crate::html::format::fragment(*def_id, cx.tcx()))
528+
}
529+
Some(UrlFragment::UserWritten(raw)) => {
530+
url.push('#');
531+
url.push_str(raw);
532+
}
533+
None => {}
527534
}
528535
Some(RenderedLink {
529536
original_text: s.clone(),

src/librustdoc/clean/utils.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::clean::{
2626
};
2727
use crate::core::DocContext;
2828
use crate::display::Joined as _;
29+
use crate::formats::item_type::ItemType;
2930

3031
#[cfg(test)]
3132
mod tests;
@@ -496,7 +497,7 @@ pub(crate) fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId {
496497

497498
let (kind, did) = match res {
498499
Res::Def(
499-
kind @ (AssocTy
500+
AssocTy
500501
| AssocFn
501502
| AssocConst
502503
| Variant
@@ -511,9 +512,9 @@ pub(crate) fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId {
511512
| Const
512513
| Static { .. }
513514
| Macro(..)
514-
| TraitAlias),
515+
| TraitAlias,
515516
did,
516-
) => (kind.into(), did),
517+
) => (ItemType::from_def_id(did, cx.tcx), did),
517518

518519
_ => panic!("register_res: unexpected {res:?}"),
519520
};

src/librustdoc/formats/item_type.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use std::fmt;
44

55
use rustc_hir::def::{CtorOf, DefKind, MacroKinds};
6+
use rustc_hir::def_id::DefId;
7+
use rustc_middle::ty::TyCtxt;
68
use rustc_span::hygiene::MacroKind;
79
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
810

@@ -147,17 +149,10 @@ impl<'a> From<&'a clean::Item> for ItemType {
147149
}
148150
}
149151

150-
impl From<DefKind> for ItemType {
151-
fn from(other: DefKind) -> Self {
152-
Self::from_def_kind(other, None)
153-
}
154-
}
155-
156152
impl ItemType {
157-
/// Depending on the parent kind, some variants have a different translation (like a `Method`
158-
/// becoming a `TyMethod`).
159-
pub(crate) fn from_def_kind(kind: DefKind, parent_kind: Option<DefKind>) -> Self {
160-
match kind {
153+
pub(crate) fn from_def_id(def_id: DefId, tcx: TyCtxt<'_>) -> Self {
154+
let def_kind = tcx.def_kind(def_id);
155+
match def_kind {
161156
DefKind::Enum => Self::Enum,
162157
DefKind::Fn => Self::Function,
163158
DefKind::Mod => Self::Module,
@@ -176,8 +171,13 @@ impl ItemType {
176171
DefKind::Variant => Self::Variant,
177172
DefKind::Field => Self::StructField,
178173
DefKind::AssocTy => Self::AssocType,
179-
DefKind::AssocFn if let Some(DefKind::Trait) = parent_kind => Self::TyMethod,
180-
DefKind::AssocFn => Self::Method,
174+
DefKind::AssocFn => {
175+
if tcx.associated_item(def_id).defaultness(tcx).has_value() {
176+
Self::Method
177+
} else {
178+
Self::TyMethod
179+
}
180+
}
181181
DefKind::Ctor(CtorOf::Struct, _) => Self::Struct,
182182
DefKind::Ctor(CtorOf::Variant, _) => Self::Variant,
183183
DefKind::AssocConst => Self::AssocConst,

src/librustdoc/html/format.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,6 @@ fn generate_item_def_id_path(
431431
original_def_id: DefId,
432432
cx: &Context<'_>,
433433
root_path: Option<&str>,
434-
original_def_kind: DefKind,
435434
) -> Result<HrefInfo, HrefError> {
436435
use rustc_middle::traits::ObligationCause;
437436
use rustc_trait_selection::infer::TyCtxtInferExt;
@@ -457,15 +456,14 @@ fn generate_item_def_id_path(
457456
let relative = clean::inline::item_relative_path(tcx, def_id);
458457
let fqp: Vec<Symbol> = once(crate_name).chain(relative).collect();
459458

460-
let def_kind = tcx.def_kind(def_id);
461-
let shortty = def_kind.into();
459+
let shortty = ItemType::from_def_id(def_id, tcx);
462460
let module_fqp = to_module_fqp(shortty, &fqp);
463461
let mut is_remote = false;
464462

465463
let url_parts = url_parts(cx.cache(), def_id, module_fqp, &cx.current, &mut is_remote)?;
466464
let mut url_parts = make_href(root_path, shortty, url_parts, &fqp, is_remote);
467465
if def_id != original_def_id {
468-
let kind = ItemType::from_def_kind(original_def_kind, Some(def_kind));
466+
let kind = ItemType::from_def_id(original_def_id, tcx);
469467
url_parts = format!("{url_parts}#{kind}.{}", tcx.item_name(original_def_id))
470468
};
471469
Ok(HrefInfo { url: url_parts, kind: shortty, rust_path: fqp })
@@ -605,7 +603,7 @@ pub(crate) fn href_with_root_path(
605603
} else if did.is_local() {
606604
return Err(HrefError::Private);
607605
} else {
608-
return generate_item_def_id_path(did, original_did, cx, root_path, def_kind);
606+
return generate_item_def_id_path(did, original_did, cx, root_path);
609607
}
610608
}
611609
};
@@ -835,35 +833,35 @@ fn print_higher_ranked_params_with_space(
835833
})
836834
}
837835

836+
pub(crate) fn fragment(did: DefId, tcx: TyCtxt<'_>) -> String {
837+
let def_kind = tcx.def_kind(did);
838+
match def_kind {
839+
DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => {
840+
let item_type = ItemType::from_def_id(did, tcx);
841+
format!("#{}.{}", item_type.as_str(), tcx.item_name(did))
842+
}
843+
DefKind::Field => {
844+
let parent_def_id = tcx.parent(did);
845+
let s;
846+
let kind = if tcx.def_kind(parent_def_id) == DefKind::Variant {
847+
s = format!("variant.{}.field", tcx.item_name(parent_def_id).as_str());
848+
&s
849+
} else {
850+
"structfield"
851+
};
852+
format!("#{kind}.{}", tcx.item_name(did))
853+
}
854+
_ => String::new(),
855+
}
856+
}
857+
838858
pub(crate) fn print_anchor(did: DefId, text: Symbol, cx: &Context<'_>) -> impl Display {
839859
fmt::from_fn(move |f| {
840860
if let Ok(HrefInfo { url, kind, rust_path }) = href(did, cx) {
841-
let tcx = cx.tcx();
842-
let def_kind = tcx.def_kind(did);
843-
let anchor = match def_kind {
844-
DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => {
845-
let parent_def_id = tcx.parent(did);
846-
let item_type =
847-
ItemType::from_def_kind(def_kind, Some(tcx.def_kind(parent_def_id)));
848-
format!("#{}.{}", item_type.as_str(), tcx.item_name(did))
849-
}
850-
DefKind::Field => {
851-
let s;
852-
let parent_id = tcx.parent(did);
853-
let kind = if tcx.def_kind(parent_id) == DefKind::Variant {
854-
s = format!("variant.{}.field", tcx.item_name(parent_id).as_str());
855-
&s
856-
} else {
857-
"structfield"
858-
};
859-
format!("#{kind}.{}", tcx.item_name(did))
860-
}
861-
_ => String::new(),
862-
};
863-
864861
write!(
865862
f,
866863
r#"<a class="{kind}" href="{url}{anchor}" title="{kind} {path}">{text}</a>"#,
864+
anchor = fragment(did, cx.tcx()),
867865
path = join_path_syms(rust_path),
868866
text = EscapeBodyText(text.as_str()),
869867
)

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -203,43 +203,6 @@ pub(crate) enum UrlFragment {
203203
UserWritten(String),
204204
}
205205

206-
impl UrlFragment {
207-
/// Render the fragment, including the leading `#`.
208-
pub(crate) fn render(&self, s: &mut String, tcx: TyCtxt<'_>) {
209-
s.push('#');
210-
match self {
211-
&UrlFragment::Item(def_id) => {
212-
let kind = match tcx.def_kind(def_id) {
213-
DefKind::AssocFn => {
214-
if tcx.associated_item(def_id).defaultness(tcx).has_value() {
215-
"method."
216-
} else {
217-
"tymethod."
218-
}
219-
}
220-
DefKind::AssocConst => "associatedconstant.",
221-
DefKind::AssocTy => "associatedtype.",
222-
DefKind::Variant => "variant.",
223-
DefKind::Field => {
224-
let parent_id = tcx.parent(def_id);
225-
if tcx.def_kind(parent_id) == DefKind::Variant {
226-
s.push_str("variant.");
227-
s.push_str(tcx.item_name(parent_id).as_str());
228-
".field."
229-
} else {
230-
"structfield."
231-
}
232-
}
233-
kind => bug!("unexpected associated item kind: {kind:?}"),
234-
};
235-
s.push_str(kind);
236-
s.push_str(tcx.item_name(def_id).as_str());
237-
}
238-
UrlFragment::UserWritten(raw) => s.push_str(raw),
239-
}
240-
}
241-
}
242-
243206
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
244207
pub(crate) struct ResolutionInfo {
245208
item_id: DefId,

0 commit comments

Comments
 (0)