From 17d40c6057c88f4c432b0d7bac88e1b84cb7e67f Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:03:36 +0200 Subject: Adding upstream version 1.65.0+dfsg1. Signed-off-by: Daniel Baumann --- src/librustdoc/html/render/mod.rs | 399 +++++++++++++++++++++++--------------- 1 file changed, 248 insertions(+), 151 deletions(-) (limited to 'src/librustdoc/html/render/mod.rs') diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index a262c8f7d..1e6f20d2b 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -191,12 +191,6 @@ impl StylePath { } } -fn write_srclink(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer) { - if let Some(l) = cx.src_href(item) { - write!(buf, "source", l) - } -} - #[derive(Debug, Eq, PartialEq, Hash)] struct ItemEntry { url: String, @@ -522,7 +516,14 @@ fn portability(item: &clean::Item, parent: Option<&clean::Item>) -> Option cfg.as_deref().cloned(), }; - debug!("Portability {:?} - {:?} = {:?}", item.cfg, parent.and_then(|p| p.cfg.as_ref()), cfg); + debug!( + "Portability {:?} {:?} (parent: {:?}) - {:?} = {:?}", + item.name, + item.cfg, + parent, + parent.and_then(|p| p.cfg.as_ref()), + cfg + ); Some(format!("
{}
", cfg?.render_long_html())) } @@ -569,7 +570,10 @@ fn short_item_info( message.push_str(&format!(": {}", html.into_string())); } extra_info.push(format!( - "
๐Ÿ‘Ž {}
", + "
\ + ๐Ÿ‘Ž\ + {}\ +
", message, )); } @@ -582,8 +586,9 @@ fn short_item_info( .filter(|stab| stab.feature != sym::rustc_private) .map(|stab| (stab.level, stab.feature)) { - let mut message = - "๐Ÿ”ฌ This is a nightly-only experimental API.".to_owned(); + let mut message = "๐Ÿ”ฌ\ + This is a nightly-only experimental API." + .to_owned(); let mut feature = format!("{}", Escape(feature.as_str())); if let (Some(url), Some(issue)) = (&cx.shared.issue_tracker_base_url, issue) { @@ -594,7 +599,7 @@ fn short_item_info( )); } - message.push_str(&format!(" ({})", feature)); + message.push_str(&format!(" ({})", feature)); extra_info.push(format!("
{}
", message)); } @@ -608,10 +613,10 @@ fn short_item_info( // Render the list of items inside one of the sections "Trait Implementations", // "Auto Trait Implementations," "Blanket Trait Implementations" (on struct/enum pages). -fn render_impls( +pub(crate) fn render_impls( cx: &mut Context<'_>, w: &mut Buffer, - impls: &[&&Impl], + impls: &[&Impl], containing_item: &clean::Item, toggle_open_by_default: bool, ) { @@ -816,7 +821,7 @@ fn assoc_method( href = href, name = name, generics = g.print(cx), - decl = d.full_print(header_len, indent, header.asyncness, cx), + decl = d.full_print(header_len, indent, cx), notable_traits = notable_traits_decl(d, cx), where_clause = print_where_clause(g, cx, indent, end_newline), ) @@ -836,12 +841,13 @@ fn assoc_method( /// Note that it is possible for an unstable function to be const-stable. In that case, the span /// will include the const-stable version, but no stable version will be emitted, as a natural /// consequence of the above rules. -fn render_stability_since_raw( +fn render_stability_since_raw_with_extra( w: &mut Buffer, ver: Option, const_stability: Option, containing_ver: Option, containing_const_ver: Option, + extra_class: &str, ) -> bool { let stable_version = ver.filter(|inner| !inner.is_empty() && Some(*inner) != containing_ver); @@ -889,12 +895,30 @@ fn render_stability_since_raw( } if !stability.is_empty() { - write!(w, r#"{}"#, title, stability); + write!(w, r#"{stability}"#); } !stability.is_empty() } +#[inline] +fn render_stability_since_raw( + w: &mut Buffer, + ver: Option, + const_stability: Option, + containing_ver: Option, + containing_const_ver: Option, +) -> bool { + render_stability_since_raw_with_extra( + w, + ver, + const_stability, + containing_ver, + containing_const_ver, + "", + ) +} + fn render_assoc_item( w: &mut Buffer, item: &clean::Item, @@ -1001,6 +1025,47 @@ impl<'a> AssocItemLink<'a> { } } +fn write_impl_section_heading(w: &mut Buffer, title: &str, id: &str) { + write!( + w, + "

