Skip to content

Commit

Permalink
Rollup merge of rust-lang#120999 - fmease:rustdoc-rm-instantiation-pa…
Browse files Browse the repository at this point in the history
…ram, r=notriddle

rustdoc: replace `clean::InstantiationParam` with `clean::GenericArg`

Probably better known as `SubstParam` (until rust-lang#120958 which should've probably renamed it to `InstantiatedParam` but anyways).

It doesn't make any sense to me why it should exist as a separate type. `GenericArg` is exactly what we want here anyways from a semantic perspective. Both have the same size btw.

I also took the liberty of doing some drive-by cleanups.
  • Loading branch information
matthiaskrgr authored Feb 13, 2024
2 parents db9591c + 36d7f76 commit 2e98b27
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 73 deletions.
52 changes: 13 additions & 39 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,14 @@ fn clean_poly_trait_ref_with_bindings<'tcx>(
}

fn clean_lifetime<'tcx>(lifetime: &hir::Lifetime, cx: &mut DocContext<'tcx>) -> Lifetime {
let def = cx.tcx.named_bound_var(lifetime.hir_id);
if let Some(
rbv::ResolvedArg::EarlyBound(node_id)
| rbv::ResolvedArg::LateBound(_, _, node_id)
| rbv::ResolvedArg::Free(_, node_id),
) = def
rbv::ResolvedArg::EarlyBound(did)
| rbv::ResolvedArg::LateBound(_, _, did)
| rbv::ResolvedArg::Free(_, did),
) = cx.tcx.named_bound_var(lifetime.hir_id)
&& let Some(lt) = cx.args.get(&did).and_then(|arg| arg.as_lt())
{
if let Some(lt) = cx.args.get(&node_id).and_then(|p| p.as_lt()).cloned() {
return lt;
}
return lt.clone();
}
Lifetime(lifetime.ident.name)
}
Expand Down Expand Up @@ -1791,12 +1789,12 @@ fn maybe_expand_private_type_alias<'tcx>(
_ => None,
});
if let Some(lt) = lifetime {
let cleaned = if !lt.is_anonymous() {
let lt = if !lt.is_anonymous() {
clean_lifetime(lt, cx)
} else {
Lifetime::elided()
};
args.insert(param.def_id.to_def_id(), InstantiationParam::Lifetime(cleaned));
args.insert(param.def_id.to_def_id(), GenericArg::Lifetime(lt));
}
indices.lifetimes += 1;
}
Expand All @@ -1805,44 +1803,20 @@ fn maybe_expand_private_type_alias<'tcx>(
let type_ = generic_args.args.iter().find_map(|arg| match arg {
hir::GenericArg::Type(ty) => {
if indices.types == j {
return Some(ty);
return Some(*ty);
}
j += 1;
None
}
_ => None,
});
if let Some(ty) = type_ {
args.insert(
param.def_id.to_def_id(),
InstantiationParam::Type(clean_ty(ty, cx)),
);
} else if let Some(default) = *default {
args.insert(
param.def_id.to_def_id(),
InstantiationParam::Type(clean_ty(default, cx)),
);
if let Some(ty) = type_.or(*default) {
args.insert(param.def_id.to_def_id(), GenericArg::Type(clean_ty(ty, cx)));
}
indices.types += 1;
}
hir::GenericParamKind::Const { .. } => {
let mut j = 0;
let const_ = generic_args.args.iter().find_map(|arg| match arg {
hir::GenericArg::Const(ct) => {
if indices.consts == j {
return Some(ct);
}
j += 1;
None
}
_ => None,
});
if let Some(_) = const_ {
args.insert(param.def_id.to_def_id(), InstantiationParam::Constant);
}
// FIXME(const_generics_defaults)
indices.consts += 1;
}
// FIXME(#82852): Instantiate const parameters.
hir::GenericParamKind::Const { .. } => {}
}
}

Expand Down
39 changes: 10 additions & 29 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2228,6 +2228,16 @@ pub(crate) enum GenericArg {
Infer,
}

impl GenericArg {
pub(crate) fn as_lt(&self) -> Option<&Lifetime> {
if let Self::Lifetime(lt) = self { Some(lt) } else { None }
}

pub(crate) fn as_ty(&self) -> Option<&Type> {
if let Self::Type(ty) = self { Some(ty) } else { None }
}
}

#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub(crate) enum GenericArgs {
AngleBracketed { args: Box<[GenericArg]>, bindings: ThinVec<TypeBinding> },
Expand Down Expand Up @@ -2530,35 +2540,6 @@ pub(crate) enum TypeBindingKind {
Constraint { bounds: Vec<GenericBound> },
}

/// The type, lifetime, or constant that a private type alias's parameter should be
/// replaced with when expanding a use of that type alias.
///
/// For example:
///
/// ```
/// type PrivAlias<T> = Vec<T>;
///
/// pub fn public_fn() -> PrivAlias<i32> { vec![] }
/// ```
///
/// `public_fn`'s docs will show it as returning `Vec<i32>`, since `PrivAlias` is private.
/// [`InstantiationParam`] is used to record that `T` should be mapped to `i32`.
pub(crate) enum InstantiationParam {
Type(Type),
Lifetime(Lifetime),
Constant,
}

impl InstantiationParam {
pub(crate) fn as_ty(&self) -> Option<&Type> {
if let Self::Type(ty) = self { Some(ty) } else { None }
}

pub(crate) fn as_lt(&self) -> Option<&Lifetime> {
if let Self::Lifetime(lt) = self { Some(lt) } else { None }
}
}

// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
mod size_asserts {
Expand Down
13 changes: 8 additions & 5 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ pub(crate) struct DocContext<'tcx> {
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
/// the same time.
pub(crate) active_extern_traits: DefIdSet,
// The current set of parameter instantiations,
// for expanding type aliases at the HIR level:
/// Table `DefId` of type, lifetime, or const parameter -> instantiated type, lifetime, or const
pub(crate) args: DefIdMap<clean::InstantiationParam>,
/// The current set of parameter instantiations for expanding type aliases at the HIR level.
///
/// Maps from the `DefId` of a lifetime or type parameter to the
/// generic argument it's currently instantiated to in this context.
// FIXME(#82852): We don't record const params since we don't visit const exprs at all and
// 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>>,
Expand Down Expand Up @@ -87,7 +90,7 @@ impl<'tcx> DocContext<'tcx> {
/// the generic parameters for a type alias' RHS.
pub(crate) fn enter_alias<F, R>(
&mut self,
args: DefIdMap<clean::InstantiationParam>,
args: DefIdMap<clean::GenericArg>,
def_id: DefId,
f: F,
) -> R
Expand Down

0 comments on commit 2e98b27

Please sign in to comment.