Skip to content

Commit 70c04a2

Browse files
authored
Rollup merge of rust-lang#104184 - jyn514:rustdoc-version, r=davidtwco
Fix `rustdoc --version` when used with download-rustc Previously, rustdoc would unconditionally report the version that *rustc* was compiled with. That showed things like `nightly-2022-10-30`, which wasn't right, since this was a `dev` build compiled from source. Fix it by changing `rustc_driver::version` to a macro expanded at invocation time. cc rust-lang#103206 (comment)
2 parents 656f56c + a68ec22 commit 70c04a2

File tree

6 files changed

+42
-29
lines changed

6 files changed

+42
-29
lines changed

compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl DebugContext {
5959

6060
let producer = format!(
6161
"cg_clif (rustc {}, cranelift {})",
62-
rustc_interface::util::version_str().unwrap_or("unknown version"),
62+
rustc_interface::util::rustc_version_str().unwrap_or("unknown version"),
6363
cranelift_codegen::VERSION,
6464
);
6565
let comp_dir = tcx

compiler/rustc_driver/src/lib.rs

+32-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
88
#![feature(once_cell)]
9+
#![feature(decl_macro)]
910
#![recursion_limit = "256"]
1011
#![allow(rustc::potential_query_instability)]
1112
#![deny(rustc::untranslatable_diagnostic)]
@@ -753,20 +754,41 @@ fn print_crate_info(
753754
}
754755

755756
/// Prints version information
756-
pub fn version(binary: &str, matches: &getopts::Matches) {
757+
///
758+
/// NOTE: this is a macro to support drivers built at a different time than the main `rustc_driver` crate.
759+
pub macro version($binary: literal, $matches: expr) {
760+
fn unw(x: Option<&str>) -> &str {
761+
x.unwrap_or("unknown")
762+
}
763+
$crate::version_at_macro_invocation(
764+
$binary,
765+
$matches,
766+
unw(option_env!("CFG_VERSION")),
767+
unw(option_env!("CFG_VER_HASH")),
768+
unw(option_env!("CFG_VER_DATE")),
769+
unw(option_env!("CFG_RELEASE")),
770+
)
771+
}
772+
773+
#[doc(hidden)] // use the macro instead
774+
pub fn version_at_macro_invocation(
775+
binary: &str,
776+
matches: &getopts::Matches,
777+
version: &str,
778+
commit_hash: &str,
779+
commit_date: &str,
780+
release: &str,
781+
) {
757782
let verbose = matches.opt_present("verbose");
758783

759-
println!("{} {}", binary, util::version_str().unwrap_or("unknown version"));
784+
println!("{} {}", binary, version);
760785

761786
if verbose {
762-
fn unw(x: Option<&str>) -> &str {
763-
x.unwrap_or("unknown")
764-
}
765787
println!("binary: {}", binary);
766-
println!("commit-hash: {}", unw(util::commit_hash_str()));
767-
println!("commit-date: {}", unw(util::commit_date_str()));
788+
println!("commit-hash: {}", commit_hash);
789+
println!("commit-date: {}", commit_date);
768790
println!("host: {}", config::host_triple());
769-
println!("release: {}", unw(util::release_str()));
791+
println!("release: {}", release);
770792

771793
let debug_flags = matches.opt_strs("Z");
772794
let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend="));
@@ -1082,7 +1104,7 @@ pub fn handle_options(args: &[String]) -> Option<getopts::Matches> {
10821104
}
10831105

10841106
if matches.opt_present("version") {
1085-
version("rustc", &matches);
1107+
version!("rustc", &matches);
10861108
return None;
10871109
}
10881110

@@ -1227,7 +1249,7 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
12271249
format!("we would appreciate a bug report: {}", bug_report_url).into(),
12281250
format!(
12291251
"rustc {} running on {}",
1230-
util::version_str().unwrap_or("unknown_version"),
1252+
util::version_str!().unwrap_or("unknown_version"),
12311253
config::host_triple()
12321254
)
12331255
.into(),

compiler/rustc_interface/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(box_patterns)]
2+
#![feature(decl_macro)]
23
#![feature(internal_output_capture)]
34
#![feature(thread_spawn_unchecked)]
45
#![feature(once_cell)]

compiler/rustc_interface/src/util.rs

+6-16
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ fn get_codegen_sysroot(maybe_sysroot: &Option<PathBuf>, backend_name: &str) -> M
327327
let mut file: Option<PathBuf> = None;
328328

329329
let expected_names = &[
330-
format!("rustc_codegen_{}-{}", backend_name, release_str().expect("CFG_RELEASE")),
330+
format!("rustc_codegen_{}-{}", backend_name, env!("CFG_RELEASE")),
331331
format!("rustc_codegen_{}", backend_name),
332332
];
333333
for entry in d.filter_map(|e| e.ok()) {
@@ -554,22 +554,12 @@ pub fn build_output_filenames(
554554
}
555555
}
556556

557-
/// Returns a version string such as "1.46.0 (04488afe3 2020-08-24)"
558-
pub fn version_str() -> Option<&'static str> {
557+
/// Returns a version string such as "1.46.0 (04488afe3 2020-08-24)" when invoked by an in-tree tool.
558+
pub macro version_str() {
559559
option_env!("CFG_VERSION")
560560
}
561561

562-
/// Returns a version string such as "0.12.0-dev".
563-
pub fn release_str() -> Option<&'static str> {
564-
option_env!("CFG_RELEASE")
565-
}
566-
567-
/// Returns the full SHA1 hash of HEAD of the Git repo from which rustc was built.
568-
pub fn commit_hash_str() -> Option<&'static str> {
569-
option_env!("CFG_VER_HASH")
570-
}
571-
572-
/// Returns the "commit date" of HEAD of the Git repo from which rustc was built as a static string.
573-
pub fn commit_date_str() -> Option<&'static str> {
574-
option_env!("CFG_VER_DATE")
562+
/// Returns the version string for `rustc` itself (which may be different from a tool version).
563+
pub fn rustc_version_str() -> Option<&'static str> {
564+
version_str!()
575565
}

src/librustdoc/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ impl Options {
326326
crate::usage("rustdoc");
327327
return Err(0);
328328
} else if matches.opt_present("version") {
329-
rustc_driver::version("rustdoc", matches);
329+
rustc_driver::version!("rustdoc", matches);
330330
return Err(0);
331331
}
332332

src/librustdoc/html/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub(crate) fn render<T: Print, S: Print>(
7171
let mut themes: Vec<String> = style_files.iter().map(|s| s.basename().unwrap()).collect();
7272
themes.sort();
7373

74-
let rustdoc_version = rustc_interface::util::version_str().unwrap_or("unknown version");
74+
let rustdoc_version = rustc_interface::util::version_str!().unwrap_or("unknown version");
7575
let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar.
7676
let sidebar = Buffer::html().to_display(sidebar);
7777
PageLayout {

0 commit comments

Comments
 (0)