Skip to content
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

Rustdoc render public underscore_imports as Re-exports #80267

Closed
wants to merge 7 commits into from
Closed
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
1 change: 1 addition & 0 deletions compiler/rustc_error_codes/src/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions compiler/rustc_error_codes/src/error_codes/E0780.md
Original file line number Diff line number Diff line change
@@ -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 _;
```
16 changes: 15 additions & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2157,11 +2157,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() {
Expand All @@ -2174,7 +2189,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 {
Expand Down
12 changes: 11 additions & 1 deletion src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,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.
Expand Down
3 changes: 3 additions & 0 deletions src/test/rustdoc-ui/auxiliary/issue-61592.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![crate_name = "foo"]

pub trait Foo {}
10 changes: 10 additions & 0 deletions src/test/rustdoc-ui/issue-61592-2.rs
Original file line number Diff line number Diff line change
@@ -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() {}
12 changes: 12 additions & 0 deletions src/test/rustdoc-ui/issue-61592-2.stderr
Original file line number Diff line number Diff line change
@@ -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`.
8 changes: 8 additions & 0 deletions src/test/rustdoc-ui/issue-61592.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// aux-build:issue-61592.rs

extern crate foo;

#[doc(inline)] //~ ERROR
pub use foo::Foo as _;

fn main() {}
11 changes: 11 additions & 0 deletions src/test/rustdoc-ui/issue-61592.stderr
Original file line number Diff line number Diff line change
@@ -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`.
4 changes: 4 additions & 0 deletions src/test/rustdoc/auxiliary/issue-61592.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![crate_name = "foo"]

pub trait FooTrait {}
pub struct FooStruct;
15 changes: 15 additions & 0 deletions src/test/rustdoc/issue-61592.rs
Original file line number Diff line number Diff line change
@@ -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 _;