]> git.lizzy.rs Git - rust.git/commitdiff
rustc: Give a friendlier error when writing deps
authorAlex Crichton <alex@alexcrichton.com>
Tue, 15 Apr 2014 14:35:17 +0000 (07:35 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 23 Apr 2014 17:04:29 +0000 (10:04 -0700)
When an error is encountered when writing dependencies, this presents a nicer
error rather than an ICE.

Closes #13517

src/librustc/driver/driver.rs
src/test/run-make/error-writing-dependencies/Makefile [new file with mode: 0644]
src/test/run-make/error-writing-dependencies/foo.rs [new file with mode: 0644]

index cf0e7e161c1fc3d89110fbe74134f1cffeb01ef4..6674ef8abb4ac2c3a4eeca5066cf318710ff479f 100644 (file)
@@ -495,7 +495,7 @@ pub fn stop_after_phase_5(sess: &Session) -> bool {
 fn write_out_deps(sess: &Session,
                   input: &Input,
                   outputs: &OutputFilenames,
-                  krate: &ast::Crate) -> io::IoResult<()> {
+                  krate: &ast::Crate) {
     let id = link::find_crate_id(krate.attrs.as_slice(), outputs.out_filestem);
 
     let mut out_filenames = Vec::new();
@@ -524,28 +524,34 @@ fn write_out_deps(sess: &Session,
             StrInput(..) => {
                 sess.warn("can not write --dep-info without a filename \
                            when compiling stdin.");
-                return Ok(());
+                return
             },
         },
-        _ => return Ok(()),
+        _ => return,
     };
 
-    // Build a list of files used to compile the output and
-    // write Makefile-compatible dependency rules
-    let files: Vec<~str> = sess.codemap().files.borrow()
-                               .iter().filter_map(|fmap| {
-                                    if fmap.is_real_file() {
-                                        Some(fmap.name.clone())
-                                    } else {
-                                        None
-                                    }
-                                }).collect();
-    let mut file = try!(io::File::create(&deps_filename));
-    for path in out_filenames.iter() {
-        try!(write!(&mut file as &mut Writer,
-                      "{}: {}\n\n", path.display(), files.connect(" ")));
+    let result = (|| {
+        // Build a list of files used to compile the output and
+        // write Makefile-compatible dependency rules
+        let files: Vec<~str> = sess.codemap().files.borrow()
+                                   .iter().filter(|fmap| fmap.is_real_file())
+                                   .map(|fmap| fmap.name.clone())
+                                   .collect();
+        let mut file = try!(io::File::create(&deps_filename));
+        for path in out_filenames.iter() {
+            try!(write!(&mut file as &mut Writer,
+                          "{}: {}\n\n", path.display(), files.connect(" ")));
+        }
+        Ok(())
+    })();
+
+    match result {
+        Ok(()) => {}
+        Err(e) => {
+            sess.fatal(format!("error writing dependencies to `{}`: {}",
+                               deps_filename.display(), e));
+        }
     }
-    Ok(())
 }
 
 pub fn compile_input(sess: Session, cfg: ast::CrateConfig, input: &Input,
@@ -569,7 +575,7 @@ pub fn compile_input(sess: Session, cfg: ast::CrateConfig, input: &Input,
                                                                          krate, &id);
             (outputs, expanded_crate, ast_map)
         };
-        write_out_deps(&sess, input, &outputs, &expanded_crate).unwrap();
+        write_out_deps(&sess, input, &outputs, &expanded_crate);
 
         if stop_after_phase_2(&sess) { return; }
 
diff --git a/src/test/run-make/error-writing-dependencies/Makefile b/src/test/run-make/error-writing-dependencies/Makefile
new file mode 100644 (file)
index 0000000..9f91618
--- /dev/null
@@ -0,0 +1,8 @@
+-include ../tools.mk
+
+all:
+       # Let's get a nice error message
+       $(RUSTC) foo.rs --dep-info foo/bar/baz 2>&1 | \
+               grep "error writing dependencies"
+       # Make sure the filename shows up
+       $(RUSTC) foo.rs --dep-info foo/bar/baz 2>&1 | grep "baz"
diff --git a/src/test/run-make/error-writing-dependencies/foo.rs b/src/test/run-make/error-writing-dependencies/foo.rs
new file mode 100644 (file)
index 0000000..8ae3d07
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn main() {}