From 36d7f763f03f9110e0d6b7052799f7ec40cc3c34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Tue, 13 Feb 2024 04:20:04 +0100 Subject: [PATCH] Replace clean::InstantiationParam with clean::GenericArg --- src/librustdoc/clean/mod.rs | 52 +++++++++-------------------------- src/librustdoc/clean/types.rs | 39 +++++++------------------- src/librustdoc/core.rs | 13 +++++---- 3 files changed, 31 insertions(+), 73 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index f4527d1e55e85..77a7d33f6e7c5 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -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) } @@ -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; } @@ -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 { .. } => {} } } diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 96b4d1a45f6ea..cee1b25591d33 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -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 }, @@ -2530,35 +2540,6 @@ pub(crate) enum TypeBindingKind { Constraint { bounds: Vec }, } -/// 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 = Vec; -/// -/// pub fn public_fn() -> PrivAlias { vec![] } -/// ``` -/// -/// `public_fn`'s docs will show it as returning `Vec`, 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 { diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 28ccda39e4de2..efad5a8d808dd 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -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, + /// 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, pub(crate) current_type_aliases: DefIdMap, /// Table synthetic type parameter for `impl Trait` in argument position -> bounds pub(crate) impl_trait_bounds: FxHashMap>, @@ -87,7 +90,7 @@ impl<'tcx> DocContext<'tcx> { /// the generic parameters for a type alias' RHS. pub(crate) fn enter_alias( &mut self, - args: DefIdMap, + args: DefIdMap, def_id: DefId, f: F, ) -> R