Skip to content

Commit a68ec22

Browse files
committed
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.
1 parent 7eef946 commit a68ec22

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)]
@@ -742,20 +743,41 @@ fn print_crate_info(
742743
}
743744

744745
/// Prints version information
745-
pub fn version(binary: &str, matches: &getopts::Matches) {
746+
///
747+
/// NOTE: this is a macro to support drivers built at a different time than the main `rustc_driver` crate.
748+
pub macro version($binary: literal, $matches: expr) {
749+
fn unw(x: Option<&str>) -> &str {
750+
x.unwrap_or("unknown")
751+
}
752+
$crate::version_at_macro_invocation(
753+
$binary,
754+
$matches,
755+
unw(option_env!("CFG_VERSION")),
756+
unw(option_env!("CFG_VER_HASH")),
757+
unw(option_env!("CFG_VER_DATE")),
758+
unw(option_env!("CFG_RELEASE")),
759+
)
760+
}
761+
762+
#[doc(hidden)] // use the macro instead
763+
pub fn version_at_macro_invocation(
764+
binary: &str,
765+
matches: &getopts::Matches,
766+
version: &str,
767+
commit_hash: &str,
768+
commit_date: &str,
769+
release: &str,
770+
) {
746771
let verbose = matches.opt_present("verbose");
747772

748-
println!("{} {}", binary, util::version_str().unwrap_or("unknown version"));
773+
println!("{} {}", binary, version);
749774

750775
if verbose {
751-
fn unw(x: Option<&str>) -> &str {
752-
x.unwrap_or("unknown")
753-
}
754776
println!("binary: {}", binary);
755-
println!("commit-hash: {}", unw(util::commit_hash_str()));
756-
println!("commit-date: {}", unw(util::commit_date_str()));
777+
println!("commit-hash: {}", commit_hash);
778+
println!("commit-date: {}", commit_date);
757779
println!("host: {}", config::host_triple());
758-
println!("release: {}", unw(util::release_str()));
780+
println!("release: {}", release);
759781

760782
let debug_flags = matches.opt_strs("Z");
761783
let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend="));
@@ -1071,7 +1093,7 @@ pub fn handle_options(args: &[String]) -> Option<getopts::Matches> {
10711093
}
10721094

10731095
if matches.opt_present("version") {
1074-
version("rustc", &matches);
1096+
version!("rustc", &matches);
10751097
return None;
10761098
}
10771099

@@ -1216,7 +1238,7 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
12161238
format!("we would appreciate a bug report: {}", bug_report_url).into(),
12171239
format!(
12181240
"rustc {} running on {}",
1219-
util::version_str().unwrap_or("unknown_version"),
1241+
util::version_str!().unwrap_or("unknown_version"),
12201242
config::host_triple()
12211243
)
12221244
.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)