Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit acb1395

Browse files
authoredMar 13, 2025
Rollup merge of rust-lang#138109 - Kohei316:feat/rust-doc-precise-capturing-arg, r=aDotInTheVoid,compiler-errors
make precise capturing args in rustdoc Json typed close rust-lang#137616 This PR includes below changes. - Add `rustc_hir::PreciseCapturingArgKind` which allows the query system to return a arg's data. - Add `rustdoc::clean::types::PreciseCapturingArg` and change to use it. - Add `rustdoc-json-types::PreciseCapturingArg` and change to use it. - Update `tests/rustdoc-json/impl-trait-precise-capturing.rs`. - Bump `rustdoc_json_types::FORMAT_VERSION`.
2 parents 4a18fe5 + 112f7b0 commit acb1395

File tree

11 files changed

+96
-20
lines changed

11 files changed

+96
-20
lines changed
 

‎compiler/rustc_hir/src/hir.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -3373,13 +3373,16 @@ pub struct OpaqueTy<'hir> {
33733373
pub span: Span,
33743374
}
33753375

3376-
#[derive(Debug, Clone, Copy, HashStable_Generic)]
3377-
pub enum PreciseCapturingArg<'hir> {
3378-
Lifetime(&'hir Lifetime),
3376+
#[derive(Debug, Clone, Copy, HashStable_Generic, Encodable, Decodable)]
3377+
pub enum PreciseCapturingArgKind<T, U> {
3378+
Lifetime(T),
33793379
/// Non-lifetime argument (type or const)
3380-
Param(PreciseCapturingNonLifetimeArg),
3380+
Param(U),
33813381
}
33823382

3383+
pub type PreciseCapturingArg<'hir> =
3384+
PreciseCapturingArgKind<&'hir Lifetime, PreciseCapturingNonLifetimeArg>;
3385+
33833386
impl PreciseCapturingArg<'_> {
33843387
pub fn hir_id(self) -> HirId {
33853388
match self {

‎compiler/rustc_hir_analysis/src/collect.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_errors::{
2828
use rustc_hir::def::DefKind;
2929
use rustc_hir::def_id::{DefId, LocalDefId};
3030
use rustc_hir::intravisit::{self, InferKind, Visitor, VisitorExt, walk_generics};
31-
use rustc_hir::{self as hir, GenericParamKind, HirId, Node};
31+
use rustc_hir::{self as hir, GenericParamKind, HirId, Node, PreciseCapturingArgKind};
3232
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
3333
use rustc_infer::traits::ObligationCause;
3434
use rustc_middle::hir::nested_filter;
@@ -1791,7 +1791,7 @@ fn opaque_ty_origin<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> hir::OpaqueT
17911791
fn rendered_precise_capturing_args<'tcx>(
17921792
tcx: TyCtxt<'tcx>,
17931793
def_id: LocalDefId,
1794-
) -> Option<&'tcx [Symbol]> {
1794+
) -> Option<&'tcx [PreciseCapturingArgKind<Symbol, Symbol>]> {
17951795
if let Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) =
17961796
tcx.opt_rpitit_info(def_id.to_def_id())
17971797
{
@@ -1800,7 +1800,12 @@ fn rendered_precise_capturing_args<'tcx>(
18001800

18011801
tcx.hir_node_by_def_id(def_id).expect_opaque_ty().bounds.iter().find_map(|bound| match bound {
18021802
hir::GenericBound::Use(args, ..) => {
1803-
Some(&*tcx.arena.alloc_from_iter(args.iter().map(|arg| arg.name())))
1803+
Some(&*tcx.arena.alloc_from_iter(args.iter().map(|arg| match arg {
1804+
PreciseCapturingArgKind::Lifetime(_) => {
1805+
PreciseCapturingArgKind::Lifetime(arg.name())
1806+
}
1807+
PreciseCapturingArgKind::Param(_) => PreciseCapturingArgKind::Param(arg.name()),
1808+
})))
18041809
}
18051810
_ => None,
18061811
})

‎compiler/rustc_metadata/src/rmeta/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_abi::{FieldIdx, ReprOptions, VariantIdx};
1010
use rustc_ast::expand::StrippedCfgItem;
1111
use rustc_data_structures::fx::FxHashMap;
1212
use rustc_data_structures::svh::Svh;
13+
use rustc_hir::PreciseCapturingArgKind;
1314
use rustc_hir::def::{CtorKind, DefKind, DocLinkResMap};
1415
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIndex, DefPathHash, StableCrateId};
1516
use rustc_hir::definitions::DefKey;
@@ -440,7 +441,7 @@ define_tables! {
440441
coerce_unsized_info: Table<DefIndex, LazyValue<ty::adjustment::CoerceUnsizedInfo>>,
441442
mir_const_qualif: Table<DefIndex, LazyValue<mir::ConstQualifs>>,
442443
rendered_const: Table<DefIndex, LazyValue<String>>,
443-
rendered_precise_capturing_args: Table<DefIndex, LazyArray<Symbol>>,
444+
rendered_precise_capturing_args: Table<DefIndex, LazyArray<PreciseCapturingArgKind<Symbol, Symbol>>>,
444445
asyncness: Table<DefIndex, ty::Asyncness>,
445446
fn_arg_names: Table<DefIndex, LazyArray<Ident>>,
446447
coroutine_kind: Table<DefIndex, hir::CoroutineKind>,

‎compiler/rustc_middle/src/query/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_hir::def_id::{
2525
CrateNum, DefId, DefIdMap, LocalDefId, LocalDefIdMap, LocalDefIdSet, LocalModDefId,
2626
};
2727
use rustc_hir::lang_items::{LangItem, LanguageItems};
28-
use rustc_hir::{Crate, ItemLocalId, ItemLocalMap, TraitCandidate};
28+
use rustc_hir::{Crate, ItemLocalId, ItemLocalMap, PreciseCapturingArgKind, TraitCandidate};
2929
use rustc_index::IndexVec;
3030
use rustc_lint_defs::LintId;
3131
use rustc_macros::rustc_queries;
@@ -1424,7 +1424,7 @@ rustc_queries! {
14241424
}
14251425

14261426
/// Gets the rendered precise capturing args for an opaque for use in rustdoc.
1427-
query rendered_precise_capturing_args(def_id: DefId) -> Option<&'tcx [Symbol]> {
1427+
query rendered_precise_capturing_args(def_id: DefId) -> Option<&'tcx [PreciseCapturingArgKind<Symbol, Symbol>]> {
14281428
desc { |tcx| "rendering precise capturing args for `{}`", tcx.def_path_str(def_id) }
14291429
separate_provide_extern
14301430
}

‎compiler/rustc_middle/src/ty/parameterized.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::hash::Hash;
33
use rustc_data_structures::unord::UnordMap;
44
use rustc_hir::def_id::DefIndex;
55
use rustc_index::{Idx, IndexVec};
6+
use rustc_span::Symbol;
67

78
use crate::ty;
89

@@ -96,6 +97,7 @@ trivially_parameterized_over_tcx! {
9697
rustc_hir::def_id::DefIndex,
9798
rustc_hir::definitions::DefKey,
9899
rustc_hir::OpaqueTyOrigin<rustc_hir::def_id::DefId>,
100+
rustc_hir::PreciseCapturingArgKind<Symbol, Symbol>,
99101
rustc_index::bit_set::DenseBitSet<u32>,
100102
rustc_index::bit_set::FiniteBitSet<u32>,
101103
rustc_session::cstore::ForeignModule,

‎src/librustdoc/clean/mod.rs

+25-2
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ fn clean_generic_bound<'tcx>(
231231
GenericBound::TraitBound(clean_poly_trait_ref(t, cx), t.modifiers)
232232
}
233233
hir::GenericBound::Use(args, ..) => {
234-
GenericBound::Use(args.iter().map(|arg| arg.name()).collect())
234+
GenericBound::Use(args.iter().map(|arg| clean_precise_capturing_arg(arg, cx)).collect())
235235
}
236236
})
237237
}
@@ -286,6 +286,18 @@ fn clean_lifetime(lifetime: &hir::Lifetime, cx: &DocContext<'_>) -> Lifetime {
286286
Lifetime(lifetime.ident.name)
287287
}
288288

289+
pub(crate) fn clean_precise_capturing_arg(
290+
arg: &hir::PreciseCapturingArg<'_>,
291+
cx: &DocContext<'_>,
292+
) -> PreciseCapturingArg {
293+
match arg {
294+
hir::PreciseCapturingArg::Lifetime(lt) => {
295+
PreciseCapturingArg::Lifetime(clean_lifetime(lt, cx))
296+
}
297+
hir::PreciseCapturingArg::Param(param) => PreciseCapturingArg::Param(param.ident.name),
298+
}
299+
}
300+
289301
pub(crate) fn clean_const<'tcx>(
290302
constant: &hir::ConstArg<'tcx>,
291303
_cx: &mut DocContext<'tcx>,
@@ -2364,7 +2376,18 @@ fn clean_middle_opaque_bounds<'tcx>(
23642376
}
23652377

23662378
if let Some(args) = cx.tcx.rendered_precise_capturing_args(impl_trait_def_id) {
2367-
bounds.push(GenericBound::Use(args.to_vec()));
2379+
bounds.push(GenericBound::Use(
2380+
args.iter()
2381+
.map(|arg| match arg {
2382+
hir::PreciseCapturingArgKind::Lifetime(lt) => {
2383+
PreciseCapturingArg::Lifetime(Lifetime(*lt))
2384+
}
2385+
hir::PreciseCapturingArgKind::Param(param) => {
2386+
PreciseCapturingArg::Param(*param)
2387+
}
2388+
})
2389+
.collect(),
2390+
));
23682391
}
23692392

23702393
ImplTrait(bounds)

‎src/librustdoc/clean/types.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ pub(crate) enum GenericBound {
12401240
TraitBound(PolyTrait, hir::TraitBoundModifiers),
12411241
Outlives(Lifetime),
12421242
/// `use<'a, T>` precise-capturing bound syntax
1243-
Use(Vec<Symbol>),
1243+
Use(Vec<PreciseCapturingArg>),
12441244
}
12451245

