Skip to content

Commit 543cc23

Browse files
Correctly handle line comments in attributes
1 parent bbd161f commit 543cc23

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/librustdoc/doctest/make.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,18 @@ impl DocTestBuilder {
176176

177177
// Now push any outer attributes from the example, assuming they
178178
// are intended to be crate attributes.
179-
prog.push_str(&self.crate_attrs);
180-
prog.push_str(&self.maybe_crate_attrs);
181-
prog.push_str(&self.crates);
179+
if !self.crate_attrs.is_empty() {
180+
prog.push_str(&self.crate_attrs);
181+
prog.push('\n');
182+
}
183+
if !self.maybe_crate_attrs.is_empty() {
184+
prog.push_str(&self.maybe_crate_attrs);
185+
prog.push('\n');
186+
}
187+
if !self.crates.is_empty() {
188+
prog.push_str(&self.crates);
189+
prog.push('\n');
190+
}
182191

183192
// Don't inject `extern crate std` because it's already injected by the
184193
// compiler.

src/librustdoc/doctest/tests.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ fn make_test_crate_attrs() {
197197
assert_eq!(2+2, 4);";
198198
let expected = "#![allow(unused)]
199199
#![feature(sick_rad)]
200+
201+
200202
fn main() {
201203
assert_eq!(2+2, 4);
202204
}"
@@ -401,3 +403,26 @@ fn check_split_args() {
401403
compare("a\n\t \rb", &["a", "b"]);
402404
compare("a\n\t1 \rb", &["a", "1", "b"]);
403405
}
406+
407+
#[test]
408+
fn comment_in_attrs() {
409+
// if input already has a fn main, it should insert a space before it
410+
let opts = default_global_opts("");
411+
let input = "\
412+
#![feature(rustdoc_internals)]
413+
#![allow(internal_features)]
414+
#![doc(rust_logo)]
415+
//! This crate has the Rust(tm) branding on it.";
416+
let expected = "\
417+
#![allow(unused)]
418+
#![feature(rustdoc_internals)]
419+
#![allow(internal_features)]
420+
#![doc(rust_logo)]
421+
//! This crate has the Rust(tm) branding on it.
422+
fn main() {
423+
424+
}"
425+
.to_string();
426+
let (output, len) = make_test(input, None, false, &opts, None);
427+
assert_eq!((output, len), (expected, 2));
428+
}

0 commit comments

Comments
 (0)