\ + {title}\ + \ +

" + ); +} + +pub(crate) fn render_all_impls( + w: &mut Buffer, + cx: &mut Context<'_>, + containing_item: &clean::Item, + concrete: &[&Impl], + synthetic: &[&Impl], + blanket_impl: &[&Impl], +) { + let mut impls = Buffer::empty_from(w); + render_impls(cx, &mut impls, concrete, containing_item, true); + let impls = impls.into_inner(); + if !impls.is_empty() { + write_impl_section_heading(w, "Trait Implementations", "trait-implementations"); + write!(w, "
{}
", impls); + } + + if !synthetic.is_empty() { + write_impl_section_heading(w, "Auto Trait Implementations", "synthetic-implementations"); + w.write_str("
"); + render_impls(cx, w, synthetic, containing_item, false); + w.write_str("
"); + } + + if !blanket_impl.is_empty() { + write_impl_section_heading(w, "Blanket Implementations", "blanket-implementations"); + w.write_str("
"); + render_impls(cx, w, blanket_impl, containing_item, false); + w.write_str("
"); + } +} + fn render_assoc_items( w: &mut Buffer, cx: &mut Context<'_>, @@ -1030,12 +1095,7 @@ fn render_assoc_items_inner( let mut tmp_buf = Buffer::empty_from(w); let (render_mode, id) = match what { AssocItemRender::All => { - tmp_buf.write_str( - "

\ - Implementations\ - \ -

", - ); + write_impl_section_heading(&mut tmp_buf, "Implementations", "implementations"); (RenderMode::Normal, "implementations-list".to_owned()) } AssocItemRender::DerefFor { trait_, type_, deref_mut_ } => { @@ -1044,15 +1104,14 @@ fn render_assoc_items_inner( if let Some(def_id) = type_.def_id(cx.cache()) { cx.deref_id_map.insert(def_id, id.clone()); } - write!( - tmp_buf, - "

\ - Methods from {trait_}<Target = {type_}>\ - \ -

", - id = id, - trait_ = trait_.print(cx), - type_ = type_.print(cx), + write_impl_section_heading( + &mut tmp_buf, + &format!( + "Methods from {trait_}<Target = {type_}>", + trait_ = trait_.print(cx), + type_ = type_.print(cx), + ), + &id, ); (RenderMode::ForDeref { mut_: deref_mut_ }, cx.derive_id(id)) } @@ -1099,49 +1158,12 @@ fn render_assoc_items_inner( return; } - let (synthetic, concrete): (Vec<&&Impl>, Vec<&&Impl>) = - traits.iter().partition(|t| t.inner_impl().kind.is_auto()); - let (blanket_impl, concrete): (Vec<&&Impl>, _) = + let (synthetic, concrete): (Vec<&Impl>, Vec<&Impl>) = + traits.into_iter().partition(|t| t.inner_impl().kind.is_auto()); + let (blanket_impl, concrete): (Vec<&Impl>, _) = concrete.into_iter().partition(|t| t.inner_impl().kind.is_blanket()); - let mut impls = Buffer::empty_from(w); - render_impls(cx, &mut impls, &concrete, containing_item, true); - let impls = impls.into_inner(); - if !impls.is_empty() { - write!( - w, - "

\ - Trait Implementations\ - \ -

\ -
{}
", - impls - ); - } - - if !synthetic.is_empty() { - w.write_str( - "

\ - Auto Trait Implementations\ - \ -

\ -
", - ); - render_impls(cx, w, &synthetic, containing_item, false); - w.write_str("
"); - } - - if !blanket_impl.is_empty() { - w.write_str( - "

\ - Blanket Implementations\ - \ -

