about summary refs log tree commit diff
diff options
context:
space:
mode:
authorvarkor <github@varkor.com>2018-01-07 01:11:23 +0000
committervarkor <github@varkor.com>2018-01-29 11:07:27 +0000
commit45e962ecc023749fed2bb926c3386059c8e1407a (patch)
tree033de3b5acd449de987e1d1456b8773ba1bd5874
parent3d55974be4142244d98007c9557ac93e9b9b7d0d (diff)
downloadrust-45e962ecc023749fed2bb926c3386059c8e1407a.tar.gz
rust-45e962ecc023749fed2bb926c3386059c8e1407a.zip
Use correct output file paths for error checking
-rw-r--r--src/librustc/session/config.rs39
-rw-r--r--src/librustc_driver/driver.rs96
2 files changed, 69 insertions, 66 deletions
diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs
index b04376000c0..af91e294db6 100644
--- a/src/librustc/session/config.rs
+++ b/src/librustc/session/config.rs
@@ -548,45 +548,6 @@ impl OutputFilenames {
     pub fn filestem(&self) -> String {
         format!("{}{}", self.out_filestem, self.extra)
     }
-
-    fn check_output<F, T>(&self, f: F) -> Option<T> where F: Fn(PathBuf) -> Option<T> {
-        match self.single_output_file {
-            Some(ref output_path) => {
-                f(output_path.clone())
-            },
-            None => {
-                for k in self.outputs.keys() {
-                    let output_path = self.path(k.to_owned());
-                    if let Some(result) = f(output_path) {
-                        return Some(result);
-                    }
-                }
-                None
-            }
-        }
-    }
-
-    pub fn contains_path(&self, input_path: &PathBuf) -> bool {
-        let input_path = input_path.canonicalize().ok();
-        if input_path.is_none() {
-            return false
-        }
-        let check = |output_path: PathBuf| {
-            if output_path.canonicalize().ok() == input_path {
-                Some(())
-            } else { None }
-        };
-        self.check_output(check).is_some()
-    }
-
-    pub fn conflicts_with_dir(&self) -> Option<PathBuf> {
-        let check = |output_path: PathBuf| {
-            if output_path.is_dir() {
-                Some(output_path)
-            } else { None }
-        };
-        self.check_output(check)
-    }
 }
 
 pub fn host_triple() -> &'static str {
diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs
index 05bf5e5a7d9..a8ba795845f 100644
--- a/src/librustc_driver/driver.rs
+++ b/src/librustc_driver/driver.rs
@@ -121,19 +121,41 @@ pub fn compile_input(trans: Box<TransCrate>,
         };
 
         let outputs = build_output_filenames(input, outdir, output, &krate.attrs, sess);
+        let crate_name =
+            ::rustc_trans_utils::link::find_crate_name(Some(sess), &krate.attrs, input);
+        let ExpansionResult { expanded_crate, defs, analysis, resolutions, mut hir_forest } = {
+            phase_2_configure_and_expand(
+                sess,
+                &cstore,
+                krate,
+                registry,
+                &crate_name,
+                addl_plugins,
+                control.make_glob_map,
+                |expanded_crate| {
+                    let mut state = CompileState::state_after_expand(
+                        input, sess, outdir, output, &cstore, expanded_crate, &crate_name,
+                    );
+                    controller_entry_point!(after_expand, sess, state, Ok(()));
+                    Ok(())
+                }
+            )?
+        };
+        
+        let output_paths = generated_output_paths(sess, &outputs, &crate_name);
 
         // Ensure the source file isn't accidentally overwritten during compilation.
         match *input_path {
             Some(ref input_path) => {
                 if sess.opts.will_create_output_file() {
-                    if outputs.contains_path(input_path) {
+                    if output_contains_path(&output_paths, input_path) {
                         sess.err(&format!(
                             "the input file \"{}\" would be overwritten by the generated \
                             executable",
                             input_path.display()));
                         return Err(CompileIncomplete::Stopped);
                     }
-                    if let Some(dir_path) = outputs.conflicts_with_dir() {
+                    if let Some(dir_path) = output_conflicts_with_dir(&output_paths) {
                         sess.err(&format!(
                             "the generated executable for the input file \"{}\" conflicts with the \
                             existing directory \"{}\"",
@@ -145,29 +167,7 @@ pub fn compile_input(trans: Box<TransCrate>,
             None => {}
         }
 
-        let crate_name =
-            ::rustc_trans_utils::link::find_crate_name(Some(sess), &krate.attrs, input);
-
-        let ExpansionResult { expanded_crate, defs, analysis, resolutions, mut hir_forest } = {
-            phase_2_configure_and_expand(
-                sess,
-                &cstore,
-                krate,
-                registry,
-                &crate_name,
-                addl_plugins,
-                control.make_glob_map,
-                |expanded_crate| {
-                    let mut state = CompileState::state_after_expand(
-                        input, sess, outdir, output, &cstore, expanded_crate, &crate_name,
-                    );
-                    controller_entry_point!(after_expand, sess, state, Ok(()));
-                    Ok(())
-                }
-            )?
-        };
-
-        write_out_deps(sess, &outputs, &crate_name);
+        write_out_deps(sess, &outputs, &output_paths);
         if sess.opts.output_types.contains_key(&OutputType::DepInfo) &&
             sess.opts.output_types.keys().count() == 1 {
             return Ok(())
@@ -1111,7 +1111,10 @@ fn escape_dep_filename(filename: &FileName) -> String {
     filename.to_string().replace(" ", "\\ ")
 }
 
-fn write_out_deps(sess: &Session, outputs: &OutputFilenames, crate_name: &str) {
+// Returns all the paths that correspond to generated files.
+fn generated_output_paths(sess: &Session,
+                          outputs: &OutputFilenames,
+                          crate_name: &str) -> Vec<PathBuf> {
     let mut out_filenames = Vec::new();
     for output_type in sess.opts.output_types.keys() {
         let file = outputs.path(*output_type);
@@ -1135,7 +1138,46 @@ fn write_out_deps(sess: &Session, outputs: &OutputFilenames, crate_name: &str) {
             }
         }
     }
+    out_filenames
+}
+
+// Runs `f` on every output file path and returns the first non-None result, or None if `f`
+// returns None for every file path.
+fn check_output<F, T>(output_paths: &Vec<PathBuf>, f: F) -> Option<T>
+        where F: Fn(&PathBuf) -> Option<T> {
+            for output_path in output_paths {
+                if let Some(result) = f(output_path) {
+                    return Some(result);
+                }
+            }
+            None
+}
+
+pub fn output_contains_path(output_paths: &Vec<PathBuf>, input_path: &PathBuf) -> bool {
+    let input_path = input_path.canonicalize().ok();
+    if input_path.is_none() {
+        return false
+    }
+    let check = |output_path: &PathBuf| {
+        if output_path.canonicalize().ok() == input_path {
+            Some(())
+        } else { None }
+    };
+    check_output(output_paths, check).is_some()
+}
+
+pub fn output_conflicts_with_dir(output_paths: &Vec<PathBuf>) -> Option<PathBuf> {
+    let check = |output_path: &PathBuf| {
+        if output_path.is_dir() {
+            Some(output_path.clone())
+        } else { None }
+    };
+    check_output(output_paths, check)
+}
 
+fn write_out_deps(sess: &Session,
+                  outputs: &OutputFilenames,
+                  out_filenames: &Vec<PathBuf>) {
     // Write out dependency rules to the dep-info file if requested
     if !sess.opts.output_types.contains_key(&OutputType::DepInfo) {
         return;
@@ -1154,7 +1196,7 @@ fn write_out_deps(sess: &Session, outputs: &OutputFilenames, crate_name: &str) {
                                          .map(|fmap| escape_dep_filename(&fmap.name))
                                          .collect();
             let mut file = fs::File::create(&deps_filename)?;
-            for path in &out_filenames {
+            for path in out_filenames {
                 write!(file, "{}: {}\n\n", path.display(), files.join(" "))?;
             }