From 5644bf75b2d159060f0d9ebddbf037f64c5485de Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Wed, 3 Feb 2021 19:06:40 +0100 Subject: [PATCH 1/2] Pass the error message format to rustdoc Goes with rust-lang/rust#81675. Will help with rust-lang/rust#81662. --- src/cargo/core/compiler/context/mod.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/cargo/core/compiler/context/mod.rs b/src/cargo/core/compiler/context/mod.rs index c1ed092f966..1429485326a 100644 --- a/src/cargo/core/compiler/context/mod.rs +++ b/src/cargo/core/compiler/context/mod.rs @@ -214,6 +214,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> { let mut unstable_opts = false; let mut args = compiler::extern_args(&self, unit, &mut unstable_opts)?; args.extend(compiler::lto_args(&self, unit)); + for feature in &unit.features { args.push("--cfg".into()); args.push(format!("feature=\"{}\"", feature).into()); @@ -228,6 +229,16 @@ impl<'a, 'cfg> Context<'a, 'cfg> { } } args.extend(self.bcx.rustdocflags_args(unit).iter().map(Into::into)); + + use super::MessageFormat; + let format = match self.bcx.build_config.message_format { + MessageFormat::Short => "short", + MessageFormat::Human => "human", + MessageFormat::Json { .. } => "json", + }; + args.push("--error-format".into()); + args.push(format.into()); + self.compilation.to_doc_test.push(compilation::Doctest { unit: unit.clone(), args, From 83b353bc9ba6b95cc08ed5a2bbb7fa6aa5db2a5b Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Sun, 14 Feb 2021 18:01:50 +0100 Subject: [PATCH 2/2] Test the passing of --error-format to rustdoc --- tests/testsuite/message_format.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/testsuite/message_format.rs b/tests/testsuite/message_format.rs index a62be3d967f..de8e69dd0b9 100644 --- a/tests/testsuite/message_format.rs +++ b/tests/testsuite/message_format.rs @@ -1,6 +1,6 @@ //! Tests for --message-format flag. -use cargo_test_support::{basic_manifest, project}; +use cargo_test_support::{basic_lib_manifest, basic_manifest, is_nightly, project}; #[cargo_test] fn cannot_specify_two() { @@ -109,3 +109,30 @@ fn cargo_renders_ansi() { .with_stdout_contains("[..]\\u001b[38;5;9merror[..]") .run(); } + +#[cargo_test] +fn cargo_renders_doctests() { + if !is_nightly() { + // --error-format=short support added in 1.51 + return; + } + + let p = project() + .file("Cargo.toml", &basic_lib_manifest("foo")) + .file( + "src/lib.rs", + "\ + /// ```rust + /// bar() + /// ``` + pub fn bar() {} + ", + ) + .build(); + + p.cargo("test --doc --message-format short") + .with_status(101) + .with_stdout_contains("src/lib.rs:2:1: error[E0425]:[..]") + .with_stdout_contains("[..]src/lib.rs - bar (line 1)[..]") + .run(); +}