mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-12 09:57:43 +00:00
Auto merge of #32169 - mitaa:anon-tip, r=nrc
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.
This commit is contained in:
commit
6d215fe04c
@ -172,8 +172,12 @@ pub enum PrintRequest {
|
|||||||
pub enum Input {
|
pub enum Input {
|
||||||
/// Load source from file
|
/// Load source from file
|
||||||
File(PathBuf),
|
File(PathBuf),
|
||||||
/// The string is the source
|
Str {
|
||||||
Str(String)
|
/// String that is shown in place of a filename
|
||||||
|
name: String,
|
||||||
|
/// Anonymous source string
|
||||||
|
input: String,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Input {
|
impl Input {
|
||||||
@ -181,7 +185,7 @@ impl Input {
|
|||||||
match *self {
|
match *self {
|
||||||
Input::File(ref ifile) => ifile.file_stem().unwrap()
|
Input::File(ref ifile) => ifile.file_stem().unwrap()
|
||||||
.to_str().unwrap().to_string(),
|
.to_str().unwrap().to_string(),
|
||||||
Input::Str(_) => "rust_out".to_string(),
|
Input::Str { .. } => "rust_out".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -231,7 +231,6 @@ pub fn compile_input(sess: &Session,
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// The name used for source code that doesn't originate in a file
|
/// The name used for source code that doesn't originate in a file
|
||||||
/// (e.g. source from stdin or a string)
|
/// (e.g. source from stdin or a string)
|
||||||
pub fn anon_src() -> String {
|
pub fn anon_src() -> String {
|
||||||
@ -242,7 +241,7 @@ pub fn source_name(input: &Input) -> String {
|
|||||||
match *input {
|
match *input {
|
||||||
// FIXME (#9639): This needs to handle non-utf8 paths
|
// FIXME (#9639): This needs to handle non-utf8 paths
|
||||||
Input::File(ref ifile) => ifile.to_str().unwrap().to_string(),
|
Input::File(ref ifile) => ifile.to_str().unwrap().to_string(),
|
||||||
Input::Str(_) => anon_src(),
|
Input::Str { ref name, .. } => name.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -434,9 +433,9 @@ pub fn phase_1_parse_input<'a>(sess: &'a Session,
|
|||||||
Input::File(ref file) => {
|
Input::File(ref file) => {
|
||||||
parse::parse_crate_from_file(file, cfg.clone(), &sess.parse_sess)
|
parse::parse_crate_from_file(file, cfg.clone(), &sess.parse_sess)
|
||||||
}
|
}
|
||||||
Input::Str(ref src) => {
|
Input::Str { ref input, ref name } => {
|
||||||
parse::parse_crate_from_source_str(anon_src().to_string(),
|
parse::parse_crate_from_source_str(name.clone(),
|
||||||
src.to_string(),
|
input.clone(),
|
||||||
cfg.clone(),
|
cfg.clone(),
|
||||||
&sess.parse_sess)
|
&sess.parse_sess)
|
||||||
}
|
}
|
||||||
|
@ -223,7 +223,8 @@ fn make_input(free_matches: &[String]) -> Option<(Input, Option<PathBuf>)> {
|
|||||||
if ifile == "-" {
|
if ifile == "-" {
|
||||||
let mut src = String::new();
|
let mut src = String::new();
|
||||||
io::stdin().read_to_string(&mut src).unwrap();
|
io::stdin().read_to_string(&mut src).unwrap();
|
||||||
Some((Input::Str(src), None))
|
Some((Input::Str { name: driver::anon_src(), input: src },
|
||||||
|
None))
|
||||||
} else {
|
} else {
|
||||||
Some((Input::File(PathBuf::from(ifile)),
|
Some((Input::File(PathBuf::from(ifile)),
|
||||||
Some(PathBuf::from(ifile))))
|
Some(PathBuf::from(ifile))))
|
||||||
@ -511,7 +512,7 @@ impl RustcDefaultCalls {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
println!("{}", String::from_utf8(v).unwrap());
|
println!("{}", String::from_utf8(v).unwrap());
|
||||||
}
|
}
|
||||||
&Input::Str(_) => {
|
&Input::Str { .. } => {
|
||||||
early_error(ErrorOutputType::default(), "cannot list metadata for stdin");
|
early_error(ErrorOutputType::default(), "cannot list metadata for stdin");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -994,9 +995,9 @@ fn parse_crate_attrs<'a>(sess: &'a Session, input: &Input) -> PResult<'a, Vec<as
|
|||||||
Input::File(ref ifile) => {
|
Input::File(ref ifile) => {
|
||||||
parse::parse_crate_attrs_from_file(ifile, Vec::new(), &sess.parse_sess)
|
parse::parse_crate_attrs_from_file(ifile, Vec::new(), &sess.parse_sess)
|
||||||
}
|
}
|
||||||
Input::Str(ref src) => {
|
Input::Str { ref name, ref input } => {
|
||||||
parse::parse_crate_attrs_from_source_str(driver::anon_src().to_string(),
|
parse::parse_crate_attrs_from_source_str(name.clone(),
|
||||||
src.to_string(),
|
input.clone(),
|
||||||
Vec::new(),
|
Vec::new(),
|
||||||
&sess.parse_sess)
|
&sess.parse_sess)
|
||||||
}
|
}
|
||||||
|
@ -113,7 +113,10 @@ fn test_env<F>(source_string: &str,
|
|||||||
Rc::new(CodeMap::new()), cstore.clone());
|
Rc::new(CodeMap::new()), cstore.clone());
|
||||||
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
|
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
|
||||||
let krate_config = Vec::new();
|
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_1_parse_input(&sess, krate_config, &input).unwrap();
|
||||||
let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate, "test", None)
|
let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate, "test", None)
|
||||||
.expect("phase 2 aborted");
|
.expect("phase 2 aborted");
|
||||||
|
@ -205,7 +205,7 @@ impl<'a, 'tcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx> {
|
|||||||
current_dir().unwrap().join(path)
|
current_dir().unwrap().join(path)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Input::Str(_) => PathBuf::new() // FIXME: this is wrong
|
Input::Str { ref name, .. } => PathBuf::from(name.clone()),
|
||||||
};
|
};
|
||||||
|
|
||||||
Crate {
|
Crate {
|
||||||
|
@ -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
|
// the test harness wants its own `main` & top level functions, so
|
||||||
// never wrap the test in `fn main() { ... }`
|
// never wrap the test in `fn main() { ... }`
|
||||||
let test = maketest(test, Some(cratename), as_test_harness, opts);
|
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();
|
let mut outputs = HashMap::new();
|
||||||
outputs.insert(OutputType::Exe, None);
|
outputs.insert(OutputType::Exe, None);
|
||||||
|
|
||||||
|
@ -216,7 +216,10 @@ fn build_exec_options(sysroot: PathBuf) -> Options {
|
|||||||
/// for crates used in the given input.
|
/// for crates used in the given input.
|
||||||
fn compile_program(input: &str, sysroot: PathBuf)
|
fn compile_program(input: &str, sysroot: PathBuf)
|
||||||
-> Option<(llvm::ModuleRef, Vec<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 thread = Builder::new().name("compile_program".to_string());
|
||||||
|
|
||||||
let handle = thread.spawn(move || {
|
let handle = thread.spawn(move || {
|
||||||
|
@ -18,7 +18,7 @@ extern crate syntax;
|
|||||||
|
|
||||||
use rustc::session::{build_session, Session};
|
use rustc::session::{build_session, Session};
|
||||||
use rustc::session::config::{basic_options, build_configuration, Input, OutputType};
|
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 rustc_metadata::cstore::CStore;
|
||||||
use syntax::diagnostics::registry::Registry;
|
use syntax::diagnostics::registry::Registry;
|
||||||
use syntax::parse::token;
|
use syntax::parse::token;
|
||||||
@ -67,7 +67,7 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) {
|
|||||||
|
|
||||||
compile_input(&sess, &cstore,
|
compile_input(&sess, &cstore,
|
||||||
cfg,
|
cfg,
|
||||||
&Input::Str(code),
|
&Input::Str { name: anon_src(), input: code },
|
||||||
&None,
|
&None,
|
||||||
&Some(output),
|
&Some(output),
|
||||||
None,
|
None,
|
||||||
|
Loading…
Reference in New Issue
Block a user