Skip to content

Commit

Permalink
Auto merge of #73314 - GuillaumeGomez:display-warnings, r=jyn514
Browse files Browse the repository at this point in the history
Rename "--display-warnings" to "--display-doctest-warnings"

Fixes #41574.

cc `@ollie27`
r? `@kinnison`
  • Loading branch information
bors committed Sep 14, 2021
2 parents ec9a1bd + b531a7f commit c3c0f80
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/doc/rustdoc/src/unstable-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,13 @@ all these files are linked from every page, changing where they are can be cumbe
specially cache them. This flag will rename all these files in the output to include the suffix in
the filename. For example, `light.css` would become `light-suf.css` with the above command.

### `--display-warnings`: display warnings when documenting or running documentation tests
### `--display-doctest-warnings`: display warnings when documenting or running documentation tests

Using this flag looks like this:

```bash
$ rustdoc src/lib.rs -Z unstable-options --display-warnings
$ rustdoc --test src/lib.rs -Z unstable-options --display-warnings
$ rustdoc src/lib.rs -Z unstable-options --display-doctest-warnings
$ rustdoc --test src/lib.rs -Z unstable-options --display-doctest-warnings
```

The intent behind this flag is to allow the user to see warnings that occur within their library or
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ crate struct Options {
crate manual_passes: Vec<String>,
/// Whether to display warnings during doc generation or while gathering doctests. By default,
/// all non-rustdoc-specific lints are allowed when generating docs.
crate display_warnings: bool,
crate display_doctest_warnings: bool,
/// Whether to run the `calculate-doc-coverage` pass, which counts the number of public items
/// with and without documentation.
crate show_coverage: bool,
Expand Down Expand Up @@ -192,7 +192,7 @@ impl fmt::Debug for Options {
.field("persist_doctests", &self.persist_doctests)
.field("default_passes", &self.default_passes)
.field("manual_passes", &self.manual_passes)
.field("display_warnings", &self.display_warnings)
.field("display_doctest_warnings", &self.display_doctest_warnings)
.field("show_coverage", &self.show_coverage)
.field("crate_version", &self.crate_version)
.field("render_options", &self.render_options)
Expand Down Expand Up @@ -632,7 +632,7 @@ impl Options {
let proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
let playground_url = matches.opt_str("playground-url");
let maybe_sysroot = matches.opt_str("sysroot").map(PathBuf::from);
let display_warnings = matches.opt_present("display-warnings");
let display_doctest_warnings = matches.opt_present("display-doctest-warnings");
let sort_modules_alphabetically = !matches.opt_present("sort-modules-by-appearance");
let resource_suffix = matches.opt_str("resource-suffix").unwrap_or_default();
let enable_minification = !matches.opt_present("disable-minification");
Expand Down Expand Up @@ -696,7 +696,7 @@ impl Options {
test_args,
default_passes,
manual_passes,
display_warnings,
display_doctest_warnings,
show_coverage,
crate_version,
test_run_directory,
Expand Down
3 changes: 1 addition & 2 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ crate fn create_config(
lint_opts,
describe_lints,
lint_cap,
display_warnings,
..
}: RustdocOptions,
) -> rustc_interface::Config {
Expand Down Expand Up @@ -237,7 +236,7 @@ crate fn create_config(
maybe_sysroot,
search_paths: libs,
crate_types,
lint_opts: if !display_warnings { lint_opts } else { vec![] },
lint_opts,
lint_cap,
cg: codegen_options,
externs,
Expand Down
22 changes: 13 additions & 9 deletions src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ crate struct TestOptions {
crate no_crate_inject: bool,
/// Whether to emit compilation warnings when compiling doctests. Setting this will suppress
/// the default `#![allow(unused)]`.
crate display_warnings: bool,
crate display_doctest_warnings: bool,
/// Additional crate-level attributes to add to doctests.
crate attrs: Vec<String>,
}
Expand Down Expand Up @@ -72,7 +72,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
maybe_sysroot: options.maybe_sysroot.clone(),
search_paths: options.libs.clone(),
crate_types,
lint_opts: if !options.display_warnings { lint_opts } else { vec![] },
lint_opts: if !options.display_doctest_warnings { lint_opts } else { vec![] },
lint_cap: Some(options.lint_cap.unwrap_or_else(|| lint::Forbid)),
cg: options.codegen_options.clone(),
externs: options.externs.clone(),
Expand Down Expand Up @@ -106,7 +106,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
};

let test_args = options.test_args.clone();
let display_warnings = options.display_warnings;
let display_doctest_warnings = options.display_doctest_warnings;
let nocapture = options.nocapture;
let externs = options.externs.clone();
let json_unused_externs = options.json_unused_externs;
Expand All @@ -119,7 +119,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
let crate_attrs = tcx.hir().attrs(CRATE_HIR_ID);

let mut opts = scrape_test_config(crate_attrs);
opts.display_warnings |= options.display_warnings;
opts.display_doctest_warnings |= options.display_doctest_warnings;
let enable_per_target_ignores = options.enable_per_target_ignores;
let mut collector = Collector::new(
tcx.crate_name(LOCAL_CRATE),
Expand Down Expand Up @@ -163,7 +163,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
Err(ErrorReported) => return Err(ErrorReported),
};

run_tests(test_args, nocapture, display_warnings, tests);
run_tests(test_args, nocapture, display_doctest_warnings, tests);

// Collect and warn about unused externs, but only if we've gotten
// reports for each doctest
Expand Down Expand Up @@ -209,22 +209,26 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
crate fn run_tests(
mut test_args: Vec<String>,
nocapture: bool,
display_warnings: bool,
display_doctest_warnings: bool,
tests: Vec<test::TestDescAndFn>,
) {
test_args.insert(0, "rustdoctest".to_string());
if nocapture {
test_args.push("--nocapture".to_string());
}
test::test_main(&test_args, tests, Some(test::Options::new().display_output(display_warnings)));
test::test_main(
&test_args,
tests,
Some(test::Options::new().display_output(display_doctest_warnings)),
);
}

// Look for `#![doc(test(no_crate_inject))]`, used by crates in the std facade.
fn scrape_test_config(attrs: &[ast::Attribute]) -> TestOptions {
use rustc_ast_pretty::pprust;

let mut opts =
TestOptions { no_crate_inject: false, display_warnings: false, attrs: Vec::new() };
TestOptions { no_crate_inject: false, display_doctest_warnings: false, attrs: Vec::new() };

let test_attrs: Vec<_> = attrs
.iter()
Expand Down Expand Up @@ -504,7 +508,7 @@ crate fn make_test(
let mut prog = String::new();
let mut supports_color = false;

if opts.attrs.is_empty() && !opts.display_warnings {
if opts.attrs.is_empty() && !opts.display_doctest_warnings {
// If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
// lints that are commonly triggered in doctests. The crate-level test attributes are
// commonly used to make tests fail in case they trigger warnings, so having this there in
Expand Down
7 changes: 4 additions & 3 deletions src/librustdoc/doctest/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ assert_eq!(2+2, 4);
fn make_test_no_crate_inject() {
// Even if you do use the crate within the test, setting `opts.no_crate_inject` will skip
// adding it anyway.
let opts = TestOptions { no_crate_inject: true, display_warnings: false, attrs: vec![] };
let opts =
TestOptions { no_crate_inject: true, display_doctest_warnings: false, attrs: vec![] };
let input = "use asdf::qwop;
assert_eq!(2+2, 4);";
let expected = "#![allow(unused)]
Expand Down Expand Up @@ -215,10 +216,10 @@ assert_eq!(2+2, 4);"
}

#[test]
fn make_test_display_warnings() {
fn make_test_display_doctest_warnings() {
// If the user is asking to display doctest warnings, suppress the default `allow(unused)`.
let mut opts = TestOptions::default();
opts.display_warnings = true;
opts.display_doctest_warnings = true;
let input = "assert_eq!(2+2, 4);";
let expected = "fn main() {
assert_eq!(2+2, 4);
Expand Down
8 changes: 6 additions & 2 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,12 @@ fn opts() -> Vec<RustcOptGroup> {
"URL",
)
}),
unstable("display-warnings", |o| {
o.optflagmulti("", "display-warnings", "to print code warnings when testing doc")
unstable("display-doctest-warnings", |o| {
o.optflagmulti(
"",
"display-doctest-warnings",
"show warnings that originate in doctests",
)
}),
stable("crate-version", |o| {
o.optopt("", "crate-version", "crate version to print into documentation", "VERSION")
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ crate fn test(options: Options) -> Result<(), String> {
.map_err(|err| format!("{}: {}", options.input.display(), err))?;
let mut opts = TestOptions::default();
opts.no_crate_inject = true;
opts.display_warnings = options.display_warnings;
opts.display_doctest_warnings = options.display_doctest_warnings;
let mut collector = Collector::new(
Symbol::intern(&options.input.display().to_string()),
options.clone(),
Expand All @@ -138,7 +138,7 @@ crate fn test(options: Options) -> Result<(), String> {
crate::doctest::run_tests(
options.test_args,
options.nocapture,
options.display_warnings,
options.display_doctest_warnings,
collector.tests,
);
Ok(())
Expand Down
9 changes: 9 additions & 0 deletions src/test/rustdoc-ui/display-output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// check-pass
// compile-flags:-Zunstable-options --display-doctest-warnings --test
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
// normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"

/// ```
/// let x = 12;
/// ```
pub fn foo() {}
24 changes: 24 additions & 0 deletions src/test/rustdoc-ui/display-output.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

running 1 test
test $DIR/display-output.rs - foo (line 6) ... ok

successes:

---- $DIR/display-output.rs - foo (line 6) stdout ----
warning: unused variable: `x`
--> $DIR/display-output.rs:7:5
|
LL | let x = 12;
| ^ help: if this is intentional, prefix it with an underscore: `_x`
|
= note: `#[warn(unused_variables)]` on by default

warning: 1 warning emitted



successes:
$DIR/display-output.rs - foo (line 6)

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME

0 comments on commit c3c0f80

Please sign in to comment.