12461246
impl GenericBound {
@@ -1304,6 +1304,21 @@ impl Lifetime {
13041304
}
13051305
}
13061306

1307+
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
1308+
pub(crate) enum PreciseCapturingArg {
1309+
Lifetime(Lifetime),
1310+
Param(Symbol),
1311+
}
1312+
1313+
impl PreciseCapturingArg {
1314+
pub(crate) fn name(self) -> Symbol {
1315+
match self {
1316+
PreciseCapturingArg::Lifetime(lt) => lt.0,
1317+
PreciseCapturingArg::Param(param) => param,
1318+
}
1319+
}
1320+
}
1321+
13071322
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
13081323
pub(crate) enum WherePredicate {
13091324
BoundPredicate { ty: Type, bounds: Vec<GenericBound>, bound_params: Vec<GenericParamDef> },

‎src/librustdoc/html/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ impl clean::GenericBound {
283283
} else {
284284
f.write_str("use&lt;")?;
285285
}
286-
args.iter().joined(", ", f)?;
286+
args.iter().map(|arg| arg.name()).joined(", ", f)?;
287287
if f.alternate() { f.write_str(">") } else { f.write_str("&gt;") }
288288
}
289289
})

‎src/librustdoc/json/conversions.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,18 @@ impl FromClean<clean::GenericBound> for GenericBound {
548548
}
549549
}
550550
Outlives(lifetime) => GenericBound::Outlives(convert_lifetime(lifetime)),
551-
Use(args) => GenericBound::Use(args.into_iter().map(|arg| arg.to_string()).collect()),
551+
Use(args) => GenericBound::Use(
552+
args.iter()
553+
.map(|arg| match arg {
554+
clean::PreciseCapturingArg::Lifetime(lt) => {
555+
PreciseCapturingArg::Lifetime(convert_lifetime(*lt))
556+
}
557+
clean::PreciseCapturingArg::Param(param) => {
558+
PreciseCapturingArg::Param(param.to_string())
559+
}
560+
})
561+
.collect(),
562+
),
552563
}
553564
}
554565
}

‎src/rustdoc-json-types/lib.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub type FxHashMap<K, V> = HashMap<K, V>; // re-export for use in src/librustdoc
3030
/// This integer is incremented with every breaking change to the API,
3131
/// and is returned along with the JSON blob as [`Crate::format_version`].
3232
/// Consuming code should assert that this value matches the format version(s) that it supports.
33-
pub const FORMAT_VERSION: u32 = 40;
33+
pub const FORMAT_VERSION: u32 = 41;
3434

3535
/// The root of the emitted JSON blob.
3636
///
@@ -909,7 +909,7 @@ pub enum GenericBound {
909909
/// ```
910910
Outlives(String),
911911
/// `use<'a, T>` precise-capturing bound syntax
912-
Use(Vec<String>),
912+
Use(Vec<PreciseCapturingArg>),
913913
}
914914

915915
/// A set of modifiers applied to a trait.
@@ -927,6 +927,22 @@ pub enum TraitBoundModifier {
927927
MaybeConst,
928928
}
929929

930+
/// One precise capturing argument. See [the rust reference](https://doc.rust-lang.org/reference/types/impl-trait.html#precise-capturing).
931+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
932+
#[serde(rename_all = "snake_case")]
933+
pub enum PreciseCapturingArg {
934+
/// A lifetime.
935+
/// ```rust
936+
/// pub fn hello<'a, T, const N: usize>() -> impl Sized + use<'a, T, N> {}
937+
/// // ^^
938+
Lifetime(String),
939+
/// A type or constant parameter.
940+
/// ```rust
941+
/// pub fn hello<'a, T, const N: usize>() -> impl Sized + use<'a, T, N> {}
942+
/// // ^ ^
943+
Param(String),
944+
}
945+
930946
/// Either a type or a constant, usually stored as the right-hand side of an equation in places like
931947
/// [`AssocItemConstraint`]
932948
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ is "$.index[*][?(@.name=='hello')].inner.function.sig.output.impl_trait[1].use[0]" \"\'a\"
2-
//@ is "$.index[*][?(@.name=='hello')].inner.function.sig.output.impl_trait[1].use[1]" \"T\"
3-
//@ is "$.index[*][?(@.name=='hello')].inner.function.sig.output.impl_trait[1].use[2]" \"N\"
1+
//@ is "$.index[*][?(@.name=='hello')].inner.function.sig.output.impl_trait[1].use[0].lifetime" \"\'a\"
2+
//@ is "$.index[*][?(@.name=='hello')].inner.function.sig.output.impl_trait[1].use[1].param" \"T\"
3+
//@ is "$.index[*][?(@.name=='hello')].inner.function.sig.output.impl_trait[1].use[2].param" \"N\"
44
pub fn hello<'a, T, const N: usize>() -> impl Sized + use<'a, T, N> {}

0 commit comments

Comments
 (0)
Failed to load comments.