Skip to content

Commit 153de76

Browse files
committed
rustc_codegen_ssa: fix get_rpath_relative_to_output panic when lib only contains file name
1 parent 05965ae commit 153de76

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

compiler/rustc_codegen_ssa/src/back/rpath.rs

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ fn get_rpath_relative_to_output(config: &RPathConfig<'_>, lib: &Path) -> OsStrin
8585
// Strip filenames
8686
let lib = lib.parent().unwrap();
8787
let output = config.out_filename.parent().unwrap();
88+
89+
// If output is empty, just assume it locates in current path
90+
let output = if output == Path::new("") { Path::new(".") } else { output };
91+
8892
let lib = try_canonicalize(lib).unwrap();
8993
let output = try_canonicalize(output).unwrap();
9094
let relative = path_relative_from(&lib, &output)

compiler/rustc_codegen_ssa/src/back/rpath/tests.rs

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ fn test_rpath_relative() {
4444
};
4545
let res = get_rpath_relative_to_output(config, Path::new("lib/libstd.so"));
4646
assert_eq!(res, "@loader_path/../lib");
47+
// Should not panic when lib only contains filename.
48+
// Issue 119571
49+
let _ = get_rpath_relative_to_output(config, Path::new("libstd.so"));
4750
} else {
4851
let config = &mut RPathConfig {
4952
libs: &[],
@@ -54,6 +57,9 @@ fn test_rpath_relative() {
5457
};
5558
let res = get_rpath_relative_to_output(config, Path::new("lib/libstd.so"));
5659
assert_eq!(res, "$ORIGIN/../lib");
60+
// Should not panic when lib only contains filename.
61+
// Issue 119571
62+
let _ = get_rpath_relative_to_output(config, Path::new("libstd.so"));
5763
}
5864
}
5965

0 commit comments

Comments
 (0)