Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 03118fa

Browse files
Simplify has_main_fn to be a boolean instead of a Option<Span>
1 parent 010731d commit 03118fa

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

src/librustdoc/doctest.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,13 +536,17 @@ fn run_test(
536536
compiler.arg("--error-format=short");
537537
let input_file =
538538
doctest.test_opts.outdir.path().join(&format!("doctest_{}.rs", doctest.edition));
539+
eprintln!("OUUUUUUUT>>>>>>> {input_file:?}");
539540
if std::fs::write(&input_file, &doctest.full_test_code).is_err() {
540541
// If we cannot write this file for any reason, we leave. All combined tests will be
541542
// tested as standalone tests.
542543
return Err(TestFailure::CompileError);
543544
}
544545
compiler.arg(input_file);
545-
compiler.stderr(Stdio::null());
546+
// compiler.stderr(Stdio::null());
547+
let mut buffer = String::new();
548+
eprintln!("Press ENTER");
549+
let _ = std::io::stdin().read_line(&mut buffer);
546550
} else {
547551
compiler.arg("-");
548552
compiler.stdin(Stdio::piped());
@@ -764,7 +768,7 @@ struct CreateRunnableDoctests {
764768

765769
impl CreateRunnableDoctests {
766770
fn new(rustdoc_options: RustdocOptions, opts: GlobalTestOptions) -> CreateRunnableDoctests {
767-
let can_merge_doctests = rustdoc_options.edition >= Edition::Edition2024;
771+
let can_merge_doctests = true;//rustdoc_options.edition >= Edition::Edition2024;
768772
CreateRunnableDoctests {
769773
standalone_tests: Vec::new(),
770774
mergeable_tests: FxHashMap::default(),

src/librustdoc/doctest/make.rs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ use rustc_session::parse::ParseSess;
1313
use rustc_span::edition::Edition;
1414
use rustc_span::source_map::SourceMap;
1515
use rustc_span::symbol::sym;
16-
use rustc_span::{FileName, Span, DUMMY_SP};
16+
use rustc_span::FileName;
1717

1818
use super::GlobalTestOptions;
1919

2020
pub(crate) struct DocTest {
2121
pub(crate) supports_color: bool,
2222
pub(crate) already_has_extern_crate: bool,
23-
pub(crate) main_fn_span: Option<Span>,
23+
pub(crate) has_main_fn: bool,
2424
pub(crate) crate_attrs: String,
2525
pub(crate) crates: String,
2626
pub(crate) everything_else: String,
@@ -43,7 +43,7 @@ impl DocTest {
4343

4444
// Uses librustc_ast to parse the doctest and find if there's a main fn and the extern
4545
// crate already is included.
46-
let Ok((main_fn_span, already_has_extern_crate, failed_ast)) =
46+
let Ok((has_main_fn, already_has_extern_crate, failed_ast)) =
4747
check_for_main_and_extern_crate(
4848
crate_name,
4949
source,
@@ -58,7 +58,7 @@ impl DocTest {
5858
// The error will be reported during compilation.
5959
return DocTest {
6060
supports_color: false,
61-
main_fn_span: None,
61+
has_main_fn: false,
6262
crate_attrs,
6363
crates,
6464
everything_else,
@@ -70,7 +70,7 @@ impl DocTest {
7070
};
7171
Self {
7272
supports_color,
73-
main_fn_span,
73+
has_main_fn,
7474
crate_attrs,
7575
crates,
7676
everything_else,
@@ -141,7 +141,7 @@ impl DocTest {
141141
}
142142

143143
// FIXME: This code cannot yet handle no_std test cases yet
144-
if dont_insert_main || self.main_fn_span.is_some() || prog.contains("![no_std]") {
144+
if dont_insert_main || self.has_main_fn || prog.contains("![no_std]") {
145145
prog.push_str(everything_else);
146146
} else {
147147
let returns_result = everything_else.ends_with("(())");
@@ -218,7 +218,7 @@ fn cancel_error_count(psess: &ParseSess) {
218218

219219
fn parse_source(
220220
source: String,
221-
found_main_span: &mut Option<Span>,
221+
has_main_fn: &mut bool,
222222
found_extern_crate: &mut bool,
223223
found_macro: &mut bool,
224224
crate_name: &Option<&str>,
@@ -263,22 +263,22 @@ fn parse_source(
263263
// functions, we would thing all top-level items (so basically nothing).
264264
fn check_item(
265265
item: &ast::Item,
266-
found_main_span: &mut Option<Span>,
266+
has_main_fn: &mut bool,
267267
found_extern_crate: &mut bool,
268268
found_macro: &mut bool,
269269
crate_name: &Option<&str>,
270270
) {
271271
match item.kind {
272-
ast::ItemKind::Fn(ref fn_item) if found_main_span.is_none() => {
272+
ast::ItemKind::Fn(ref fn_item) if !*has_main_fn => {
273273
if item.ident.name == sym::main {
274-
*found_main_span = Some(item.span);
274+
*has_main_fn = true;
275275
}
276276
if let Some(ref body) = fn_item.body {
277277
for stmt in &body.stmts {
278278
match stmt.kind {
279279
ast::StmtKind::Item(ref item) => check_item(
280280
item,
281-
found_main_span,
281+
has_main_fn,
282282
found_extern_crate,
283283
found_macro,
284284
crate_name,
@@ -305,9 +305,9 @@ fn parse_source(
305305
loop {
306306
match parser.parse_item(ForceCollect::No) {
307307
Ok(Some(item)) => {
308-
check_item(&item, found_main_span, found_extern_crate, found_macro, crate_name);
308+
check_item(&item, has_main_fn, found_extern_crate, found_macro, crate_name);
309309

310-
if found_main_span.is_some() && *found_extern_crate {
310+
if *has_main_fn && *found_extern_crate {
311311
break;
312312
}
313313
}
@@ -319,7 +319,7 @@ fn parse_source(
319319
}
320320
}
321321

322-
// The supplied slice is only used for diagnostics,
322+
// The supplied item is only used for diagnostics,
323323
// which are swallowed here anyway.
324324
parser.maybe_consume_incorrect_semicolon(None);
325325
}
@@ -328,6 +328,7 @@ fn parse_source(
328328
parsing_result
329329
}
330330

331+
/// Returns `(has_main_fn, already_has_extern_crate, failed_ast)`.
331332
fn check_for_main_and_extern_crate(
332333
crate_name: Option<&str>,
333334
original_source_code: &str,
@@ -336,16 +337,16 @@ fn check_for_main_and_extern_crate(
336337
edition: Edition,
337338
supports_color: &mut bool,
338339
can_merge_doctests: bool,
339-
) -> Result<(Option<Span>, bool, bool), FatalError> {
340+
) -> Result<(bool, bool, bool), FatalError> {
340341
let result = rustc_driver::catch_fatal_errors(|| {
341342
rustc_span::create_session_if_not_set_then(edition, |_| {
342-
let mut found_main_span = None;
343+
let mut has_main_fn = false;
343344
let mut found_extern_crate = crate_name.is_none();
344345
let mut found_macro = false;
345346

346347
let mut parsing_result = parse_source(
347348
format!("{crates}{everything_else}"),
348-
&mut found_main_span,
349+
&mut has_main_fn,
349350
&mut found_extern_crate,
350351
&mut found_macro,
351352
&crate_name,
@@ -366,21 +367,21 @@ fn check_for_main_and_extern_crate(
366367
// faster doctests run time.
367368
parsing_result = parse_source(
368369
format!("{crates}\nfn __doctest_wrap(){{{everything_else}\n}}"),
369-
&mut found_main_span,
370+
&mut has_main_fn,
370371
&mut found_extern_crate,
371372
&mut found_macro,
372373
&crate_name,
373374
supports_color,
374375
);
375376
}
376377

377-
(found_main_span, found_extern_crate, found_macro, parsing_result)
378+
(has_main_fn, found_extern_crate, found_macro, parsing_result)
378379
})
379380
});
380-
let (mut main_fn_span, already_has_extern_crate, found_macro, parsing_result) = match result {
381+
let (mut has_main_fn, already_has_extern_crate, found_macro, parsing_result) = match result {
381382
Err(..) | Ok((_, _, _, ParsingResult::Failed)) => return Err(FatalError),
382-
Ok((main_fn_span, already_has_extern_crate, found_macro, parsing_result)) => {
383-
(main_fn_span, already_has_extern_crate, found_macro, parsing_result)
383+
Ok((has_main_fn, already_has_extern_crate, found_macro, parsing_result)) => {
384+
(has_main_fn, already_has_extern_crate, found_macro, parsing_result)
384385
}
385386
};
386387

@@ -389,7 +390,7 @@ fn check_for_main_and_extern_crate(
389390
// function written inside a macro invocation. See
390391
// https://github.com/rust-lang/rust/issues/56898
391392
if found_macro
392-
&& main_fn_span.is_none()
393+
&& !has_main_fn
393394
&& original_source_code
394395
.lines()
395396
.map(|line| {
@@ -398,10 +399,10 @@ fn check_for_main_and_extern_crate(
398399
})
399400
.any(|code| code.contains("fn main"))
400401
{
401-
main_fn_span = Some(DUMMY_SP);
402+
has_main_fn = true;
402403
}
403404

404-
Ok((main_fn_span, already_has_extern_crate, parsing_result != ParsingResult::Ok))
405+
Ok((has_main_fn, already_has_extern_crate, parsing_result != ParsingResult::Ok))
405406
}
406407

407408
fn check_if_attr_is_complete(source: &str, edition: Edition) -> bool {
@@ -448,6 +449,7 @@ fn check_if_attr_is_complete(source: &str, edition: Edition) -> bool {
448449
.unwrap_or(false)
449450
}
450451

452+
/// Returns `(crate_attrs, content, crates)`.
451453
fn partition_source(s: &str, edition: Edition) -> (String, String, String) {
452454
#[derive(Copy, Clone, PartialEq)]
453455
enum PartitionState {
@@ -456,7 +458,7 @@ fn partition_source(s: &str, edition: Edition) -> (String, String, String) {
456458
Other,
457459
}
458460
let mut state = PartitionState::Attrs;
459-
let mut before = String::new();
461+
let mut crate_attrs = String::new();
460462
let mut crates = String::new();
461463
let mut after = String::new();
462464

@@ -520,8 +522,8 @@ fn partition_source(s: &str, edition: Edition) -> (String, String, String) {
520522

521523
match state {
522524
PartitionState::Attrs => {
523-
before.push_str(line);
524-
before.push('\n');
525+
crate_attrs.push_str(line);
526+
crate_attrs.push('\n');
525527
}
526528
PartitionState::Crates => {
527529
crates.push_str(line);

src/librustdoc/doctest/runner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ fn generate_mergeable_doctest(
149149
writeln!(output, "mod {test_id} {{\n").unwrap();
150150
} else {
151151
writeln!(output, "mod {test_id} {{\n{}", doctest.crates).unwrap();
152-
if doctest.main_fn_span.is_some() {
152+
if doctest.has_main_fn {
153153
output.push_str(&doctest.everything_else);
154154
} else {
155155
let returns_result = if doctest.everything_else.trim_end().ends_with("(())") {

0 commit comments

Comments
 (0)