Skip to content

Commit e08de86

Browse files
committed
Auto merge of #116487 - tamird:avoid-unwrap-absolute, r=bjorn3
compiler: env/path handling fixes Please see individual commits. r? `@bjorn3` cf. #116426
2 parents 1516ca1 + 3cac3de commit e08de86

File tree

5 files changed

+30
-28
lines changed

5 files changed

+30
-28
lines changed

compiler/rustc_codegen_cranelift/build_system/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ enum CodegenBackend {
5555
}
5656

5757
fn main() {
58-
if env::var("RUST_BACKTRACE").is_err() {
58+
if env::var_os("RUST_BACKTRACE").is_none() {
5959
env::set_var("RUST_BACKTRACE", "1");
6060
}
6161
env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1");

compiler/rustc_codegen_ssa/src/back/rpath.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use pathdiff::diff_paths;
22
use rustc_data_structures::fx::FxHashSet;
3-
use std::env;
3+
use rustc_fs_util::try_canonicalize;
44
use std::ffi::OsString;
5-
use std::fs;
65
use std::path::{Path, PathBuf};
76

87
pub struct RPathConfig<'a> {
@@ -82,12 +81,11 @@ fn get_rpath_relative_to_output(config: &mut RPathConfig<'_>, lib: &Path) -> OsS
8281
// Mac doesn't appear to support $ORIGIN
8382
let prefix = if config.is_like_osx { "@loader_path" } else { "$ORIGIN" };
8483

85-
let cwd = env::current_dir().unwrap();
86-
let mut lib = fs::canonicalize(&cwd.join(lib)).unwrap_or_else(|_| cwd.join(lib));
87-
lib.pop(); // strip filename
88-
let mut output = cwd.join(&config.out_filename);
89-
output.pop(); // strip filename
90-
let output = fs::canonicalize(&output).unwrap_or(output);
84+
// Strip filenames
85+
let lib = lib.parent().unwrap();
86+
let output = config.out_filename.parent().unwrap();
87+
let lib = try_canonicalize(lib).unwrap();
88+
let output = try_canonicalize(output).unwrap();
9189
let relative = path_relative_from(&lib, &output)
9290
.unwrap_or_else(|| panic!("couldn't create relative path from {output:?} to {lib:?}"));
9391

compiler/rustc_driver_impl/src/lib.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -1287,17 +1287,21 @@ pub fn ice_path() -> &'static Option<PathBuf> {
12871287
if !rustc_feature::UnstableFeatures::from_environment(None).is_nightly_build() {
12881288
return None;
12891289
}
1290-
if let Ok("0") = std::env::var("RUST_BACKTRACE").as_deref() {
1290+
if let Some(s) = std::env::var_os("RUST_BACKTRACE") && s == "0" {
12911291
return None;
12921292
}
1293-
let mut path = match std::env::var("RUSTC_ICE").as_deref() {
1294-
// Explicitly opting out of writing ICEs to disk.
1295-
Ok("0") => return None,
1296-
Ok(s) => PathBuf::from(s),
1297-
Err(_) => std::env::current_dir().unwrap_or_default(),
1293+
let mut path = match std::env::var_os("RUSTC_ICE") {
1294+
Some(s) => {
1295+
if s == "0" {
1296+
// Explicitly opting out of writing ICEs to disk.
1297+
return None;
1298+
}
1299+
PathBuf::from(s)
1300+
}
1301+
None => std::env::current_dir().unwrap_or_default(),
12981302
};
12991303
let now: OffsetDateTime = SystemTime::now().into();
1300-
let file_now = now.format(&Rfc3339).unwrap_or(String::new());
1304+
let file_now = now.format(&Rfc3339).unwrap_or_default();
13011305
let pid = std::process::id();
13021306
path.push(format!("rustc-ice-{file_now}-{pid}.txt"));
13031307
Some(path)
@@ -1322,7 +1326,7 @@ pub fn install_ice_hook(bug_report_url: &'static str, extra_info: fn(&Handler))
13221326
// by the user. Compiler developers and other rustc users can
13231327
// opt in to less-verbose backtraces by manually setting "RUST_BACKTRACE"
13241328
// (e.g. `RUST_BACKTRACE=1`)
1325-
if std::env::var("RUST_BACKTRACE").is_err() {
1329+
if std::env::var_os("RUST_BACKTRACE").is_none() {
13261330
std::env::set_var("RUST_BACKTRACE", "full");
13271331
}
13281332

@@ -1411,12 +1415,11 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str, extra_info:
14111415

14121416
static FIRST_PANIC: AtomicBool = AtomicBool::new(true);
14131417

1414-
let file = if let Some(path) = ice_path().as_ref() {
1418+
let file = if let Some(path) = ice_path() {
14151419
// Create the ICE dump target file.
14161420
match crate::fs::File::options().create(true).append(true).open(&path) {
14171421
Ok(mut file) => {
1418-
handler
1419-
.emit_note(session_diagnostics::IcePath { path: path.display().to_string() });
1422+
handler.emit_note(session_diagnostics::IcePath { path: path.clone() });
14201423
if FIRST_PANIC.swap(false, Ordering::SeqCst) {
14211424
let _ = write!(file, "\n\nrustc version: {version}\nplatform: {triple}");
14221425
}
@@ -1425,10 +1428,10 @@ pub fn report_ice(info: &panic::PanicInfo<'_>, bug_report_url: &str, extra_info:
14251428
Err(err) => {
14261429
// The path ICE couldn't be written to disk, provide feedback to the user as to why.
14271430
handler.emit_warning(session_diagnostics::IcePathError {
1428-
path: path.display().to_string(),
1431+
path: path.clone(),
14291432
error: err.to_string(),
1430-
env_var: std::env::var("RUSTC_ICE")
1431-
.ok()
1433+
env_var: std::env::var_os("RUSTC_ICE")
1434+
.map(PathBuf::from)
14321435
.map(|env_var| session_diagnostics::IcePathErrorEnv { env_var }),
14331436
});
14341437
handler.emit_note(session_diagnostics::IceVersion { version, triple });

compiler/rustc_driver_impl/src/session_diagnostics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ pub(crate) struct IceVersion<'a> {
5252
#[derive(Diagnostic)]
5353
#[diag(driver_impl_ice_path)]
5454
pub(crate) struct IcePath {
55-
pub path: String,
55+
pub path: std::path::PathBuf,
5656
}
5757

5858
#[derive(Diagnostic)]
5959
#[diag(driver_impl_ice_path_error)]
6060
pub(crate) struct IcePathError {
61-
pub path: String,
61+
pub path: std::path::PathBuf,
6262
pub error: String,
6363
#[subdiagnostic]
6464
pub env_var: Option<IcePathErrorEnv>,
@@ -67,7 +67,7 @@ pub(crate) struct IcePathError {
6767
#[derive(Subdiagnostic)]
6868
#[note(driver_impl_ice_path_error_env)]
6969
pub(crate) struct IcePathErrorEnv {
70-
pub env_var: String,
70+
pub env_var: std::path::PathBuf,
7171
}
7272

7373
#[derive(Diagnostic)]

compiler/rustc_metadata/src/creader.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_data_structures::fx::FxHashSet;
1010
use rustc_data_structures::svh::Svh;
1111
use rustc_data_structures::sync::{FreezeReadGuard, FreezeWriteGuard};
1212
use rustc_expand::base::SyntaxExtension;
13+
use rustc_fs_util::try_canonicalize;
1314
use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, StableCrateIdMap, LOCAL_CRATE};
1415
use rustc_hir::definitions::Definitions;
1516
use rustc_index::IndexVec;
@@ -31,7 +32,7 @@ use std::error::Error;
3132
use std::ops::Fn;
3233
use std::path::Path;
3334
use std::time::Duration;
34-
use std::{cmp, env, iter};
35+
use std::{cmp, iter};
3536

3637
pub struct CStore {
3738
metadata_loader: Box<MetadataLoaderDyn>,
@@ -677,7 +678,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
677678
stable_crate_id: StableCrateId,
678679
) -> Result<&'static [ProcMacro], CrateError> {
679680
// Make sure the path contains a / or the linker will search for it.
680-
let path = env::current_dir().unwrap().join(path);
681+
let path = try_canonicalize(path).unwrap();
681682
let lib = load_dylib(&path, 5).map_err(|err| CrateError::DlOpen(err))?;
682683

683684
let sym_name = self.sess.generate_proc_macro_decls_symbol(stable_crate_id);

0 commit comments

Comments
 (0)