rustdoc: heavily simplify synthesis of auto trait impls
This commit is contained in:
parent
cbd593ed18
commit
069e7f2a76
@ -25,8 +25,8 @@ pub enum RegionTarget<'tcx> {
|
||||
|
||||
#[derive(Default, Debug, Clone)]
|
||||
pub struct RegionDeps<'tcx> {
|
||||
larger: FxIndexSet<RegionTarget<'tcx>>,
|
||||
smaller: FxIndexSet<RegionTarget<'tcx>>,
|
||||
pub larger: FxIndexSet<RegionTarget<'tcx>>,
|
||||
pub smaller: FxIndexSet<RegionTarget<'tcx>>,
|
||||
}
|
||||
|
||||
pub enum AutoTraitResult<A> {
|
||||
@ -81,19 +81,12 @@ impl<'tcx> AutoTraitFinder<'tcx> {
|
||||
|
||||
let infcx = tcx.infer_ctxt().build();
|
||||
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(
|
||||
tcx,
|
||||
ObligationCause::dummy(),
|
||||
orig_env,
|
||||
ty::TraitPredicate {
|
||||
trait_ref,
|
||||
polarity: if polarity {
|
||||
ty::PredicatePolarity::Positive
|
||||
} else {
|
||||
ty::PredicatePolarity::Negative
|
||||
},
|
||||
},
|
||||
ty::TraitPredicate { trait_ref, polarity },
|
||||
));
|
||||
if let Ok(Some(ImplSource::UserDefined(_))) = result {
|
||||
debug!(
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -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::PredicateOrigin;
|
||||
use rustc_hir_analysis::lower_ty;
|
||||
use rustc_infer::infer::region_constraints::{Constraint, RegionConstraintData};
|
||||
use rustc_middle::metadata::Reexport;
|
||||
use rustc_middle::middle::resolve_bound_vars as rbv;
|
||||
use rustc_middle::ty::fold::TypeFolder;
|
||||
use rustc_middle::ty::GenericArgsRef;
|
||||
use rustc_middle::ty::TypeVisitableExt;
|
||||
use rustc_middle::ty::{self, AdtKind, Ty, TyCtxt};
|
||||
@ -36,7 +34,6 @@ use rustc_trait_selection::traits::wf::object_region_bounds;
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::collections::BTreeMap;
|
||||
use std::hash::Hash;
|
||||
use std::mem;
|
||||
use thin_vec::ThinVec;
|
||||
|
||||
@ -501,6 +498,7 @@ fn projection_to_path_segment<'tcx>(
|
||||
|
||||
fn clean_generic_param_def<'tcx>(
|
||||
def: &ty::GenericParamDef,
|
||||
defaults: ParamDefaults,
|
||||
cx: &mut DocContext<'tcx>,
|
||||
) -> GenericParamDef {
|
||||
let (name, kind) = match def.kind {
|
||||
@ -508,7 +506,9 @@ fn clean_generic_param_def<'tcx>(
|
||||
(def.name, GenericParamDefKind::Lifetime { outlives: ThinVec::new() })
|
||||
}
|
||||
ty::GenericParamDefKind::Type { has_default, synthetic, .. } => {
|
||||
let default = if has_default {
|
||||
let default = if let ParamDefaults::Yes = defaults
|
||||
&& has_default
|
||||
{
|
||||
Some(clean_middle_ty(
|
||||
ty::Binder::dummy(cx.tcx.type_of(def.def_id).instantiate_identity()),
|
||||
cx,
|
||||
@ -541,11 +541,14 @@ fn clean_generic_param_def<'tcx>(
|
||||
Some(def.def_id),
|
||||
None,
|
||||
)),
|
||||
default: match has_default {
|
||||
true => Some(Box::new(
|
||||
default: if let ParamDefaults::Yes = defaults
|
||||
&& has_default
|
||||
{
|
||||
Some(Box::new(
|
||||
cx.tcx.const_param_default(def.def_id).instantiate_identity().to_string(),
|
||||
)),
|
||||
false => None,
|
||||
))
|
||||
} else {
|
||||
None
|
||||
},
|
||||
is_host_effect,
|
||||
},
|
||||
@ -555,6 +558,12 @@ fn clean_generic_param_def<'tcx>(
|
||||
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>(
|
||||
cx: &mut DocContext<'tcx>,
|
||||
generics: Option<&hir::Generics<'tcx>>,
|
||||
@ -758,34 +767,30 @@ fn clean_ty_generics<'tcx>(
|
||||
gens: &ty::Generics,
|
||||
preds: ty::GenericPredicates<'tcx>,
|
||||
) -> Generics {
|
||||
// Don't populate `cx.impl_trait_bounds` before `clean`ning `where` clauses,
|
||||
// since `Clean for ty::Predicate` would consume them.
|
||||
// Don't populate `cx.impl_trait_bounds` before cleaning where clauses,
|
||||
// since `clean_predicate` would consume them.
|
||||
let mut impl_trait = BTreeMap::<u32, Vec<GenericBound>>::default();
|
||||
|
||||
// Bounds in the type_params and lifetimes fields are repeated in the
|
||||
// predicates field (see rustc_hir_analysis::collect::ty_generics), so remove
|
||||
// them.
|
||||
let stripped_params = gens
|
||||
let params: ThinVec<_> = gens
|
||||
.params
|
||||
.iter()
|
||||
.filter_map(|param| match param.kind {
|
||||
ty::GenericParamDefKind::Lifetime if param.is_anonymous_lifetime() => None,
|
||||
ty::GenericParamDefKind::Lifetime => Some(clean_generic_param_def(param, cx)),
|
||||
.filter(|param| match param.kind {
|
||||
ty::GenericParamDefKind::Lifetime => !param.is_anonymous_lifetime(),
|
||||
ty::GenericParamDefKind::Type { synthetic, .. } => {
|
||||
if param.name == kw::SelfUpper {
|
||||
assert_eq!(param.index, 0);
|
||||
return None;
|
||||
debug_assert_eq!(param.index, 0);
|
||||
return false;
|
||||
}
|
||||
if synthetic {
|
||||
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 { .. } => Some(clean_generic_param_def(param, cx)),
|
||||
ty::GenericParamDefKind::Const { is_host_effect, .. } => !is_host_effect,
|
||||
})
|
||||
.collect::<ThinVec<GenericParamDef>>();
|
||||
.map(|param| clean_generic_param_def(param, ParamDefaults::Yes, cx))
|
||||
.collect();
|
||||
|
||||
// param index -> [(trait DefId, associated type name & generics, term)]
|
||||
let mut impl_trait_proj =
|
||||
@ -881,56 +886,13 @@ fn clean_ty_generics<'tcx>(
|
||||
|
||||
// Now that `cx.impl_trait_bounds` is populated, we can process
|
||||
// remaining predicates which could contain `impl Trait`.
|
||||
let mut where_predicates =
|
||||
where_predicates.into_iter().flat_map(|p| clean_predicate(*p, cx)).collect::<Vec<_>>();
|
||||
let where_predicates =
|
||||
where_predicates.into_iter().flat_map(|p| clean_predicate(*p, cx)).collect();
|
||||
|
||||
// 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'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),
|
||||
}
|
||||
let mut generics = Generics { params, where_predicates };
|
||||
simplify::sized_bounds(cx, &mut generics);
|
||||
generics.where_predicates = simplify::where_clauses(cx, generics.where_predicates);
|
||||
generics
|
||||
}
|
||||
|
||||
fn clean_ty_alias_inner_type<'tcx>(
|
||||
|
@ -12,6 +12,7 @@
|
||||
//! bounds by special casing scenarios such as these. Fun!
|
||||
|
||||
use rustc_data_structures::fx::FxIndexMap;
|
||||
use rustc_data_structures::unord::UnordSet;
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_middle::ty;
|
||||
use thin_vec::ThinVec;
|
||||
@ -21,7 +22,7 @@ use crate::clean::GenericArgs as PP;
|
||||
use crate::clean::WherePredicate as WP;
|
||||
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.
|
||||
//
|
||||
// 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_))
|
||||
}
|
||||
|
||||
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(¶m.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
|
||||
/// the respective parameter.
|
||||
///
|
||||
|
@ -1277,13 +1277,6 @@ impl GenericBound {
|
||||
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> {
|
||||
if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, _) = *self {
|
||||
Some(trait_.clone())
|
||||
|
@ -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::render_macro_matchers::render_macro_matcher;
|
||||
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 {
|
||||
let segments = match *p {
|
||||
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(
|
||||
cx: &mut DocContext<'_>,
|
||||
item_def_id: DefId,
|
||||
@ -493,8 +485,8 @@ pub(crate) fn get_auto_trait_and_blanket_impls(
|
||||
let auto_impls = cx
|
||||
.sess()
|
||||
.prof
|
||||
.generic_activity("get_auto_trait_impls")
|
||||
.run(|| AutoTraitFinder::new(cx).get_auto_trait_impls(item_def_id));
|
||||
.generic_activity("synthesize_auto_trait_impls")
|
||||
.run(|| synthesize_auto_trait_impls(cx, item_def_id));
|
||||
let blanket_impls = cx
|
||||
.sess()
|
||||
.prof
|
||||
|
21
tests/rustdoc/synthetic_auto/bounds.rs
Normal file
21
tests/rustdoc/synthetic_auto/bounds.rs
Normal 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;
|
||||
}
|
@ -21,8 +21,8 @@ mod foo {
|
||||
|
||||
// @has complex/struct.NotOuter.html
|
||||
// @has - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]//h3[@class="code-header"]' \
|
||||
// "impl<'a, T, K: ?Sized> Send for Outer<'a, T, K>where 'a: 'static, T: MyTrait<'a>, \
|
||||
// K: for<'b> Fn((&'b bool, &'a u8)) -> &'b i8, <T as MyTrait<'a>>::MyItem: Copy,"
|
||||
// "impl<'a, T, K> Send for Outer<'a, T, K>where 'a: 'static, T: MyTrait<'a>, \
|
||||
// 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};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user