Skip to content

Commit

Permalink
Auto merge of #32169 - mitaa:anon-tip, r=nrc
Browse files Browse the repository at this point in the history
Allow custom filenames for anonymous inputs

This came out of #29253 but doesn't fix it.

I thought it might be worth merging on its own nonetheless.
  • Loading branch information
bors committed Mar 14, 2016
2 parents 0111892 + ea7cf90 commit 6d215fe
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 19 deletions.
10 changes: 7 additions & 3 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,20 @@ pub enum PrintRequest {
pub enum Input {
/// Load source from file
File(PathBuf),
/// The string is the source
Str(String)
Str {
/// String that is shown in place of a filename
name: String,
/// Anonymous source string
input: String,
},
}

impl Input {
pub fn filestem(&self) -> String {
match *self {
Input::File(ref ifile) => ifile.file_stem().unwrap()
.to_str().unwrap().to_string(),
Input::Str(_) => "rust_out".to_string(),
Input::Str { .. } => "rust_out".to_string(),
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ pub fn compile_input(sess: &Session,
Ok(())
}


/// The name used for source code that doesn't originate in a file
/// (e.g. source from stdin or a string)
pub fn anon_src() -> String {
Expand All @@ -242,7 +241,7 @@ pub fn source_name(input: &Input) -> String {
match *input {
// FIXME (#9639): This needs to handle non-utf8 paths
Input::File(ref ifile) => ifile.to_str().unwrap().to_string(),
Input::Str(_) => anon_src(),
Input::Str { ref name, .. } => name.clone(),
}
}

Expand Down Expand Up @@ -434,9 +433,9 @@ pub fn phase_1_parse_input<'a>(sess: &'a Session,
Input::File(ref file) => {
parse::parse_crate_from_file(file, cfg.clone(), &sess.parse_sess)
}
Input::Str(ref src) => {
parse::parse_crate_from_source_str(anon_src().to_string(),
src.to_string(),
Input::Str { ref input, ref name } => {
parse::parse_crate_from_source_str(name.clone(),
input.clone(),
cfg.clone(),
&sess.parse_sess)
}
Expand Down
11 changes: 6 additions & 5 deletions src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ fn make_input(free_matches: &[String]) -> Option<(Input, Option<PathBuf>)> {
if ifile == "-" {
let mut src = String::new();
io::stdin().read_to_string(&mut src).unwrap();
Some((Input::Str(src), None))
Some((Input::Str { name: driver::anon_src(), input: src },
None))
} else {
Some((Input::File(PathBuf::from(ifile)),
Some(PathBuf::from(ifile))))
Expand Down Expand Up @@ -511,7 +512,7 @@ impl RustcDefaultCalls {
.unwrap();
println!("{}", String::from_utf8(v).unwrap());
}
&Input::Str(_) => {
&Input::Str { .. } => {
early_error(ErrorOutputType::default(), "cannot list metadata for stdin");
}
}
Expand Down Expand Up @@ -994,9 +995,9 @@ fn parse_crate_attrs<'a>(sess: &'a Session, input: &Input) -> PResult<'a, Vec<as
Input::File(ref ifile) => {
parse::parse_crate_attrs_from_file(ifile, Vec::new(), &sess.parse_sess)
}
Input::Str(ref src) => {
parse::parse_crate_attrs_from_source_str(driver::anon_src().to_string(),
src.to_string(),
Input::Str { ref name, ref input } => {
parse::parse_crate_attrs_from_source_str(name.clone(),
input.clone(),
Vec::new(),
&sess.parse_sess)
}
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_driver/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ fn test_env<F>(source_string: &str,
Rc::new(CodeMap::new()), cstore.clone());
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
let krate_config = Vec::new();
let input = config::Input::Str(source_string.to_string());
let input = config::Input::Str {
name: driver::anon_src(),
input: source_string.to_string(),
};
let krate = driver::phase_1_parse_input(&sess, krate_config, &input).unwrap();
let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate, "test", None)
.expect("phase 2 aborted");
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl<'a, 'tcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx> {
current_dir().unwrap().join(path)
}
},
Input::Str(_) => PathBuf::new() // FIXME: this is wrong
Input::Str { ref name, .. } => PathBuf::from(name.clone()),
};

Crate {
Expand Down
5 changes: 4 additions & 1 deletion src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
// the test harness wants its own `main` & top level functions, so
// never wrap the test in `fn main() { ... }`
let test = maketest(test, Some(cratename), as_test_harness, opts);
let input = config::Input::Str(test.to_string());
let input = config::Input::Str {
name: driver::anon_src(),
input: test.to_owned(),
};
let mut outputs = HashMap::new();
outputs.insert(OutputType::Exe, None);

Expand Down
5 changes: 4 additions & 1 deletion src/test/run-make/execution-engine/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,10 @@ fn build_exec_options(sysroot: PathBuf) -> Options {
/// for crates used in the given input.
fn compile_program(input: &str, sysroot: PathBuf)
-> Option<(llvm::ModuleRef, Vec<PathBuf>)> {
let input = Input::Str(input.to_string());
let input = Input::Str {
name: driver::anon_src(),
input: input.to_string(),
};
let thread = Builder::new().name("compile_program".to_string());

let handle = thread.spawn(move || {
Expand Down
4 changes: 2 additions & 2 deletions src/test/run-make/issue-19371/foo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extern crate syntax;

use rustc::session::{build_session, Session};
use rustc::session::config::{basic_options, build_configuration, Input, OutputType};
use rustc_driver::driver::{compile_input, CompileController};
use rustc_driver::driver::{compile_input, CompileController, anon_src};
use rustc_metadata::cstore::CStore;
use syntax::diagnostics::registry::Registry;
use syntax::parse::token;
Expand Down Expand Up @@ -67,7 +67,7 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {

compile_input(&sess, &cstore,
cfg,
&Input::Str(code),
&Input::Str { name: anon_src(), input: code },
&None,
&Some(output),
None,
Expand Down

0 comments on commit 6d215fe

Please sign in to comment.