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

Commit be13105

Browse files
Improve comment and test for generated doctest with code comments
1 parent 123ea25 commit be13105

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/librustdoc/doctest/make.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ impl DocTestBuilder {
8585
maybe_crate_attrs,
8686
})) = result
8787
else {
88+
// If the AST returned an error, we don't want this doctest to be merged with the
89+
// others.
8890
return Self::invalid(
8991
String::new(),
9092
String::new(),
@@ -98,8 +100,7 @@ impl DocTestBuilder {
98100
debug!("crates:\n{crates}");
99101
debug!("after:\n{everything_else}");
100102

101-
// If the AST returned an error, we don't want this doctest to be merged with the
102-
// others. Same if it contains `#[feature]` or `#[no_std]`.
103+
// If it contains `#[feature]` or `#[no_std]`, we don't want it to be merged either.
103104
let can_be_merged = can_merge_doctests
104105
&& !has_global_allocator
105106
&& crate_attrs.is_empty()

src/librustdoc/doctest/tests.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,9 @@ fn check_split_args() {
405405

406406
#[test]
407407
fn comment_in_attrs() {
408-
// if input already has a fn main, it should insert a space before it
408+
// If there is an inline code comment after attributes, we need to ensure that
409+
// a backline will be added to prevent generating code "inside" it (and thus generating)
410+
// invalid code.
409411
let opts = default_global_opts("");
410412
let input = "\
411413
#![feature(rustdoc_internals)]
@@ -424,4 +426,24 @@ fn main() {
424426
.to_string();
425427
let (output, len) = make_test(input, None, false, &opts, None);
426428
assert_eq!((output, len), (expected, 2));
429+
430+
// And same, if there is a `main` function provided by the user, we ensure that it's
431+
// correctly separated.
432+
let input = "\
433+
#![feature(rustdoc_internals)]
434+
#![allow(internal_features)]
435+
#![doc(rust_logo)]
436+
//! This crate has the Rust(tm) branding on it.
437+
fn main() {}";
438+
let expected = "\
439+
#![allow(unused)]
440+
#![feature(rustdoc_internals)]
441+
#![allow(internal_features)]
442+
#![doc(rust_logo)]
443+
//! This crate has the Rust(tm) branding on it.
444+
445+
fn main() {}"
446+
.to_string();
447+
let (output, len) = make_test(input, None, false, &opts, None);
448+
assert_eq!((output, len), (expected, 1));
427449
}

0 commit comments

Comments
 (0)