Skip to content

Commit b0803d4

Browse files
Display correct filename with --test option
1 parent 324b175 commit b0803d4

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use std::fmt::{self, Write};
3535
use std::slice;
3636
use std::str;
3737
use syntax::feature_gate::UnstableFeatures;
38+
use syntax::codemap::Span;
3839

3940
use html::render::derive_id;
4041
use html::toc::TocBuilder;
@@ -429,7 +430,7 @@ pub fn render(w: &mut fmt::Formatter,
429430
}
430431
}
431432

432-
pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, start_line: usize) {
433+
pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Span) {
433434
extern fn block(_ob: *mut hoedown_buffer,
434435
text: *const hoedown_buffer,
435436
lang: *const hoedown_buffer,
@@ -454,11 +455,12 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, start_line:
454455
});
455456
let text = lines.collect::<Vec<&str>>().join("\n");
456457
let line = tests.get_line() + line;
458+
let filename = tests.get_filename();
457459
tests.add_test(text.to_owned(),
458460
block_info.should_panic, block_info.no_run,
459461
block_info.ignore, block_info.test_harness,
460462
block_info.compile_fail, block_info.error_codes,
461-
line);
463+
line, filename);
462464
}
463465
}
464466

@@ -479,7 +481,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, start_line:
479481
}
480482
}
481483

482-
tests.set_line(start_line);
484+
tests.set_position(position);
483485
unsafe {
484486
let ob = hoedown_buffer_new(DEF_OUNIT);
485487
let renderer = hoedown_html_renderer_new(0, 0);

src/librustdoc/markdown.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use getopts;
1818
use testing;
1919
use rustc::session::search_paths::SearchPaths;
2020
use rustc::session::config::Externs;
21+
use syntax::codemap::DUMMY_SP;
2122

2223
use externalfiles::{ExternalHtml, LoadStringError, load_string};
2324

@@ -154,9 +155,8 @@ pub fn test(input: &str, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
154155
let mut opts = TestOptions::default();
155156
opts.no_crate_inject = true;
156157
let mut collector = Collector::new(input.to_string(), cfgs, libs, externs,
157-
true, opts, maybe_sysroot, "input".to_string(),
158-
None);
159-
find_testable_code(&input_str, &mut collector, 0);
158+
true, opts, maybe_sysroot, None);
159+
find_testable_code(&input_str, &mut collector, DUMMY_SP);
160160
test_args.insert(0, "rustdoctest".to_string());
161161
testing::test_main(&test_args, collector.tests);
162162
0

src/librustdoc/test.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use rustc_trans::back::link;
3737
use syntax::ast;
3838
use syntax::codemap::CodeMap;
3939
use syntax::feature_gate::UnstableFeatures;
40-
use syntax_pos::{BytePos, DUMMY_SP, Pos};
40+
use syntax_pos::{BytePos, DUMMY_SP, Pos, Span};
4141
use errors;
4242
use errors::emitter::ColorConfig;
4343

@@ -97,15 +97,13 @@ pub fn run(input: &str,
9797
link::find_crate_name(None, &hir_forest.krate().attrs, &input)
9898
});
9999
let opts = scrape_test_config(hir_forest.krate());
100-
let filename = input_path.to_str().unwrap_or("").to_owned();
101100
let mut collector = Collector::new(crate_name,
102101
cfgs,
103102
libs,
104103
externs,
105104
false,
106105
opts,
107106
maybe_sysroot,
108-
filename,
109107
Some(codemap));
110108

111109
{
@@ -391,15 +389,14 @@ pub struct Collector {
391389
cratename: String,
392390
opts: TestOptions,
393391
maybe_sysroot: Option<PathBuf>,
394-
filename: String,
395-
start_line: usize,
392+
position: Span,
396393
codemap: Option<Rc<CodeMap>>,
397394
}
398395

399396
impl Collector {
400397
pub fn new(cratename: String, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
401398
use_headers: bool, opts: TestOptions, maybe_sysroot: Option<PathBuf>,
402-
filename: String, codemap: Option<Rc<CodeMap>>) -> Collector {
399+
codemap: Option<Rc<CodeMap>>) -> Collector {
403400
Collector {
404401
tests: Vec::new(),
405402
names: Vec::new(),
@@ -412,17 +409,16 @@ impl Collector {
412409
cratename: cratename,
413410
opts: opts,
414411
maybe_sysroot: maybe_sysroot,
415-
filename: filename,
416-
start_line: 0,
412+
position: DUMMY_SP,
417413
codemap: codemap,
418414
}
419415
}
420416

421417
pub fn add_test(&mut self, test: String,
422418
should_panic: bool, no_run: bool, should_ignore: bool,
423419
as_test_harness: bool, compile_fail: bool, error_codes: Vec<String>,
424-
line: usize) {
425-
let name = format!("{} - line {}", self.filename, line);
420+
line: usize, filename: String) {
421+
let name = format!("{} - line {}", filename, line);
426422
self.cnt += 1;
427423
let cfgs = self.cfgs.clone();
428424
let libs = self.libs.clone();
@@ -467,16 +463,25 @@ impl Collector {
467463
}
468464

469465
pub fn get_line(&self) -> usize {
470-
if let Some(ref codemap) = self.codemap{
471-
let line = codemap.lookup_char_pos(BytePos(self.start_line as u32)).line;
466+
if let Some(ref codemap) = self.codemap {
467+
let line = self.position.lo.to_usize();
468+
let line = codemap.lookup_char_pos(BytePos(line as u32)).line;
472469
if line > 0 { line - 1 } else { line }
473470
} else {
474-
self.start_line
471+
0
475472
}
476473
}
477474

478-
pub fn set_line(&mut self, start_line: usize) {
479-
self.start_line = start_line;
475+
pub fn set_position(&mut self, position: Span) {
476+
self.position = position;
477+
}
478+
479+
pub fn get_filename(&self) -> String {
480+
if let Some(ref codemap) = self.codemap {
481+
codemap.span_to_filename(self.position)
482+
} else {
483+
"<input>".to_owned()
484+
}
480485
}
481486

482487
pub fn register_header(&mut self, name: &str, level: u32) {
@@ -520,7 +525,7 @@ impl<'a, 'hir> HirCollector<'a, 'hir> {
520525
if let Some(doc) = attrs.doc_value() {
521526
self.collector.cnt = 0;
522527
markdown::find_testable_code(doc, self.collector,
523-
attrs.span.unwrap_or(DUMMY_SP).lo.to_usize());
528+
attrs.span.unwrap_or(DUMMY_SP));
524529
}
525530

526531
nested(self);

0 commit comments

Comments
 (0)