Skip to content

Rollup of 9 pull requests #142492

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 40 commits into from
Jun 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
20cf8ca
float tests: use assert_biteq in more places
RalfJung Jun 9, 2025
79cb013
float tests: deduplicate min, max, and rounding tests
RalfJung Jun 9, 2025
2766b77
make the default float comparison tolerance type-dependent
RalfJung Jun 9, 2025
1b6bb45
float midpoint tests: add missing NAN cases
RalfJung Jun 9, 2025
3497619
compiler-builtins: Remove unused `lints.rust` table
tgross35 Jun 10, 2025
b5fafd9
ci: Fix a typo that was causing a command failure
tgross35 Jun 10, 2025
5864247
Give more information into extracted doctest information
GuillaumeGomez May 22, 2025
21aabaf
Add new extracted option doctest ui test for result returning doctest
GuillaumeGomez May 22, 2025
eb61be3
Improve naming of variables in `DocTestBuilder::generate_unique_doctest`
GuillaumeGomez May 23, 2025
89e3510
Add more tests to check extracted doctests feature
GuillaumeGomez Jun 4, 2025
f1ceb07
Update extracted-doctest feature documentation
GuillaumeGomez Jun 4, 2025
ded2afc
Delegate `<SocketAddr as Debug>` to `ByteStr`
tamird May 24, 2025
c0e02e2
Unimplement unsized_locals
mejrs Jun 12, 2025
013e06c
Eliminate `build.rs`-generated Aarch64 atomic macros (#951)
qinghon Jun 13, 2025
99929fa
Implement temporary lifetime extension for tuple ctors.
m-ou-se May 2, 2025
ff5ba7a
Update tests.
m-ou-se Jun 13, 2025
e71a93b
Add comment.
m-ou-se May 2, 2025
578fa8e
Add tests.
m-ou-se May 3, 2025
855ea48
Add test for temporary lifetime extension in `Self()` syntax.
m-ou-se May 7, 2025
25ec235
tweak runtime/const macro management
RalfJung Jun 13, 2025
10ba25f
fmaximum,fminimum: Fix incorrect result and add tests
tgross35 Jun 2, 2025
963fdbc
variadic functions: remove list of supported ABIs from error
RalfJung Jun 13, 2025
509b8f2
Clean up and sort manifest keys
tgross35 Jun 13, 2025
19b47ae
Mark compiler-builtins as `publish = false`
tgross35 Jun 13, 2025
c9995d2
Failing test
JonathanBrouwer Jun 13, 2025
1d036f4
Fix incorrect suggestion when calling an associated type with a type …
JonathanBrouwer Jun 13, 2025
dac9d78
Remove unneeded lifetimes from signature of BTreeSet::extract_if
dtolnay Jun 14, 2025
4b2b463
Delete `.release-plz.toml`
tgross35 Jun 14, 2025
c629d85
Update the upstream Rust version
tgross35 Jun 14, 2025
c1cd1ef
Merge ref 'd087f112b7d1:/library/compiler-builtins' from https://gith…
tgross35 Jun 14, 2025
7222fa6
Work around out-of-tree testing with a shim crate
tgross35 Jun 14, 2025
ade745e
Rollup merge of #140593 - m-ou-se:some-temp, r=Nadrieril
matthiaskrgr Jun 14, 2025
9bdf5d3
Rollup merge of #141399 - GuillaumeGomez:extracted-doctest, r=aDotInT…
matthiaskrgr Jun 14, 2025
8f90d05
Rollup merge of #141493 - tamird:addreskind-bytestr, r=joshtriplett
matthiaskrgr Jun 14, 2025
db23a76
Rollup merge of #141811 - mejrs:bye_locals, r=compiler-errors
matthiaskrgr Jun 14, 2025
4cf4473
Rollup merge of #142243 - RalfJung:float-test-dedup, r=tgross35
matthiaskrgr Jun 14, 2025
fe54c3a
Rollup merge of #142464 - RalfJung:variadic-fn-abi-error, r=workingju…
matthiaskrgr Jun 14, 2025
4bf7765
Rollup merge of #142477 - JonathanBrouwer:associated-type-suggestion,…
matthiaskrgr Jun 14, 2025
22f91ae
Rollup merge of #142484 - dtolnay:bsetextract, r=m-ou-se
matthiaskrgr Jun 14, 2025
fd1f479
Rollup merge of #142489 - tgross35:update-builtins, r=tgross35
matthiaskrgr Jun 14, 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
Prev Previous commit
Next Next commit
Give more information into extracted doctest information
  • Loading branch information
GuillaumeGomez committed Jun 10, 2025
commit 5864247b1028f2fe6a1deca3f45d62fb848f61d0
4 changes: 2 additions & 2 deletions src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,14 +1053,14 @@ fn doctest_run_fn(
let report_unused_externs = |uext| {
unused_externs.lock().unwrap().push(uext);
};
let (full_test_code, full_test_line_offset) = doctest.generate_unique_doctest(
let (wrapper, full_test_line_offset) = doctest.generate_unique_doctest(
&scraped_test.text,
scraped_test.langstr.test_harness,
&global_opts,
Some(&global_opts.crate_name),
);
let runnable_test = RunnableDocTest {
full_test_code,
full_test_code: wrapper.to_string(),
full_test_line_offset,
test_opts,
global_opts,
Expand Down
35 changes: 30 additions & 5 deletions src/librustdoc/doctest/extracted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use serde::Serialize;

use super::make::DocTestWrapResult;
use super::{BuildDocTestBuilder, ScrapedDocTest};
use crate::config::Options as RustdocOptions;
use crate::html::markdown;
Expand All @@ -14,7 +15,7 @@ use crate::html::markdown;
/// This integer is incremented with every breaking change to the API,
/// and is returned along with the JSON blob into the `format_version` root field.
/// Consuming code should assert that this value matches the format version(s) that it supports.
const FORMAT_VERSION: u32 = 1;
const FORMAT_VERSION: u32 = 2;

#[derive(Serialize)]
pub(crate) struct ExtractedDocTests {
Expand Down Expand Up @@ -44,8 +45,7 @@ impl ExtractedDocTests {
.edition(edition)
.lang_str(&langstr)
.build(None);

let (full_test_code, size) = doctest.generate_unique_doctest(
let (wrapper, _size) = doctest.generate_unique_doctest(
&text,
langstr.test_harness,
opts,
Expand All @@ -55,21 +55,46 @@ impl ExtractedDocTests {
file: filename.prefer_remapped_unconditionaly().to_string(),
line,
doctest_attributes: langstr.into(),
doctest_code: if size != 0 { Some(full_test_code) } else { None },
doctest_code: match wrapper {
DocTestWrapResult::Valid { crate_level_code, wrapper, code } => Some(DocTest {
crate_level: crate_level_code,
code,
wrapper: wrapper.map(
|super::make::WrapperInfo { before, after, returns_result, .. }| {
WrapperInfo { before, after, returns_result }
},
),
}),
DocTestWrapResult::SyntaxError { .. } => None,
},
original_code: text,
name,
});
}
}

#[derive(Serialize)]
pub(crate) struct WrapperInfo {
before: String,
after: String,
returns_result: bool,
}

#[derive(Serialize)]
pub(crate) struct DocTest {
crate_level: String,
code: String,
wrapper: Option<WrapperInfo>,
}

#[derive(Serialize)]
pub(crate) struct ExtractedDocTest {
file: String,
line: usize,
doctest_attributes: LangString,
original_code: String,
/// `None` if the code syntax is invalid.
doctest_code: Option<String>,
doctest_code: Option<DocTest>,
name: String,
}

Expand Down
137 changes: 98 additions & 39 deletions src/librustdoc/doctest/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,73 @@ pub(crate) struct DocTestBuilder {
pub(crate) can_be_merged: bool,
}

/// Contains needed information for doctest to be correctly generated with expected "wrapping".
pub(crate) struct WrapperInfo {
pub(crate) before: String,
pub(crate) after: String,
pub(crate) returns_result: bool,
insert_indent_space: bool,
}

impl WrapperInfo {
fn len(&self) -> usize {
self.before.len() + self.after.len()
}
}

/// Contains a doctest information. Can be converted into code with the `to_string()` method.
pub(crate) enum DocTestWrapResult {
Valid {
crate_level_code: String,
wrapper: Option<WrapperInfo>,
code: String,
},
/// Contains the original source code.
SyntaxError(String),
}

impl std::string::ToString for DocTestWrapResult {
fn to_string(&self) -> String {
match self {
Self::SyntaxError(s) => s.clone(),
Self::Valid { crate_level_code, wrapper, code } => {
let mut prog_len = code.len() + crate_level_code.len();
if let Some(wrapper) = wrapper {
prog_len += wrapper.len();
if wrapper.insert_indent_space {
prog_len += code.lines().count() * 4;
}
}
let mut prog = String::with_capacity(prog_len);

prog.push_str(crate_level_code);
if let Some(wrapper) = wrapper {
prog.push_str(&wrapper.before);

// add extra 4 spaces for each line to offset the code block
if wrapper.insert_indent_space {
write!(
prog,
"{}",
fmt::from_fn(|f| code
.lines()
.map(|line| fmt::from_fn(move |f| write!(f, " {line}")))
.joined("\n", f))
)
.unwrap();
} else {
prog.push_str(code);
}
prog.push_str(&wrapper.after);
} else {
prog.push_str(code);
}
prog
}
}
}
}

impl DocTestBuilder {
fn invalid(
global_crate_attrs: Vec<String>,
Expand Down Expand Up @@ -228,50 +295,49 @@ impl DocTestBuilder {
dont_insert_main: bool,
opts: &GlobalTestOptions,
crate_name: Option<&str>,
) -> (String, usize) {
) -> (DocTestWrapResult, usize) {
if self.invalid_ast {
// If the AST failed to compile, no need to go generate a complete doctest, the error
// will be better this way.
debug!("invalid AST:\n{test_code}");
return (test_code.to_string(), 0);
return (DocTestWrapResult::SyntaxError(test_code.to_string()), 0);
}
let mut line_offset = 0;
let mut prog = String::new();
let everything_else = self.everything_else.trim();

let mut crate_level_code = String::new();
let code = self.everything_else.trim();
if self.global_crate_attrs.is_empty() {
// If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
// lints that are commonly triggered in doctests. The crate-level test attributes are
// commonly used to make tests fail in case they trigger warnings, so having this there in
// that case may cause some tests to pass when they shouldn't have.
prog.push_str("#![allow(unused)]\n");
crate_level_code.push_str("#![allow(unused)]\n");
line_offset += 1;
}

// Next, any attributes that came from #![doc(test(attr(...)))].
for attr in &self.global_crate_attrs {
prog.push_str(&format!("#![{attr}]\n"));
crate_level_code.push_str(&format!("#![{attr}]\n"));
line_offset += 1;
}

// Now push any outer attributes from the example, assuming they
// are intended to be crate attributes.
if !self.crate_attrs.is_empty() {
prog.push_str(&self.crate_attrs);
crate_level_code.push_str(&self.crate_attrs);
if !self.crate_attrs.ends_with('\n') {
prog.push('\n');
crate_level_code.push('\n');
}
}
if !self.maybe_crate_attrs.is_empty() {
prog.push_str(&self.maybe_crate_attrs);
crate_level_code.push_str(&self.maybe_crate_attrs);
if !self.maybe_crate_attrs.ends_with('\n') {
prog.push('\n');
crate_level_code.push('\n');
}
}
if !self.crates.is_empty() {
prog.push_str(&self.crates);
crate_level_code.push_str(&self.crates);
if !self.crates.ends_with('\n') {
prog.push('\n');
crate_level_code.push('\n');
}
}

Expand All @@ -289,17 +355,20 @@ impl DocTestBuilder {
{
// rustdoc implicitly inserts an `extern crate` item for the own crate
// which may be unused, so we need to allow the lint.
prog.push_str("#[allow(unused_extern_crates)]\n");
crate_level_code.push_str("#[allow(unused_extern_crates)]\n");

prog.push_str(&format!("extern crate r#{crate_name};\n"));
crate_level_code.push_str(&format!("extern crate r#{crate_name};\n"));
line_offset += 1;
}

// FIXME: This code cannot yet handle no_std test cases yet
if dont_insert_main || self.has_main_fn || prog.contains("![no_std]") {
prog.push_str(everything_else);
let wrapper = if dont_insert_main
|| self.has_main_fn
|| crate_level_code.contains("![no_std]")
{
None
} else {
let returns_result = everything_else.ends_with("(())");
let returns_result = code.ends_with("(())");
// Give each doctest main function a unique name.
// This is for example needed for the tooling around `-C instrument-coverage`.
let inner_fn_name = if let Some(ref test_id) = self.test_id {
Expand Down Expand Up @@ -333,28 +402,18 @@ impl DocTestBuilder {
// /// ``` <- end of the inner main
line_offset += 1;

prog.push_str(&main_pre);

// add extra 4 spaces for each line to offset the code block
if opts.insert_indent_space {
write!(
prog,
"{}",
fmt::from_fn(|f| everything_else
.lines()
.map(|line| fmt::from_fn(move |f| write!(f, " {line}")))
.joined("\n", f))
)
.unwrap();
} else {
prog.push_str(everything_else);
};
prog.push_str(&main_post);
}

debug!("final doctest:\n{prog}");
Some(WrapperInfo {
before: main_pre,
after: main_post,
returns_result,
insert_indent_space: opts.insert_indent_space,
})
};

(prog, line_offset)
(
DocTestWrapResult::Valid { code: code.to_string(), wrapper, crate_level_code },
line_offset,
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/doctest/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ fn make_test(
builder = builder.test_id(test_id.to_string());
}
let doctest = builder.build(None);
let (code, line_offset) =
let (wrapper, line_offset) =
doctest.generate_unique_doctest(test_code, dont_insert_main, opts, crate_name);
(code, line_offset)
(wrapper.to_string(), line_offset)
}

/// Default [`GlobalTestOptions`] for these unit tests.
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
builder = builder.crate_name(krate);
}
let doctest = builder.build(None);
let (test, _) = doctest.generate_unique_doctest(&test, false, &opts, krate);
let (wrapped, _) = doctest.generate_unique_doctest(&test, false, &opts, krate);
let test = wrapped.to_string();
let channel = if test.contains("#![feature(") { "&amp;version=nightly" } else { "" };

let test_escaped = small_url_encode(test);
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc-ui/extract-doctests.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"format_version":1,"doctests":[{"file":"$DIR/extract-doctests.rs","line":8,"doctest_attributes":{"original":"ignore (checking attributes)","should_panic":false,"no_run":false,"ignore":"All","rust":true,"test_harness":false,"compile_fail":false,"standalone_crate":false,"error_codes":[],"edition":null,"added_css_classes":[],"unknown":[]},"original_code":"let x = 12;\nlet y = 14;","doctest_code":"#![allow(unused)]\nfn main() {\nlet x = 12;\nlet y = 14;\n}","name":"$DIR/extract-doctests.rs - (line 8)"},{"file":"$DIR/extract-doctests.rs","line":13,"doctest_attributes":{"original":"edition2018,compile_fail","should_panic":false,"no_run":true,"ignore":"None","rust":true,"test_harness":false,"compile_fail":true,"standalone_crate":false,"error_codes":[],"edition":"2018","added_css_classes":[],"unknown":[]},"original_code":"let","doctest_code":null,"name":"$DIR/extract-doctests.rs - (line 13)"}]}
{"format_version":2,"doctests":[{"file":"$DIR/extract-doctests.rs","line":8,"doctest_attributes":{"original":"ignore (checking attributes)","should_panic":false,"no_run":false,"ignore":"All","rust":true,"test_harness":false,"compile_fail":false,"standalone_crate":false,"error_codes":[],"edition":null,"added_css_classes":[],"unknown":[]},"original_code":"let x = 12;\nlet y = 14;","doctest_code":{"crate_level":"#![allow(unused)]\n","code":"let x = 12;\nlet y = 14;","wrapper":{"before":"fn main() {\n","after":"\n}","returns_result":false}},"name":"$DIR/extract-doctests.rs - (line 8)"},{"file":"$DIR/extract-doctests.rs","line":13,"doctest_attributes":{"original":"edition2018,compile_fail","should_panic":false,"no_run":true,"ignore":"None","rust":true,"test_harness":false,"compile_fail":true,"standalone_crate":false,"error_codes":[],"edition":"2018","added_css_classes":[],"unknown":[]},"original_code":"let","doctest_code":null,"name":"$DIR/extract-doctests.rs - (line 13)"}]}