Auto merge of #53162 - QuietMisdreavus:crouching-impl-hidden-trait, r=GuillaumeGomez
rustdoc: collect trait impls as an early pass Fixes https://github.com/rust-lang/rust/issues/52545, fixes https://github.com/rust-lang/rust/issues/41480, fixes https://github.com/rust-lang/rust/issues/36922 Right now, rustdoc pulls all its impl information by scanning a crate's HIR for any items it finds. However, it doesn't recurse into anything other than modules, preventing it from seeing trait impls that may be inside things like functions or consts. Thanks to https://github.com/rust-lang/rust/pull/53002, now these items actually *exist* for rustdoc to see, but they still weren't getting collected for display. But there was a secret. Whenever we pull in an item from another crate, we don't have any of its impls in the local HIR, so instead we ask the compiler for *everything* and filter out after the fact. This process is only triggered if there's a cross-crate re-export in the crate being documented, which can sometimes leave this info out of the docs. This PR instead moves this collection into an early pass, which occurs immediately after crate cleaning, so that that collection occurs regardless. In addition, by including the HIR's own `trait_impls` in addition to the existing `all_trait_implementations` calls, we can collect all these tricky trait impls without having to scan for them!
This commit is contained in:
commit
3bc2ca7e4f
25 changed files with 643 additions and 221 deletions
|
|
@ -8,6 +8,8 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// ignore-stage1
|
||||
|
||||
// Issue #52129: ICE when trying to document the `quote` proc-macro from proc_macro
|
||||
|
||||
// As of this writing, we don't currently attempt to document proc-macros. However, we shouldn't
|
||||
|
|
|
|||
23
src/test/rustdoc/inline_cross/auxiliary/trait-vis.rs
Normal file
23
src/test/rustdoc/inline_cross/auxiliary/trait-vis.rs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
#![crate_name = "inner"]
|
||||
|
||||
pub struct SomeStruct;
|
||||
|
||||
fn asdf() {
|
||||
const _FOO: () = {
|
||||
impl Clone for SomeStruct {
|
||||
fn clone(&self) -> Self {
|
||||
SomeStruct
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
17
src/test/rustdoc/inline_cross/trait-vis.rs
Normal file
17
src/test/rustdoc/inline_cross/trait-vis.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// aux-build:trait-vis.rs
|
||||
|
||||
extern crate inner;
|
||||
|
||||
// @has trait_vis/struct.SomeStruct.html
|
||||
// @has - '//code' 'impl Clone for SomeStruct'
|
||||
pub use inner::SomeStruct;
|
||||
28
src/test/rustdoc/inline_local/trait-vis.rs
Normal file
28
src/test/rustdoc/inline_local/trait-vis.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
pub trait ThisTrait {}
|
||||
|
||||
mod asdf {
|
||||
use ThisTrait;
|
||||
|
||||
pub struct SomeStruct;
|
||||
|
||||
impl ThisTrait for SomeStruct {}
|
||||
|
||||
trait PrivateTrait {}
|
||||
|
||||
impl PrivateTrait for SomeStruct {}
|
||||
}
|
||||
|
||||
// @has trait_vis/struct.SomeStruct.html
|
||||
// @has - '//code' 'impl ThisTrait for SomeStruct'
|
||||
// !@has - '//code' 'impl PrivateTrait for SomeStruct'
|
||||
pub use asdf::SomeStruct;
|
||||
40
src/test/rustdoc/intra-link-in-bodies.rs
Normal file
40
src/test/rustdoc/intra-link-in-bodies.rs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// we need to make sure that intra-doc links on trait impls get resolved in the right scope
|
||||
|
||||
#![deny(intra_doc_link_resolution_failure)]
|
||||
|
||||
pub mod inner {
|
||||
pub struct SomethingOutOfScope;
|
||||
}
|
||||
|
||||
pub mod other {
|
||||
use inner::SomethingOutOfScope;
|
||||
use SomeTrait;
|
||||
|
||||
pub struct OtherStruct;
|
||||
|
||||
/// Let's link to [SomethingOutOfScope] while we're at it.
|
||||
impl SomeTrait for OtherStruct {}
|
||||
}
|
||||
|
||||
pub trait SomeTrait {}
|
||||
|
||||
pub struct SomeStruct;
|
||||
|
||||
fn __implementation_details() {
|
||||
use inner::SomethingOutOfScope;
|
||||
|
||||
// FIXME: intra-links resolve in their nearest module scope, not their actual scope in cases
|
||||
// like this
|
||||
// Let's link to [SomethingOutOfScope] while we're at it.
|
||||
impl SomeTrait for SomeStruct {}
|
||||
}
|
||||
|
|
@ -10,9 +10,6 @@
|
|||
|
||||
#![crate_name = "foo"]
|
||||
|
||||
// we need to reexport something from libstd so that `all_trait_implementations` is called.
|
||||
pub use std::string::String;
|
||||
|
||||
include!("primitive/primitive-generic-impl.rs");
|
||||
|
||||
// @has foo/primitive.i32.html '//h3[@id="impl-ToString"]//code' 'impl<T> ToString for T'
|
||||
|
|
|
|||
23
src/test/rustdoc/traits-in-bodies-private.rs
Normal file
23
src/test/rustdoc/traits-in-bodies-private.rs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// when implementing the fix for traits-in-bodies, there was an ICE when documenting private items
|
||||
// and a trait was defined in non-module scope
|
||||
|
||||
// compile-flags:--document-private-items
|
||||
|
||||
// @has traits_in_bodies_private/struct.SomeStruct.html
|
||||
// @!has - '//code' 'impl HiddenTrait for SomeStruct'
|
||||
pub struct SomeStruct;
|
||||
|
||||
fn __implementation_details() {
|
||||
trait HiddenTrait {}
|
||||
impl HiddenTrait for SomeStruct {}
|
||||
}
|
||||
|
|
@ -11,11 +11,10 @@
|
|||
//prior to fixing `everybody_loops` to preserve items, rustdoc would crash on this file, as it
|
||||
//didn't see that `SomeStruct` implemented `Clone`
|
||||
|
||||
//FIXME(misdreavus): whenever rustdoc shows traits impl'd inside bodies, make sure this test
|
||||
//reflects that
|
||||
|
||||
pub struct Bounded<T: Clone>(T);
|
||||
|
||||
// @has traits_in_bodies/struct.SomeStruct.html
|
||||
// @has - '//code' 'impl Clone for SomeStruct'
|
||||
pub struct SomeStruct;
|
||||
|
||||
fn asdf() -> Bounded<SomeStruct> {
|
||||
|
|
@ -27,3 +26,37 @@ fn asdf() -> Bounded<SomeStruct> {
|
|||
|
||||
Bounded(SomeStruct)
|
||||
}
|
||||
|
||||
// @has traits_in_bodies/struct.Point.html
|
||||
// @has - '//code' 'impl Copy for Point'
|
||||
#[derive(Clone)]
|
||||
pub struct Point {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
|
||||
const _FOO: () = {
|
||||
impl Copy for Point {}
|
||||
()
|
||||
};
|
||||
|
||||
// @has traits_in_bodies/struct.Inception.html
|
||||
// @has - '//code' 'impl Clone for Inception'
|
||||
pub struct Inception;
|
||||
|
||||
static _BAR: usize = {
|
||||
trait HiddenTrait {
|
||||
fn hidden_fn(&self) {
|
||||
for _ in 0..5 {
|
||||
impl Clone for Inception {
|
||||
fn clone(&self) -> Self {
|
||||
// we need to go deeper
|
||||
Inception
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
5
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue