Skip to content

Commit 458d5c3

Browse files
authored
Rollup merge of rust-lang#120999 - fmease:rustdoc-rm-instantiation-param, 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.
2 parents 8d0e6f0 + 36d7f76 commit 458d5c3

File tree

3 files changed

+31
-73
lines changed

3 files changed

+31
-73
lines changed

src/librustdoc/clean/mod.rs

+13-39
Original file line numberDiff line numberDiff line change
@@ -254,16 +254,14 @@ fn clean_poly_trait_ref_with_bindings<'tcx>(
254254
}
255255

256256
fn clean_lifetime<'tcx>(lifetime: &hir::Lifetime, cx: &mut DocContext<'tcx>) -> Lifetime {
257-
let def = cx.tcx.named_bound_var(lifetime.hir_id);
258257
if let Some(
259-
rbv::ResolvedArg::EarlyBound(node_id)
260-
| rbv::ResolvedArg::LateBound(_, _, node_id)
261-
| rbv::ResolvedArg::Free(_, node_id),
262-
) = def
258+
rbv::ResolvedArg::EarlyBound(did)
259+
| rbv::ResolvedArg::LateBound(_, _, did)
260+
| rbv::ResolvedArg::Free(_, did),
261+
) = cx.tcx.named_bound_var(lifetime.hir_id)
262+
&& let Some(lt) = cx.args.get(&did).and_then(|arg| arg.as_lt())
263263
{
264-
if let Some(lt) = cx.args.get(&node_id).and_then(|p| p.as_lt()).cloned() {
265-
return lt;
266-
}
264+
return lt.clone();
267265
}
268266
Lifetime(lifetime.ident.name)
269267
}
@@ -1791,12 +1789,12 @@ fn maybe_expand_private_type_alias<'tcx>(
17911789
_ => None,
17921790
});
17931791
if let Some(lt) = lifetime {
1794-
let cleaned = if !lt.is_anonymous() {
1792+
let lt = if !lt.is_anonymous() {
17951793
clean_lifetime(lt, cx)
17961794
} else {
17971795
Lifetime::elided()
17981796
};
1799-
args.insert(param.def_id.to_def_id(), InstantiationParam::Lifetime(cleaned));
1797+
args.insert(param.def_id.to_def_id(), GenericArg::Lifetime(lt));
18001798
}
18011799
indices.lifetimes += 1;
18021800
}
@@ -1805,44 +1803,20 @@ fn maybe_expand_private_type_alias<'tcx>(
18051803
let type_ = generic_args.args.iter().find_map(|arg| match arg {
18061804
hir::GenericArg::Type(ty) => {
18071805
if indices.types == j {
1808-
return Some(ty);
1806+
return Some(*ty);
18091807
}
18101808
j += 1;
18111809
None
18121810
}
18131811
_ => None,
18141812
});
1815-
if let Some(ty) = type_ {
1816-
args.insert(
1817-
param.def_id.to_def_id(),
1818-
InstantiationParam::Type(clean_ty(ty, cx)),
1819-
);
1820-
} else if let Some(default) = *default {
1821-
args.insert(
1822-
param.def_id.to_def_id(),
1823-
InstantiationParam::Type(clean_ty(default, cx)),
1824-
);
1813+
if let Some(ty) = type_.or(*default) {
1814+
args.insert(param.def_id.to_def_id(), GenericArg::Type(clean_ty(ty, cx)));
18251815
}
18261816
indices.types += 1;
18271817
}
1828-
hir::GenericParamKind::Const { .. } => {
1829-
let mut j = 0;
1830-
let const_ = generic_args.args.iter().find_map(|arg| match arg {
1831-
hir::GenericArg::Const(ct) => {
1832-
if indices.consts == j {
1833-
return Some(ct);
1834-
}
1835-
j += 1;
1836-
None
1837-
}
1838-
_ => None,
1839-
});
1840-
if let Some(_) = const_ {
1841-
args.insert(param.def_id.to_def_id(), InstantiationParam::Constant);
1842-
}
1843-
// FIXME(const_generics_defaults)
1844-
indices.consts += 1;
1845-
}
1818+
// FIXME(#82852): Instantiate const parameters.
1819+
hir::GenericParamKind::Const { .. } => {}
18461820
}
18471821
}
18481822