\ -
", - ); - render_impls(cx, w, &blanket_impl, containing_item, false); - w.write_str("
"); - } + render_all_impls(w, cx, containing_item, &concrete, &synthetic, &blanket_impl); } } @@ -1550,6 +1572,15 @@ fn render_impl( rendering_params: ImplRenderingParameters, ) { for trait_item in &t.items { + // Skip over any default trait items that are impossible to call + // (e.g. if it has a `Self: Sized` bound on an unsized type). + if let Some(impl_def_id) = parent.item_id.as_def_id() + && let Some(trait_item_def_id) = trait_item.item_id.as_def_id() + && cx.tcx().is_impossible_method((impl_def_id, trait_item_def_id)) + { + continue; + } + let n = trait_item.name; if i.items.iter().any(|m| m.name == n) { continue; @@ -1668,23 +1699,29 @@ fn render_rightside( RenderMode::Normal => (item.const_stability(tcx), containing_item.const_stable_since(tcx)), RenderMode::ForDeref { .. } => (None, None), }; + let src_href = cx.src_href(item); + let has_src_ref = src_href.is_some(); let mut rightside = Buffer::new(); - let has_stability = render_stability_since_raw( + let has_stability = render_stability_since_raw_with_extra( &mut rightside, item.stable_since(tcx), const_stability, containing_item.stable_since(tcx), const_stable_since, + if has_src_ref { "" } else { " rightside" }, ); - let mut srclink = Buffer::empty_from(w); - write_srclink(cx, item, &mut srclink); - if has_stability && !srclink.is_empty() { - rightside.write_str(" ยท "); + if let Some(l) = src_href { + if has_stability { + write!(rightside, " ยท source", l) + } else { + write!(rightside, "source", l) + } } - rightside.push_buffer(srclink); - if !rightside.is_empty() { + if has_stability && has_src_ref { write!(w, "{}", rightside.into_inner()); + } else { + w.push_buffer(rightside); } } @@ -1700,8 +1737,8 @@ pub(crate) fn render_impl_summary( // in documentation pages for trait with automatic implementations like "Send" and "Sync". aliases: &[String], ) { - let id = - cx.derive_id(get_id_for_impl(&i.inner_impl().for_, i.inner_impl().trait_.as_ref(), cx)); + let inner_impl = i.inner_impl(); + let id = cx.derive_id(get_id_for_impl(&inner_impl.for_, inner_impl.trait_.as_ref(), cx)); let aliases = if aliases.is_empty() { String::new() } else { @@ -1713,9 +1750,9 @@ pub(crate) fn render_impl_summary( write!(w, "

"); if let Some(use_absolute) = use_absolute { - write!(w, "{}", i.inner_impl().print(use_absolute, cx)); + write!(w, "{}", inner_impl.print(use_absolute, cx)); if show_def_docs { - for it in &i.inner_impl().items { + for it in &inner_impl.items { if let clean::AssocTypeItem(ref tydef, ref _bounds) = *it.kind { w.write_str(" "); assoc_type( @@ -1733,11 +1770,11 @@ pub(crate) fn render_impl_summary( } } } else { - write!(w, "{}", i.inner_impl().print(false, cx)); + write!(w, "{}", inner_impl.print(false, cx)); } write!(w, "

