Skip to content

Rollup of 10 pull requests #136943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 30 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
5932b2f
tests/assembly: make windows ABI test cross-compile
workingjubilee Feb 9, 2025
b3464fa
tests/assembly: make typed-swap test much less fragile
workingjubilee Feb 9, 2025
ee111b2
tests/assembly: use -Copt-level=3 instead of -O
workingjubilee Feb 9, 2025
833f070
tests/assembly: cross-compile x86-return-float
workingjubilee Feb 9, 2025
3580698
tests: issue-122805 -> dont-shuffle-bswaps
workingjubilee Feb 10, 2025
95357c7
Check whole Unsize predicate for escaping bound vars
compiler-errors Feb 10, 2025
8a0a4df
Add docs to bootstrap:src:utils:cache
Shourya742 Feb 11, 2025
24150eb
add unit test to bootstrap:util:cache:tests
Shourya742 Feb 11, 2025
7972aa2
dev-guide: Link to t-lang procedures for new features
madsmtm Feb 11, 2025
4c17270
tests: simplify dont-shuffle-bswaps test
workingjubilee Feb 10, 2025
3c0c9b6
tests/codegen: use -Copt-level=3 instead of -O
workingjubilee Feb 9, 2025
d9c7abb
compiler: narrow scope of nightly cfg in rustc_abi
workingjubilee Feb 10, 2025
038c183
compiler: remove rustc_target reexport of rustc_abi::HashStableContext
workingjubilee Feb 10, 2025
8abff35
compiler: compare and hash ExternAbi like its string
workingjubilee Feb 10, 2025
edff4fe
compiler: remove AbiDatas
workingjubilee Feb 11, 2025
f8570e8
compiler: remove rustc_abi::lookup and AbiUnsupported
workingjubilee Feb 11, 2025
7564f3c
compiler: Make middle errors `pub(crate)` and bury some dead code
workingjubilee Feb 12, 2025
8a70219
use cc archiver as default in `cc2ar`
onur-ozkan Feb 12, 2025
d99d8c2
Nuke `Buffer` abstraction from `librustdoc` 💣
yotamofek Feb 9, 2025
21bb8cb
Change swap_nonoverlapping from lang to library UB
saethlin Feb 11, 2025
a3f76ad
Rollup merge of #136758 - workingjubilee:specify-opt-level-for-tests,…
GuillaumeGomez Feb 12, 2025
f0af030
Rollup merge of #136761 - workingjubilee:specify-opt-level-for-codege…
GuillaumeGomez Feb 12, 2025
10b5596
Rollup merge of #136784 - yotamofek:pr/rustdoc-remove-buffer-take2, r…
GuillaumeGomez Feb 12, 2025
993eb34
Rollup merge of #136838 - compiler-errors:escaping-unsize, r=fmease
GuillaumeGomez Feb 12, 2025
2494905
Rollup merge of #136848 - Shourya742:2025-02-11-add-docs-and-ut-for-u…
GuillaumeGomez Feb 12, 2025
428baf5
Rollup merge of #136871 - madsmtm:link-to-lang-procedures, r=scottmcm
GuillaumeGomez Feb 12, 2025
269d784
Rollup merge of #136890 - saethlin:swap_nonoverlapping, r=RalfJung
GuillaumeGomez Feb 12, 2025
27dc222
Rollup merge of #136901 - workingjubilee:stabilize-externabi-hashing-…
GuillaumeGomez Feb 12, 2025
54b4b1c
Rollup merge of #136907 - workingjubilee:middle-errors-cleanup, r=com…
GuillaumeGomez Feb 12, 2025
8567fbb
Rollup merge of #136916 - onur-ozkan:fix-cc2ar, r=jieyouxu
GuillaumeGomez Feb 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 9 additions & 114 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,113 +37,8 @@ use crate::html::render::Context;
use crate::joined::Joined as _;
use crate::passes::collect_intra_doc_links::UrlFragment;

pub(crate) trait Print {
fn print(self, buffer: &mut Buffer);
}

impl<F> Print for F
where
F: FnOnce(&mut Buffer),
{
fn print(self, buffer: &mut Buffer) {
(self)(buffer)
}
}

