@@ -176,15 +176,16 @@ fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions {
176
176
opts
177
177
}
178
178
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 ,
180
181
externs : Externs ,
181
182
should_panic : bool , no_run : bool , as_test_harness : bool ,
182
183
compile_fail : bool , mut error_codes : Vec < String > , opts : & TestOptions ,
183
184
maybe_sysroot : Option < PathBuf > ,
184
185
linker : Option < PathBuf > ) {
185
186
// the test harness wants its own `main` & top level functions, so
186
187
// 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) ;
188
189
// FIXME(#44940): if doctests ever support path remapping, then this filename
189
190
// needs to be the result of CodeMap::span_to_unmapped_path
190
191
let input = config:: Input :: Str {
@@ -234,7 +235,9 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, cfgs: Vec<String>,
234
235
}
235
236
}
236
237
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
+ ) ) ;
238
241
let emitter = errors:: emitter:: EmitterWriter :: new ( box Sink ( data. clone ( ) ) ,
239
242
Some ( codemap. clone ( ) ) ,
240
243
false ) ;
@@ -326,13 +329,14 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, cfgs: Vec<String>,
326
329
}
327
330
}
328
331
332
+ /// Makes the test file. Also returns the number of lines before the code begins
329
333
pub fn make_test ( s : & str ,
330
334
cratename : Option < & str > ,
331
335
dont_insert_main : bool ,
332
336
opts : & TestOptions )
333
- -> String {
337
+ -> ( String , usize ) {
334
338
let ( crate_attrs, everything_else) = partition_source ( s) ;
335
-
339
+ let mut line_offset = 0 ;
336
340
let mut prog = String :: new ( ) ;
337
341
338
342
if opts. attrs . is_empty ( ) {
@@ -341,11 +345,13 @@ pub fn make_test(s: &str,
341
345
// commonly used to make tests fail in case they trigger warnings, so having this there in
342
346
// that case may cause some tests to pass when they shouldn't have.
343
347
prog. push_str ( "#![allow(unused)]\n " ) ;
348
+ line_offset += 1 ;
344
349
}
345
350
346
351
// Next, any attributes that came from the crate root via #![doc(test(attr(...)))].
347
352
for attr in & opts. attrs {
348
353
prog. push_str ( & format ! ( "#![{}]\n " , attr) ) ;
354
+ line_offset += 1 ;
349
355
}
350
356
351
357
// Now push any outer attributes from the example, assuming they
@@ -358,6 +364,7 @@ pub fn make_test(s: &str,
358
364
if let Some ( cratename) = cratename {
359
365
if s. contains ( cratename) {
360
366
prog. push_str ( & format ! ( "extern crate {};\n " , cratename) ) ;
367
+ line_offset += 1 ;
361
368
}
362
369
}
363
370
}
@@ -379,14 +386,15 @@ pub fn make_test(s: &str,
379
386
prog. push_str ( & everything_else) ;
380
387
} else {
381
388
prog. push_str ( "fn main() {\n " ) ;
389
+ line_offset += 1 ;
382
390
prog. push_str ( & everything_else) ;
383
391
prog = prog. trim ( ) . into ( ) ;
384
392
prog. push_str ( "\n }" ) ;
385
393
}
386
394
387
395
info ! ( "final test program: {}" , prog) ;
388
396
389
- prog
397
+ ( prog, line_offset )
390
398
}
391
399
392
400
// FIXME(aburka): use a real parser to deal with multiline attributes
@@ -543,6 +551,7 @@ impl Collector {
543
551
run_test ( & test,
544
552
& cratename,
545
553
& filename,
554
+ line,
546
555
cfgs,
547
556
libs,
548
557
externs,
0 commit comments