@@ -176,15 +176,16 @@ fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions {
176176 opts
177177}
178178
179- fn run_test ( test : & str , cratename : & str , filename : & FileName , cfgs : Vec < String > , libs : SearchPaths ,
179+ fn run_test ( test : & str , cratename : & str , filename : & FileName , line : usize ,
180+ cfgs : Vec < String > , libs : SearchPaths ,
180181 externs : Externs ,
181182 should_panic : bool , no_run : bool , as_test_harness : bool ,
182183 compile_fail : bool , mut error_codes : Vec < String > , opts : & TestOptions ,
183184 maybe_sysroot : Option < PathBuf > ,
184185 linker : Option < PathBuf > ) {
185186 // the test harness wants its own `main` & top level functions, so
186187 // never wrap the test in `fn main() { ... }`
187- let test = make_test ( test, Some ( cratename) , as_test_harness, opts) ;
188+ let ( test, line_offset ) = make_test ( test, Some ( cratename) , as_test_harness, opts) ;
188189 // FIXME(#44940): if doctests ever support path remapping, then this filename
189190 // needs to be the result of CodeMap::span_to_unmapped_path
190191 let input = config:: Input :: Str {
@@ -234,7 +235,9 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, cfgs: Vec<String>,
234235 }
235236 }
236237 let data = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ;
237- let codemap = Rc :: new ( CodeMap :: new ( sessopts. file_path_mapping ( ) ) ) ;
238+ let codemap = Rc :: new ( CodeMap :: new_doctest (
239+ sessopts. file_path_mapping ( ) , filename. clone ( ) , line as isize - line_offset as isize
240+ ) ) ;
238241 let emitter = errors:: emitter:: EmitterWriter :: new ( box Sink ( data. clone ( ) ) ,
239242 Some ( codemap. clone ( ) ) ,
240243 false ) ;
@@ -326,13 +329,14 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, cfgs: Vec<String>,
326329 }
327330}
328331
332+ /// Makes the test file. Also returns the number of lines before the code begins
329333pub fn make_test ( s : & str ,
330334 cratename : Option < & str > ,
331335 dont_insert_main : bool ,
332336 opts : & TestOptions )
333- -> String {
337+ -> ( String , usize ) {
334338 let ( crate_attrs, everything_else) = partition_source ( s) ;
335-
339+ let mut line_offset = 0 ;
336340 let mut prog = String :: new ( ) ;
337341
338342 if opts. attrs . is_empty ( ) {
@@ -341,11 +345,13 @@ pub fn make_test(s: &str,
341345 // commonly used to make tests fail in case they trigger warnings, so having this there in
342346 // that case may cause some tests to pass when they shouldn't have.
343347 prog. push_str ( "#![allow(unused)]\n " ) ;
348+ line_offset += 1 ;
344349 }
345350
346351 // Next, any attributes that came from the crate root via #![doc(test(attr(...)))].
347352 for attr in & opts. attrs {
348353 prog. push_str ( & format ! ( "#![{}]\n " , attr) ) ;
354+ line_offset += 1 ;
349355 }
350356
351357 // Now push any outer attributes from the example, assuming they
@@ -358,6 +364,7 @@ pub fn make_test(s: &str,
358364 if let Some ( cratename) = cratename {
359365 if s. contains ( cratename) {
360366 prog. push_str ( & format ! ( "extern crate {};\n " , cratename) ) ;
367+ line_offset += 1 ;
361368 }
362369 }
363370 }
@@ -379,14 +386,15 @@ pub fn make_test(s: &str,
379386 prog. push_str ( & everything_else) ;
380387 } else {
381388 prog. push_str ( "fn main() {\n " ) ;
389+ line_offset += 1 ;
382390 prog. push_str ( & everything_else) ;
383391 prog = prog. trim ( ) . into ( ) ;
384392 prog. push_str ( "\n }" ) ;
385393 }
386394
387395 info ! ( "final test program: {}" , prog) ;
388396
389- prog
397+ ( prog, line_offset )
390398}
391399
392400// FIXME(aburka): use a real parser to deal with multiline attributes
@@ -543,6 +551,7 @@ impl Collector {
543551 run_test ( & test,
544552 & cratename,
545553 & filename,
554+ line,
546555 cfgs,
547556 libs,
548557 externs,
0 commit comments