Auto merge of #80267 - 0urobor0s:ouro/61592, r=jyn514

Rustdoc render public underscore_imports as Re-exports

Fixes #61592
This commit is contained in:
bors 2020-12-31 09:07:52 +00:00
commit a6bd5246da
11 changed files with 109 additions and 2 deletions

View File

@ -464,6 +464,7 @@ E0776: include_str!("./error_codes/E0776.md"),
E0777: include_str!("./error_codes/E0777.md"),
E0778: include_str!("./error_codes/E0778.md"),
E0779: include_str!("./error_codes/E0779.md"),
E0780: include_str!("./error_codes/E0780.md"),
;
// E0006, // merged with E0005
// E0008, // cannot bind by-move into a pattern guard

View File

@ -0,0 +1,19 @@
Cannot use `doc(inline)` with anonymous imports
Erroneous code example:
```ignore (cannot-doctest-multicrate-project)
#[doc(inline)] // error: invalid doc argument
pub use foo::Foo as _;
```
Anonymous imports are always rendered with `#[doc(no_inline)]`. To fix this
error, remove the `#[doc(inline)]` attribute.
Example:
```ignore (cannot-doctest-multicrate-project)
pub use foo::Foo as _;
```

View File

@ -2173,11 +2173,26 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
return Vec::new();
}
let (doc_meta_item, please_inline) = self.attrs.lists(sym::doc).get_word_attr(sym::inline);
let pub_underscore = self.vis.node.is_pub() && self.name == kw::Underscore;
if pub_underscore && please_inline {
rustc_errors::struct_span_err!(
cx.tcx.sess,
doc_meta_item.unwrap().span(),
E0780,
"anonymous imports cannot be inlined"
)
.span_label(self.span, "anonymous import")
.emit();
}
// We consider inlining the documentation of `pub use` statements, but we
// forcefully don't inline if this is not public or if the
// #[doc(no_inline)] attribute is present.
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
let mut denied = !self.vis.node.is_pub()
|| pub_underscore
|| self.attrs.iter().any(|a| {
a.has_name(sym::doc)
&& match a.meta_item_list() {
@ -2190,7 +2205,6 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
});
// Also check whether imports were asked to be inlined, in case we're trying to re-export a
// crate in Rust 2018+
let please_inline = self.attrs.lists(sym::doc).has_word(sym::inline);
let path = self.path.clean(cx);
let inner = if self.glob {
if !denied {

View File

@ -439,12 +439,22 @@ impl AttributesExt for [ast::Attribute] {
crate trait NestedAttributesExt {
/// Returns `true` if the attribute list contains a specific `Word`
fn has_word(self, word: Symbol) -> bool;
fn get_word_attr(self, word: Symbol) -> (Option<ast::NestedMetaItem>, bool);
}
impl<I: IntoIterator<Item = ast::NestedMetaItem>> NestedAttributesExt for I {
impl<I: Iterator<Item = ast::NestedMetaItem> + IntoIterator<Item = ast::NestedMetaItem>>
NestedAttributesExt for I
{
fn has_word(self, word: Symbol) -> bool {
self.into_iter().any(|attr| attr.is_word() && attr.has_name(word))
}
fn get_word_attr(mut self, word: Symbol) -> (Option<ast::NestedMetaItem>, bool) {
match self.find(|attr| attr.is_word() && attr.has_name(word)) {
Some(a) => (Some(a), true),
None => (None, false),
}
}
}
/// A portion of documentation, extracted from a `#[doc]` attribute.

View File

@ -0,0 +1,3 @@
#![crate_name = "foo"]
pub trait Foo {}

View File

@ -0,0 +1,10 @@
// aux-build:issue-61592.rs
extern crate foo;
#[doc = "bar"]
#[doc(inline)] //~ ERROR
#[doc = "baz"]
pub use foo::Foo as _;
fn main() {}

View File

@ -0,0 +1,12 @@
error[E0780]: anonymous imports cannot be inlined
--> $DIR/issue-61592-2.rs:6:7
|
LL | #[doc(inline)]
| ^^^^^^
LL | #[doc = "baz"]
LL | pub use foo::Foo as _;
| ---------------------- anonymous import
error: aborting due to previous error
For more information about this error, try `rustc --explain E0780`.

View File

@ -0,0 +1,8 @@
// aux-build:issue-61592.rs
extern crate foo;
#[doc(inline)] //~ ERROR
pub use foo::Foo as _;
fn main() {}

View File

@ -0,0 +1,11 @@
error[E0780]: anonymous imports cannot be inlined
--> $DIR/issue-61592.rs:5:7
|
LL | #[doc(inline)]
| ^^^^^^
LL | pub use foo::Foo as _;
| ---------------------- anonymous import
error: aborting due to previous error
For more information about this error, try `rustc --explain E0780`.

View File

@ -0,0 +1,4 @@
#![crate_name = "foo"]
pub trait FooTrait {}
pub struct FooStruct;

View File

@ -0,0 +1,15 @@
// aux-build:issue-61592.rs
extern crate foo;
// @has issue_61592/index.html
// @has - '//a[@href="#reexports"]' 'Re-exports'
// @has - '//code' 'pub use foo::FooTrait as _;'
// @!has - '//a[@href="trait._.html"]'
pub use foo::FooTrait as _;
// @has issue_61592/index.html
// @has - '//a[@href="#reexports"]' 'Re-exports'
// @has - '//code' 'pub use foo::FooStruct as _;'
// @!has - '//a[@href="struct._.html"]'
pub use foo::FooStruct as _;