Auto merge of #80267 - 0urobor0s:ouro/61592, r=jyn514
Rustdoc render public underscore_imports as Re-exports Fixes #61592
This commit is contained in:
commit
a6bd5246da
@ -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
|
||||
|
19
compiler/rustc_error_codes/src/error_codes/E0780.md
Normal file
19
compiler/rustc_error_codes/src/error_codes/E0780.md
Normal 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 _;
|
||||
```
|
@ -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 {
|
||||
|
@ -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.
|
||||
|
3
src/test/rustdoc-ui/auxiliary/issue-61592.rs
Normal file
3
src/test/rustdoc-ui/auxiliary/issue-61592.rs
Normal file
@ -0,0 +1,3 @@
|
||||
#![crate_name = "foo"]
|
||||
|
||||
pub trait Foo {}
|
10
src/test/rustdoc-ui/issue-61592-2.rs
Normal file
10
src/test/rustdoc-ui/issue-61592-2.rs
Normal 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() {}
|
12
src/test/rustdoc-ui/issue-61592-2.stderr
Normal file
12
src/test/rustdoc-ui/issue-61592-2.stderr
Normal 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`.
|
8
src/test/rustdoc-ui/issue-61592.rs
Normal file
8
src/test/rustdoc-ui/issue-61592.rs
Normal file
@ -0,0 +1,8 @@
|
||||
// aux-build:issue-61592.rs
|
||||
|
||||
extern crate foo;
|
||||
|
||||
#[doc(inline)] //~ ERROR
|
||||
pub use foo::Foo as _;
|
||||
|
||||
fn main() {}
|
11
src/test/rustdoc-ui/issue-61592.stderr
Normal file
11
src/test/rustdoc-ui/issue-61592.stderr
Normal 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`.
|
4
src/test/rustdoc/auxiliary/issue-61592.rs
Normal file
4
src/test/rustdoc/auxiliary/issue-61592.rs
Normal file
@ -0,0 +1,4 @@
|
||||
#![crate_name = "foo"]
|
||||
|
||||
pub trait FooTrait {}
|
||||
pub struct FooStruct;
|
15
src/test/rustdoc/issue-61592.rs
Normal file
15
src/test/rustdoc/issue-61592.rs
Normal 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 _;
|
Loading…
Reference in New Issue
Block a user