impl Print for String {
fn print(self, buffer: &mut Buffer) {
buffer.write_str(&self);
}
}

impl Print for &'_ str {
fn print(self, buffer: &mut Buffer) {
buffer.write_str(self);
}
}

#[derive(Debug, Clone)]
pub(crate) struct Buffer {
for_html: bool,
buffer: String,
}

impl core::fmt::Write for Buffer {
#[inline]
fn write_str(&mut self, s: &str) -> fmt::Result {
self.buffer.write_str(s)
}

#[inline]
fn write_char(&mut self, c: char) -> fmt::Result {
self.buffer.write_char(c)
}

#[inline]
fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result {
self.buffer.write_fmt(args)
}
}

impl Buffer {
pub(crate) fn empty_from(v: &Buffer) -> Buffer {
Buffer { for_html: v.for_html, buffer: String::new() }
}

pub(crate) fn html() -> Buffer {
Buffer { for_html: true, buffer: String::new() }
}

pub(crate) fn new() -> Buffer {
Buffer { for_html: false, buffer: String::new() }
}

pub(crate) fn is_empty(&self) -> bool {
self.buffer.is_empty()
}

pub(crate) fn into_inner(self) -> String {
self.buffer
}

pub(crate) fn push(&mut self, c: char) {
self.buffer.push(c);
}

pub(crate) fn push_str(&mut self, s: &str) {
self.buffer.push_str(s);
}

pub(crate) fn push_buffer(&mut self, other: Buffer) {
self.buffer.push_str(&other.buffer);
}

// Intended for consumption by write! and writeln! (std::fmt) but without
// the fmt::Result return type imposed by fmt::Write (and avoiding the trait
// import).
pub(crate) fn write_str(&mut self, s: &str) {
self.buffer.push_str(s);
}

// Intended for consumption by write! and writeln! (std::fmt) but without
// the fmt::Result return type imposed by fmt::Write (and avoiding the trait
// import).
pub(crate) fn write_fmt(&mut self, v: fmt::Arguments<'_>) {
self.buffer.write_fmt(v).unwrap();
}

pub(crate) fn to_display<T: Print>(mut self, t: T) -> String {
t.print(&mut self);
self.into_inner()
}

pub(crate) fn reserve(&mut self, additional: usize) {
self.buffer.reserve(additional)
}

pub(crate) fn len(&self) -> usize {
self.buffer.len()
}
pub(crate) fn write_str(s: &mut String, f: fmt::Arguments<'_>) {
s.write_fmt(f).unwrap();
}

pub(crate) fn print_generic_bounds<'a, 'tcx: 'a>(
Expand Down Expand Up @@ -772,27 +667,27 @@ pub(crate) fn link_tooltip(did: DefId, fragment: &Option<UrlFragment>, cx: &Cont
else {
return String::new();
};
let mut buf = Buffer::new();
let mut buf = String::new();
let fqp = if *shortty == ItemType::Primitive {
// primitives are documented in a crate, but not actually part of it
&fqp[fqp.len() - 1..]
} else {
fqp
};
if let &Some(UrlFragment::Item(id)) = fragment {
write!(buf, "{} ", cx.tcx().def_descr(id));
write_str(&mut buf, format_args!("{} ", cx.tcx().def_descr(id)));
for component in fqp {
write!(buf, "{component}::");
write_str(&mut buf, format_args!("{component}::"));
}
write!(buf, "{}", cx.tcx().item_name(id));
write_str(&mut buf, format_args!("{}", cx.tcx().item_name(id)));
} else if !fqp.is_empty() {
let mut fqp_it = fqp.iter();
write!(buf, "{shortty} {}", fqp_it.next().unwrap());
write_str(&mut buf, format_args!("{shortty} {}", fqp_it.next().unwrap()));
for component in fqp_it {
write!(buf, "::{component}");
write_str(&mut buf, format_args!("::{component}"));
}
}
buf.into_inner()
buf
}

/// Used to render a [`clean::Path`].
Expand Down
80 changes: 44 additions & 36 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_span::edition::Edition;
use rustc_span::symbol::Symbol;
use rustc_span::{BytePos, DUMMY_SP, Span};

use super::format::{self, Buffer};
use super::format::{self, write_str};
use crate::clean::PrimitiveType;
use crate::html::escape::EscapeBodyText;
use crate::html::render::{Context, LinkFromSrc};
Expand Down Expand Up @@ -48,7 +48,7 @@ pub(crate) enum Tooltip {
/// Highlights `src` as an inline example, returning the HTML output.
pub(crate) fn render_example_with_highlighting(
src: &str,
out: &mut Buffer,
out: &mut String,
tooltip: Tooltip,
playground_button: Option<&str>,
extra_classes: &[String],
Expand All @@ -59,61 +59,69 @@ pub(crate) fn render_example_with_highlighting(
}

fn write_header(
out: &mut Buffer,
out: &mut String,
class: &str,
extra_content: Option<Buffer>,
extra_content: Option<&str>,
tooltip: Tooltip,
extra_classes: &[String],
) {
write!(
write_str(
out,
"<div class=\"example-wrap{}\">",
match tooltip {
Tooltip::Ignore => " ignore",
Tooltip::CompileFail => " compile_fail",
Tooltip::ShouldPanic => " should_panic",
Tooltip::Edition(_) => " edition",
Tooltip::None => "",
},
format_args!(
"<div class=\"example-wrap{}\">",
match tooltip {
Tooltip::Ignore => " ignore",
Tooltip::CompileFail => " compile_fail",
Tooltip::ShouldPanic => " should_panic",
Tooltip::Edition(_) => " edition",
Tooltip::None => "",
}
),
);

if tooltip != Tooltip::None {
let edition_code;
write!(
write_str(
out,
"<a href=\"#\" class=\"tooltip\" title=\"{}\">ⓘ</a>",
match tooltip {
Tooltip::Ignore => "This example is not tested",
Tooltip::CompileFail => "This example deliberately fails to compile",
Tooltip::ShouldPanic => "This example panics",
Tooltip::Edition(edition) => {
edition_code = format!("This example runs with edition {edition}");
&edition_code
format_args!(
"<a href=\"#\" class=\"tooltip\" title=\"{}\">ⓘ</a>",
match tooltip {
Tooltip::Ignore => "This example is not tested",
Tooltip::CompileFail => "This example deliberately fails to compile",
Tooltip::ShouldPanic => "This example panics",
Tooltip::Edition(edition) => {
edition_code = format!("This example runs with edition {edition}");
&edition_code
}
Tooltip::None => unreachable!(),
}
Tooltip::None => unreachable!(),
},
),
);
}

if let Some(extra) = extra_content {
out.push_buffer(extra);
out.push_str(&extra);
}
if class.is_empty() {
write!(
write_str(
out,
"<pre class=\"rust{}{}\">",
if extra_classes.is_empty() { "" } else { " " },
extra_classes.join(" "),
format_args!(
"<pre class=\"rust{}{}\">",
if extra_classes.is_empty() { "" } else { " " },
extra_classes.join(" ")
),
);
} else {
write!(
write_str(
out,
"<pre class=\"rust {class}{}{}\">",
if extra_classes.is_empty() { "" } else { " " },
extra_classes.join(" "),
format_args!(
"<pre class=\"rust {class}{}{}\">",
if extra_classes.is_empty() { "" } else { " " },
extra_classes.join(" ")
),
);
}
write!(out, "<code>");
write_str(out, format_args!("<code>"));
}

/// Check if two `Class` can be merged together. In the following rules, "unclassified" means `None`
Expand Down Expand Up @@ -398,8 +406,8 @@ pub(super) fn write_code(
});
}

fn write_footer(out: &mut Buffer, playground_button: Option<&str>) {
writeln!(out, "</code></pre>{}</div>", playground_button.unwrap_or_default());
fn write_footer(out: &mut String, playground_button: Option<&str>) {
write_str(out, format_args_nl!("</code></pre>{}</div>", playground_button.unwrap_or_default()));
}

/// How a span of text is classified. Mostly corresponds to token kinds.
Expand Down
21 changes: 10 additions & 11 deletions src/librustdoc/html/highlight/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use rustc_data_structures::fx::FxIndexMap;
use rustc_span::create_default_session_globals_then;

use super::{DecorationInfo, write_code};
use crate::html::format::Buffer;

const STYLE: &str = r#"
<style>
Expand All @@ -22,9 +21,9 @@ fn test_html_highlighting() {
create_default_session_globals_then(|| {
let src = include_str!("fixtures/sample.rs");
let html = {
let mut out = Buffer::new();
let mut out = String::new();
write_code(&mut out, src, None, None, None);
format!("{STYLE}<pre><code>{}</code></pre>\n", out.into_inner())
format!("{STYLE}<pre><code>{out}</code></pre>\n")
};
expect_file!["fixtures/sample.html"].assert_eq(&html);
});
Expand All @@ -36,9 +35,9 @@ fn test_dos_backline() {
let src = "pub fn foo() {\r\n\
println!(\"foo\");\r\n\
}\r\n";
let mut html = Buffer::new();
let mut html = String::new();
write_code(&mut html, src, None, None, None);
expect_file!["fixtures/dos_line.html"].assert_eq(&html.into_inner());
expect_file!["fixtures/dos_line.html"].assert_eq(&html);
});
}

Expand All @@ -50,19 +49,19 @@ use self::whatever;
let x = super::b::foo;
let y = Self::whatever;";

let mut html = Buffer::new();
let mut html = String::new();
write_code(&mut html, src, None, None, None);
expect_file!["fixtures/highlight.html"].assert_eq(&html.into_inner());
expect_file!["fixtures/highlight.html"].assert_eq(&html);
});
}

#[test]
fn test_union_highlighting() {
create_default_session_globals_then(|| {
let src = include_str!("fixtures/union.rs");
let mut html = Buffer::new();
let mut html = String::new();
write_code(&mut html, src, None, None, None);
expect_file!["fixtures/union.html"].assert_eq(&html.into_inner());
expect_file!["fixtures/union.html"].assert_eq(&html);
});
}

Expand All @@ -77,8 +76,8 @@ let a = 4;";
decorations.insert("example", vec![(0, 10), (11, 21)]);
decorations.insert("example2", vec![(22, 32)]);

let mut html = Buffer::new();
let mut html = String::new();
write_code(&mut html, src, None, Some(&DecorationInfo(decorations)), None);
expect_file!["fixtures/decorations.html"].assert_eq(&html.into_inner());
expect_file!["fixtures/decorations.html"].assert_eq(&html);
});
}
25 changes: 21 additions & 4 deletions src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::fmt::{self, Display};
use std::path::PathBuf;

