Skip to content
Open
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
44 changes: 26 additions & 18 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ pub(crate) fn clean_trait_ref_with_constraints<'tcx>(
path
}

fn clean_poly_trait_ref_with_constraints<'tcx>(
pub(crate) fn clean_poly_trait_ref_with_constraints<'tcx>(
cx: &mut DocContext<'tcx>,
poly_trait_ref: ty::PolyTraitRef<'tcx>,
constraints: ThinVec<AssocItemConstraint>,
Expand Down Expand Up @@ -458,7 +458,7 @@ fn clean_type_outlives_predicate<'tcx>(
}
}

fn clean_middle_term<'tcx>(
pub(crate) fn clean_middle_term<'tcx>(
term: ty::Binder<'tcx, ty::Term<'tcx>>,
cx: &mut DocContext<'tcx>,
) -> Term {
Expand Down Expand Up @@ -526,7 +526,7 @@ fn should_fully_qualify_path(self_def_id: Option<DefId>, trait_: &Path, self_typ
.map_or(!self_type.is_self_type(), |(id, trait_)| id != trait_)
}

fn projection_to_path_segment<'tcx>(
pub(crate) fn projection_to_path_segment<'tcx>(
proj: ty::Binder<'tcx, ty::AliasTerm<'tcx>>,
cx: &mut DocContext<'tcx>,
) -> PathSegment {
Expand Down Expand Up @@ -698,8 +698,13 @@ pub(crate) fn clean_generics<'tcx>(
let param = clean_generic_param(cx, Some(gens), param);
match param.kind {
GenericParamDefKind::Lifetime { .. } => unreachable!(),
GenericParamDefKind::Type { ref bounds, .. } => {
cx.impl_trait_bounds.insert(param.def_id.into(), bounds.to_vec());
GenericParamDefKind::Type { ref bounds, ref synthetic, .. } => {
debug_assert!(*synthetic, "non-synthetic generic for impl trait: {param:?}");
let param_def_id = param.def_id;
cx.impl_trait_bounds.insert(
param_def_id.into(),
(bounds.to_vec(), ImplTraitOrigin::Param { def_id: param_def_id }),
);
}
GenericParamDefKind::Const { .. } => unreachable!(),
}
Expand Down Expand Up @@ -819,7 +824,7 @@ fn clean_ty_generics_inner<'tcx>(
) -> Generics {
// 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();
let mut impl_trait = BTreeMap::<u32, (DefId, Vec<GenericBound>)>::default();

let params: ThinVec<_> = gens
.own_params
Expand All @@ -832,7 +837,7 @@ fn clean_ty_generics_inner<'tcx>(
return false;
}
if synthetic {
impl_trait.insert(param.index, vec![]);
impl_trait.insert(param.index, (param.def_id, vec![]));
return false;
}
true
Expand Down Expand Up @@ -873,7 +878,7 @@ fn clean_ty_generics_inner<'tcx>(
};

if let Some(param_idx) = param_idx
&& let Some(bounds) = impl_trait.get_mut(&param_idx)
&& let Some((_, bounds)) = impl_trait.get_mut(&param_idx)
{
let pred = clean_predicate(*pred, cx)?;

Expand All @@ -895,7 +900,7 @@ fn clean_ty_generics_inner<'tcx>(
})
.collect::<Vec<_>>();

for (idx, mut bounds) in impl_trait {
for (idx, (param_def_id, mut bounds)) in impl_trait {
let mut has_sized = false;
bounds.retain(|b| {
if b.is_sized_bound(cx) {
Expand Down Expand Up @@ -929,7 +934,8 @@ fn clean_ty_generics_inner<'tcx>(
}
}

cx.impl_trait_bounds.insert(idx.into(), bounds);
cx.impl_trait_bounds
.insert(idx.into(), (bounds, ImplTraitOrigin::Param { def_id: param_def_id }));
}

// Now that `cx.impl_trait_bounds` is populated, we can process
Expand Down Expand Up @@ -1157,7 +1163,7 @@ fn clean_poly_fn_sig<'tcx>(
// function isn't async without needing to execute the query `asyncness` at
// all which gives us a noticeable performance boost.
if let Some(did) = did
&& let Type::ImplTrait(_) = output
&& let Type::ImplTrait { .. } = output
&& cx.tcx.asyncness(did).is_async()
{
output = output.sugared_async_return_type();
Expand Down Expand Up @@ -1648,8 +1654,8 @@ fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type
if let Some(new_ty) = cx.args.get(&did).and_then(|p| p.as_ty()).cloned() {
return new_ty;
}
if let Some(bounds) = cx.impl_trait_bounds.remove(&did.into()) {
return ImplTrait(bounds);
if let Some((bounds, origin)) = cx.impl_trait_bounds.remove(&did.into()) {
return ImplTrait { bounds, origin };
}
}

Expand Down Expand Up @@ -1838,7 +1844,9 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
}
TyKind::Tup(tys) => Tuple(tys.iter().map(|ty| clean_ty(ty, cx)).collect()),
TyKind::OpaqueDef(ty) => {
ImplTrait(ty.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect())
let bounds =
ty.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect::<Vec<_>>();
ImplTrait { bounds, origin: ImplTraitOrigin::Opaque { def_id: ty.def_id.to_def_id() } }
}
TyKind::Path(_) => clean_qpath(ty, cx),
TyKind::TraitObject(bounds, lifetime) => {
Expand Down Expand Up @@ -2226,8 +2234,8 @@ pub(crate) fn clean_middle_ty<'tcx>(
}

ty::Param(ref p) => {
if let Some(bounds) = cx.impl_trait_bounds.remove(&p.index.into()) {
ImplTrait(bounds)
if let Some((bounds, origin)) = cx.impl_trait_bounds.remove(&p.index.into()) {
ImplTrait { bounds, origin }
} else if p.name == kw::SelfUpper {
SelfTy
} else {
Expand Down Expand Up @@ -2363,7 +2371,7 @@ fn clean_middle_opaque_bounds<'tcx>(
));
}

ImplTrait(bounds)
ImplTrait { bounds, origin: ImplTraitOrigin::Opaque { def_id: impl_trait_def_id } }
}

pub(crate) fn clean_field<'tcx>(field: &hir::FieldDef<'tcx>, cx: &mut DocContext<'tcx>) -> Item {
Expand Down Expand Up @@ -3177,7 +3185,7 @@ fn clean_assoc_item_constraint<'tcx>(
}
}

fn clean_bound_vars<'tcx>(
pub(crate) fn clean_bound_vars<'tcx>(
bound_vars: &ty::List<ty::BoundVariableKind>,
cx: &mut DocContext<'tcx>,
) -> Vec<GenericParamDef> {
Expand Down
39 changes: 33 additions & 6 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,15 @@ pub(crate) struct PolyTrait {
pub(crate) generic_params: Vec<GenericParamDef>,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub(crate) enum ImplTraitOrigin {
/// Synthetic type parameter for `impl Trait` in argument position.
Param { def_id: DefId },

/// Opaque type backing `impl Trait`, such as in RPIT or TAIT.
Opaque { def_id: DefId },
}

/// Rustdoc's representation of types, mostly based on the [`hir::Ty`].
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub(crate) enum Type {
Expand Down Expand Up @@ -1337,7 +1346,25 @@ pub(crate) enum Type {
Infer,

/// An `impl Trait`: `impl TraitA + TraitB + ...`
ImplTrait(Vec<GenericBound>),
ImplTrait {
/// The bounds that are syntactically present:
/// ```rust
/// # trait TraitA {}
/// # trait TraitB {}
/// # struct Both;
/// # impl TraitA for Both {}
/// # impl TraitB for Both {}
/// fn example() -> impl TraitA + TraitB {
/// // ^^^^^^ ^^^^^^
/// // ...
/// # Both
/// }
/// ```
bounds: Vec<GenericBound>,
/// Whether this `impl Trait` is syntactic sugar for an anonymous generic parameter,
/// or represents an opaque type.
origin: ImplTraitOrigin,
},

UnsafeBinder(Box<UnsafeBinderTy>),
}
Expand Down Expand Up @@ -1465,8 +1492,8 @@ impl Type {
/// This function will panic if the return type does not match the expected sugaring for async
/// functions.
pub(crate) fn sugared_async_return_type(self) -> Type {
if let Type::ImplTrait(mut v) = self
&& let Some(GenericBound::TraitBound(PolyTrait { mut trait_, .. }, _)) = v.pop()
if let Type::ImplTrait { mut bounds, .. } = self
&& let Some(GenericBound::TraitBound(PolyTrait { mut trait_, .. }, _)) = bounds.pop()
&& let Some(segment) = trait_.segments.pop()
&& let GenericArgs::AngleBracketed { mut constraints, .. } = segment.args
&& let Some(constraint) = constraints.pop()
Expand Down Expand Up @@ -1536,7 +1563,7 @@ impl Type {
Type::Pat(..) => PrimitiveType::Pat,
RawPointer(..) => PrimitiveType::RawPointer,
QPath(box QPathData { self_type, .. }) => return self_type.def_id(cache),
Generic(_) | SelfTy | Infer | ImplTrait(_) | UnsafeBinder(_) => return None,
Generic(_) | SelfTy | Infer | ImplTrait { .. } | UnsafeBinder(_) => return None,
};
Primitive(t).def_id(cache)
}
Expand Down Expand Up @@ -2392,14 +2419,14 @@ mod size_asserts {
// tidy-alphabetical-start
static_assert_size!(Crate, 16); // frequently moved by-value
static_assert_size!(DocFragment, 48);
static_assert_size!(GenericArg, 32);
static_assert_size!(GenericArg, 40);
static_assert_size!(GenericArgs, 24);
static_assert_size!(GenericParamDef, 40);
static_assert_size!(Generics, 16);
static_assert_size!(Item, 8);
static_assert_size!(ItemInner, 144);
static_assert_size!(ItemKind, 48);
static_assert_size!(PathSegment, 32);
static_assert_size!(Type, 32);
static_assert_size!(Type, 40);
// tidy-alphabetical-end
}
6 changes: 4 additions & 2 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ pub(crate) struct DocContext<'tcx> {
// therefore wouldn't use the corresp. generic arg anyway. Add support for them.
pub(crate) args: DefIdMap<clean::GenericArg>,
pub(crate) current_type_aliases: DefIdMap<usize>,
/// Table synthetic type parameter for `impl Trait` in argument position -> bounds
pub(crate) impl_trait_bounds: FxHashMap<ImplTraitParam, Vec<clean::GenericBound>>,
/// Table of synthetic type parameter
/// for `impl Trait` in argument position -> (bounds, trait origin)
pub(crate) impl_trait_bounds:
FxHashMap<ImplTraitParam, (Vec<clean::GenericBound>, clean::ImplTraitOrigin)>,
/// Auto-trait or blanket impls processed so far, as `(self_ty, trait_def_id)`.
// FIXME(eddyb) make this a `ty::TraitRef<'tcx>` set.
pub(crate) generated_synthetics: FxHashSet<(Ty<'tcx>, DefId)>,
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,15 +1015,15 @@ fn fmt_type(
{
true
}
clean::ImplTrait(ref bounds) if bounds.len() > 1 => true,
clean::ImplTrait { ref bounds, .. } if bounds.len() > 1 => true,
_ => false,
};
Wrapped::with_parens()
.when(needs_parens)
.wrap_fn(|f| fmt_type(ty, f, use_absolute, cx))
.fmt(f)
}
clean::ImplTrait(bounds) => {
clean::ImplTrait { bounds, .. } => {
f.write_str("impl ")?;
print_generic_bounds(bounds, cx).fmt(f)
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/render/search_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2043,7 +2043,7 @@ fn get_index_type_id(
clean::Type::Pat(..)
| clean::Generic(_)
| clean::SelfTy
| clean::ImplTrait(_)
| clean::ImplTrait { .. }
| clean::Infer
| clean::UnsafeBinder(_) => None,
}
Expand Down Expand Up @@ -2141,7 +2141,7 @@ fn simplify_fn_type<'a, 'tcx>(
RenderType { id: Some(RenderTypeId::Index(idx)), generics: None, bindings: None }
})
}
Type::ImplTrait(ref bounds) => {
Type::ImplTrait { ref bounds, .. } => {
let type_bounds = bounds
.iter()
.filter_map(|bound| bound.get_trait_path())
Expand Down
Loading
Loading