Skip to content

[EXPERIMENT] rustdoc: Cleanup external_traits #90365

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

Closed
wants to merge 4 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
13 changes: 4 additions & 9 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,16 +674,11 @@ crate fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
return;
}

{
if cx.external_traits.borrow().contains_key(&did) || cx.active_extern_traits.contains(&did)
{
return;
}
if cx.external_traits.contains_key(&did) || cx.active_extern_traits.contains(&did) {
return;
}

{
cx.active_extern_traits.insert(did);
}
cx.active_extern_traits.insert(did);

debug!("record_extern_trait: {:?}", did);
let trait_ = build_external_trait(cx, did);
Expand All @@ -692,6 +687,6 @@ crate fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
trait_,
is_notable: clean::utils::has_doc_flag(cx.tcx.get_attrs(did), sym::notable_trait),
};
cx.external_traits.borrow_mut().insert(did, trait_);
cx.external_traits.insert(did, trait_);
cx.active_extern_traits.remove(&did);
}
4 changes: 0 additions & 4 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::cell::RefCell;
use std::default::Default;
use std::hash::{Hash, Hasher};
use std::iter::FromIterator;
use std::lazy::SyncOnceCell as OnceCell;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::Arc;
use std::{slice, vec};

Expand Down Expand Up @@ -120,8 +118,6 @@ crate struct Crate {
crate module: Item,
crate externs: Vec<ExternalCrate>,
crate primitives: ThinVec<(DefId, PrimitiveType)>,
/// Only here so that they can be filtered through the rustdoc passes.
crate external_traits: Rc<RefCell<FxHashMap<DefId, TraitWithExtraInfo>>>,
crate collapsed: bool,
}

Expand Down
10 changes: 1 addition & 9 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,7 @@ crate fn krate(cx: &mut DocContext<'_>) -> Crate {
}));
}

Crate {
name,
src,
module,
externs,
primitives,
external_traits: cx.external_traits.clone(),
collapsed: false,
}
Crate { name, src, module, externs, primitives, collapsed: false }
}

fn external_generic_args(
Expand Down
10 changes: 6 additions & 4 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ crate struct DocContext<'tcx> {
///
/// Most of this logic is copied from rustc_lint::late.
crate param_env: ParamEnv<'tcx>,
/// Later on moved through `clean::Crate` into `cache`
crate external_traits: Rc<RefCell<FxHashMap<DefId, clean::TraitWithExtraInfo>>>,
/// Traits defined outside the current crate.
///
/// Later on moved into `cache`.
crate external_traits: FxHashMap<DefId, clean::TraitWithExtraInfo>,
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
/// the same time.
crate active_extern_traits: FxHashSet<DefId>,
Expand Down Expand Up @@ -375,7 +377,6 @@ crate fn run_global_ctxt(
let mut sized_trait = build_external_trait(&mut ctxt, sized_trait_did);
sized_trait.is_auto = true;
ctxt.external_traits
.borrow_mut()
.insert(sized_trait_did, TraitWithExtraInfo { trait_: sized_trait, is_notable: false });
}

Expand Down Expand Up @@ -494,7 +495,8 @@ crate fn run_global_ctxt(

let render_options = ctxt.render_options;
let mut cache = ctxt.cache;
krate = tcx.sess.time("create_format_cache", || cache.populate(krate, tcx, &render_options));
cache.traits = ctxt.external_traits;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this implies ctxt.cache and ctxt.external_traits don't need to be separate fields, right? Could we store them in the cache to start so it's less stateful? Right now this depends on the field being assigned just before populate and nowhere else for correctness.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try that.

krate = tcx.sess.time("create_format_cache", || cache.populate(tcx, krate, &render_options));

// The main crate doc comments are always collapsed.
krate.collapsed = true;
Expand Down
9 changes: 0 additions & 9 deletions src/librustdoc/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,6 @@ crate trait DocFolder: Sized {

fn fold_crate(&mut self, mut c: Crate) -> Crate {
c.module = self.fold_item(c.module).unwrap();

{
let external_traits = { std::mem::take(&mut *c.external_traits.borrow_mut()) };
for (k, mut v) in external_traits {
v.trait_.items =
v.trait_.items.into_iter().filter_map(|i| self.fold_item(i)).collect();
c.external_traits.borrow_mut().insert(k, v);
}
}
Comment on lines -89 to -96
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the only part of the change that could break something. Locally, 2 tests failed, but with strange found crate std compiled by an incompatible version of rustc errors that may be unrelated. We'll see if CI passes or not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this altogether does not seem right, you're no longer running any passes on associated trait items at all. Try keeping the same logic from before but just removing the borrow_mut().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll try that. If it works, it'll at least be incremental progress over the current situation :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, just removing the borrow_mut won't work. The main purpose of this change is to stop storing external_traits in Crate, and thus allow it to not be a Rc<RefCell<...>>. What change are you suggesting?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have access to a DocContext from within this method? If not, you can change DocFolder to add a method returning a reference to it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see what you mean. That's an interesting idea, I'll see if that's possible.

c
}
}
3 changes: 1 addition & 2 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,12 @@ impl Cache {
/// in `krate` due to the data being moved into the `Cache`.
crate fn populate(
&mut self,
mut krate: clean::Crate,
tcx: TyCtxt<'_>,
mut krate: clean::Crate,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: there's no need to reorder these parameters

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, though generally we pass tcx as the first argument. I can revert this change if you'd prefer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't spotted much of a pattern for this; I would rather keep the old order to reduce churn.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll change it back then.

render_options: &RenderOptions,
) -> clean::Crate {
// Crawl the crate to build various caches used for the output
debug!(?self.crate_version);
self.traits = krate.external_traits.take();
let RenderOptions { extern_html_root_takes_precedence, output: dst, .. } = render_options;

// Cache where all our extern crates are located
Expand Down