-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[rustdoc] List matching impls on type aliases #112429
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,12 +53,15 @@ use rustc_data_structures::captures::Captures; | |
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; | ||
use rustc_hir::def_id::{DefId, DefIdSet}; | ||
use rustc_hir::Mutability; | ||
use rustc_infer::infer::TyCtxtInferExt; | ||
use rustc_infer::traits::{Obligation, ObligationCause}; | ||
use rustc_middle::middle::stability; | ||
use rustc_middle::ty::TyCtxt; | ||
use rustc_middle::ty::{ParamEnv, TyCtxt}; | ||
use rustc_span::{ | ||
symbol::{sym, Symbol}, | ||
BytePos, FileName, RealFileName, | ||
}; | ||
use rustc_trait_selection::traits::ObligationCtxt; | ||
use serde::ser::{SerializeMap, SerializeSeq}; | ||
use serde::{Serialize, Serializer}; | ||
|
||
|
@@ -1112,28 +1115,76 @@ fn render_assoc_items<'a, 'cx: 'a>( | |
containing_item: &'a clean::Item, | ||
it: DefId, | ||
what: AssocItemRender<'a>, | ||
aliased_type: Option<DefId>, | ||
) -> impl fmt::Display + 'a + Captures<'cx> { | ||
let mut derefs = DefIdSet::default(); | ||
derefs.insert(it); | ||
display_fn(move |f| { | ||
render_assoc_items_inner(f, cx, containing_item, it, what, &mut derefs); | ||
render_assoc_items_inner(f, cx, containing_item, it, what, &mut derefs, aliased_type); | ||
Ok(()) | ||
}) | ||
} | ||
|
||
/// Check whether `impl_def_id` may apply to *some instantiation* of `item_def_id`. | ||
fn is_valid_impl_for(tcx: TyCtxt<'_>, item_def_id: DefId, impl_def_id: DefId) -> bool { | ||
let infcx = tcx.infer_ctxt().intercrate(true).build(); | ||
let ocx = ObligationCtxt::new(&infcx); | ||
let param_env = ParamEnv::empty(); | ||
|
||
let alias_substs = infcx.fresh_substs_for_item(rustc_span::DUMMY_SP, item_def_id); | ||
let alias_ty = tcx.type_of(item_def_id).subst(tcx, alias_substs); | ||
let alias_bounds = tcx.predicates_of(item_def_id).instantiate(tcx, alias_substs); | ||
|
||
let impl_substs = infcx.fresh_substs_for_item(rustc_span::DUMMY_SP, impl_def_id); | ||
let impl_self_ty = tcx.type_of(impl_def_id).subst(tcx, impl_substs); | ||
let impl_bounds = tcx.predicates_of(impl_def_id).instantiate(tcx, impl_substs); | ||
|
||
if ocx.eq(&ObligationCause::dummy(), param_env, impl_self_ty, alias_ty).is_err() { | ||
return false; | ||
} | ||
ocx.register_obligations( | ||
alias_bounds | ||
.iter() | ||
.chain(impl_bounds) | ||
.map(|(p, _)| Obligation::new(tcx, ObligationCause::dummy(), param_env, p)), | ||
); | ||
|
||
let errors = ocx.select_where_possible(); | ||
errors.is_empty() | ||
} | ||
|
||
// If `aliased_type` is `Some`, it means `it` is a type alias and `aliased_type` is the "actual" | ||
// type aliased behind `it`. It is used to check whether or not the implementation of the aliased | ||
// type can be displayed on the alias doc page. | ||
fn render_assoc_items_inner( | ||
mut w: &mut dyn fmt::Write, | ||
cx: &mut Context<'_>, | ||
containing_item: &clean::Item, | ||
it: DefId, | ||
what: AssocItemRender<'_>, | ||
derefs: &mut DefIdSet, | ||
aliased_type: Option<DefId>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sure threading this parameter through all these function calls is probably cheaper than using But is pulling the information out of TyCtxt expensive enough to be worth having a function with seven parameters? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a variable to tell us that this is an alias. From this, since we already have the EDIT: Just re-read your comment. I'm not sure to like it much simply because we re-compute an information we already have. But that's pretty much my only argument against it. ^^' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I edited my comment, I don't know how but I misread your comment the first time. Sorry about that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's not a lot of computation going into it, though. It's looking information up in a hash table. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not the part worrying me but since I can't put the finger on it, I'll just remove the newly added argument and let's see how it goes. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah found it! I need it because of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense. Thanks for the info! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The doc comment for |
||
) { | ||
info!("Documenting associated items of {:?}", containing_item.name); | ||
let shared = Rc::clone(&cx.shared); | ||
let cache = &shared.cache; | ||
let Some(v) = cache.impls.get(&it) else { return }; | ||
let (non_trait, traits): (Vec<_>, _) = v.iter().partition(|i| i.inner_impl().trait_.is_none()); | ||
let empty = Vec::new(); | ||
let v = match cache.impls.get(&it) { | ||
Some(v) => v, | ||
None => &empty, | ||
}; | ||
let v2 = match aliased_type { | ||
Some(aliased_type) => cache.impls.get(&aliased_type).unwrap_or(&empty), | ||
None => &empty, | ||
}; | ||
if v.is_empty() && v2.is_empty() { | ||
return; | ||
} | ||
let mut saw_impls = FxHashSet::default(); | ||
let (non_trait, traits): (Vec<_>, _) = | ||
v.iter().chain(v2).partition(|i| i.inner_impl().trait_.is_none()); | ||
let tcx = cx.tcx(); | ||
let is_alias = aliased_type.is_some(); | ||
if !non_trait.is_empty() { | ||
let mut tmp_buf = Buffer::html(); | ||
let (render_mode, id, class_html) = match what { | ||
|
@@ -1165,6 +1216,12 @@ fn render_assoc_items_inner( | |
}; | ||
let mut impls_buf = Buffer::html(); | ||
for i in &non_trait { | ||
if !saw_impls.insert(i.def_id()) { | ||
continue; | ||
} | ||
if is_alias && !is_valid_impl_for(tcx, it, i.def_id()) { | ||
continue; | ||
} | ||
render_impl( | ||
&mut impls_buf, | ||
cx, | ||
|
@@ -1193,9 +1250,14 @@ fn render_assoc_items_inner( | |
if !traits.is_empty() { | ||
let deref_impl = | ||
traits.iter().find(|t| t.trait_did() == cx.tcx().lang_items().deref_trait()); | ||
if let Some(impl_) = deref_impl { | ||
if let Some(impl_) = deref_impl && | ||
(!is_alias || is_valid_impl_for(tcx, it, impl_.def_id())) | ||
{ | ||
let has_deref_mut = | ||
traits.iter().any(|t| t.trait_did() == cx.tcx().lang_items().deref_mut_trait()); | ||
traits.iter().any(|t| { | ||
t.trait_did() == cx.tcx().lang_items().deref_mut_trait() && | ||
(!is_alias || is_valid_impl_for(tcx, it, t.def_id())) | ||
}); | ||
render_deref_methods(&mut w, cx, impl_, containing_item, has_deref_mut, derefs); | ||
} | ||
|
||
|
@@ -1205,10 +1267,14 @@ fn render_assoc_items_inner( | |
return; | ||
} | ||
|
||
let (synthetic, concrete): (Vec<&Impl>, Vec<&Impl>) = | ||
traits.into_iter().partition(|t| t.inner_impl().kind.is_auto()); | ||
let (blanket_impl, concrete): (Vec<&Impl>, _) = | ||
concrete.into_iter().partition(|t| t.inner_impl().kind.is_blanket()); | ||
let (synthetic, concrete): (Vec<&Impl>, Vec<&Impl>) = traits | ||
.into_iter() | ||
.filter(|t| saw_impls.insert(t.def_id())) | ||
.partition(|t| t.inner_impl().kind.is_auto()); | ||
let (blanket_impl, concrete): (Vec<&Impl>, _) = concrete | ||
.into_iter() | ||
.filter(|t| !is_alias || is_valid_impl_for(tcx, it, t.def_id())) | ||
.partition(|t| t.inner_impl().kind.is_blanket()); | ||
|
||
render_all_impls(w, cx, containing_item, &concrete, &synthetic, &blanket_impl); | ||
} | ||
|
@@ -1247,10 +1313,10 @@ fn render_deref_methods( | |
return; | ||
} | ||
} | ||
render_assoc_items_inner(&mut w, cx, container_item, did, what, derefs); | ||
render_assoc_items_inner(&mut w, cx, container_item, did, what, derefs, None); | ||
} else if let Some(prim) = target.primitive_type() { | ||
if let Some(&did) = cache.primitive_locations.get(&prim) { | ||
render_assoc_items_inner(&mut w, cx, container_item, did, what, derefs); | ||
render_assoc_items_inner(&mut w, cx, container_item, did, what, derefs, None); | ||
} | ||
} | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.