Skip to content

Commit 400e955

Browse files
committed
Auto merge of #26199 - swgillespie:issue-26092, r=alexcrichton
`driver::build_output_filenames` calls `file_stem` on a PathBuf obtained from the output file compiler flag. It's possible to pass the empty string to this compiler flag. When file_stem is called on an empty Path, it returns None, which is unwrapped and the compiler panics. This change modifies the `unwrap` to an `unwrap_or` so that the empty string is passed through the compilation pipeline until it reaches `trans::back::write_output_file`, which will emit an appropriate error. Instead of panicking, the error that is emitted now is: ``` $ rustc -o "" thing.rs error: could not write output to : No such file or directory ``` The `:` is a little strange, but it /is/ reporting the filename (the empty string) correctly, I suppose. Both gcc and clang hand the output file to ld, which emits a similar error message when faced with the empty string as an output file: ``` $ clang -o "" thing.c ld: can't open output file for writing: , errno=2 for architecture x86_64 ``` This PR also adds a test for this, in `run-make`. This fixes issue #26092.
2 parents b5b3a99 + 91effb3 commit 400e955

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/librustc_driver/driver.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use super::Compilation;
3333
use serialize::json;
3434

3535
use std::env;
36-
use std::ffi::OsString;
36+
use std::ffi::{OsString, OsStr};
3737
use std::fs;
3838
use std::io::{self, Write};
3939
use std::path::{Path, PathBuf};
@@ -966,7 +966,7 @@ pub fn build_output_filenames(input: &Input,
966966

967967
OutputFilenames {
968968
out_directory: out_file.parent().unwrap_or(cur_dir).to_path_buf(),
969-
out_filestem: out_file.file_stem().unwrap()
969+
out_filestem: out_file.file_stem().unwrap_or(OsStr::new(""))
970970
.to_str().unwrap().to_string(),
971971
single_output_file: ofile,
972972
extra: sess.opts.cg.extra_filename.clone(),
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) -o "" blank.rs 2>&1 | \
5+
grep 'No such file or directory'
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {}

0 commit comments

Comments
 (0)