Skip to content

Commit

Permalink
Rollup merge of rust-lang#104184 - jyn514:rustdoc-version, r=davidtwco
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
Manishearth authored Nov 9, 2022
2 parents ede323e + a68ec22 commit aed1890
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 29 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl DebugContext {

let producer = format!(
"cg_clif (rustc {}, cranelift {})",
rustc_interface::util::version_str().unwrap_or("unknown version"),
rustc_interface::util::rustc_version_str().unwrap_or("unknown version"),
cranelift_codegen::VERSION,
);
let comp_dir = tcx
Expand Down
42 changes: 32 additions & 10 deletions compiler/rustc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![feature(once_cell)]
#![feature(decl_macro)]
#![recursion_limit = "256"]
#![allow(rustc::potential_query_instability)]
#![deny(rustc::untranslatable_diagnostic)]
Expand Down Expand Up @@ -753,20 +754,41 @@ fn print_crate_info(
}

/// Prints version information
pub fn version(binary: &str, matches: &getopts::Matches) {
///
/// NOTE: this is a macro to support drivers built at a different time than the main `rustc_driver` crate.
pub macro version($binary: literal, $matches: expr) {
fn unw(x: Option<&str>) -> &str {
x.unwrap_or("unknown")
}
$crate::version_at_macro_invocation(
$binary,
$matches,
unw(option_env!("CFG_VERSION")),
unw(option_env!("CFG_VER_HASH")),
unw(option_env!("CFG_VER_DATE")),
unw(option_env!("CFG_RELEASE")),
)
}

#[doc(hidden)] // use the macro instead
pub fn version_at_macro_invocation(
binary: &str,
matches: &getopts::Matches,
version: &str,
commit_hash: &str,
commit_date: &str,
release: &str,
) {
let verbose = matches.opt_present("verbose");

println!("{} {}", binary, util::version_str().unwrap_or("unknown version"));
println!("{} {}", binary, version);

if verbose {
fn unw(x: Option<&str>) -> &str {
x.unwrap_or("unknown")
}
println!("binary: {}", binary);
println!("commit-hash: {}", unw(util::commit_hash_str()));
println!("commit-date: {}", unw(util::commit_date_str()));
println!("commit-hash: {}", commit_hash);
println!("commit-date: {}", commit_date);
println!("host: {}", config::host_triple());
println!("release: {}", unw(util::release_str()));
println!("release: {}", release);

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

if matches.opt_present("version") {
version("rustc", &matches);
version!("rustc", &matches);
return None;
}

Expand Down Expand Up @@ -1227,7 +1249,7 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str) {
format!("we would appreciate a bug report: {}", bug_report_url).into(),
format!(
"rustc {} running on {}",
util::version_str().unwrap_or("unknown_version"),
util::version_str!().unwrap_or("unknown_version"),
config::host_triple()
)
.into(),
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(box_patterns)]
#![feature(decl_macro)]
#![feature(internal_output_capture)]
#![feature(thread_spawn_unchecked)]
#![feature(once_cell)]
Expand Down
22 changes: 6 additions & 16 deletions compiler/rustc_interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ fn get_codegen_sysroot(maybe_sysroot: &Option<PathBuf>, backend_name: &str) -> M
let mut file: Option<PathBuf> = None;

let expected_names = &[
format!("rustc_codegen_{}-{}", backend_name, release_str().expect("CFG_RELEASE")),
format!("rustc_codegen_{}-{}", backend_name, env!("CFG_RELEASE")),
format!("rustc_codegen_{}", backend_name),
];
for entry in d.filter_map(|e| e.ok()) {
Expand Down Expand Up @@ -554,22 +554,12 @@ pub fn build_output_filenames(
}
}

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

/// Returns a version string such as "0.12.0-dev".
pub fn release_str() -> Option<&'static str> {
option_env!("CFG_RELEASE")
}

/// Returns the full SHA1 hash of HEAD of the Git repo from which rustc was built.
pub fn commit_hash_str() -> Option<&'static str> {
option_env!("CFG_VER_HASH")
}

/// Returns the "commit date" of HEAD of the Git repo from which rustc was built as a static string.
pub fn commit_date_str() -> Option<&'static str> {
option_env!("CFG_VER_DATE")
/// Returns the version string for `rustc` itself (which may be different from a tool version).
pub fn rustc_version_str() -> Option<&'static str> {
version_str!()
}
2 changes: 1 addition & 1 deletion src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl Options {
crate::usage("rustdoc");
return Err(0);
} else if matches.opt_present("version") {
rustc_driver::version("rustdoc", matches);
rustc_driver::version!("rustdoc", matches);
return Err(0);
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub(crate) fn render<T: Print, S: Print>(
let mut themes: Vec<String> = style_files.iter().map(|s| s.basename().unwrap()).collect();
themes.sort();

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

0 comments on commit aed1890

Please sign in to comment.