use rinja::Template;
use rustc_data_structures::fx::FxIndexMap;

use super::static_files::{STATIC_FILES, StaticFiles};
use crate::externalfiles::ExternalHtml;
use crate::html::format::{Buffer, Print};
use crate::html::render::{StylePath, ensure_trailing_slash};

#[derive(Clone)]
Expand Down Expand Up @@ -71,7 +71,24 @@ struct PageLayout<'a> {

pub(crate) use crate::html::render::sidebar::filters;

pub(crate) fn render<T: Print, S: Print>(
/// Implements [`Display`] for a function that accepts a mutable reference to a [`String`], and (optionally) writes to it.
///
/// The wrapped function will receive an empty string, and can modify it,
/// and the `Display` implementation will write the contents of the string after the function has finished.
pub(crate) struct BufDisplay<F>(pub F);

impl<F> Display for BufDisplay<F>
where
F: Fn(&mut String),
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut buf = String::new();
self.0(&mut buf);
f.write_str(&buf)
}
}

pub(crate) fn render<T: Display, S: Display>(
layout: &Layout,
page: &Page<'_>,
sidebar: S,
Expand All @@ -98,8 +115,8 @@ pub(crate) fn render<T: Print, S: Print>(
let mut themes: Vec<String> = style_files.iter().map(|s| s.basename().unwrap()).collect();
themes.sort();

let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar.
let sidebar = Buffer::html().to_display(sidebar);
let content = t.to_string(); // Note: This must happen before making the sidebar.
let sidebar = sidebar.to_string();
PageLayout {
static_root_path,
page,
Expand Down
Loading