"); - let is_trait = i.inner_impl().trait_.is_some(); + let is_trait = inner_impl.trait_.is_some(); if is_trait { if let Some(portability) = portability(&i.impl_item, Some(parent)) { write!(w, "{}", portability); @@ -1931,6 +1968,70 @@ fn small_url_encode(s: String) -> String { } } +pub(crate) fn sidebar_render_assoc_items( + cx: &Context<'_>, + out: &mut Buffer, + id_map: &mut IdMap, + concrete: Vec<&Impl>, + synthetic: Vec<&Impl>, + blanket_impl: Vec<&Impl>, +) { + let format_impls = |impls: Vec<&Impl>, id_map: &mut IdMap| { + let mut links = FxHashSet::default(); + + let mut ret = impls + .iter() + .filter_map(|it| { + let trait_ = it.inner_impl().trait_.as_ref()?; + let encoded = + id_map.derive(get_id_for_impl(&it.inner_impl().for_, Some(trait_), cx)); + + let i_display = format!("{:#}", trait_.print(cx)); + let out = Escape(&i_display); + let prefix = match it.inner_impl().polarity { + ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "", + ty::ImplPolarity::Negative => "!", + }; + let generated = format!("{}{}", encoded, prefix, out); + if links.insert(generated.clone()) { Some(generated) } else { None } + }) + .collect::>(); + ret.sort(); + ret + }; + + let concrete_format = format_impls(concrete, id_map); + let synthetic_format = format_impls(synthetic, id_map); + let blanket_format = format_impls(blanket_impl, id_map); + + if !concrete_format.is_empty() { + print_sidebar_block( + out, + "trait-implementations", + "Trait Implementations", + concrete_format.iter(), + ); + } + + if !synthetic_format.is_empty() { + print_sidebar_block( + out, + "synthetic-implementations", + "Auto Trait Implementations", + synthetic_format.iter(), + ); + } + + if !blanket_format.is_empty() { + print_sidebar_block( + out, + "blanket-implementations", + "Blanket Implementations", + blanket_format.iter(), + ); + } +} + fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) { let did = it.item_id.expect_def_id(); let cache = cx.cache(); @@ -1976,68 +2077,15 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) { { let mut derefs = FxHashSet::default(); derefs.insert(did); - sidebar_deref_methods(cx, out, impl_, v, &mut derefs); + sidebar_deref_methods(cx, out, impl_, v, &mut derefs, &mut used_links); } - let format_impls = |impls: Vec<&Impl>, id_map: &mut IdMap| { - let mut links = FxHashSet::default(); - - let mut ret = impls - .iter() - .filter_map(|it| { - let trait_ = it.inner_impl().trait_.as_ref()?; - let encoded = - id_map.derive(get_id_for_impl(&it.inner_impl().for_, Some(trait_), cx)); - - let i_display = format!("{:#}", trait_.print(cx)); - let out = Escape(&i_display); - let prefix = match it.inner_impl().polarity { - ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "", - ty::ImplPolarity::Negative => "!", - }; - let generated = format!("{}{}", encoded, prefix, out); - if links.insert(generated.clone()) { Some(generated) } else { None } - }) - .collect::>(); - ret.sort(); - ret - }; - let (synthetic, concrete): (Vec<&Impl>, Vec<&Impl>) = v.iter().partition::, _>(|i| i.inner_impl().kind.is_auto()); let (blanket_impl, concrete): (Vec<&Impl>, Vec<&Impl>) = concrete.into_iter().partition::, _>(|i| i.inner_impl().kind.is_blanket()); - let concrete_format = format_impls(concrete, &mut id_map); - let synthetic_format = format_impls(synthetic, &mut id_map); - let blanket_format = format_impls(blanket_impl, &mut id_map); - - if !concrete_format.is_empty() { - print_sidebar_block( - out, - "trait-implementations", - "Trait Implementations", - concrete_format.iter(), - ); - } - - if !synthetic_format.is_empty() { - print_sidebar_block( - out, - "synthetic-implementations", - "Auto Trait Implementations", - synthetic_format.iter(), - ); - } - - if !blanket_format.is_empty() { - print_sidebar_block( - out, - "blanket-implementations", - "Blanket Implementations", - blanket_format.iter(), - ); - } + sidebar_render_assoc_items(cx, out, &mut id_map, concrete, synthetic, blanket_impl); } } } @@ -2048,6 +2096,7 @@ fn sidebar_deref_methods( impl_: &Impl, v: &[Impl], derefs: &mut FxHashSet, + used_links: &mut FxHashSet, ) { let c = cx.cache(); @@ -2080,13 +2129,10 @@ fn sidebar_deref_methods( .and_then(|did| c.impls.get(&did)); if let Some(impls) = inner_impl { debug!("found inner_impl: {:?}", impls); - let mut used_links = FxHashSet::default(); let mut ret = impls .iter() .filter(|i| i.inner_impl().trait_.is_none()) - .flat_map(|i| { - get_methods(i.inner_impl(), true, &mut used_links, deref_mut, cx.tcx()) - }) + .flat_map(|i| get_methods(i.inner_impl(), true, used_links, deref_mut, cx.tcx())) .collect::>(); if !ret.is_empty() { let id = if let Some(target_def_id) = real_target.def_id(c) { @@ -2115,7 +2161,14 @@ fn sidebar_deref_methods( .map(|t| Some(t.def_id()) == cx.tcx().lang_items().deref_trait()) .unwrap_or(false) }) { - sidebar_deref_methods(cx, out, target_deref_impl, target_impls, derefs); + sidebar_deref_methods( + cx, + out, + target_deref_impl, + target_impls, + derefs, + used_links, + ); } } } @@ -2302,9 +2355,54 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean buf.push_str("") } +/// Returns the list of implementations for the primitive reference type, filtering out any +/// implementations that are on concrete or partially generic types, only keeping implementations +/// of the form `impl Trait for &T`. +pub(crate) fn get_filtered_impls_for_reference<'a>( + shared: &'a Rc>, + it: &clean::Item, +) -> (Vec<&'a Impl>, Vec<&'a Impl>, Vec<&'a Impl>) { + let def_id = it.item_id.expect_def_id(); + // If the reference primitive is somehow not defined, exit early. + let Some(v) = shared.cache.impls.get(&def_id) else { return (Vec::new(), Vec::new(), Vec::new()) }; + // Since there is no "direct implementation" on the reference primitive type, we filter out + // every implementation which isn't a trait implementation. + let traits = v.iter().filter(|i| i.inner_impl().trait_.is_some()); + let (synthetic, concrete): (Vec<&Impl>, Vec<&Impl>) = + traits.partition(|t| t.inner_impl().kind.is_auto()); + + let (blanket_impl, concrete): (Vec<&Impl>, _) = + concrete.into_iter().partition(|t| t.inner_impl().kind.is_blanket()); + // Now we keep only references over full generic types. + let concrete: Vec<_> = concrete + .into_iter() + .filter(|t| match t.inner_impl().for_ { + clean::Type::BorrowedRef { ref type_, .. } => type_.is_full_generic(), + _ => false, + }) + .collect(); + + (concrete, synthetic, blanket_impl) +} + fn sidebar_primitive(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item) { let mut sidebar = Buffer::new(); - sidebar_assoc_items(cx, &mut sidebar, it); + + if it.name.map(|n| n.as_str() != "reference").unwrap_or(false) { + sidebar_assoc_items(cx, &mut sidebar, it); + } else { + let shared = Rc::clone(&cx.shared); + let (concrete, synthetic, blanket_impl) = get_filtered_impls_for_reference(&shared, it); + + sidebar_render_assoc_items( + cx, + &mut sidebar, + &mut IdMap::new(), + concrete, + synthetic, + blanket_impl, + ); + } if !sidebar.is_empty() { write!(buf, "
{}
", sidebar.into_inner()); @@ -2614,8 +2712,8 @@ fn collect_paths_for_type(first_ty: clean::Type, cache: &Cache) -> Vec { clean::Type::BorrowedRef { type_, .. } => { work.push_back(*type_); } - clean::Type::QPath { self_type, trait_, .. } => { - work.push_back(*self_type); + clean::Type::QPath(box clean::QPathData { self_type, trait_, .. }) => { + work.push_back(self_type); process_path(trait_.def_id()); } _ => {} @@ -2668,7 +2766,7 @@ fn render_call_locations(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Ite let contents = match fs::read_to_string(&path) { Ok(contents) => contents, Err(err) => { - let span = item.span(tcx).inner(); + let span = item.span(tcx).map_or(rustc_span::DUMMY_SP, |span| span.inner()); tcx.sess .span_err(span, &format!("failed to read file {}: {}", path.display(), err)); return false; @@ -2764,11 +2862,10 @@ fn render_call_locations(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Ite sources::print_src( w, contents_subset, - call_data.edition, file_span, cx, &root_path, - Some(highlight::DecorationInfo(decoration_info)), + highlight::DecorationInfo(decoration_info), sources::SourceContext::Embedded { offset: line_min }, ); write!(w, ""); -- cgit v1.2.3