Skip to content

Commit 1839c14

Browse files
committed
Auto merge of #54517 - mcr431:53956-panic-on-include_bytes-of-own-file, r=michaelwoerister
53956 panic on include bytes of own file fix #53956 When using `include_bytes!` on a source file in the project, compiler would panic on subsequent compilations because `expand_include_bytes` would overwrite files in the source_map with no source. This PR changes `expand_include_bytes` to check source_map and use the already existing src, if any.
2 parents 4988b09 + f0f8aa9 commit 1839c14

File tree

21 files changed

+180
-119
lines changed

21 files changed

+180
-119
lines changed

src/librustc/ich/impls_syntax.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,13 +417,14 @@ impl_stable_hash_for!(enum ::syntax_pos::hygiene::CompilerDesugaringKind {
417417
impl_stable_hash_for!(enum ::syntax_pos::FileName {
418418
Real(pb),
419419
Macros(s),
420-
QuoteExpansion,
421-
Anon,
422-
MacroExpansion,
423-
ProcMacroSourceCode,
424-
CliCrateAttr,
425-
CfgSpec,
426-
Custom(s)
420+
QuoteExpansion(s),
421+
Anon(s),
422+
MacroExpansion(s),
423+
ProcMacroSourceCode(s),
424+
CliCrateAttr(s),
425+
CfgSpec(s),
426+
Custom(s),
427+
DocTest(pb, line),
427428
});
428429

429430
impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {

src/librustc/session/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,8 +1756,8 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> ast::CrateConfig {
17561756
.into_iter()
17571757
.map(|s| {
17581758
let sess = parse::ParseSess::new(FilePathMapping::empty());
1759-
let mut parser =
1760-
parse::new_parser_from_source_str(&sess, FileName::CfgSpec, s.to_string());
1759+
let filename = FileName::cfg_spec_source_code(&s);
1760+
let mut parser = parse::new_parser_from_source_str(&sess, filename, s.to_string());
17611761

17621762
macro_rules! error {($reason: expr) => {
17631763
early_error(ErrorOutputType::default(),

src/librustc_driver/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ fn make_input(free_matches: &[String]) -> Option<(Input, Option<PathBuf>, Option
594594
} else {
595595
None
596596
};
597-
Some((Input::Str { name: FileName::Anon, input: src },
597+
Some((Input::Str { name: FileName::anon_source_code(&src), input: src },
598598
None, err))
599599
} else {
600600
Some((Input::File(PathBuf::from(ifile)),

src/librustc_driver/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ fn test_env_with_pool<F>(
129129
let cstore = CStore::new(::get_codegen_backend(&sess).metadata_loader());
130130
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
131131
let input = config::Input::Str {
132-
name: FileName::Anon,
132+
name: FileName::anon_source_code(&source_string),
133133
input: source_string.to_string(),
134134
};
135135
let krate =

src/librustc_errors/emitter.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ impl EmitterWriter {
10441044
buffer.append(buffer_msg_line_offset,
10451045
&format!("{}:{}:{}",
10461046
loc.file.name,
1047-
sm.doctest_offset_line(loc.line),
1047+
sm.doctest_offset_line(&loc.file.name, loc.line),
10481048
loc.col.0 + 1),
10491049
Style::LineAndColumn);
10501050
for _ in 0..max_line_num_len {
@@ -1054,7 +1054,7 @@ impl EmitterWriter {
10541054
buffer.prepend(0,
10551055
&format!("{}:{}:{}: ",
10561056
loc.file.name,
1057-
sm.doctest_offset_line(loc.line),
1057+
sm.doctest_offset_line(&loc.file.name, loc.line),
10581058
loc.col.0 + 1),
10591059
Style::LineAndColumn);
10601060
}
@@ -1075,7 +1075,8 @@ impl EmitterWriter {
10751075
};
10761076
format!("{}:{}{}",
10771077
annotated_file.file.name,
1078-
sm.doctest_offset_line(first_line.line_index),
1078+
sm.doctest_offset_line(
1079+
&annotated_file.file.name, first_line.line_index),
10791080
col)
10801081
} else {
10811082
annotated_file.file.name.to_string()

src/librustc_errors/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ pub trait SourceMapper {
130130
fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span>;
131131
fn call_span_if_macro(&self, sp: Span) -> Span;
132132
fn ensure_source_file_source_present(&self, source_file: Lrc<SourceFile>) -> bool;
133-
fn doctest_offset_line(&self, line: usize) -> usize;
133+
fn doctest_offset_line(&self, file: &FileName, line: usize) -> usize;
134134
}
135135

136136
impl CodeSuggestion {

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3008,7 +3008,7 @@ pub struct Span {
30083008
impl Span {
30093009
pub fn empty() -> Span {
30103010
Span {
3011-
filename: FileName::Anon,
3011+
filename: FileName::Anon(0),
30123012
loline: 0, locol: 0,
30133013
hiline: 0, hicol: 0,
30143014
}

src/librustdoc/test.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,14 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, line: usize,
197197
let (test, line_offset) = make_test(test, Some(cratename), as_test_harness, opts);
198198
// FIXME(#44940): if doctests ever support path remapping, then this filename
199199
// needs to be the result of SourceMap::span_to_unmapped_path
200+
201+
let path = match filename {
202+
FileName::Real(path) => path.clone(),
203+
_ => PathBuf::from(r"doctest.rs"),
204+
};
205+
200206
let input = config::Input::Str {
201-
name: filename.to_owned(),
207+
name: FileName::DocTest(path, line as isize - line_offset as isize),
202208
input: test,
203209
};
204210
let outputs = OutputTypes::new(&[(OutputType::Exe, None)]);
@@ -252,9 +258,7 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, line: usize,
252258
let _bomb = Bomb(data.clone(), old.unwrap_or(box io::stdout()));
253259

254260
let (libdir, outdir, compile_result) = driver::spawn_thread_pool(sessopts, |sessopts| {
255-
let source_map = Lrc::new(SourceMap::new_doctest(
256-
sessopts.file_path_mapping(), filename.clone(), line as isize - line_offset as isize
257-
));
261+
let source_map = Lrc::new(SourceMap::new(sessopts.file_path_mapping()));
258262
let emitter = errors::emitter::EmitterWriter::new(box Sink(data.clone()),
259263
Some(source_map.clone()),
260264
false,
@@ -401,7 +405,7 @@ pub fn make_test(s: &str,
401405
use errors::emitter::EmitterWriter;
402406
use errors::Handler;
403407

404-
let filename = FileName::Anon;
408+
let filename = FileName::anon_source_code(s);
405409
let source = crates + &everything_else;
406410

407411
// any errors in parsing should also appear when the doctest is compiled for real, so just
@@ -411,8 +415,6 @@ pub fn make_test(s: &str,
411415
let handler = Handler::with_emitter(false, false, box emitter);
412416
let sess = ParseSess::with_span_handler(handler, cm);
413417

414-
debug!("about to parse: \n{}", source);
415-
416418
let mut found_main = false;
417419
let mut found_extern_crate = cratename.is_none();
418420

@@ -487,8 +489,6 @@ pub fn make_test(s: &str,
487489
prog.push_str("\n}");
488490
}
489491

490-
info!("final test program: {}", prog);
491-
492492
(prog, line_offset)
493493
}
494494

src/libsyntax/attr/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ pub fn inject(mut krate: ast::Crate, parse_sess: &ParseSess, attrs: &[String]) -
803803
for raw_attr in attrs {
804804
let mut parser = parse::new_parser_from_source_str(
805805
parse_sess,
806-
FileName::CliCrateAttr,
806+
FileName::cli_crate_attr_source_code(&raw_attr),
807807
raw_attr.clone(),
808808
);
809809

src/libsyntax/ext/quote.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,27 +353,27 @@ pub mod rt {
353353
impl<'a> ExtParseUtils for ExtCtxt<'a> {
354354
fn parse_item(&self, s: String) -> P<ast::Item> {
355355
panictry!(parse::parse_item_from_source_str(
356-
FileName::QuoteExpansion,
356+
FileName::quote_expansion_source_code(&s),
357357
s,
358358
self.parse_sess())).expect("parse error")
359359
}
360360

361361
fn parse_stmt(&self, s: String) -> ast::Stmt {
362362
panictry!(parse::parse_stmt_from_source_str(
363-
FileName::QuoteExpansion,
363+
FileName::quote_expansion_source_code(&s),
364364
s,
365365
self.parse_sess())).expect("parse error")
366366
}
367367

368368
fn parse_expr(&self, s: String) -> P<ast::Expr> {
369369
panictry!(parse::parse_expr_from_source_str(
370-
FileName::QuoteExpansion,
370+
FileName::quote_expansion_source_code(&s),
371371
s,
372372
self.parse_sess()))
373373
}
374374

375375
fn parse_tts(&self, s: String) -> Vec<TokenTree> {
376-
let source_name = FileName::QuoteExpansion;
376+
let source_name = FileName::quote_expansion_source_code(&s);
377377
parse::parse_stream_from_source_str(source_name, s, self.parse_sess(), None)
378378
.into_trees().collect()
379379
}

0 commit comments

Comments
 (0)