Skip to content

Commit 572e201

Browse files
committed
Auto merge of #9128 - poliorcetics:respect-shortness-rustdoc, r=ehuss
Pass the error message format to rustdoc - Goes with rust-lang/rust#81675. - Will help with rust-lang/rust#81662. This is my first PR to Cargo and I haven't finished reading the contributor guide yet, how should I add tests for this ? Did I had the code in the correct place ?
2 parents 0613244 + 83b353b commit 572e201

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/cargo/core/compiler/context/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
228228
let mut unstable_opts = false;
229229
let mut args = compiler::extern_args(&self, unit, &mut unstable_opts)?;
230230
args.extend(compiler::lto_args(&self, unit));
231+
231232
for feature in &unit.features {
232233
args.push("--cfg".into());
233234
args.push(format!("feature=\"{}\"", feature).into());
@@ -242,6 +243,16 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
242243
}
243244
}
244245
args.extend(self.bcx.rustdocflags_args(unit).iter().map(Into::into));
246+
247+
use super::MessageFormat;
248+
let format = match self.bcx.build_config.message_format {
249+
MessageFormat::Short => "short",
250+
MessageFormat::Human => "human",
251+
MessageFormat::Json { .. } => "json",
252+
};
253+
args.push("--error-format".into());
254+
args.push(format.into());
255+
245256
self.compilation.to_doc_test.push(compilation::Doctest {
246257
unit: unit.clone(),
247258
args,

tests/testsuite/message_format.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Tests for --message-format flag.
22
3-
use cargo_test_support::{basic_manifest, project};
3+
use cargo_test_support::{basic_lib_manifest, basic_manifest, is_nightly, project};
44

55
#[cargo_test]
66
fn cannot_specify_two() {
@@ -109,3 +109,30 @@ fn cargo_renders_ansi() {
109109
.with_stdout_contains("[..]\\u001b[38;5;9merror[..]")
110110
.run();
111111
}
112+
113+
#[cargo_test]
114+
fn cargo_renders_doctests() {
115+
if !is_nightly() {
116+
// --error-format=short support added in 1.51
117+
return;
118+
}
119+
120+
let p = project()
121+
.file("Cargo.toml", &basic_lib_manifest("foo"))
122+
.file(
123+
"src/lib.rs",
124+
"\
125+
/// ```rust
126+
/// bar()
127+
/// ```
128+
pub fn bar() {}
129+
",
130+
)
131+
.build();
132+
133+
p.cargo("test --doc --message-format short")
134+
.with_status(101)
135+
.with_stdout_contains("src/lib.rs:2:1: error[E0425]:[..]")
136+
.with_stdout_contains("[..]src/lib.rs - bar (line 1)[..]")
137+
.run();
138+
}

0 commit comments

Comments
 (0)