Skip to content

Commit b397b6b

Browse files
authored
Rollup merge of #147762 - weihanglo:rustdoc-depinfo-stdout, r=fmease
feat(rustdoc): `--emit=depinfo` output to stdout via `-` rustdoc's `--emit=depinfo` flag now supports using `-` to write the output to stdout, aligning with rustc's behavior. This will fix <#147649>. ### How to review * The first commit demonstrates that `rustdoc --emit=depinfo=-` hasn't yet supported emitting to stdout. * The second implements it and the diff shows how the behavior changes.
2 parents e93ec9a + e7130d3 commit b397b6b

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

compiler/rustc_session/src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ impl Input {
11111111
}
11121112
}
11131113

1114-
#[derive(Clone, Hash, Debug, HashStable_Generic, PartialEq, Encodable, Decodable)]
1114+
#[derive(Clone, Hash, Debug, HashStable_Generic, PartialEq, Eq, Encodable, Decodable)]
11151115
pub enum OutFileName {
11161116
Real(PathBuf),
11171117
Stdout,

src/librustdoc/config.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use rustc_data_structures::fx::FxIndexMap;
99
use rustc_errors::DiagCtxtHandle;
1010
use rustc_session::config::{
1111
self, CodegenOptions, CrateType, ErrorOutputType, Externs, Input, JsonUnusedExterns,
12-
OptionsTargetModifiers, Sysroot, UnstableOptions, get_cmd_lint_options, nightly_options,
13-
parse_crate_types_from_list, parse_externs, parse_target_triple,
12+
OptionsTargetModifiers, OutFileName, Sysroot, UnstableOptions, get_cmd_lint_options,
13+
nightly_options, parse_crate_types_from_list, parse_externs, parse_target_triple,
1414
};
1515
use rustc_session::lint::Level;
1616
use rustc_session::search_paths::SearchPath;
@@ -314,7 +314,7 @@ pub(crate) enum EmitType {
314314
Unversioned,
315315
Toolchain,
316316
InvocationSpecific,
317-
DepInfo(Option<PathBuf>),
317+
DepInfo(Option<OutFileName>),
318318
}
319319

320320
impl FromStr for EmitType {
@@ -326,13 +326,11 @@ impl FromStr for EmitType {
326326
"toolchain-shared-resources" => Ok(Self::Toolchain),
327327
"invocation-specific" => Ok(Self::InvocationSpecific),
328328
"dep-info" => Ok(Self::DepInfo(None)),
329-
option => {
330-
if let Some(file) = option.strip_prefix("dep-info=") {
331-
Ok(Self::DepInfo(Some(Path::new(file).into())))
332-
} else {
333-
Err(())
334-
}
335-
}
329+
option => match option.strip_prefix("dep-info=") {
330+
Some("-") => Ok(Self::DepInfo(Some(OutFileName::Stdout))),
331+
Some(f) => Ok(Self::DepInfo(Some(OutFileName::Real(f.into())))),
332+
None => Err(()),
333+
},
336334
}
337335
}
338336
}
@@ -342,10 +340,10 @@ impl RenderOptions {
342340
self.emit.is_empty() || self.emit.contains(&EmitType::InvocationSpecific)
343341
}
344342

345-
pub(crate) fn dep_info(&self) -> Option<Option<&Path>> {
343+
pub(crate) fn dep_info(&self) -> Option<Option<&OutFileName>> {
346344
for emit in &self.emit {
347345
if let EmitType::DepInfo(file) = emit {
348-
return Some(file.as_deref());
346+
return Some(file.as_ref());
349347
}
350348
}
351349
None

src/librustdoc/core.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_lint::{MissingDoc, late_lint_mod};
1919
use rustc_middle::hir::nested_filter;
2020
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt};
2121
use rustc_session::config::{
22-
self, CrateType, ErrorOutputType, Input, OutFileName, OutputType, OutputTypes, ResolveDocLinks,
22+
self, CrateType, ErrorOutputType, Input, OutputType, OutputTypes, ResolveDocLinks,
2323
};
2424
pub(crate) use rustc_session::config::{Options, UnstableOptions};
2525
use rustc_session::{Session, lint};
@@ -271,10 +271,7 @@ pub(crate) fn create_config(
271271
test,
272272
remap_path_prefix,
273273
output_types: if let Some(file) = render_options.dep_info() {
274-
OutputTypes::new(&[(
275-
OutputType::DepInfo,
276-
file.map(|f| OutFileName::Real(f.to_path_buf())),
277-
)])
274+
OutputTypes::new(&[(OutputType::DepInfo, file.cloned())])
278275
} else {
279276
OutputTypes::new(&[])
280277
},

tests/run-make/rustdoc-dep-info/rmake.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,15 @@ fn main() {
4545
assert!(!path("precedence1.d").exists());
4646
assert!(!path("precedence2.d").exists());
4747
assert!(path("precedence3.d").exists());
48+
49+
// stdout (-) also wins if being the last.
50+
let result = rustdoc()
51+
.input("lib.rs")
52+
.arg("-Zunstable-options")
53+
.emit("dep-info=precedence1.d")
54+
.emit("dep-info=-")
55+
.run();
56+
assert!(!path("precedence1.d").exists());
57+
assert!(!path("-").exists()); // `-` shouldn't be treated as a file path
58+
assert!(!result.stdout().is_empty()); // Something emitted to stdout
4859
}

0 commit comments

Comments
 (0)