Skip to content

Commit 7e403e8

Browse files
Correctly handle line comments in attributes
1 parent 1240a7d commit 7e403e8

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

Diff for: src/librustdoc/doctest/make.rs

+12-3
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.

Diff for: src/librustdoc/doctest/tests.rs

+25
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+
}

Diff for: tests/rustdoc/playground.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424
2525
//@ matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=%23!%5Ballow(unused)%5D%0Afn+main()+%7B%0A++++println!(%22Hello,+world!%22);%0A%7D&edition=2015"]' ""
2626
//@ matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=%23!%5Ballow(unused)%5D%0Afn+main()+%7B%0A++++println!(%22Hello,+world!%22);%0A%7D&edition=2015"]' ""
27-
//@ matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=%23!%5Ballow(unused)%5D%0A%23!%5Bfeature(something)%5D%0A%0Afn+main()+%7B%0A++++println!(%22Hello,+world!%22);%0A%7D&version=nightly&edition=2015"]' ""
27+
//@ matches foo/index.html '//a[@class="test-arrow"][@href="https://www.example.com/?code=%23!%5Ballow(unused)%5D%0A%23!%5Bfeature(something)%5D%0A%0A%0A%0Afn+main()+%7B%0A++++println!(%22Hello,+world!%22);%0A%7D&version=nightly&edition=2015"]' ""

0 commit comments

Comments
 (0)