Skip to content

Commit 6d215fe

Browse files
committed
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.
2 parents 0111892 + ea7cf90 commit 6d215fe

File tree

8 files changed

+32
-19
lines changed

8 files changed

+32
-19
lines changed

Diff for: src/librustc/session/config.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,20 @@ pub enum PrintRequest {
172172
pub enum Input {
173173
/// Load source from file
174174
File(PathBuf),
175-
/// The string is the source
176-
Str(String)
175+
Str {
176+
/// String that is shown in place of a filename
177+
name: String,
178+
/// Anonymous source string
179+
input: String,
180+
},
177181
}
178182

179183
impl Input {
180184
pub fn filestem(&self) -> String {
181185
match *self {
182186
Input::File(ref ifile) => ifile.file_stem().unwrap()
183187
.to_str().unwrap().to_string(),
184-
Input::Str(_) => "rust_out".to_string(),
188+
Input::Str { .. } => "rust_out".to_string(),
185189
}
186190
}
187191
}

Diff for: src/librustc_driver/driver.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ pub fn compile_input(sess: &Session,
231231
Ok(())
232232
}
233233

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

@@ -434,9 +433,9 @@ pub fn phase_1_parse_input<'a>(sess: &'a Session,
434433
Input::File(ref file) => {
435434
parse::parse_crate_from_file(file, cfg.clone(), &sess.parse_sess)
436435
}
437-
Input::Str(ref src) => {
438-
parse::parse_crate_from_source_str(anon_src().to_string(),
439-
src.to_string(),
436+
Input::Str { ref input, ref name } => {
437+
parse::parse_crate_from_source_str(name.clone(),
438+
input.clone(),
440439
cfg.clone(),
441440
&sess.parse_sess)
442441
}

Diff for: src/librustc_driver/lib.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ fn make_input(free_matches: &[String]) -> Option<(Input, Option<PathBuf>)> {
223223
if ifile == "-" {
224224
let mut src = String::new();
225225
io::stdin().read_to_string(&mut src).unwrap();
226-
Some((Input::Str(src), None))
226+
Some((Input::Str { name: driver::anon_src(), input: src },
227+
None))
227228
} else {
228229
Some((Input::File(PathBuf::from(ifile)),
229230
Some(PathBuf::from(ifile))))
@@ -511,7 +512,7 @@ impl RustcDefaultCalls {
511512
.unwrap();
512513
println!("{}", String::from_utf8(v).unwrap());
513514
}
514-
&Input::Str(_) => {
515+
&Input::Str { .. } => {
515516
early_error(ErrorOutputType::default(), "cannot list metadata for stdin");
516517
}
517518
}
@@ -994,9 +995,9 @@ fn parse_crate_attrs<'a>(sess: &'a Session, input: &Input) -> PResult<'a, Vec<as
994995
Input::File(ref ifile) => {
995996
parse::parse_crate_attrs_from_file(ifile, Vec::new(), &sess.parse_sess)
996997
}
997-
Input::Str(ref src) => {
998-
parse::parse_crate_attrs_from_source_str(driver::anon_src().to_string(),
999-
src.to_string(),
998+
Input::Str { ref name, ref input } => {
999+
parse::parse_crate_attrs_from_source_str(name.clone(),
1000+
input.clone(),
10001001
Vec::new(),
10011002
&sess.parse_sess)
10021003
}

Diff for: src/librustc_driver/test.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ fn test_env<F>(source_string: &str,
113113
Rc::new(CodeMap::new()), cstore.clone());
114114
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
115115
let krate_config = Vec::new();
116-
let input = config::Input::Str(source_string.to_string());
116+
let input = config::Input::Str {
117+
name: driver::anon_src(),
118+
input: source_string.to_string(),
119+
};
117120
let krate = driver::phase_1_parse_input(&sess, krate_config, &input).unwrap();
118121
let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate, "test", None)
119122
.expect("phase 2 aborted");

Diff for: src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl<'a, 'tcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx> {
205205
current_dir().unwrap().join(path)
206206
}
207207
},
208-
Input::Str(_) => PathBuf::new() // FIXME: this is wrong
208+
Input::Str { ref name, .. } => PathBuf::from(name.clone()),
209209
};
210210

211211
Crate {

Diff for: src/librustdoc/test.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,10 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
180180
// the test harness wants its own `main` & top level functions, so
181181
// never wrap the test in `fn main() { ... }`
182182
let test = maketest(test, Some(cratename), as_test_harness, opts);
183-
let input = config::Input::Str(test.to_string());
183+
let input = config::Input::Str {
184+
name: driver::anon_src(),
185+
input: test.to_owned(),
186+
};
184187
let mut outputs = HashMap::new();
185188
outputs.insert(OutputType::Exe, None);
186189

Diff for: src/test/run-make/execution-engine/test.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ fn build_exec_options(sysroot: PathBuf) -> Options {
216216
/// for crates used in the given input.
217217
fn compile_program(input: &str, sysroot: PathBuf)
218218
-> Option<(llvm::ModuleRef, Vec<PathBuf>)> {
219-
let input = Input::Str(input.to_string());
219+
let input = Input::Str {
220+
name: driver::anon_src(),
221+
input: input.to_string(),
222+
};
220223
let thread = Builder::new().name("compile_program".to_string());
221224

222225
let handle = thread.spawn(move || {

Diff for: src/test/run-make/issue-19371/foo.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extern crate syntax;
1818

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

6868
compile_input(&sess, &cstore,
6969
cfg,
70-
&Input::Str(code),
70+
&Input::Str { name: anon_src(), input: code },
7171
&None,
7272
&Some(output),
7373
None,

0 commit comments

Comments
 (0)