Auto merge of #116487 - tamird:avoid-unwrap-absolute, r=bjorn3
compiler: env/path handling fixes Please see individual commits. r? `@bjorn3` cf. #116426
This commit is contained in:
commit
e08de86036
5 changed files with 30 additions and 28 deletions
|
@ -55,7 +55,7 @@ enum CodegenBackend {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
if env::var("RUST_BACKTRACE").is_err() {
|
if env::var_os("RUST_BACKTRACE").is_none() {
|
||||||
env::set_var("RUST_BACKTRACE", "1");
|
env::set_var("RUST_BACKTRACE", "1");
|
||||||
}
|
}
|
||||||
env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1");
|
env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1");
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
use pathdiff::diff_paths;
|
use pathdiff::diff_paths;
|
||||||
use rustc_data_structures::fx::FxHashSet;
|
use rustc_data_structures::fx::FxHashSet;
|
||||||
use std::env;
|
use rustc_fs_util::try_canonicalize;
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::fs;
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
pub struct RPathConfig<'a> {
|
pub struct RPathConfig<'a> {
|
||||||
|
@ -82,12 +81,11 @@ fn get_rpath_relative_to_output(config: &mut RPathConfig<'_>, lib: &Path) -> OsS
|
||||||
// Mac doesn't appear to support $ORIGIN
|
// Mac doesn't appear to support $ORIGIN
|
||||||
let prefix = if config.is_like_osx { "@loader_path" } else { "$ORIGIN" };
|
let prefix = if config.is_like_osx { "@loader_path" } else { "$ORIGIN" };
|
||||||
|
|
||||||
let cwd = env::current_dir().unwrap();
|
// Strip filenames
|
||||||
let mut lib = fs::canonicalize(&cwd.join(lib)).unwrap_or_else(|_| cwd.join(lib));
|
let lib = lib.parent().unwrap();
|
||||||
lib.pop(); // strip filename
|
let output = config.out_filename.parent().unwrap();
|
||||||
let mut output = cwd.join(&config.out_filename);
|
let lib = try_canonicalize(lib).unwrap();
|
||||||
output.pop(); // strip filename
|
let output = try_canonicalize(output).unwrap();
|
||||||
let output = fs::canonicalize(&output).unwrap_or(output);
|
|
||||||
let relative = path_relative_from(&lib, &output)
|
let relative = path_relative_from(&lib, &output)
|
||||||
.unwrap_or_else(|| panic!("couldn't create relative path from {output:?} to {lib:?}"));
|
.unwrap_or_else(|| panic!("couldn't create relative path from {output:?} to {lib:?}"));
|
||||||
|
|
||||||
|
|
|
@ -1287,17 +1287,21 @@ pub fn ice_path() -> &'static Option<PathBuf> {
|
||||||
if !rustc_feature::UnstableFeatures::from_environment(None).is_nightly_build() {
|
if !rustc_feature::UnstableFeatures::from_environment(None).is_nightly_build() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
if let Ok("0") = std::env::var("RUST_BACKTRACE").as_deref() {
|
if let Some(s) = std::env::var_os("RUST_BACKTRACE") && s == "0" {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let mut path = match std::env::var("RUSTC_ICE").as_deref() {
|
let mut path = match std::env::var_os("RUSTC_ICE") {
|
||||||
// Explicitly opting out of writing ICEs to disk.
|
Some(s) => {
|
||||||
Ok("0") => return None,
|
if s == "0" {
|
||||||
Ok(s) => PathBuf::from(s),
|
// Explicitly opting out of writing ICEs to disk.
|
||||||
Err(_) => std::env::current_dir().unwrap_or_default(),
|
return None;
|
||||||
|
}
|
||||||
|
PathBuf::from(s)
|
||||||
|
}
|
||||||
|
None => std::env::current_dir().unwrap_or_default(),
|
||||||
};
|
};
|
||||||
let now: OffsetDateTime = SystemTime::now().into();
|
let now: OffsetDateTime = SystemTime::now().into();
|
||||||
let file_now = now.format(&Rfc3339).unwrap_or(String::new());
|
let file_now = now.format(&Rfc3339).unwrap_or_default();
|
||||||
let pid = std::process::id();
|
let pid = std::process::id();
|
||||||
path.push(format!("rustc-ice-{file_now}-{pid}.txt"));
|
path.push(format!("rustc-ice-{file_now}-{pid}.txt"));
|
||||||
Some(path)
|
Some(path)
|
||||||
|
@ -1322,7 +1326,7 @@ pub fn install_ice_hook(bug_report_url: &'static str, extra_info: fn(&Handler))
|
||||||
// by the user. Compiler developers and other rustc users can
|
// by the user. Compiler developers and other rustc users can
|
||||||
// opt in to less-verbose backtraces by manually setting "RUST_BACKTRACE"
|
// opt in to less-verbose backtraces by manually setting "RUST_BACKTRACE"
|
||||||
// (e.g. `RUST_BACKTRACE=1`)
|
// (e.g. `RUST_BACKTRACE=1`)
|
||||||
if std::env::var("RUST_BACKTRACE").is_err() {
|
if std::env::var_os("RUST_BACKTRACE").is_none() {
|
||||||
std::env::set_var("RUST_BACKTRACE", "full");
|
std::env::set_var("RUST_BACKTRACE", "full");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1411,12 +1415,11 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str, extra_info:
|
||||||
|
|
||||||
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
|
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
|
||||||
|
|
||||||
let file = if let Some(path) = ice_path().as_ref() {
|
let file = if let Some(path) = ice_path() {
|
||||||
// Create the ICE dump target file.
|
// Create the ICE dump target file.
|
||||||
match crate::fs::File::options().create(true).append(true).open(&path) {
|
match crate::fs::File::options().create(true).append(true).open(&path) {
|
||||||
Ok(mut file) => {
|
Ok(mut file) => {
|
||||||
handler
|
handler.emit_note(session_diagnostics::IcePath { path: path.clone() });
|
||||||
.emit_note(session_diagnostics::IcePath { path: path.display().to_string() });
|
|
||||||
if FIRST_PANIC.swap(false, Ordering::SeqCst) {
|
if FIRST_PANIC.swap(false, Ordering::SeqCst) {
|
||||||
let _ = write!(file, "\n\nrustc version: {version}\nplatform: {triple}");
|
let _ = write!(file, "\n\nrustc version: {version}\nplatform: {triple}");
|
||||||
}
|
}
|
||||||
|
@ -1425,10 +1428,10 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str, extra_info:
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
// The path ICE couldn't be written to disk, provide feedback to the user as to why.
|
// The path ICE couldn't be written to disk, provide feedback to the user as to why.
|
||||||
handler.emit_warning(session_diagnostics::IcePathError {
|
handler.emit_warning(session_diagnostics::IcePathError {
|
||||||
path: path.display().to_string(),
|
path: path.clone(),
|
||||||
error: err.to_string(),
|
error: err.to_string(),
|
||||||
env_var: std::env::var("RUSTC_ICE")
|
env_var: std::env::var_os("RUSTC_ICE")
|
||||||
.ok()
|
.map(PathBuf::from)
|
||||||
.map(|env_var| session_diagnostics::IcePathErrorEnv { env_var }),
|
.map(|env_var| session_diagnostics::IcePathErrorEnv { env_var }),
|
||||||
});
|
});
|
||||||
handler.emit_note(session_diagnostics::IceVersion { version, triple });
|
handler.emit_note(session_diagnostics::IceVersion { version, triple });
|
||||||
|
|
|
@ -52,13 +52,13 @@ pub(crate) struct IceVersion<'a> {
|
||||||
#[derive(Diagnostic)]
|
#[derive(Diagnostic)]
|
||||||
#[diag(driver_impl_ice_path)]
|
#[diag(driver_impl_ice_path)]
|
||||||
pub(crate) struct IcePath {
|
pub(crate) struct IcePath {
|
||||||
pub path: String,
|
pub path: std::path::PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Diagnostic)]
|
#[derive(Diagnostic)]
|
||||||
#[diag(driver_impl_ice_path_error)]
|
#[diag(driver_impl_ice_path_error)]
|
||||||
pub(crate) struct IcePathError {
|
pub(crate) struct IcePathError {
|
||||||
pub path: String,
|
pub path: std::path::PathBuf,
|
||||||
pub error: String,
|
pub error: String,
|
||||||
#[subdiagnostic]
|
#[subdiagnostic]
|
||||||
pub env_var: Option<IcePathErrorEnv>,
|
pub env_var: Option<IcePathErrorEnv>,
|
||||||
|
@ -67,7 +67,7 @@ pub(crate) struct IcePathError {
|
||||||
#[derive(Subdiagnostic)]
|
#[derive(Subdiagnostic)]
|
||||||
#[note(driver_impl_ice_path_error_env)]
|
#[note(driver_impl_ice_path_error_env)]
|
||||||
pub(crate) struct IcePathErrorEnv {
|
pub(crate) struct IcePathErrorEnv {
|
||||||
pub env_var: String,
|
pub env_var: std::path::PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Diagnostic)]
|
#[derive(Diagnostic)]
|
||||||
|
|
|
@ -10,6 +10,7 @@ use rustc_data_structures::fx::FxHashSet;
|
||||||
use rustc_data_structures::svh::Svh;
|
use rustc_data_structures::svh::Svh;
|
||||||
use rustc_data_structures::sync::{FreezeReadGuard, FreezeWriteGuard};
|
use rustc_data_structures::sync::{FreezeReadGuard, FreezeWriteGuard};
|
||||||
use rustc_expand::base::SyntaxExtension;
|
use rustc_expand::base::SyntaxExtension;
|
||||||
|
use rustc_fs_util::try_canonicalize;
|
||||||
use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, StableCrateIdMap, LOCAL_CRATE};
|
use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, StableCrateIdMap, LOCAL_CRATE};
|
||||||
use rustc_hir::definitions::Definitions;
|
use rustc_hir::definitions::Definitions;
|
||||||
use rustc_index::IndexVec;
|
use rustc_index::IndexVec;
|
||||||
|
@ -31,7 +32,7 @@ use std::error::Error;
|
||||||
use std::ops::Fn;
|
use std::ops::Fn;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::{cmp, env, iter};
|
use std::{cmp, iter};
|
||||||
|
|
||||||
pub struct CStore {
|
pub struct CStore {
|
||||||
metadata_loader: Box<MetadataLoaderDyn>,
|
metadata_loader: Box<MetadataLoaderDyn>,
|
||||||
|
@ -677,7 +678,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
|
||||||
stable_crate_id: StableCrateId,
|
stable_crate_id: StableCrateId,
|
||||||
) -> Result<&'static [ProcMacro], CrateError> {
|
) -> Result<&'static [ProcMacro], CrateError> {
|
||||||
// Make sure the path contains a / or the linker will search for it.
|
// Make sure the path contains a / or the linker will search for it.
|
||||||
let path = env::current_dir().unwrap().join(path);
|
let path = try_canonicalize(path).unwrap();
|
||||||
let lib = load_dylib(&path, 5).map_err(|err| CrateError::DlOpen(err))?;
|
let lib = load_dylib(&path, 5).map_err(|err| CrateError::DlOpen(err))?;
|
||||||
|
|
||||||
let sym_name = self.sess.generate_proc_macro_decls_symbol(stable_crate_id);
|
let sym_name = self.sess.generate_proc_macro_decls_symbol(stable_crate_id);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue