Auto merge of #123340 - fmease:rustdoc-simplify-auto-trait-impl-synth, r=GuillaumeGomez

rustdoc: heavily simplify the synthesis of auto trait impls

`gd --numstat HEAD~2 HEAD src/librustdoc/clean/auto_trait.rs`
**+315 -705** 🟩🟥🟥🟥

---

As outlined in issue #113015, there are currently 3[^1] large separate routines that “clean” `rustc_middle::ty` data types related to generics & predicates to rustdoc data types. Every single one has their own kinds of bugs. While I've patched a lot of bugs in each of the routines in the past, it's about time to unify them. This PR is only the first in a series. It completely **yanks** the custom “bounds cleaning” of mod `auto_trait` and reuses the routines found in mod `simplify`. As alluded to, `simplify` is also flawed but it's still more complete than `auto_trait`'s routines. [See also my review comment over at `tests/rustdoc/synthetic_auto/bounds.rs`](https://github.com/rust-lang/rust/pull/123340#discussion_r1546900539).

This is preparatory work for rewriting “bounds cleaning” from scratch in follow-up PRs in order to finally [fix] #113015.

Apart from that, I've eliminated all potential sources of *instability* in the rendered output.
See also #119597. I'm pretty sure this fixes #119597.

This PR does not attempt to fix [any other issues related to synthetic auto trait impls](https://github.com/rust-lang/rust/issues?q=is%3Aissue+is%3Aopen+label%3AA-synthetic-impls%20label%3AA-auto-traits).
However, it's definitely meant to be a *stepping stone* by making `auto_trait` more contributor-friendly.

---

* Replace `FxHash{Map,Set}` with `FxIndex{Map,Set}` to guarantee a stable iteration order
  * Or as a perf opt, `UnordSet` (a thin wrapper around `FxHashSet`) in cases where we never iterate over the set.
  * Yes, we do make use of `swap_remove` but that shouldn't matter since all the callers are deterministic. It does make the output less “predictable” but it's still better than before. Ofc, I rely on `rustc_infer` being deterministic. I hope that holds.
* Utilizing `clean::simplify` over the custom “bounds cleaning” routines wipes out the last reference to `collect_referenced_late_bound_regions` in rustdoc (`simplify` uses `bound_vars`) which was a source of instability / unpredictability (cc #116388)
* Remove the types `RegionTarget` and `RegionDeps` from `librustdoc`. They were duplicates of the identical types found in `rustc`. Just import them from `rustc`. For some reason, they were duplicated when splitting `auto_trait` in two in #49711.
* Get rid of the useless “type namespace” `AutoTraitFinder` in `librustdoc`
  * The struct only held a `DocContext`, it was over-engineered
  * Turn the associated functions into free ones
    * Eliminates rightward drift; increases legibility
  * `rustc` also contains a useless `AutoTraitFinder` struct but I plan on removing that in a follow-up PR
* Rename a bunch of methods to be way more descriptive
* Eliminate `use super::*;`
  * Lead to `clean/mod.rs` accumulating a lot of unnecessary imports
  * Made `auto_traits` less modular
* Eliminate a custom `TypeFolder`: We can just use the rustc helper `fold_regions` which does that for us

I plan on adding extensive documentation to `librustdoc`'s `auto_trait` in follow-up PRs.
I don't want to do that in this PR because further refactoring & bug fix PRs may alter the overall structure of `librustdoc`'s & `rustc`'s `auto_trait` modules to a great degree. I'm slowly digging into the dark details of `rustc`'s `auto_trait` module again and once I have the full picture I will be able to provide proper docs.

---

While this PR does indeed touch `rustc`'s `auto_trait` — mostly tiny refactorings — I argue this PR doesn't need any compiler reviewers next to rustdoc ones since that module falls under the purview of rustdoc — it used to be part of `librustdoc` after all (#49711).

Sorry for not having split this into more commits. If you'd like me to I can try to split it into more atomic commits retroactively. However, I don't know if that would actually make reviewing easier. I think the best way to review this might just be to place the master version of `auto_trait` on the left of your screen and the patched one on the right, not joking.

r? `@GuillaumeGomez`

[^1]: Or even 4 depending on the way you're counting.
This commit is contained in:
bors 2024-04-02 12:13:44 +00:00
commit 5dbaafdb93
11 changed files with 479 additions and 876 deletions

View File

@ -6,13 +6,13 @@ use super::*;
use crate::errors::UnableToConstructConstantValue; use crate::errors::UnableToConstructConstantValue;
use crate::infer::region_constraints::{Constraint, RegionConstraintData}; use crate::infer::region_constraints::{Constraint, RegionConstraintData};
use crate::traits::project::ProjectAndUnifyResult; use crate::traits::project::ProjectAndUnifyResult;
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet, IndexEntry};
use rustc_data_structures::unord::UnordSet;
use rustc_infer::infer::DefineOpaqueTypes; use rustc_infer::infer::DefineOpaqueTypes;
use rustc_middle::mir::interpret::ErrorHandled; use rustc_middle::mir::interpret::ErrorHandled;
use rustc_middle::ty::{Region, RegionVid}; use rustc_middle::ty::{Region, RegionVid};
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
use std::collections::hash_map::Entry;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::iter; use std::iter;
@ -25,8 +25,8 @@ pub enum RegionTarget<'tcx> {
#[derive(Default, Debug, Clone)] #[derive(Default, Debug, Clone)]
pub struct RegionDeps<'tcx> { pub struct RegionDeps<'tcx> {
larger: FxIndexSet<RegionTarget<'tcx>>, pub larger: FxIndexSet<RegionTarget<'tcx>>,
smaller: FxIndexSet<RegionTarget<'tcx>>, pub smaller: FxIndexSet<RegionTarget<'tcx>>,
} }
pub enum AutoTraitResult<A> { pub enum AutoTraitResult<A> {
@ -35,17 +35,10 @@ pub enum AutoTraitResult<A> {
NegativeImpl, NegativeImpl,
} }
#[allow(dead_code)]
impl<A> AutoTraitResult<A> {
fn is_auto(&self) -> bool {
matches!(self, AutoTraitResult::PositiveImpl(_) | AutoTraitResult::NegativeImpl)
}
}
pub struct AutoTraitInfo<'cx> { pub struct AutoTraitInfo<'cx> {
pub full_user_env: ty::ParamEnv<'cx>, pub full_user_env: ty::ParamEnv<'cx>,
pub region_data: RegionConstraintData<'cx>, pub region_data: RegionConstraintData<'cx>,
pub vid_to_region: FxHashMap<ty::RegionVid, ty::Region<'cx>>, pub vid_to_region: FxIndexMap<ty::RegionVid, ty::Region<'cx>>,
} }
pub struct AutoTraitFinder<'tcx> { pub struct AutoTraitFinder<'tcx> {
@ -88,19 +81,12 @@ impl<'tcx> AutoTraitFinder<'tcx> {
let infcx = tcx.infer_ctxt().build(); let infcx = tcx.infer_ctxt().build();
let mut selcx = SelectionContext::new(&infcx); let mut selcx = SelectionContext::new(&infcx);
for polarity in [true, false] { for polarity in [ty::PredicatePolarity::Positive, ty::PredicatePolarity::Negative] {
let result = selcx.select(&Obligation::new( let result = selcx.select(&Obligation::new(
tcx, tcx,
ObligationCause::dummy(), ObligationCause::dummy(),
orig_env, orig_env,
ty::TraitPredicate { ty::TraitPredicate { trait_ref, polarity },
trait_ref,
polarity: if polarity {
ty::PredicatePolarity::Positive
} else {
ty::PredicatePolarity::Negative
},
},
)); ));
if let Ok(Some(ImplSource::UserDefined(_))) = result { if let Ok(Some(ImplSource::UserDefined(_))) = result {
debug!( debug!(
@ -114,7 +100,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
} }
let infcx = tcx.infer_ctxt().build(); let infcx = tcx.infer_ctxt().build();
let mut fresh_preds = FxHashSet::default(); let mut fresh_preds = FxIndexSet::default();
// Due to the way projections are handled by SelectionContext, we need to run // Due to the way projections are handled by SelectionContext, we need to run
// evaluate_predicates twice: once on the original param env, and once on the result of // evaluate_predicates twice: once on the original param env, and once on the result of
@ -239,7 +225,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
ty: Ty<'tcx>, ty: Ty<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
user_env: ty::ParamEnv<'tcx>, user_env: ty::ParamEnv<'tcx>,
fresh_preds: &mut FxHashSet<ty::Predicate<'tcx>>, fresh_preds: &mut FxIndexSet<ty::Predicate<'tcx>>,
) -> Option<(ty::ParamEnv<'tcx>, ty::ParamEnv<'tcx>)> { ) -> Option<(ty::ParamEnv<'tcx>, ty::ParamEnv<'tcx>)> {
let tcx = infcx.tcx; let tcx = infcx.tcx;
@ -252,7 +238,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
let mut select = SelectionContext::new(infcx); let mut select = SelectionContext::new(infcx);
let mut already_visited = FxHashSet::default(); let mut already_visited = UnordSet::new();
let mut predicates = VecDeque::new(); let mut predicates = VecDeque::new();
predicates.push_back(ty::Binder::dummy(ty::TraitPredicate { predicates.push_back(ty::Binder::dummy(ty::TraitPredicate {
trait_ref: ty::TraitRef::new(infcx.tcx, trait_did, [ty]), trait_ref: ty::TraitRef::new(infcx.tcx, trait_did, [ty]),
@ -473,9 +459,9 @@ impl<'tcx> AutoTraitFinder<'tcx> {
fn map_vid_to_region<'cx>( fn map_vid_to_region<'cx>(
&self, &self,
regions: &RegionConstraintData<'cx>, regions: &RegionConstraintData<'cx>,
) -> FxHashMap<ty::RegionVid, ty::Region<'cx>> { ) -> FxIndexMap<ty::RegionVid, ty::Region<'cx>> {
let mut vid_map: FxHashMap<RegionTarget<'cx>, RegionDeps<'cx>> = FxHashMap::default(); let mut vid_map = FxIndexMap::<RegionTarget<'cx>, RegionDeps<'cx>>::default();
let mut finished_map = FxHashMap::default(); let mut finished_map = FxIndexMap::default();
for (constraint, _) in &regions.constraints { for (constraint, _) in &regions.constraints {
match constraint { match constraint {
@ -513,25 +499,22 @@ impl<'tcx> AutoTraitFinder<'tcx> {
} }
while !vid_map.is_empty() { while !vid_map.is_empty() {
#[allow(rustc::potential_query_instability)] let target = *vid_map.keys().next().unwrap();
let target = *vid_map.keys().next().expect("Keys somehow empty"); let deps = vid_map.swap_remove(&target).unwrap();
let deps = vid_map.remove(&target).expect("Entry somehow missing");
for smaller in deps.smaller.iter() { for smaller in deps.smaller.iter() {
for larger in deps.larger.iter() { for larger in deps.larger.iter() {
match (smaller, larger) { match (smaller, larger) {
(&RegionTarget::Region(_), &RegionTarget::Region(_)) => { (&RegionTarget::Region(_), &RegionTarget::Region(_)) => {
if let Entry::Occupied(v) = vid_map.entry(*smaller) { if let IndexEntry::Occupied(v) = vid_map.entry(*smaller) {
let smaller_deps = v.into_mut(); let smaller_deps = v.into_mut();
smaller_deps.larger.insert(*larger); smaller_deps.larger.insert(*larger);
// FIXME(#120456) - is `swap_remove` correct?
smaller_deps.larger.swap_remove(&target); smaller_deps.larger.swap_remove(&target);
} }
if let Entry::Occupied(v) = vid_map.entry(*larger) { if let IndexEntry::Occupied(v) = vid_map.entry(*larger) {
let larger_deps = v.into_mut(); let larger_deps = v.into_mut();
larger_deps.smaller.insert(*smaller); larger_deps.smaller.insert(*smaller);
// FIXME(#120456) - is `swap_remove` correct?
larger_deps.smaller.swap_remove(&target); larger_deps.smaller.swap_remove(&target);
} }
} }
@ -542,17 +525,15 @@ impl<'tcx> AutoTraitFinder<'tcx> {
// Do nothing; we don't care about regions that are smaller than vids. // Do nothing; we don't care about regions that are smaller than vids.
} }
(&RegionTarget::RegionVid(_), &RegionTarget::RegionVid(_)) => { (&RegionTarget::RegionVid(_), &RegionTarget::RegionVid(_)) => {
if let Entry::Occupied(v) = vid_map.entry(*smaller) { if let IndexEntry::Occupied(v) = vid_map.entry(*smaller) {
let smaller_deps = v.into_mut(); let smaller_deps = v.into_mut();
smaller_deps.larger.insert(*larger); smaller_deps.larger.insert(*larger);
// FIXME(#120456) - is `swap_remove` correct?
smaller_deps.larger.swap_remove(&target); smaller_deps.larger.swap_remove(&target);
} }
if let Entry::Occupied(v) = vid_map.entry(*larger) { if let IndexEntry::Occupied(v) = vid_map.entry(*larger) {
let larger_deps = v.into_mut(); let larger_deps = v.into_mut();
larger_deps.smaller.insert(*smaller); larger_deps.smaller.insert(*smaller);
// FIXME(#120456) - is `swap_remove` correct?
larger_deps.smaller.swap_remove(&target); larger_deps.smaller.swap_remove(&target);
} }
} }
@ -560,6 +541,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
} }
} }
} }
finished_map finished_map
} }
@ -588,7 +570,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
ty: Ty<'_>, ty: Ty<'_>,
nested: impl Iterator<Item = PredicateObligation<'tcx>>, nested: impl Iterator<Item = PredicateObligation<'tcx>>,
computed_preds: &mut FxIndexSet<ty::Predicate<'tcx>>, computed_preds: &mut FxIndexSet<ty::Predicate<'tcx>>,
fresh_preds: &mut FxHashSet<ty::Predicate<'tcx>>, fresh_preds: &mut FxIndexSet<ty::Predicate<'tcx>>,
predicates: &mut VecDeque<ty::PolyTraitPredicate<'tcx>>, predicates: &mut VecDeque<ty::PolyTraitPredicate<'tcx>>,
selcx: &mut SelectionContext<'_, 'tcx>, selcx: &mut SelectionContext<'_, 'tcx>,
) -> bool { ) -> bool {

File diff suppressed because it is too large Load Diff

View File

@ -21,10 +21,8 @@ use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LocalDefId, LOCAL_CRATE}; use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LocalDefId, LOCAL_CRATE};
use rustc_hir::PredicateOrigin; use rustc_hir::PredicateOrigin;
use rustc_hir_analysis::lower_ty; use rustc_hir_analysis::lower_ty;
use rustc_infer::infer::region_constraints::{Constraint, RegionConstraintData};
use rustc_middle::metadata::Reexport; use rustc_middle::metadata::Reexport;
use rustc_middle::middle::resolve_bound_vars as rbv; use rustc_middle::middle::resolve_bound_vars as rbv;
use rustc_middle::ty::fold::TypeFolder;
use rustc_middle::ty::GenericArgsRef; use rustc_middle::ty::GenericArgsRef;
use rustc_middle::ty::TypeVisitableExt; use rustc_middle::ty::TypeVisitableExt;
use rustc_middle::ty::{self, AdtKind, Ty, TyCtxt}; use rustc_middle::ty::{self, AdtKind, Ty, TyCtxt};
@ -35,9 +33,7 @@ use rustc_span::{self, ExpnKind};
use rustc_trait_selection::traits::wf::object_region_bounds; use rustc_trait_selection::traits::wf::object_region_bounds;
use std::borrow::Cow; use std::borrow::Cow;
use std::collections::hash_map::Entry;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::hash::Hash;
use std::mem; use std::mem;
use thin_vec::ThinVec; use thin_vec::ThinVec;
@ -502,6 +498,7 @@ fn projection_to_path_segment<'tcx>(
fn clean_generic_param_def<'tcx>( fn clean_generic_param_def<'tcx>(
def: &ty::GenericParamDef, def: &ty::GenericParamDef,
defaults: ParamDefaults,
cx: &mut DocContext<'tcx>, cx: &mut DocContext<'tcx>,
) -> GenericParamDef { ) -> GenericParamDef {
let (name, kind) = match def.kind { let (name, kind) = match def.kind {
@ -509,7 +506,9 @@ fn clean_generic_param_def<'tcx>(
(def.name, GenericParamDefKind::Lifetime { outlives: ThinVec::new() }) (def.name, GenericParamDefKind::Lifetime { outlives: ThinVec::new() })
} }
ty::GenericParamDefKind::Type { has_default, synthetic, .. } => { ty::GenericParamDefKind::Type { has_default, synthetic, .. } => {
let default = if has_default { let default = if let ParamDefaults::Yes = defaults
&& has_default
{
Some(clean_middle_ty( Some(clean_middle_ty(
ty::Binder::dummy(cx.tcx.type_of(def.def_id).instantiate_identity()), ty::Binder::dummy(cx.tcx.type_of(def.def_id).instantiate_identity()),
cx, cx,
@ -542,11 +541,14 @@ fn clean_generic_param_def<'tcx>(
Some(def.def_id), Some(def.def_id),
None, None,
)), )),
default: match has_default { default: if let ParamDefaults::Yes = defaults
true => Some(Box::new( && has_default
{
Some(Box::new(
cx.tcx.const_param_default(def.def_id).instantiate_identity().to_string(), cx.tcx.const_param_default(def.def_id).instantiate_identity().to_string(),
)), ))
false => None, } else {
None
}, },
is_host_effect, is_host_effect,
}, },
@ -556,6 +558,12 @@ fn clean_generic_param_def<'tcx>(
GenericParamDef { name, def_id: def.def_id, kind } GenericParamDef { name, def_id: def.def_id, kind }
} }
/// Whether to clean generic parameter defaults or not.
enum ParamDefaults {
Yes,
No,
}
fn clean_generic_param<'tcx>( fn clean_generic_param<'tcx>(
cx: &mut DocContext<'tcx>, cx: &mut DocContext<'tcx>,
generics: Option<&hir::Generics<'tcx>>, generics: Option<&hir::Generics<'tcx>>,
@ -759,34 +767,30 @@ fn clean_ty_generics<'tcx>(
gens: &ty::Generics, gens: &ty::Generics,
preds: ty::GenericPredicates<'tcx>, preds: ty::GenericPredicates<'tcx>,
) -> Generics { ) -> Generics {
// Don't populate `cx.impl_trait_bounds` before `clean`ning `where` clauses, // Don't populate `cx.impl_trait_bounds` before cleaning where clauses,
// since `Clean for ty::Predicate` would consume them. // since `clean_predicate` would consume them.
let mut impl_trait = BTreeMap::<u32, Vec<GenericBound>>::default(); let mut impl_trait = BTreeMap::<u32, Vec<GenericBound>>::default();
// Bounds in the type_params and lifetimes fields are repeated in the let params: ThinVec<_> = gens
// predicates field (see rustc_hir_analysis::collect::ty_generics), so remove
// them.
let stripped_params = gens
.params .params
.iter() .iter()
.filter_map(|param| match param.kind { .filter(|param| match param.kind {
ty::GenericParamDefKind::Lifetime if param.is_anonymous_lifetime() => None, ty::GenericParamDefKind::Lifetime => !param.is_anonymous_lifetime(),
ty::GenericParamDefKind::Lifetime => Some(clean_generic_param_def(param, cx)),
ty::GenericParamDefKind::Type { synthetic, .. } => { ty::GenericParamDefKind::Type { synthetic, .. } => {
if param.name == kw::SelfUpper { if param.name == kw::SelfUpper {
assert_eq!(param.index, 0); debug_assert_eq!(param.index, 0);
return None; return false;
} }
if synthetic { if synthetic {
impl_trait.insert(param.index, vec![]); impl_trait.insert(param.index, vec![]);
return None; return false;
} }
Some(clean_generic_param_def(param, cx)) true
} }
ty::GenericParamDefKind::Const { is_host_effect: true, .. } => None, ty::GenericParamDefKind::Const { is_host_effect, .. } => !is_host_effect,
ty::GenericParamDefKind::Const { .. } => Some(clean_generic_param_def(param, cx)),
}) })
.collect::<ThinVec<GenericParamDef>>(); .map(|param| clean_generic_param_def(param, ParamDefaults::Yes, cx))
.collect();
// param index -> [(trait DefId, associated type name & generics, term)] // param index -> [(trait DefId, associated type name & generics, term)]
let mut impl_trait_proj = let mut impl_trait_proj =
@ -882,56 +886,13 @@ fn clean_ty_generics<'tcx>(
// Now that `cx.impl_trait_bounds` is populated, we can process // Now that `cx.impl_trait_bounds` is populated, we can process
// remaining predicates which could contain `impl Trait`. // remaining predicates which could contain `impl Trait`.
let mut where_predicates = let where_predicates =
where_predicates.into_iter().flat_map(|p| clean_predicate(*p, cx)).collect::<Vec<_>>(); where_predicates.into_iter().flat_map(|p| clean_predicate(*p, cx)).collect();
// In the surface language, all type parameters except `Self` have an let mut generics = Generics { params, where_predicates };
// implicit `Sized` bound unless removed with `?Sized`. simplify::sized_bounds(cx, &mut generics);
// However, in the list of where-predicates below, `Sized` appears like a generics.where_predicates = simplify::where_clauses(cx, generics.where_predicates);
// normal bound: It's either present (the type is sized) or generics
// absent (the type might be unsized) but never *maybe* (i.e. `?Sized`).
//
// This is unsuitable for rendering.
// Thus, as a first step remove all `Sized` bounds that should be implicit.
//
// Note that associated types also have an implicit `Sized` bound but we
// don't actually know the set of associated types right here so that's
// handled when cleaning associated types.
let mut sized_params = FxHashSet::default();
where_predicates.retain(|pred| {
if let WherePredicate::BoundPredicate { ty: Generic(g), bounds, .. } = pred
&& *g != kw::SelfUpper
&& bounds.iter().any(|b| b.is_sized_bound(cx))
{
sized_params.insert(*g);
false
} else {
true
}
});
// As a final step, go through the type parameters again and insert a
// `?Sized` bound for each one we didn't find to be `Sized`.
for tp in &stripped_params {
if let types::GenericParamDefKind::Type { .. } = tp.kind
&& !sized_params.contains(&tp.name)
{
where_predicates.push(WherePredicate::BoundPredicate {
ty: Type::Generic(tp.name),
bounds: vec![GenericBound::maybe_sized(cx)],
bound_params: Vec::new(),
})
}
}
// It would be nice to collect all of the bounds on a type and recombine
// them if possible, to avoid e.g., `where T: Foo, T: Bar, T: Sized, T: 'a`
// and instead see `where T: Foo + Bar + Sized + 'a`
Generics {
params: stripped_params,
where_predicates: simplify::where_clauses(cx, where_predicates),
}
} }
fn clean_ty_alias_inner_type<'tcx>( fn clean_ty_alias_inner_type<'tcx>(

View File

@ -12,6 +12,7 @@
//! bounds by special casing scenarios such as these. Fun! //! bounds by special casing scenarios such as these. Fun!
use rustc_data_structures::fx::FxIndexMap; use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::unord::UnordSet;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_middle::ty; use rustc_middle::ty;
use thin_vec::ThinVec; use thin_vec::ThinVec;
@ -21,7 +22,7 @@ use crate::clean::GenericArgs as PP;
use crate::clean::WherePredicate as WP; use crate::clean::WherePredicate as WP;
use crate::core::DocContext; use crate::core::DocContext;
pub(crate) fn where_clauses(cx: &DocContext<'_>, clauses: Vec<WP>) -> ThinVec<WP> { pub(crate) fn where_clauses(cx: &DocContext<'_>, clauses: ThinVec<WP>) -> ThinVec<WP> {
// First, partition the where clause into its separate components. // First, partition the where clause into its separate components.
// //
// We use `FxIndexMap` so that the insertion order is preserved to prevent messing up to // We use `FxIndexMap` so that the insertion order is preserved to prevent messing up to
@ -128,6 +129,48 @@ fn trait_is_same_or_supertrait(cx: &DocContext<'_>, child: DefId, trait_: DefId)
.any(|did| trait_is_same_or_supertrait(cx, did, trait_)) .any(|did| trait_is_same_or_supertrait(cx, did, trait_))
} }
pub(crate) fn sized_bounds(cx: &mut DocContext<'_>, generics: &mut clean::Generics) {
let mut sized_params = UnordSet::new();
// In the surface language, all type parameters except `Self` have an
// implicit `Sized` bound unless removed with `?Sized`.
// However, in the list of where-predicates below, `Sized` appears like a
// normal bound: It's either present (the type is sized) or
// absent (the type might be unsized) but never *maybe* (i.e. `?Sized`).
//
// This is unsuitable for rendering.
// Thus, as a first step remove all `Sized` bounds that should be implicit.
//
// Note that associated types also have an implicit `Sized` bound but we
// don't actually know the set of associated types right here so that
// should be handled when cleaning associated types.
generics.where_predicates.retain(|pred| {
if let WP::BoundPredicate { ty: clean::Generic(param), bounds, .. } = pred
&& *param != rustc_span::symbol::kw::SelfUpper
&& bounds.iter().any(|b| b.is_sized_bound(cx))
{
sized_params.insert(*param);
false
} else {
true
}
});
// As a final step, go through the type parameters again and insert a
// `?Sized` bound for each one we didn't find to be `Sized`.
for param in &generics.params {
if let clean::GenericParamDefKind::Type { .. } = param.kind
&& !sized_params.contains(&param.name)
{
generics.where_predicates.push(WP::BoundPredicate {
ty: clean::Type::Generic(param.name),
bounds: vec![clean::GenericBound::maybe_sized(cx)],
bound_params: Vec::new(),
})
}
}
}
/// Move bounds that are (likely) directly attached to generic parameters from the where-clause to /// Move bounds that are (likely) directly attached to generic parameters from the where-clause to
/// the respective parameter. /// the respective parameter.
/// ///

View File

@ -1277,13 +1277,6 @@ impl GenericBound {
false false
} }
pub(crate) fn get_poly_trait(&self) -> Option<PolyTrait> {
if let GenericBound::TraitBound(ref p, _) = *self {
return Some(p.clone());
}
None
}
pub(crate) fn get_trait_path(&self) -> Option<Path> { pub(crate) fn get_trait_path(&self) -> Option<Path> {
if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, _) = *self { if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, _) = *self {
Some(trait_.clone()) Some(trait_.clone())

View File

@ -1,4 +1,4 @@
use crate::clean::auto_trait::AutoTraitFinder; use crate::clean::auto_trait::synthesize_auto_trait_impls;
use crate::clean::blanket_impl::BlanketImplFinder; use crate::clean::blanket_impl::BlanketImplFinder;
use crate::clean::render_macro_matchers::render_macro_matcher; use crate::clean::render_macro_matchers::render_macro_matcher;
use crate::clean::{ use crate::clean::{
@ -251,15 +251,6 @@ pub(super) fn clean_middle_path<'tcx>(
} }
} }
/// Remove the generic arguments from a path.
pub(crate) fn strip_path_generics(mut path: Path) -> Path {
for ps in path.segments.iter_mut() {
ps.args = GenericArgs::AngleBracketed { args: Default::default(), bindings: ThinVec::new() }
}
path
}
pub(crate) fn qpath_to_string(p: &hir::QPath<'_>) -> String { pub(crate) fn qpath_to_string(p: &hir::QPath<'_>) -> String {
let segments = match *p { let segments = match *p {
hir::QPath::Resolved(_, path) => &path.segments, hir::QPath::Resolved(_, path) => &path.segments,
@ -486,6 +477,7 @@ pub(crate) fn resolve_type(cx: &mut DocContext<'_>, path: Path) -> Type {
} }
} }
// FIXME(fmease): Update the `get_*` terminology to the `synthesize_` one.
pub(crate) fn get_auto_trait_and_blanket_impls( pub(crate) fn get_auto_trait_and_blanket_impls(
cx: &mut DocContext<'_>, cx: &mut DocContext<'_>,
item_def_id: DefId, item_def_id: DefId,
@ -493,8 +485,8 @@ pub(crate) fn get_auto_trait_and_blanket_impls(
let auto_impls = cx let auto_impls = cx
.sess() .sess()
.prof .prof
.generic_activity("get_auto_trait_impls") .generic_activity("synthesize_auto_trait_impls")
.run(|| AutoTraitFinder::new(cx).get_auto_trait_impls(item_def_id)); .run(|| synthesize_auto_trait_impls(cx, item_def_id));
let blanket_impls = cx let blanket_impls = cx
.sess() .sess()
.prof .prof

View File

@ -0,0 +1,21 @@
pub struct Outer<T>(Inner<T>);
pub struct Inner<T>(T);
// @has bounds/struct.Outer.html
// @has - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]//h3[@class="code-header"]' \
// "impl<T> Unpin for Outer<T>where \
// T: for<'any> Trait<A = (), B<'any> = (), X = ()>,"
impl<T> std::marker::Unpin for Inner<T>
where
T: for<'any> Trait<A = (), B<'any> = (), X = ()>,
{}
pub trait Trait: SuperTrait {
type A;
type B<'a>;
}
pub trait SuperTrait {
type X;
}

View File

@ -21,8 +21,8 @@ mod foo {
// @has complex/struct.NotOuter.html // @has complex/struct.NotOuter.html
// @has - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]//h3[@class="code-header"]' \ // @has - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]//h3[@class="code-header"]' \
// "impl<'a, T, K: ?Sized> Send for Outer<'a, T, K>where K: for<'b> Fn((&'b bool, &'a u8)) \ // "impl<'a, T, K> Send for Outer<'a, T, K>where 'a: 'static, T: MyTrait<'a>, \
// -> &'b i8, T: MyTrait<'a>, <T as MyTrait<'a>>::MyItem: Copy, 'a: 'static" // K: for<'b> Fn((&'b bool, &'a u8)) -> &'b i8 + ?Sized, <T as MyTrait<'a>>::MyItem: Copy,"
pub use foo::{Foo, Inner as NotInner, MyTrait as NotMyTrait, Outer as NotOuter}; pub use foo::{Foo, Inner as NotInner, MyTrait as NotMyTrait, Outer as NotOuter};

View File

@ -10,7 +10,7 @@ where
// @has lifetimes/struct.Foo.html // @has lifetimes/struct.Foo.html
// @has - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]//h3[@class="code-header"]' \ // @has - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]//h3[@class="code-header"]' \
// "impl<'c, K> Send for Foo<'c, K>where K: for<'b> Fn(&'b bool) -> &'c u8, 'c: 'static" // "impl<'c, K> Send for Foo<'c, K>where 'c: 'static, K: for<'b> Fn(&'b bool) -> &'c u8,"
// //
// @has - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]//h3[@class="code-header"]' \ // @has - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]//h3[@class="code-header"]' \
// "impl<'c, K> Sync for Foo<'c, K>where K: Sync" // "impl<'c, K> Sync for Foo<'c, K>where K: Sync"

View File

@ -1,6 +1,3 @@
// FIXME(fmease, #119216): Reenable this test!
//@ ignore-test
pub struct Inner<T> { pub struct Inner<T> {
field: T, field: T,
} }
@ -13,7 +10,7 @@ where
// @has no_redundancy/struct.Outer.html // @has no_redundancy/struct.Outer.html
// @has - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]//h3[@class="code-header"]' \ // @has - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]//h3[@class="code-header"]' \
// "impl<T> Send for Outer<T>where T: Send + Copy" // "impl<T> Send for Outer<T>where T: Copy + Send"
pub struct Outer<T> { pub struct Outer<T> {
inner_field: Inner<T>, inner_field: Inner<T>,
} }

View File

@ -24,11 +24,11 @@ where
// @has project/struct.Foo.html // @has project/struct.Foo.html
// @has - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]//h3[@class="code-header"]' \ // @has - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]//h3[@class="code-header"]' \
// "impl<'c, K> Send for Foo<'c, K>where K: MyTrait<MyItem = bool>, 'c: 'static" // "impl<'c, K> Send for Foo<'c, K>where 'c: 'static, K: MyTrait<MyItem = bool>,"
// //
// @has - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]//h3[@class="code-header"]' \ // @has - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]//h3[@class="code-header"]' \
// "impl<'c, K> Sync for Foo<'c, K>where K: MyTrait, <K as MyTrait>::MyItem: OtherTrait, \ // "impl<'c, K> Sync for Foo<'c, K>where 'c: 'static, K: MyTrait, \
// 'c: 'static," // <K as MyTrait>::MyItem: OtherTrait,"
pub struct Foo<'c, K: 'c> { pub struct Foo<'c, K: 'c> {
inner_field: Inner<'c, K>, inner_field: Inner<'c, K>,
} }