Skip to content

librustdoc: improve testnames for doctests #29600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2568,8 +2568,18 @@ fn resolve_type(cx: &DocContext,
debug!("resolve_type({:?},{:?})", path, id);
let tcx = match cx.tcx_opt() {
Some(tcx) => tcx,
// If we're extracting tests, this return value doesn't matter.
None => return Primitive(Bool),
// If we're extracting tests, this return value's accuracy is not
// important, all we want is a string representation to help people
// figure out what doctests are failing.
None => {
let did = DefId::local(DefIndex::from_u32(0));
return ResolvedPath {
path: path,
typarams: None,
did: did,
is_generic: false
};
}
};
let def = match tcx.def_map.borrow().get(&id) {
Some(k) => k.full_def(),
Expand Down
57 changes: 47 additions & 10 deletions src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,22 +422,59 @@ impl Collector {

impl DocFolder for Collector {
fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
let pushed = match item.name {
Some(ref name) if name.is_empty() => false,
Some(ref name) => { self.names.push(name.to_string()); true }
None => false
let current_name = match item.name {
Some(ref name) if !name.is_empty() => Some(name.clone()),
_ => typename_if_impl(&item)
};
match item.doc_value() {
Some(doc) => {
self.cnt = 0;
markdown::find_testable_code(doc, &mut *self);
}
None => {}

let pushed = if let Some(name) = current_name {
self.names.push(name);
true
} else {
false
};

if let Some(doc) = item.doc_value() {
self.cnt = 0;
markdown::find_testable_code(doc, &mut *self);
}

let ret = self.fold_item_recur(item);
if pushed {
self.names.pop();
}

return ret;

// FIXME: it would be better to not have the escaped version in the first place
fn unescape_for_testname(mut s: String) -> String {
// for refs `&foo`
if s.contains("&amp;") {
s = s.replace("&amp;", "&");

// `::&'a mut Foo::` looks weird, let's make it `::<&'a mut Foo>`::
if let Some('&') = s.chars().nth(0) {
s = format!("<{}>", s);
}
}

// either `<..>` or `->`
if s.contains("&gt;") {
s.replace("&gt;", ">")
.replace("&lt;", "<")
} else {
s
}
}

fn typename_if_impl(item: &clean::Item) -> Option<String> {
if let clean::ItemEnum::ImplItem(ref impl_) = item.inner {
let path = impl_.for_.to_string();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I may be getting lost, but what's this actually calling? I couldn't find Display for Type or an explicit to_string method, so I'm not actually sure where this is going...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no it's fine, looks like I'm just abysmal at grep

let unescaped_path = unescape_for_testname(path);
Some(unescaped_path)
} else {
None
}
}
}
}