rustdoc: Render private fields in tuple struct as `/* private fields */`
Reopening of https://github.com/rust-lang/rust/pull/110552. All that was missing was a test for the different cases so I added it into the second commit.
Description from the original PR:
> I've gotten some feedback that the current rustdoc rendering of...
>
> ```
> struct HasPrivateFields(_);
> ```
>
> ...is confusing, and I agree with that feedback, especially compared to the field struct case:
>
> ```
> struct HasPrivateFields { /* private fields */ }
> ```
>
> So this PR makes it so that when all of the fields of a tuple variant are private, just render it with the `/* private fields */` comment. We can't *always* render it like that, for example when there's a mix of private and public fields.
cc ````@jsha````
r? ````@notriddle````
Skip rendering metadata strings from include_str!/include_bytes!
The const rendering code in rustdoc completely ignores consts from expansions, but the compiler was rendering all consts. So some consts (namely those from `include_bytes!`) were rendered then ignored.
Most of the diff here is from moving `print_const_expr` from rustdoc into `rustc_hir_pretty` so that it can be used in rustdoc and when building rmeta files.
This let's us handle a multitude of things for free:
- #[doc(hidden)]
- private fields/variants
- --document-private-items
- --document-hidden-items
And correct in the process the determination of "has stripped items" by
doing the same logic done by other ones.
Remake of "List matching impls on type aliases"
* 4b1d13d984
* 6f552c800b
* 2ce7cd906b
Partially reverts "Fix infinite loop when retrieving impls for
type alias", but keeps the test case.
This version of the PR avoids the infinite loop by structurally
matching types instead of using full unification. This version
does not support type alias trait bounds, but the compiler does
not enforce those anyway
(https://github.com/rust-lang/rust/issues/21903).
Rustdoc: Add unstable --no-html-source flag
Fixes https://github.com/rust-lang/rust/issues/115060.
This is the equivalent of `#![doc(no_html_source)]` but on the command-line. It disables the generation of the source pages (and of the links pointing to them as well).
The motivation behind this is to enable to reduce documentation size when generating it in some locations without enforcing this to end users or adding a new feature to enable/disable the crate attribute.
r? `@notriddle`
It lints against features that are inteded to be internal to the
compiler and standard library. Implements MCP #596.
We allow `internal_features` in the standard library and compiler as those
use many features and this _is_ the standard library from the "internal to the compiler and
standard library" after all.
Marking some features as internal wasn't exactly the most scientific approach, I just marked some
mostly obvious features. While there is a categorization in the macro,
it's not very well upheld (should probably be fixed in another PR).
We always pass `-Ainternal_features` in the testsuite
About 400 UI tests and several other tests use internal features.
Instead of throwing the attribute on each one, just always allow them.
There's nothing wrong with testing internal features^^
[rustdoc] If re-export is private, get the next item until a public one is found or expose the private item directly
Fixes#81141.
If we have:
```rust
use Private as Something;
pub fn foo() -> Something {}
```
Then `Something` will be replaced by `Private`.
r? `@notriddle`
rustdoc: fix cross-crate `impl Sized` & `impl ?Sized`
Previously, cross-crate impl-Trait (APIT, RPIT, etc.) that only consists of a single `Sized` bound (modulo outlives-bounds) and ones that are `?Sized` were incorrectly rendered. To give you a taste (before vs. after):
```diff
- fn sized(x: impl ) -> impl
+ fn sized(x: impl Sized) -> impl Sized
- fn sized_outlives<'a>(x: impl 'a) -> impl 'a
+ fn sized_outlives<'a>(x: impl Sized + 'a) -> impl Sized + 'a
- fn maybe_sized(x: &impl ) -> &impl
+ fn maybe_sized(x: &impl ?Sized) -> &impl ?Sized
- fn debug_maybe_sized(x: &impl Debug) -> &impl ?Sized + Debug
+ fn debug_maybe_sized(x: &(impl Debug + ?Sized)) -> &(impl Debug + ?Sized)
```
Moreover, we now surround impl-Trait that has multiple bounds with parentheses if they're the pointee of a reference or raw pointer type. This affects both local and cross-crate docs. The current output isn't correct (rustc would emit the error *ambiguous `+` in a type* if we fed the rendered code back to it).
---
Best reviewed commit by commit :)
`@rustbot` label A-cross-crate-reexports
Fix missing attribute merge on glob foreign re-exports
Fixes https://github.com/rust-lang/rust/issues/113982.
The attributes were not merged with the import's in case of glob re-export of foreign items.
r? `@notriddle`
fix intra-doc links on nested `use` and `extern crate` items
This PR fixes two rustdoc ICEs that happen if there are any intra-doc links on nested `use` or `extern crate` items, for example:
```rust
/// Re-export [`fmt`] and [`io`].
pub use std::{fmt, io}; // "nested" use = use with braces
/// Re-export [`std`].
pub extern crate std;
```
Nested use items were incorrectly considered private and therefore didn't have their intra-doc links resolved. I fixed this by always resolving intra-doc links for nested `use` items that are declared `pub`.
<details>
During AST->HIR lowering, nested `use` items are desugared like this:
```rust
pub use std::{}; // "list stem"
pub use std::fmt;
pub use std::io;
```
Each of these HIR nodes has it's own effective visibility and the list stem is always considered private.
To check the effective visibility of an AST node, the AST node is mapped to a HIR node with `Resolver::local_def_id`, which returns the (private) list stem for nested use items.
</details>
For `extern crate`, there was a hack in rustdoc that stored the `DefId` of the crate itself in the cleaned item, instead of the `DefId` of the `extern crate` item. This made rustdoc look at the resolved links of the extern crate's crate root instead of the `extern crate` item. I've removed this hack and instead translate the `DefId` in the appropriate places.
As as side effect of fixing `extern crate`, i've turned
```rust
#[doc(masked)]
extern crate self as _;
```
into a no-op instead of hiding all trait impls. Proper verification for `doc(masked)` is included as a bonus.
fixes https://github.com/rust-lang/rust/issues/113896
rustdoc: fix position of `default` in method rendering
With the following code:
```rs
#![feature(specialization)]
pub trait A {
unsafe fn a();
}
impl A for () {
default unsafe fn a() {}
}
```
rustdoc would render the `impl` of `a` as
```rs
unsafe default fn a()
```
which is inconsistent with the actual position of `default`.
This PR fixes this issue.
Add tests for `--document-hidden-items` option
Since `--document-hidden-items` was greatly fixed/improved in https://github.com/rust-lang/rust/pull/113574, thought it might be worth adding some more tests for it to prevent new regressions.
As for the first commit, it allows to go from:
```
Traceback (most recent call last):
File "/home/imperio/rust/rust/src/etc/htmldocck.py", line 706, in <module>
check(sys.argv[1], get_commands(rust_test_path))
File "/home/imperio/rust/rust/src/etc/htmldocck.py", line 689, in check
for c in commands:
File "/home/imperio/rust/rust/src/etc/htmldocck.py", line 274, in get_commands
args = shlex.split(args)
File "/usr/lib/python3.10/shlex.py", line 315, in split
return list(lex)
File "/usr/lib/python3.10/shlex.py", line 300, in __next__
token = self.get_token()
File "/usr/lib/python3.10/shlex.py", line 109, in get_token
raw = self.read_token()
File "/usr/lib/python3.10/shlex.py", line 191, in read_token
raise ValueError("No closing quotation")
ValueError: No closing quotation
```
to:
```
Traceback (most recent call last):
File "/home/imperio/rust/rust/src/etc/htmldocck.py", line 708, in <module>
check(sys.argv[1], get_commands(rust_test_path))
File "/home/imperio/rust/rust/src/etc/htmldocck.py", line 691, in check
for c in commands:
File "/home/imperio/rust/rust/src/etc/htmldocck.py", line 278, in get_commands
raise Exception("line {}: {}".format(lineno + 1, exc)) from None
Exception: line 57: No closing quotation
```
Having the line where the error occurred is quite useful.
r? `@notriddle`
rustdoc: render generic params & where-clauses of cross-crate assoc tys in impls
We used to only ever render generic parameters & where-clauses of cross-crate associated types when the item was located inside of a trait and we used to just drop them when it was inside of an impl block (trait or inherent).
Fixes#112904.
`@rustbot` label A-cross-crate-reexports
Rollup of 5 pull requests
Successful merges:
- #112976 (Add test for futures with HRTB)
- #113013 (rustdoc: get rid of extra line when line-wrapping fn decls with empty arg list)
- #113030 (Add a regression test for #109071)
- #113031 (Add a regression test for #110933)
- #113036 (Accept `ReStatic` for RPITIT)
r? `@ghost`
`@rustbot` modify labels: rollup
[tests/rustdoc] Add @files command
The ``````@!has`````` checks is very problematic as it wouldn't catch if the file scheme is updated and the file is generated again. ``````@files`````` allows to ensure that the given folder contains exactly the provided entries (files and folders).
I'm wondering if we should forbid the ``````@!has`````` for files. To be discussed after this PR I suppose.
r? `````@notriddle`````
Fix union fields display
![Screenshot from 2023-06-21 16-47-24](https://github.com/rust-lang/rust/assets/3050060/833b0fe6-7fb6-4371-86c3-d82fa0c3fe49)
So two bugs in this screenshot: no whitespace between field name and type name, both fields are on the same line. Both problems come from issues in the templates because all whitespace are removed if a askama "command" follows.
r? `@notriddle`
Add `lazy_type_alias` feature gate
Add the `type_alias_type` to be able to have the weak alias used without restrictions.
Part of #112792.
cc `@compiler-errors`
r? `@oli-obk`
rustdoc: re-elide cross-crate default trait-object lifetime bounds
Hide trait-object lifetime bounds (re-exported from an external crate) if they coincide with [their default](https://doc.rust-lang.org/reference/lifetime-elision.html#default-trait-object-lifetimes).
Partially addresses #44306. Follow-up to #103885. [Zulip discussion](https://rust-lang.zulipchat.com/#narrow/stream/266220-rustdoc/topic/clean_middle_ty.3A.20I.20need.20to.20add.20a.20parameter/near/307143097).
Most notably, if `std` exported something from `core` containing a type like `Box<dyn Fn()>`, then it would now be rendered as `Box<dyn Fn(), Global>` instead of `Box<dyn Fn() + 'static, Global>` (hiding `+ 'static` as it is the default in this case). Showing `Global` here is a separate issue, #80379, which is on my agenda.
Note that I am not really fond of the fact that I had to add a parameter to such a widely used function (30+ call sites) to address such a niche bug.
CC `@GuillaumeGomez`
Requesting a review from a compiler contributor or team member as recommended on Zulip.
r? compiler
---
`@rustbot` label T-compiler T-rustdoc A-cross-crate-reexports
Fix bug where private item with intermediate doc hidden re-export was not inlined
This fixes this bug:
```rust
mod private {
/// Original.
pub struct Bar3;
}
/// Hidden.
#[doc(hidden)]
pub use crate::private::Bar3;
/// Visible.
pub use self::Bar3 as Reexport;
```
In this case, `private::Bar3` should be inlined and renamed `Reexport` but instead we have:
```
pub use self::Bar3 as Reexport;
```
and no links.
There were actually two issues: the first one is that we forgot to check if the next intermediate re-export was doc hidden. The second was that we made the `#[doc(hidden)]` attribute inheritable, which shouldn't be possible.
r? `@notriddle`
rustdoc: Fix LinkReplacer link matching
It currently just uses the first link with the same href which might not necessarily be the matching one.
This fixes replacements when there are several links to the same item but with different text (e.g. `[X] and [struct@X]`). It also fixes replacements in summaries since those use a links list with empty hrefs, so currently all links would always match the first link by href but then not match its text. This could also lead to a panic in the `original_lext[1..len() - 1]` part when the first link only has a single character, which is why the new code uses `.get(..)` instead.
Fix re-export of doc hidden macro not showing up
It's part of the follow-up of https://github.com/rust-lang/rust/pull/109697.
Re-exports of doc hidden macros should be visible. It was the only kind of re-export of doc hidden item that didn't show up.
r? `@notriddle`
rustdoc: catch and don't blow up on impl Trait cycles
Fixes#110629
An odd feature of Rust is that `Foo` is invalid, but `Bar` is okay:
type Foo<'a, 'b> = Box<dyn PartialEq<Foo<'a, 'b>>>;
type Bar<'a, 'b> = impl PartialEq<Bar<'a, 'b>>;
To get it right, track every time rustdoc descends into a type alias, so if it shows up twice, it can be write the path instead of infinitely expanding it.
An odd feature of Rust is that `Foo` is invalid, but `Bar` is okay:
type Foo<'a, 'b> = Box<dyn PartialEq<Foo<'a, 'b>>>;
type Bar<'a, 'b> = impl PartialEq<Bar<'a, 'b>>;
To get it right, track every time rustdoc descends into a type alias,
so if it shows up twice, it can be write the path instead of
infinitely expanding it.
rustdoc: Get `repr` information through `AdtDef` for foreign items
As suggested by `@notriddle,` this approach works too. The only downside is that the display of the original attribute isn't kept, but I think it's an acceptable downside.
r? `@notriddle`
rustdoc: fix weird margins between Deref impl items
## Before
![image](https://user-images.githubusercontent.com/1593513/235245977-90770591-22c1-4a27-9464-248a3729a2b7.png)
## After
![image](https://user-images.githubusercontent.com/1593513/235246009-0e83113e-42b7-4e29-981d-969f9d20af01.png)
## Description
In the old setup, if the dereffed-to item has multiple impl blocks, each one gets its own `div.impl-items` in the section, but there are no headers separating them. Since the last method in a `div.impl-items` has no bottom margin, and there are no margins between these divs, there is no margin between the last method of one impl and the first method of the following impl.
This patch fixes it by simplifying the HTML. Each Deref block gets exactly one `div.impl-items`, no matter how many impl blocks it actually has.
In the old setup, if the dereffed-to item has multiple impl blocks,
each one gets its own `div.impl-items` in the section, but there
are no headers separating them. Since the last method in a
`div.impl-items` has no bottom margin, and there are no margins
between these divs, there is no margin between the last method
of one impl and the first method of the following impl.
This patch fixes it by simplifying the HTML. Each Deref block gets
exactly one `div.impl-items`, no matter how many impl blocks it
actually has.
Missing blanket impl trait not public
Fixes#94183.
The problem was that we should have checked if the trait was reachable instead of only "directly public".
r? `@notriddle`
rustdoc: Fix invalid handling of nested items with `--document-private-items`
Fixes#110422.
The problem is that only impl block and re-exported `macro_rules!` items are "visible" as nested items. This PR adds the missing checks to handle this correctly.
cc `@compiler-errors`
r? `@notriddle`
rustdoc: Correctly handle built-in compiler proc-macros as proc-macro and not macro
Part of https://github.com/rust-lang/rust/issues/110111.
There were actually one issue split in two parts:
* Compiler built-in proc-macro were incorrectly considered as macros and not proc-macros.
* Re-exports of compiler built-in proc-macros were considering them as macros.
Both issues can be fixed by looking at the `MacroKind` variant instead of just relying on information extracted later on.
r? ``@fmease``
Don't collect return-position impl traits for documentation
#104889 modified the rustdoc ast collection step to use a HIR visitor, which more thoroughly walks the HIR tree. that means that we're going to encounter inner items (incl return-position impl traits and async fn opaque futures) that are not possible to document.
FIxes (but does not close due to being a beta regression) #109931
r? `@GuillaumeGomez`
rustdoc: escape GAT args in more cases
Fixes#109488.
Previously we printed the *un*escaped form of GAT arguments not only when `f.alternate()` was true but *also* when we failed to compute the URL of the trait associated with the type projection, i.e. when `href(…)` returned an `Err(_)`.
In this PR the argument printing logic is entirely separate from the link resolution code above as it should be.
Further, we now only try to compute the URL if the HTML format was requested with `!f.alternate()`. Before, we would sometimes compute the `href` only to throw it away later.
compiletest: Don't allow tests with overlapping prefix names
Some tests will delete their output directory before starting. The output directory is based on the test names. If one test is the prefix of another test, then when that test starts, it could try to delete the output directory of the other test with the longer path, or otherwise clash with it while the two tests are trying to create/delete/modify the same directory.
In practice, this manifested as a random error on macOS where two tests were trying to create/delete/create `rustdoc/primitive` and `rustdoc/primitive/no_std`, which resulted in an EINVAL (InvalidInput) error.
This renames some of the offending tests, adds `compiletest-ignore-dir` to prevent compiletest from processing some files, and adds a check to prevent this from happening in the future.
Fixes#109397
rustdoc: Don't strip crate module
Until we decide something for https://github.com/rust-lang/rust/issues/109695, rustdoc won't crash anymore because the crate folder doesn't exist.
r? `@notriddle`
rustdoc: Unsupport importing `doc(primitive)` and `doc(keyword)` modules
These are internal features used for a specific purpose, and modules without imports are enough for that purpose.
Some tests will delete their output directory before starting.
The output directory is based on the test names.
If one test is the prefix of another test, then when that test
starts, it could try to delete the output directory of the other
test with the longer path.
rustdoc: Remove footnote references from doc summary
Since it's one line, we don't have the footnote definition so it doesn't make sense to have the reference.
Part of https://github.com/rust-lang/rust/issues/109024.
r? `@notriddle`
rustdoc: Fix missing private inlining
Fixes https://github.com/rust-lang/rust/issues/109258.
If the item isn't inlined, it shouldn't have been added into `view_item_stack`. The problem here was that it was not removed, preventing sub items to be inlined if they have a re-export in "upper levels".
cc `@epage`
r? `@notriddle`