src/librustdoc/clean/types.rs

+10-29
Original file line numberDiff line numberDiff line change
@@ -2228,6 +2228,16 @@ pub(crate) enum GenericArg {
22282228
Infer,
22292229
}
22302230

2231+
impl GenericArg {
2232+
pub(crate) fn as_lt(&self) -> Option<&Lifetime> {
2233+
if let Self::Lifetime(lt) = self { Some(lt) } else { None }
2234+
}
2235+
2236+
pub(crate) fn as_ty(&self) -> Option<&Type> {
2237+
if let Self::Type(ty) = self { Some(ty) } else { None }
2238+
}
2239+
}
2240+
22312241
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
22322242
pub(crate) enum GenericArgs {
22332243
AngleBracketed { args: Box<[GenericArg]>, bindings: ThinVec<TypeBinding> },
@@ -2530,35 +2540,6 @@ pub(crate) enum TypeBindingKind {
25302540
Constraint { bounds: Vec<GenericBound> },
25312541
}
25322542

2533-
/// The type, lifetime, or constant that a private type alias's parameter should be
2534-
/// replaced with when expanding a use of that type alias.
2535-
///
2536-
/// For example:
2537-
///
2538-
/// ```
2539-
/// type PrivAlias<T> = Vec<T>;
2540-
///
2541-
/// pub fn public_fn() -> PrivAlias<i32> { vec![] }
2542-
/// ```
2543-
///
2544-
/// `public_fn`'s docs will show it as returning `Vec<i32>`, since `PrivAlias` is private.
2545-
/// [`InstantiationParam`] is used to record that `T` should be mapped to `i32`.
2546-
pub(crate) enum InstantiationParam {
2547-
Type(Type),
2548-
Lifetime(Lifetime),
2549-
Constant,
2550-
}
2551-
2552-
impl InstantiationParam {
2553-
pub(crate) fn as_ty(&self) -> Option<&Type> {
2554-
if let Self::Type(ty) = self { Some(ty) } else { None }
2555-
}
2556-
2557-
pub(crate) fn as_lt(&self) -> Option<&Lifetime> {
2558-
if let Self::Lifetime(lt) = self { Some(lt) } else { None }
2559-
}
2560-
}
2561-
25622543
// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
25632544
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
25642545
mod size_asserts {

src/librustdoc/core.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@ pub(crate) struct DocContext<'tcx> {
4444
/// Used while populating `external_traits` to ensure we don't process the same trait twice at
4545
/// the same time.
4646
pub(crate) active_extern_traits: DefIdSet,
47-
// The current set of parameter instantiations,
48-
// for expanding type aliases at the HIR level:
49-
/// Table `DefId` of type, lifetime, or const parameter -> instantiated type, lifetime, or const
50-
pub(crate) args: DefIdMap<clean::InstantiationParam>,
47+
/// The current set of parameter instantiations for expanding type aliases at the HIR level.
48+
///
49+
/// Maps from the `DefId` of a lifetime or type parameter to the
50+
/// generic argument it's currently instantiated to in this context.
51+
// FIXME(#82852): We don't record const params since we don't visit const exprs at all and
52+
// therefore wouldn't use the corresp. generic arg anyway. Add support for them.
53+
pub(crate) args: DefIdMap<clean::GenericArg>,
5154
pub(crate) current_type_aliases: DefIdMap<usize>,
5255
/// Table synthetic type parameter for `impl Trait` in argument position -> bounds
5356
pub(crate) impl_trait_bounds: FxHashMap<ImplTraitParam, Vec<clean::GenericBound>>,
@@ -87,7 +90,7 @@ impl<'tcx> DocContext<'tcx> {
8790
/// the generic parameters for a type alias' RHS.
8891
pub(crate) fn enter_alias<F, R>(
8992
&mut self,
90-
args: DefIdMap<clean::InstantiationParam>,
93+
args: DefIdMap<clean::GenericArg>,
9194
def_id: DefId,
9295
f: F,
9396
) -> R

0 commit comments

Comments
 (0)