Skip to content

Commit 32a6a95

Browse files
authored
Rollup merge of rust-lang#56793 - QuietMisdreavus:better-doctests, r=GuillaumeGomez
rustdoc: look for comments when scraping attributes/crates from doctests Fixes rust-lang#56727 When scraping out crate-level attributes and `extern crate` statements, we wouldn't look for comments, so any presence of comments would shunt it and everything after it into "everything else". This could cause parsing issues when looking for `fn main` and `extern crate my_crate` later on, which would in turn cause rustdoc to incorrectly wrap a test with `fn main` when it already had one declared. I took the opportunity to clean up the logic a little bit, but it would still benefit from a libsyntax-based loop like the `fn main` detection.
2 parents d91032a + 8faaef6 commit 32a6a95

File tree

2 files changed

+88
-16
lines changed

2 files changed

+88
-16
lines changed

src/librustdoc/test.rs

+58-16
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ pub fn make_test(s: &str,
395395
// Now push any outer attributes from the example, assuming they
396396
// are intended to be crate attributes.
397397
prog.push_str(&crate_attrs);
398+
prog.push_str(&crates);
398399

399400
// Uses libsyntax to parse the doctest and find if there's a main fn and the extern
400401
// crate already is included.
@@ -488,37 +489,78 @@ pub fn make_test(s: &str,
488489
prog.push_str("\n}");
489490
}
490491

492+
debug!("final doctest:\n{}", prog);
493+
491494
(prog, line_offset)
492495
}
493496

494497
// FIXME(aburka): use a real parser to deal with multiline attributes
495498
fn partition_source(s: &str) -> (String, String, String) {
496-
let mut after_header = false;
499+
#[derive(Copy, Clone, PartialEq)]
500+
enum PartitionState {
501+
Attrs,
502+
Crates,
503+
Other,
504+
}
505+
let mut state = PartitionState::Attrs;
497506
let mut before = String::new();
498507
let mut crates = String::new();
499508
let mut after = String::new();
500509

501510
for line in s.lines() {
502511
let trimline = line.trim();
503-
let header = trimline.chars().all(|c| c.is_whitespace()) ||
504-
trimline.starts_with("#![") ||
505-
trimline.starts_with("#[macro_use] extern crate") ||
506-
trimline.starts_with("extern crate");
507-
if !header || after_header {
508-
after_header = true;
509-
after.push_str(line);
510-
after.push_str("\n");
511-
} else {
512-
if trimline.starts_with("#[macro_use] extern crate")
513-
|| trimline.starts_with("extern crate") {
512+
513+
// FIXME(misdreavus): if a doc comment is placed on an extern crate statement, it will be
514+
// shunted into "everything else"
515+
match state {
516+
PartitionState::Attrs => {
517+
state = if trimline.starts_with("#![") ||
518+
trimline.chars().all(|c| c.is_whitespace()) ||
519+
(trimline.starts_with("//") && !trimline.starts_with("///"))
520+
{
521+
PartitionState::Attrs
522+
} else if trimline.starts_with("extern crate") ||
523+
trimline.starts_with("#[macro_use] extern crate")
524+
{
525+
PartitionState::Crates
526+
} else {
527+
PartitionState::Other
528+
};
529+
}
530+
PartitionState::Crates => {
531+
state = if trimline.starts_with("extern crate") ||
532+
trimline.starts_with("#[macro_use] extern crate") ||
533+
trimline.chars().all(|c| c.is_whitespace()) ||
534+
(trimline.starts_with("//") && !trimline.starts_with("///"))
535+
{
536+
PartitionState::Crates
537+
} else {
538+
PartitionState::Other
539+
};
540+
}
541+
PartitionState::Other => {}
542+
}
543+
544+
match state {
545+
PartitionState::Attrs => {
546+
before.push_str(line);
547+
before.push_str("\n");
548+
}
549+
PartitionState::Crates => {
514550
crates.push_str(line);
515551
crates.push_str("\n");
516552
}
517-
before.push_str(line);
518-
before.push_str("\n");
553+
PartitionState::Other => {
554+
after.push_str(line);
555+
after.push_str("\n");
556+
}
519557
}
520558
}
521559

560+
debug!("before:\n{}", before);
561+
debug!("crates:\n{}", crates);
562+
debug!("after:\n{}", after);
563+
522564
(before, after, crates)
523565
}
524566

@@ -1035,8 +1077,8 @@ fn main() {
10351077
assert_eq!(2+2, 4);";
10361078
let expected =
10371079
"#![allow(unused)]
1038-
fn main() {
10391080
//Ceci n'est pas une `fn main`
1081+
fn main() {
10401082
assert_eq!(2+2, 4);
10411083
}".to_string();
10421084
let output = make_test(input, None, false, &opts);
@@ -1083,8 +1125,8 @@ assert_eq!(2+2, 4);";
10831125

10841126
let expected =
10851127
"#![allow(unused)]
1086-
fn main() {
10871128
// fn main
1129+
fn main() {
10881130
assert_eq!(2+2, 4);
10891131
}".to_string();
10901132

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags:--test
12+
13+
// comments, both doc comments and regular ones, used to trick rustdoc's doctest parser into
14+
// thinking that everything after it was part of the regular program. combined with the libsyntax
15+
// parser loop failing to detect the manual main function, it would wrap everything in `fn main`,
16+
// which would cause the doctest to fail as the "extern crate" declaration was no longer valid.
17+
// oddly enough, it would pass in 2018 if a crate was in the extern prelude. see
18+
// https://github.com/rust-lang/rust/issues/56727
19+
20+
//! ```
21+
//! // crate: proc-macro-test
22+
//! //! this is a test
23+
//!
24+
//! // used to pull in proc-macro specific items
25+
//! extern crate proc_macro;
26+
//!
27+
//! use proc_macro::TokenStream;
28+
//!
29+
//! # fn main() {}
30+
//! ```

0 commit comments

Comments
 (0)