Skip to content

Some little Rustdoc fixes #21999

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 4 commits into from
Feb 8, 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
3 changes: 2 additions & 1 deletion src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,7 @@ fn encode_info_for_foreign_item(ecx: &EncodeContext,
encode_def_id(rbml_w, local_def(nitem.id));
encode_visibility(rbml_w, nitem.vis);
match nitem.node {
ast::ForeignItemFn(..) => {
ast::ForeignItemFn(ref fndecl, _) => {
encode_family(rbml_w, FN_FAMILY);
encode_bounds_and_type(rbml_w, ecx,
&lookup_item_type(ecx.tcx,local_def(nitem.id)));
Expand All @@ -1478,6 +1478,7 @@ fn encode_info_for_foreign_item(ecx: &EncodeContext,
let stab = stability::lookup(ecx.tcx, ast_util::local_def(nitem.id));
encode_stability(rbml_w, stab);
encode_symbol(ecx, rbml_w, nitem.id);
encode_method_argument_names(rbml_w, &*fndecl);
}
ast::ForeignItemStatic(_, mutbl) => {
if mutbl {
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,14 @@ fn build_impl(cx: &DocContext, tcx: &ty::ctxt,
let mut item = method.clean(cx);
item.inner = match item.inner.clone() {
clean::TyMethodItem(clean::TyMethod {
unsafety, decl, self_, generics
unsafety, decl, self_, generics, abi
}) => {
clean::MethodItem(clean::Method {
unsafety: unsafety,
decl: decl,
self_: self_,
generics: generics,
abi: abi
})
}
_ => panic!("not a tymethod"),
Expand Down
6 changes: 6 additions & 0 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub use self::FunctionRetTy::*;
pub use self::TraitMethod::*;

use syntax;
use syntax::abi;
use syntax::ast;
use syntax::ast_util;
use syntax::ast_util::PostExpansionMethod;
Expand Down Expand Up @@ -945,6 +946,7 @@ pub struct Method {
pub self_: SelfTy,
pub unsafety: ast::Unsafety,
pub decl: FnDecl,
pub abi: abi::Abi
}

impl Clean<Item> for ast::Method {
Expand Down Expand Up @@ -973,6 +975,7 @@ impl Clean<Item> for ast::Method {
self_: self.pe_explicit_self().node.clean(cx),
unsafety: self.pe_unsafety().clone(),
decl: decl,
abi: self.pe_abi()
}),
}
}
Expand All @@ -984,6 +987,7 @@ pub struct TyMethod {
pub decl: FnDecl,
pub generics: Generics,
pub self_: SelfTy,
pub abi: abi::Abi
}

impl Clean<Item> for ast::TypeMethod {
Expand Down Expand Up @@ -1011,6 +1015,7 @@ impl Clean<Item> for ast::TypeMethod {
decl: decl,
self_: self.explicit_self.node.clean(cx),
generics: self.generics.clean(cx),
abi: self.abi
}),
}
}
Expand Down Expand Up @@ -1301,6 +1306,7 @@ impl<'tcx> Clean<Item> for ty::Method<'tcx> {
generics: (&self.generics, subst::FnSpace).clean(cx),
self_: self_,
decl: (self.def_id, &sig).clean(cx),
abi: self.fty.abi
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,8 @@ impl fmt::Display for clean::Type {
f.write_str(name)
}
clean::ResolvedPath{ did, ref typarams, ref path } => {
try!(resolved_path(f, did, path, false));
// Paths like Self::Output should be rendered with all segments
Copy link
Member

Choose a reason for hiding this comment

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

I believe these sort of associated-type paths exist are possible for any type, that is, they can start with something other than Self. I have no idea how to detect such things.

Copy link
Member

Choose a reason for hiding this comment

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

(In fact, I believe a hybrid path like std::slice::Iter<T>::Item is possible too.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a good point... the ideal rendering of a type like that in rustdocs for a trait would be Iter<T>::Item, with Iter hyperlinked to the appropriate place (possibly in another crate's documentation). So is it a matter of walking path segments and looking for the first non-module and printing the rest of the path from that segment when rendering a path in "short" (less than fully-qualified) form?

try!(resolved_path(f, did, path, path.segments[0].name == "Self"));
tybounds(f, typarams)
}
clean::Infer => write!(f, "_"),
Expand Down
20 changes: 14 additions & 6 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use externalfiles::ExternalHtml;

use serialize::json;
use serialize::json::ToJson;
use syntax::abi;
use syntax::ast;
use syntax::ast_util;
use rustc::util::nodemap::NodeSet;
Expand Down Expand Up @@ -1809,15 +1810,22 @@ fn assoc_type(w: &mut fmt::Formatter, it: &clean::Item,
}

fn render_method(w: &mut fmt::Formatter, meth: &clean::Item) -> fmt::Result {
fn method(w: &mut fmt::Formatter, it: &clean::Item, unsafety: ast::Unsafety,
g: &clean::Generics, selfty: &clean::SelfTy,
d: &clean::FnDecl) -> fmt::Result {
write!(w, "{}fn <a href='#{ty}.{name}' class='fnname'>{name}</a>\
fn method(w: &mut fmt::Formatter, it: &clean::Item,
unsafety: ast::Unsafety, abi: abi::Abi,
g: &clean::Generics, selfty: &clean::SelfTy,
d: &clean::FnDecl) -> fmt::Result {
use syntax::abi::Abi;

write!(w, "{}{}fn <a href='#{ty}.{name}' class='fnname'>{name}</a>\
{generics}{decl}{where_clause}",
match unsafety {
ast::Unsafety::Unsafe => "unsafe ",
_ => "",
},
match abi {
Abi::Rust => String::new(),
a => format!("extern {} ", a.to_string())
},
ty = shortty(it),
name = it.name.as_ref().unwrap(),
generics = *g,
Expand All @@ -1826,10 +1834,10 @@ fn render_method(w: &mut fmt::Formatter, meth: &clean::Item) -> fmt::Result {
}
match meth.inner {
clean::TyMethodItem(ref m) => {
method(w, meth, m.unsafety, &m.generics, &m.self_, &m.decl)
method(w, meth, m.unsafety, m.abi, &m.generics, &m.self_, &m.decl)
}
clean::MethodItem(ref m) => {
method(w, meth, m.unsafety, &m.generics, &m.self_, &m.decl)
method(w, meth, m.unsafety, m.abi, &m.generics, &m.self_, &m.decl)
}
clean::AssociatedTypeItem(ref typ) => {
assoc_type(w, meth, typ)
Expand Down
5 changes: 5 additions & 0 deletions src/test/run-make/rustdoc-assoc-types/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-include ../tools.mk

all: lib.rs
$(HOST_RPATH_ENV) $(RUSTDOC) -w html -o $(TMPDIR)/doc lib.rs
$(HTMLDOCCK) $(TMPDIR)/doc lib.rs
20 changes: 20 additions & 0 deletions src/test/run-make/rustdoc-assoc-types/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![crate_type="lib"]

// @has lib/trait.Index.html
pub trait Index<I: ?Sized> {
// @has - '//*[@id="associatedtype.Output"]//code' 'type Output: ?Sized'
type Output: ?Sized;
// @has - '//*[@id="tymethod.index"]//code' \
// "fn index<'a>(&'a self, index: I) -> &'a Self::Output"
fn index<'a>(&'a self, index: I) -> &'a Self::Output;
}
8 changes: 8 additions & 0 deletions src/test/run-make/rustdoc-extern-method/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-include ../tools.mk

all: foo.rs bar.rs
$(HOST_RPATH_ENV) $(RUSTC) foo.rs
$(HOST_RPATH_ENV) $(RUSTDOC) -w html -o $(TMPDIR)/doc foo.rs
$(HOST_RPATH_ENV) $(RUSTDOC) -L $(TMPDIR) -w html -o $(TMPDIR)/doc bar.rs
$(HTMLDOCCK) $(TMPDIR)/doc bar.rs

24 changes: 24 additions & 0 deletions src/test/run-make/rustdoc-extern-method/bar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

extern crate foo;

// @has bar/trait.Foo.html //pre "pub trait Foo"
// @has - '//*[@id="tymethod.foo"]//code' 'extern "rust-call" fn foo'
// @has - '//*[@id="tymethod.foo_"]//code' 'extern "rust-call" fn foo_'
pub use foo::Foo;

// @has bar/trait.Bar.html //pre "pub trait Bar"
pub trait Bar {
// @has - '//*[@id="tymethod.bar"]//code' 'extern "rust-call" fn bar'
extern "rust-call" fn bar(&self, _: ());
// @has - '//*[@id="method.bar_"]//code' 'extern "rust-call" fn bar_'
extern "rust-call" fn bar_(&self, _: ()) { }
}
16 changes: 16 additions & 0 deletions src/test/run-make/rustdoc-extern-method/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![crate_type="lib"]

pub trait Foo {
extern "rust-call" fn foo(&self, _: ()) -> i32;
extern "rust-call" fn foo_(&self, _: ()) -> i32 { 0 }
}
8 changes: 8 additions & 0 deletions src/test/run-make/rustdoc-ffi/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-include ../tools.mk

all: lib.rs
$(HOST_RPATH_ENV) $(RUSTC) lib.rs
$(HOST_RPATH_ENV) $(RUSTDOC) -w html -o $(TMPDIR)/doc lib.rs
$(HOST_RPATH_ENV) $(RUSTDOC) -L $(TMPDIR) -w html -o $(TMPDIR)/doc user.rs
$(HTMLDOCCK) $(TMPDIR)/doc lib.rs
$(HTMLDOCCK) $(TMPDIR)/doc user.rs
16 changes: 16 additions & 0 deletions src/test/run-make/rustdoc-ffi/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![crate_type="lib"]

extern "C" {
// @has lib/fn.foreigner.html //pre 'pub unsafe fn foreigner(cold_as_ice: u32)'
pub fn foreigner(cold_as_ice: u32);
}
16 changes: 16 additions & 0 deletions src/test/run-make/rustdoc-ffi/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![crate_type="lib"]

extern crate lib;

// @has user/fn.foreigner.html //pre 'pub unsafe fn foreigner(cold_as_ice: u32)'
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 technically this should have an ABI listed of pub extern "C" unsafe fn, is that a known bug? (it's fine if it is)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They aren't shown that way in the docs for lib.rs in this PR either (i.e. when not inlined). I agree that the ABI should be shown. It might also be a good idea to distinguish foreign functions from Rust functions with a non-Rust ABI somehow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Opened #22038

pub use lib::foreigner;
3 changes: 1 addition & 2 deletions src/test/run-make/rustdoc-hidden-line/foo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@
pub fn foo() {}

// @!has foo/fn.foo.html invisible
// @matches - //pre '#.*\[.*derive.*\(.*Eq.*\).*\].*//.*Bar'

// @matches - //pre "#\[derive\(PartialEq\)\] // Bar"
4 changes: 2 additions & 2 deletions src/test/run-make/rustdoc-negative-impl/foo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub struct Alpha;
// @matches foo/struct.Bravo.html '//pre' "pub struct Bravo<B>"
pub struct Bravo<B>;

// @matches foo/struct.Alpha.html '//*[@class="impl"]//code' "impl !.*Send.* for .*Alpha"
// @matches foo/struct.Alpha.html '//*[@class="impl"]//code' "impl !Send for Alpha"
impl !Send for Alpha {}

// @matches foo/struct.Bravo.html '//*[@class="impl"]//code' "impl<B> !.*Send.* for .*Bravo.*<B>"
// @matches foo/struct.Bravo.html '//*[@class="impl"]//code' "impl<B> !Send for Bravo<B>"
impl<B> !Send for Bravo<B> {}
25 changes: 13 additions & 12 deletions src/test/run-make/rustdoc-where/foo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,30 @@

pub trait MyTrait {}

// @matches foo/struct.Alpha.html '//pre' "Alpha.*where.*A:.*MyTrait"
// @has foo/struct.Alpha.html '//pre' "pub struct Alpha<A> where A: MyTrait"
pub struct Alpha<A> where A: MyTrait;
// @matches foo/trait.Bravo.html '//pre' "Bravo.*where.*B:.*MyTrait"
// @has foo/trait.Bravo.html '//pre' "pub trait Bravo<B> where B: MyTrait"
pub trait Bravo<B> where B: MyTrait {}
// @matches foo/fn.charlie.html '//pre' "charlie.*where.*C:.*MyTrait"
// @has foo/fn.charlie.html '//pre' "pub fn charlie<C>() where C: MyTrait"
pub fn charlie<C>() where C: MyTrait {}

pub struct Delta<D>;
// @matches foo/struct.Delta.html '//*[@class="impl"]//code' "impl.*Delta.*where.*D:.*MyTrait"
// @has foo/struct.Delta.html '//*[@class="impl"]//code' \
// "impl<D> Delta<D> where D: MyTrait"
impl<D> Delta<D> where D: MyTrait {
pub fn delta() {}
}

pub struct Echo<E>;
// @matches foo/struct.Echo.html '//*[@class="impl"]//code' \
// "impl.*MyTrait.*for.*Echo.*where.*E:.*MyTrait"
// @matches foo/trait.MyTrait.html '//*[@id="implementors-list"]//code' \
// "impl.*MyTrait.*for.*Echo.*where.*E:.*MyTrait"
// @has foo/struct.Echo.html '//*[@class="impl"]//code' \
// "impl<E> MyTrait for Echo<E> where E: MyTrait"
// @has foo/trait.MyTrait.html '//*[@id="implementors-list"]//code' \
// "impl<E> MyTrait for Echo<E> where E: MyTrait"
impl<E> MyTrait for Echo<E> where E: MyTrait {}

pub enum Foxtrot<F> {}
// @matches foo/enum.Foxtrot.html '//*[@class="impl"]//code' \
// "impl.*MyTrait.*for.*Foxtrot.*where.*F:.*MyTrait"
// @matches foo/trait.MyTrait.html '//*[@id="implementors-list"]//code' \
// "impl.*MyTrait.*for.*Foxtrot.*where.*F:.*MyTrait"
// @has foo/enum.Foxtrot.html '//*[@class="impl"]//code' \
// "impl<F> MyTrait for Foxtrot<F> where F: MyTrait"
// @has foo/trait.MyTrait.html '//*[@id="implementors-list"]//code' \
// "impl<F> MyTrait for Foxtrot<F> where F: MyTrait"
impl<F> MyTrait for Foxtrot<F> where F: MyTrait {}