Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only depend on CFG_VERSION in rustc_interface #111345

Merged
merged 1 commit into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions compiler/rustc_attr/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ use crate::session_diagnostics::{self, IncorrectReprFormatGenericCause};
pub const VERSION_PLACEHOLDER: &str = "CURRENT_RUSTC_VERSION";

pub fn rust_version_symbol() -> Symbol {
let version = option_env!("CFG_VERSION").unwrap_or("<current>");
let version = version.split(' ').next().unwrap();
let version = option_env!("CFG_RELEASE").unwrap_or("<current>");
jyn514 marked this conversation as resolved.
Show resolved Hide resolved
Symbol::intern(&version)
}

Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,8 +806,7 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
name_in_debuginfo.push(codegen_unit_name);

debug!("build_compile_unit_di_node: {:?}", name_in_debuginfo);
let rustc_producer =
format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"),);
let rustc_producer = format!("rustc version {}", tcx.sess.cfg_version);
// FIXME(#41252) Remove "clang LLVM" if we can get GDB and LLVM to play nice.
let producer = format!("clang LLVM ({})", rustc_producer);

Expand Down
16 changes: 7 additions & 9 deletions compiler/rustc_codegen_ssa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_session::config::{CrateType, OutputFilenames, OutputType, RUST_CGU_EXT};
use rustc_session::cstore::{self, CrateSource};
use rustc_session::utils::NativeLibKind;
use rustc_session::Session;
use rustc_span::symbol::Symbol;
use rustc_span::DebuggerVisualizerFile;
use std::collections::BTreeSet;
Expand Down Expand Up @@ -175,11 +176,11 @@ pub struct CodegenResults {
pub crate_info: CrateInfo,
}

