Skip to content

Commit 325af63

Browse files
author
Ariel Ben-Yehuda
authored
Rollup merge of rust-lang#46384 - ollie27:rustdoc_inline_assoc, r=QuietMisdreavus
rustdoc: Fix issues with cross-crate inlined associated items * Visibility was missing from impl items. * Attributes and docs were missing from consts and types in impls. * Const default values were missing from traits. This unifies the code that handles associated items from impls and traits.
2 parents b7e7b65 + b444843 commit 325af63

File tree

5 files changed

+157
-96
lines changed

5 files changed

+157
-96
lines changed

src/librustdoc/clean/inline.rs

+4-68
Original file line numberDiff line numberDiff line change
@@ -332,74 +332,10 @@ pub fn build_impl(cx: &DocContext, did: DefId, ret: &mut Vec<clean::Item>) {
332332

333333
let predicates = tcx.predicates_of(did);
334334
let trait_items = tcx.associated_items(did).filter_map(|item| {
335-
match item.kind {
336-
ty::AssociatedKind::Const => {
337-
let default = if item.defaultness.has_value() {
338-
Some(print_inlined_const(cx, item.def_id))
339-
} else {
340-
None
341-
};
342-
Some(clean::Item {
343-
name: Some(item.name.clean(cx)),
344-
inner: clean::AssociatedConstItem(
345-
tcx.type_of(item.def_id).clean(cx),
346-
default,
347-
),
348-
source: tcx.def_span(item.def_id).clean(cx),
349-
attrs: clean::Attributes::default(),
350-
visibility: None,
351-
stability: tcx.lookup_stability(item.def_id).clean(cx),
352-
deprecation: tcx.lookup_deprecation(item.def_id).clean(cx),
353-
def_id: item.def_id
354-
})
355-
}
356-
ty::AssociatedKind::Method => {
357-
if item.vis != ty::Visibility::Public && associated_trait.is_none() {
358-
return None
359-
}
360-
let mut cleaned = item.clean(cx);
361-
cleaned.inner = match cleaned.inner.clone() {
362-
clean::TyMethodItem(clean::TyMethod {
363-
unsafety, decl, generics, abi
364-
}) => {
365-
let constness = if tcx.is_const_fn(item.def_id) {
366-
hir::Constness::Const
367-
} else {
368-
hir::Constness::NotConst
369-
};
370-
371-
clean::MethodItem(clean::Method {
372-
unsafety,
373-
constness,
374-
decl,
375-
generics,
376-
abi,
377-
})
378-
}
379-
ref r => panic!("not a tymethod: {:?}", r),
380-
};
381-
Some(cleaned)
382-
}
383-
ty::AssociatedKind::Type => {
384-
let typedef = clean::Typedef {
385-
type_: tcx.type_of(item.def_id).clean(cx),
386-
generics: clean::Generics {
387-
lifetimes: vec![],
388-
type_params: vec![],
389-
where_predicates: vec![]
390-
}
391-
};
392-
Some(clean::Item {
393-
name: Some(item.name.clean(cx)),
394-
inner: clean::TypedefItem(typedef, true),
395-
source: tcx.def_span(item.def_id).clean(cx),
396-
attrs: clean::Attributes::default(),
397-
visibility: None,
398-
stability: tcx.lookup_stability(item.def_id).clean(cx),
399-
deprecation: tcx.lookup_deprecation(item.def_id).clean(cx),
400-
def_id: item.def_id
401-
})
402-
}
335+
if associated_trait.is_some() || item.vis == ty::Visibility::Public {
336+
Some(item.clean(cx))
337+
} else {
338+
None
403339
}
404340
}).collect::<Vec<_>>();
405341
let polarity = tcx.impl_polarity(did);

src/librustdoc/clean/mod.rs

+46-27
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,12 @@ impl<'tcx> Clean<Item> for ty::AssociatedItem {
15951595
let inner = match self.kind {
15961596
ty::AssociatedKind::Const => {
15971597
let ty = cx.tcx.type_of(self.def_id);
1598-
AssociatedConstItem(ty.clean(cx), None)
1598+
let default = if self.defaultness.has_value() {
1599+
Some(inline::print_inlined_const(cx, self.def_id))
1600+
} else {
1601+
None
1602+
};
1603+
AssociatedConstItem(ty.clean(cx), default)
15991604
}
16001605
ty::AssociatedKind::Method => {
16011606
let generics = (cx.tcx.generics_of(self.def_id),
@@ -1626,18 +1631,21 @@ impl<'tcx> Clean<Item> for ty::AssociatedItem {
16261631
}
16271632

16281633
let provided = match self.container {
1629-
ty::ImplContainer(_) => false,
1634+
ty::ImplContainer(_) => true,
16301635
ty::TraitContainer(_) => self.defaultness.has_value()
16311636
};
16321637
if provided {
1638+
let constness = if cx.tcx.is_const_fn(self.def_id) {
1639+
hir::Constness::Const
1640+
} else {
1641+
hir::Constness::NotConst
1642+
};
16331643
MethodItem(Method {
16341644
unsafety: sig.unsafety(),
16351645
generics,
16361646
decl,
16371647
abi: sig.abi(),
1638-
1639-
// trait methods cannot (currently, at least) be const
1640-
constness: hir::Constness::NotConst,
1648+
constness,
16411649
})
16421650
} else {
16431651
TyMethodItem(TyMethod {
@@ -1651,14 +1659,14 @@ impl<'tcx> Clean<Item> for ty::AssociatedItem {
16511659
ty::AssociatedKind::Type => {
16521660
let my_name = self.name.clean(cx);
16531661

1654-
let mut bounds = if let ty::TraitContainer(did) = self.container {
1662+
if let ty::TraitContainer(did) = self.container {
16551663
// When loading a cross-crate associated type, the bounds for this type
16561664
// are actually located on the trait/impl itself, so we need to load
16571665
// all of the generics from there and then look for bounds that are
16581666
// applied to this associated type in question.
16591667
let predicates = cx.tcx.predicates_of(did);
16601668
let generics = (cx.tcx.generics_of(did), &predicates).clean(cx);
1661-
generics.where_predicates.iter().filter_map(|pred| {
1669+
let mut bounds = generics.where_predicates.iter().filter_map(|pred| {
16621670
let (name, self_type, trait_, bounds) = match *pred {
16631671
WherePredicate::BoundPredicate {
16641672
ty: QPath { ref name, ref self_type, ref trait_ },
@@ -1676,34 +1684,45 @@ impl<'tcx> Clean<Item> for ty::AssociatedItem {
16761684
_ => return None,
16771685
}
16781686
Some(bounds)
1679-
}).flat_map(|i| i.iter().cloned()).collect::<Vec<_>>()
1680-
} else {
1681-
vec![]
1682-
};
1687+
}).flat_map(|i| i.iter().cloned()).collect::<Vec<_>>();
1688+
// Our Sized/?Sized bound didn't get handled when creating the generics
1689+
// because we didn't actually get our whole set of bounds until just now
1690+
// (some of them may have come from the trait). If we do have a sized
1691+
// bound, we remove it, and if we don't then we add the `?Sized` bound
1692+
// at the end.
1693+
match bounds.iter().position(|b| b.is_sized_bound(cx)) {
1694+
Some(i) => { bounds.remove(i); }
1695+
None => bounds.push(TyParamBound::maybe_sized(cx)),
1696+
}
16831697

1684-
// Our Sized/?Sized bound didn't get handled when creating the generics
1685-
// because we didn't actually get our whole set of bounds until just now
1686-
// (some of them may have come from the trait). If we do have a sized
1687-
// bound, we remove it, and if we don't then we add the `?Sized` bound
1688-
// at the end.
1689-
match bounds.iter().position(|b| b.is_sized_bound(cx)) {
1690-
Some(i) => { bounds.remove(i); }
1691-
None => bounds.push(TyParamBound::maybe_sized(cx)),
1692-
}
1698+
let ty = if self.defaultness.has_value() {
1699+
Some(cx.tcx.type_of(self.def_id))
1700+
} else {
1701+
None
1702+
};
16931703

1694-
let ty = if self.defaultness.has_value() {
1695-
Some(cx.tcx.type_of(self.def_id))
1704+
AssociatedTypeItem(bounds, ty.clean(cx))
16961705
} else {
1697-
None
1698-
};
1699-
1700-
AssociatedTypeItem(bounds, ty.clean(cx))
1706+
TypedefItem(Typedef {
1707+
type_: cx.tcx.type_of(self.def_id).clean(cx),
1708+
generics: Generics {
1709+
lifetimes: Vec::new(),
1710+
type_params: Vec::new(),
1711+
where_predicates: Vec::new(),
1712+
},
1713+
}, true)
1714+
}
17011715
}
17021716
};
17031717

1718+
let visibility = match self.container {
1719+
ty::ImplContainer(_) => self.vis.clean(cx),
1720+
ty::TraitContainer(_) => None,
1721+
};
1722+
17041723
Item {
17051724
name: Some(self.name.clean(cx)),
1706-
visibility: Some(Inherited),
1725+
visibility,
17071726
stability: get_stability(cx, self.def_id),
17081727
deprecation: get_deprecation(cx, self.def_id),
17091728
def_id: self.def_id,

src/librustdoc/html/render.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2621,7 +2621,8 @@ fn assoc_const(w: &mut fmt::Formatter,
26212621
ty: &clean::Type,
26222622
_default: Option<&String>,
26232623
link: AssocItemLink) -> fmt::Result {
2624-
write!(w, "const <a href='{}' class=\"constant\"><b>{}</b></a>: {}",
2624+
write!(w, "{}const <a href='{}' class=\"constant\"><b>{}</b></a>: {}",
2625+
VisSpace(&it.visibility),
26252626
naive_assoc_href(it, link),
26262627
it.name.as_ref().unwrap(),
26272628
ty)?;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// aux-build:assoc-items.rs
12+
// build-aux-docs
13+
// ignore-cross-compile
14+
15+
#![crate_name = "foo"]
16+
17+
extern crate assoc_items;
18+
19+
// @has foo/struct.MyStruct.html
20+
// @!has - 'PrivateConst'
21+
// @has - '//*[@id="associatedconstant.PublicConst"]' 'pub const PublicConst: u8'
22+
// @has - '//*[@class="docblock"]' 'PublicConst: u8 = 123'
23+
// @has - '//*[@class="docblock"]' 'docs for PublicConst'
24+
// @!has - 'private_method'
25+
// @has - '//*[@id="method.public_method"]' 'pub fn public_method()'
26+
// @has - '//*[@class="docblock"]' 'docs for public_method'
27+
// @has - '//*[@id="associatedconstant.ConstNoDefault"]' 'const ConstNoDefault: i16'
28+
// @has - '//*[@class="docblock"]' 'ConstNoDefault: i16 = -123'
29+
// @has - '//*[@class="docblock"]' 'dox for ConstNoDefault'
30+
// @has - '//*[@id="associatedconstant.ConstWithDefault"]' 'const ConstWithDefault: u16'
31+
// @has - '//*[@class="docblock"]' 'ConstWithDefault: u16 = 12345'
32+
// @has - '//*[@class="docblock"]' 'docs for ConstWithDefault'
33+
// @has - '//*[@id="associatedtype.TypeNoDefault"]' 'type TypeNoDefault = i32'
34+
// @has - '//*[@class="docblock"]' 'dox for TypeNoDefault'
35+
// @has - '//*[@id="associatedtype.TypeWithDefault"]' 'type TypeWithDefault = u32'
36+
// @has - '//*[@class="docblock"]' 'docs for TypeWithDefault'
37+
// @has - '//*[@id="method.method_no_default"]' 'fn method_no_default()'
38+
// @has - '//*[@class="docblock"]' 'dox for method_no_default'
39+
// @has - '//*[@id="method.method_with_default"]' 'fn method_with_default()'
40+
// @has - '//*[@class="docblock"]' 'docs for method_with_default'
41+
pub use assoc_items::MyStruct;
42+
43+
// @has foo/trait.MyTrait.html
44+
// @has - '//*[@id="associatedconstant.ConstNoDefault"]' 'const ConstNoDefault: i16'
45+
// @has - '//*[@class="docblock"]' 'docs for ConstNoDefault'
46+
// @has - '//*[@id="associatedconstant.ConstWithDefault"]' 'const ConstWithDefault: u16'
47+
// @has - '//*[@class="docblock"]' 'ConstWithDefault: u16 = 12345'
48+
// @has - '//*[@class="docblock"]' 'docs for ConstWithDefault'
49+
// @has - '//*[@id="associatedtype.TypeNoDefault"]' 'type TypeNoDefault'
50+
// @has - '//*[@class="docblock"]' 'docs for TypeNoDefault'
51+
// @has - '//*[@id="associatedtype.TypeWithDefault"]' 'type TypeWithDefault = u32'
52+
// @has - '//*[@class="docblock"]' 'docs for TypeWithDefault'
53+
// @has - '//*[@id="tymethod.method_no_default"]' 'fn method_no_default()'
54+
// @has - '//*[@class="docblock"]' 'docs for method_no_default'
55+
// @has - '//*[@id="method.method_with_default"]' 'fn method_with_default()'
56+
// @has - '//*[@class="docblock"]' 'docs for method_with_default'
57+
pub use assoc_items::MyTrait;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(associated_type_defaults)]
12+
13+
pub struct MyStruct;
14+
15+
impl MyStruct {
16+
/// docs for PrivateConst
17+
const PrivateConst: i8 = -123;
18+
/// docs for PublicConst
19+
pub const PublicConst: u8 = 123;
20+
/// docs for private_method
21+
fn private_method() {}
22+
/// docs for public_method
23+
pub fn public_method() {}
24+
}
25+
26+
pub trait MyTrait {
27+
/// docs for ConstNoDefault
28+
const ConstNoDefault: i16;
29+
/// docs for ConstWithDefault
30+
const ConstWithDefault: u16 = 12345;
31+
/// docs for TypeNoDefault
32+
type TypeNoDefault;
33+
/// docs for TypeWithDefault
34+
type TypeWithDefault = u32;
35+
/// docs for method_no_default
36+
fn method_no_default();
37+
/// docs for method_with_default
38+
fn method_with_default() {}
39+
}
40+
41+
impl MyTrait for MyStruct {
42+
/// dox for ConstNoDefault
43+
const ConstNoDefault: i16 = -12345;
44+
/// dox for TypeNoDefault
45+
type TypeNoDefault = i32;
46+
/// dox for method_no_default
47+
fn method_no_default() {}
48+
}

0 commit comments

Comments
 (0)