@@ -232,11 +232,25 @@ pub fn run_compiler<'a>(args: &[String],
232232 let loader = file_loader. unwrap_or ( box RealFileLoader ) ;
233233 let codemap = Rc :: new ( CodeMap :: with_file_loader ( loader, sopts. file_path_mapping ( ) ) ) ;
234234 let mut sess = session:: build_session_with_codemap (
235- sopts, input_file_path, descriptions, codemap, emitter_dest,
235+ sopts, input_file_path. clone ( ) , descriptions, codemap, emitter_dest,
236236 ) ;
237237 rustc_trans:: init ( & sess) ;
238238 rustc_lint:: register_builtins ( & mut sess. lint_store . borrow_mut ( ) , Some ( & sess) ) ;
239239
240+ // Ensure the source file isn't accidentally overwritten during compilation.
241+ match input_file_path {
242+ Some ( input_file_path) => {
243+ if driver:: build_output_filenames ( & input, & odir, & ofile, & [ ] , & sess)
244+ . contains_path ( & input_file_path) && sess. opts . will_create_output_file ( ) {
245+ sess. err ( & format ! (
246+ "the input file \" {}\" would be overwritten by the generated executable" ,
247+ input_file_path. display( ) ) ) ;
248+ return ( Err ( CompileIncomplete :: Stopped ) , Some ( sess) ) ;
249+ }
250+ } ,
251+ None => { }
252+ }
253+
240254 let mut cfg = config:: build_configuration ( & sess, cfg) ;
241255 target_features:: add_configuration ( & mut cfg, & sess) ;
242256 sess. parse_sess . config = cfg;
0 commit comments