pub enum CodegenErrors<'a> {
pub enum CodegenErrors {
WrongFileType,
EmptyVersionNumber,
EncodingVersionMismatch { version_array: String, rlink_version: u32 },
RustcVersionMismatch { rustc_version: String, current_version: &'a str },
RustcVersionMismatch { rustc_version: String },
}

pub fn provide(providers: &mut Providers) {
Expand Down Expand Up @@ -213,10 +214,9 @@ pub fn looks_like_rust_object_file(filename: &str) -> bool {
const RLINK_VERSION: u32 = 1;
const RLINK_MAGIC: &[u8] = b"rustlink";

const RUSTC_VERSION: Option<&str> = option_env!("CFG_VERSION");

impl CodegenResults {
pub fn serialize_rlink(
sess: &Session,
rlink_file: &Path,
codegen_results: &CodegenResults,
) -> Result<usize, io::Error> {
Expand All @@ -225,12 +225,12 @@ impl CodegenResults {
// `emit_raw_bytes` is used to make sure that the version representation does not depend on
// Encoder's inner representation of `u32`.
encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes());
encoder.emit_str(RUSTC_VERSION.unwrap());
encoder.emit_str(sess.cfg_version);
Encodable::encode(codegen_results, &mut encoder);
encoder.finish()
}

pub fn deserialize_rlink<'a>(data: Vec<u8>) -> Result<Self, CodegenErrors<'a>> {
pub fn deserialize_rlink(sess: &Session, data: Vec<u8>) -> Result<Self, CodegenErrors> {
// The Decodable machinery is not used here because it panics if the input data is invalid
// and because its internal representation may change.
if !data.starts_with(RLINK_MAGIC) {
Expand All @@ -252,11 +252,9 @@ impl CodegenResults {

let mut decoder = MemDecoder::new(&data[4..], 0);
let rustc_version = decoder.read_str();
let current_version = RUSTC_VERSION.unwrap();
if rustc_version != current_version {
if rustc_version != sess.cfg_version {
return Err(CodegenErrors::RustcVersionMismatch {
rustc_version: rustc_version.to_string(),
current_version,
});
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Comp
let rlink_data = fs::read(file).unwrap_or_else(|err| {
sess.emit_fatal(RlinkUnableToRead { err });
});
let codegen_results = match CodegenResults::deserialize_rlink(rlink_data) {
let codegen_results = match CodegenResults::deserialize_rlink(sess, rlink_data) {
Ok(codegen) => codegen,
Err(err) => {
match err {
Expand All @@ -582,10 +582,10 @@ pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Comp
rlink_version,
})
}
CodegenErrors::RustcVersionMismatch { rustc_version, current_version } => {
CodegenErrors::RustcVersionMismatch { rustc_version } => {
sess.emit_fatal(RLinkRustcVersionMismatch {
rustc_version,
current_version,
current_version: sess.cfg_version,
})
}
};
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_hir/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ fn def_path_hash_depends_on_crate_id() {
// the crate-id of the defining crate. This is a desirable property
// because the crate-id can be more easily changed than the DefPath
// of an item, so, in the case of a crate-local DefPathHash collision,
// the user can simply "role the dice again" for all DefPathHashes in
// the user can simply "roll the dice again" for all DefPathHashes in
// the crate by changing the crate disambiguator (e.g. via bumping the
// crate's version number).

create_session_if_not_set_then(Edition::Edition2024, |_| {
let id0 = StableCrateId::new(Symbol::intern("foo"), false, vec!["1".to_string()]);
let id1 = StableCrateId::new(Symbol::intern("foo"), false, vec!["2".to_string()]);
let id0 = StableCrateId::new(Symbol::intern("foo"), false, vec!["1".to_string()], "");
let id1 = StableCrateId::new(Symbol::intern("foo"), false, vec!["2".to_string()], "");

let h0 = mk_test_hash(id0);
let h1 = mk_test_hash(id1);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// ICE this expression in particular (see #43162).
if let ExprKind::Path(QPath::Resolved(_, path)) = e.kind {
if path.segments.len() == 1 && path.segments[0].ident.name == sym::rust {
fatally_break_rust(self.tcx.sess);
fatally_break_rust(self.tcx);
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_hir_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ use rustc_middle::traits;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_session::config;
use rustc_session::Session;
use rustc_span::def_id::{DefId, LocalDefId};
use rustc_span::{sym, Span};

Expand Down Expand Up @@ -437,8 +436,8 @@ enum TupleArgumentsFlag {
TupleArguments,
}

fn fatally_break_rust(sess: &Session) {
let handler = sess.diagnostic();
fn fatally_break_rust(tcx: TyCtxt<'_>) {
let handler = tcx.sess.diagnostic();
handler.span_bug_no_panic(
MultiSpan::new(),
"It looks like you're trying to break rust; would you like some ICE?",
Expand All @@ -450,7 +449,7 @@ fn fatally_break_rust(sess: &Session) {
);
handler.note_without_error(format!(
"rustc {} running on {}",
option_env!("CFG_VERSION").unwrap_or("unknown_version"),
tcx.sess.cfg_version,
config::host_triple(),
));
}
Expand Down
35 changes: 15 additions & 20 deletions compiler/rustc_incremental/src/persist/file_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use rustc_data_structures::memmap::Mmap;
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
use rustc_serialize::Encoder;
use rustc_session::Session;
use std::borrow::Cow;
use std::env;
use std::fs;
use std::io::{self, Read};
Expand All @@ -25,17 +26,12 @@ const FILE_MAGIC: &[u8] = b"RSIC";
/// Change this if the header format changes.
const HEADER_FORMAT_VERSION: u16 = 0;

/// A version string that hopefully is always different for compiler versions
/// with different encodings of incremental compilation artifacts. Contains
/// the Git commit hash.
const RUSTC_VERSION: Option<&str> = option_env!("CFG_VERSION");

pub(crate) fn write_file_header(stream: &mut FileEncoder, nightly_build: bool) {
pub(crate) fn write_file_header(stream: &mut FileEncoder, sess: &Session) {
stream.emit_raw_bytes(FILE_MAGIC);
stream
.emit_raw_bytes(&[(HEADER_FORMAT_VERSION >> 0) as u8, (HEADER_FORMAT_VERSION >> 8) as u8]);

let rustc_version = rustc_version(nightly_build);
let rustc_version = rustc_version(sess.is_nightly_build(), sess.cfg_version);
assert_eq!(rustc_version.len(), (rustc_version.len() as u8) as usize);
stream.emit_raw_bytes(&[rustc_version.len() as u8]);
stream.emit_raw_bytes(rustc_version.as_bytes());
Expand Down Expand Up @@ -73,7 +69,7 @@ where
}
};

write_file_header(&mut encoder, sess.is_nightly_build());
write_file_header(&mut encoder, sess);

match encode(encoder) {
Ok(position) => {
Expand All @@ -100,9 +96,10 @@ where
/// - Returns `Err(..)` if some kind of IO error occurred while reading the
/// file.
pub fn read_file(
report_incremental_info: bool,
path: &Path,
nightly_build: bool,
report_incremental_info: bool,
is_nightly_build: bool,
cfg_version: &'static str,
) -> io::Result<Option<(Mmap, usize)>> {
let file = match fs::File::open(path) {
Ok(file) => file,
Expand Down Expand Up @@ -152,7 +149,7 @@ pub fn read_file(
let mut buffer = vec![0; rustc_version_str_len];
file.read_exact(&mut buffer)?;

if buffer != rustc_version(nightly_build).as_bytes() {
if buffer != rustc_version(is_nightly_build, cfg_version).as_bytes() {
report_format_mismatch(report_incremental_info, path, "Different compiler version");
return Ok(None);
}
Expand All @@ -174,17 +171,15 @@ fn report_format_mismatch(report_incremental_info: bool, file: &Path, message: &
}
}

fn rustc_version(nightly_build: bool) -> String {
/// A version string that hopefully is always different for compiler versions
/// with different encodings of incremental compilation artifacts. Contains
/// the Git commit hash.
fn rustc_version(nightly_build: bool, cfg_version: &'static str) -> Cow<'static, str> {
if nightly_build {
if let Some(val) = env::var_os("RUSTC_FORCE_RUSTC_VERSION") {
return val.to_string_lossy().into_owned();
if let Ok(val) = env::var("RUSTC_FORCE_RUSTC_VERSION") {
return val.into();
}
}

RUSTC_VERSION
.expect(
"Cannot use rustc without explicit version for \
incremental compilation",
)
.to_string()
cfg_version.into()
}
32 changes: 20 additions & 12 deletions compiler/rustc_incremental/src/persist/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,22 @@ impl<T: Default> LoadResult<T> {
}
}

fn load_data(
report_incremental_info: bool,
fn load_data(path: &Path, sess: &Session) -> LoadResult<(Mmap, usize)> {
load_data_no_sess(
path,
sess.opts.unstable_opts.incremental_info,
sess.is_nightly_build(),
sess.cfg_version,
)
}

fn load_data_no_sess(
path: &Path,
nightly_build: bool,
report_incremental_info: bool,
is_nightly_build: bool,
cfg_version: &'static str,
) -> LoadResult<(Mmap, usize)> {
match file_format::read_file(report_incremental_info, path, nightly_build) {
match file_format::read_file(path, report_incremental_info, is_nightly_build, cfg_version) {
Ok(Some(data_and_pos)) => LoadResult::Ok { data: data_and_pos },
Ok(None) => {
// The file either didn't exist or was produced by an incompatible
Expand Down Expand Up @@ -138,14 +148,13 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {
let expected_hash = sess.opts.dep_tracking_hash(false);

let mut prev_work_products = FxHashMap::default();
let nightly_build = sess.is_nightly_build();

// If we are only building with -Zquery-dep-graph but without an actual
// incr. comp. session directory, we skip this. Otherwise we'd fail
// when trying to load work products.
if sess.incr_comp_session_dir_opt().is_some() {
let work_products_path = work_products_path(sess);
let load_result = load_data(report_incremental_info, &work_products_path, nightly_build);
let load_result = load_data(&work_products_path, sess);

if let LoadResult::Ok { data: (work_products_data, start_pos) } = load_result {
// Decode the list of work_products
Expand Down Expand Up @@ -173,10 +182,13 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {
}
}

let is_nightly_build = sess.is_nightly_build();
let cfg_version = sess.cfg_version;

MaybeAsync::Async(std::thread::spawn(move || {
let _prof_timer = prof.generic_activity("incr_comp_load_dep_graph");

match load_data(report_incremental_info, &path, nightly_build) {
match load_data_no_sess(&path, report_incremental_info, is_nightly_build, cfg_version) {
LoadResult::DataOutOfDate => LoadResult::DataOutOfDate,
LoadResult::LoadDepGraph(path, err) => LoadResult::LoadDepGraph(path, err),
LoadResult::DecodeIncrCache(err) => LoadResult::DecodeIncrCache(err),
Expand Down Expand Up @@ -218,11 +230,7 @@ pub fn load_query_result_cache(sess: &Session) -> Option<OnDiskCache<'_>> {

let _prof_timer = sess.prof.generic_activity("incr_comp_load_query_result_cache");

match load_data(
sess.opts.unstable_opts.incremental_info,
&query_cache_path(sess),
sess.is_nightly_build(),
) {
match load_data(&query_cache_path(sess), sess) {
LoadResult::Ok { data: (bytes, start_pos) } => {
Some(OnDiskCache::new(sess, bytes, start_pos))
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_incremental/src/persist/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ pub fn build_dep_graph(
}
};

file_format::write_file_header(&mut encoder, sess.is_nightly_build());
file_format::write_file_header(&mut encoder, sess);

// First encode the commandline arguments hash
sess.opts.dep_tracking_hash(false).encode(&mut encoder);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub fn register_plugins<'a>(
crate_name,
sess.crate_types().contains(&CrateType::Executable),
sess.opts.cg.metadata.clone(),
sess.cfg_version,
);
sess.stable_crate_id.set(stable_crate_id).expect("not yet initialized");
rustc_incremental::prepare_session_directory(sess, crate_name, stable_crate_id)?;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_interface/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ impl Linker {

if sess.opts.unstable_opts.no_link {
let rlink_file = self.prepare_outputs.with_extension(config::RLINK_EXT);
CodegenResults::serialize_rlink(&rlink_file, &codegen_results)
CodegenResults::serialize_rlink(sess, &rlink_file, &codegen_results)
.map_err(|error| sess.emit_fatal(FailedWritingFile { path: &rlink_file, error }))?;
return Ok(());
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ fn mk_session(matches: getopts::Matches) -> (Session, CfgSpecs) {
output_file: None,
temps_dir,
};
let sess = build_session(sessopts, io, None, registry, vec![], Default::default(), None, None);
let sess =
build_session(sessopts, io, None, registry, vec![], Default::default(), None, None, "");
(sess, cfg)
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub fn create_session(
lint_caps,
file_loader,
target_override,
rustc_version_str().unwrap_or("unknown"),
);

codegen_backend.init(&sess);
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_metadata/src/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ pub(crate) struct CrateLocator<'a> {
only_needs_metadata: bool,
sysroot: &'a Path,
metadata_loader: &'a dyn MetadataLoader,
cfg_version: &'static str,

// Immutable per-search configuration.
crate_name: Symbol,
Expand Down Expand Up @@ -323,6 +324,7 @@ impl<'a> CrateLocator<'a> {
only_needs_metadata,
sysroot: &sess.sysroot,
metadata_loader,
cfg_version: sess.cfg_version,
crate_name,
exact_paths: if hash.is_none() {
sess.opts
Expand Down Expand Up @@ -655,7 +657,7 @@ impl<'a> CrateLocator<'a> {
}

fn crate_matches(&mut self, metadata: &MetadataBlob, libpath: &Path) -> Option<Svh> {
let rustc_version = rustc_version();
let rustc_version = rustc_version(self.cfg_version);
let found_version = metadata.get_rustc_version();
if found_version != rustc_version {
info!("Rejecting via version: expected {} got {}", rustc_version, found_version);
Expand Down Expand Up @@ -1097,7 +1099,7 @@ impl CrateError {
crate_name,
add_info,
found_crates,
rustc_version: rustc_version(),
rustc_version: rustc_version(sess.cfg_version),
});
} else if !locator.crate_rejections.via_invalid.is_empty() {
let mut crate_rejections = Vec::new();
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2267,7 +2267,7 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) {
};

// Encode the rustc version string in a predictable location.
rustc_version().encode(&mut ecx);
rustc_version(tcx.sess.cfg_version).encode(&mut ecx);

// Encode all the entries and extra information in the crate,
// culminating in the `CrateRoot` which points to all of it.
Expand Down
Loading