Skip to content

Commit d4f016d

Browse files
Encode precise capturing args in their type system representation
1 parent ba24715 commit d4f016d

File tree

9 files changed

+57
-16
lines changed

9 files changed

+57
-16
lines changed

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_data_structures::unord::UnordMap;
2525
use rustc_errors::{
2626
Applicability, Diag, DiagCtxtHandle, E0228, ErrorGuaranteed, StashKey, struct_span_code_err,
2727
};
28-
use rustc_hir::def::DefKind;
28+
use rustc_hir::def::{DefKind, Res};
2929
use rustc_hir::def_id::{DefId, LocalDefId};
3030
use rustc_hir::intravisit::{self, Visitor, walk_generics};
3131
use rustc_hir::{self as hir, GenericParamKind, Node};
@@ -85,7 +85,7 @@ pub fn provide(providers: &mut Providers) {
8585
coroutine_kind,
8686
coroutine_for_closure,
8787
opaque_ty_origin,
88-
rendered_precise_capturing_args,
88+
opt_precise_capturing_args,
8989
..*providers
9090
};
9191
}
@@ -1865,20 +1865,38 @@ fn opaque_ty_origin<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> hir::OpaqueT
18651865
}
18661866
}
18671867

1868-
fn rendered_precise_capturing_args<'tcx>(
1868+
fn opt_precise_capturing_args<'tcx>(
18691869
tcx: TyCtxt<'tcx>,
18701870
def_id: LocalDefId,
1871-
) -> Option<&'tcx [Symbol]> {
1871+
) -> Option<ty::EarlyBinder<'tcx, &'tcx [ty::GenericArg<'tcx>]>> {
18721872
if let Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) =
18731873
tcx.opt_rpitit_info(def_id.to_def_id())
18741874
{
1875-
return tcx.rendered_precise_capturing_args(opaque_def_id);
1875+
return tcx.opt_precise_capturing_args(opaque_def_id);
18761876
}
18771877

1878+
let icx = ItemCtxt::new(tcx, def_id);
1879+
18781880
tcx.hir_node_by_def_id(def_id).expect_item().expect_opaque_ty().bounds.iter().find_map(
18791881
|bound| match bound {
18801882
hir::GenericBound::Use(args, ..) => {
1881-
Some(&*tcx.arena.alloc_from_iter(args.iter().map(|arg| arg.name())))
1883+
Some(ty::EarlyBinder::bind(&*tcx.arena.alloc_from_iter(args.iter().map(|arg| {
1884+
match arg {
1885+
hir::PreciseCapturingArg::Lifetime(lt) => icx
1886+
.lowerer()
1887+
.lower_lifetime(*lt, RegionInferReason::OutlivesBound)
1888+
.into(),
1889+
hir::PreciseCapturingArg::Param(arg) => match arg.res {
1890+
Res::Def(DefKind::TyParam, _) => {
1891+
icx.lowerer().lower_ty_param(arg.hir_id).into()
1892+
}
1893+
Res::Def(DefKind::ConstParam, _) => {
1894+
icx.lowerer().lower_const_param(arg.hir_id).into()
1895+
}
1896+
_ => unreachable!(),
1897+
},
1898+
}
1899+
}))))
18821900
}
18831901
_ => None,
18841902
},

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>>
9494
}
9595
}
9696

97+
impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>>
98+
ProcessQueryValue<'tcx, Option<ty::EarlyBinder<'tcx, &'tcx [T]>>>
99+
for Option<DecodeIterator<'a, 'tcx, T>>
100+
{
101+
#[inline(always)]
102+
fn process_decoded(
103+
self,
104+
tcx: TyCtxt<'tcx>,
105+
_err: impl Fn() -> !,
106+
) -> Option<ty::EarlyBinder<'tcx, &'tcx [T]>> {
107+
self.map(|iter| ty::EarlyBinder::bind(&*tcx.arena.alloc_from_iter(iter)))
108+
}
109+
}
110+
97111
impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>>
98112
ProcessQueryValue<'tcx, Option<&'tcx [T]>> for Option<DecodeIterator<'a, 'tcx, T>>
99113
{
@@ -285,7 +299,7 @@ provide! { tcx, def_id, other, cdata,
285299
.process_decoded(tcx, || panic!("{def_id:?} does not have coerce_unsized_info"))) }
286300
mir_const_qualif => { table }
287301
rendered_const => { table }
288-
rendered_precise_capturing_args => { table }
302+
opt_precise_capturing_args => { table }
289303
asyncness => { table_direct }
290304
fn_arg_names => { table }
291305
coroutine_kind => { table_direct }

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,11 +1671,14 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16711671
}
16721672

16731673
fn encode_precise_capturing_args(&mut self, def_id: DefId) {
1674-
let Some(precise_capturing_args) = self.tcx.rendered_precise_capturing_args(def_id) else {
1674+
let Some(precise_capturing_args) = self.tcx.opt_precise_capturing_args(def_id) else {
16751675
return;
16761676
};
16771677

1678-
record_array!(self.tables.rendered_precise_capturing_args[def_id] <- precise_capturing_args);
1678+
record_array!(
1679+
self.tables.opt_precise_capturing_args[def_id]
1680+
<- precise_capturing_args.skip_binder()
1681+
);
16791682
}
16801683

16811684
fn encode_mir(&mut self) {

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ define_tables! {
440440
coerce_unsized_info: Table<DefIndex, LazyValue<ty::adjustment::CoerceUnsizedInfo>>,
441441
mir_const_qualif: Table<DefIndex, LazyValue<mir::ConstQualifs>>,
442442
rendered_const: Table<DefIndex, LazyValue<String>>,
443-
rendered_precise_capturing_args: Table<DefIndex, LazyArray<Symbol>>,
443+
opt_precise_capturing_args: Table<DefIndex, LazyArray<ty::GenericArg<'static>>>,
444444
asyncness: Table<DefIndex, ty::Asyncness>,
445445
fn_arg_names: Table<DefIndex, LazyArray<Ident>>,
446446
coroutine_kind: Table<DefIndex, hir::CoroutineKind>,

compiler/rustc_middle/src/query/erase.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ impl<T> EraseType for Option<&'_ [T]> {
182182
type Result = [u8; size_of::<Option<&'static [()]>>()];
183183
}
184184

185+
impl<T> EraseType for Option<ty::EarlyBinder<'_, &'_ [T]>> {
186+
type Result = [u8; size_of::<Option<ty::EarlyBinder<'static, &'static [()]>>>()];
187+
}
188+
185189
impl EraseType for Option<mir::DestructuredConstant<'_>> {
186190
type Result = [u8; size_of::<Option<mir::DestructuredConstant<'static>>>()];
187191
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,8 @@ rustc_queries! {
12721272
}
12731273

12741274
/// Gets the rendered precise capturing args for an opaque for use in rustdoc.
1275-
query rendered_precise_capturing_args(def_id: DefId) -> Option<&'tcx [Symbol]> {
1275+
query opt_precise_capturing_args(def_id: DefId)
1276+
-> Option<ty::EarlyBinder<'tcx, &'tcx [ty::GenericArg<'tcx>]>> {
12761277
desc { |tcx| "rendering precise capturing args for `{}`", tcx.def_path_str(def_id) }
12771278
separate_provide_extern
12781279
}

compiler/rustc_middle/src/ty/parameterized.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,6 @@ parameterized_over_tcx! {
138138
ty::Predicate,
139139
ty::Clause,
140140
ty::ClauseKind,
141-
ty::ImplTraitHeader
141+
ty::ImplTraitHeader,
142+
ty::GenericArg,
142143
}

src/librustdoc/clean/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ fn clean_generic_bound<'tcx>(
225225
GenericBound::TraitBound(clean_poly_trait_ref(t, cx), modifier)
226226
}
227227
hir::GenericBound::Use(args, ..) => {
228-
GenericBound::Use(args.iter().map(|arg| arg.name()).collect())
228+
GenericBound::Use(args.iter().map(|arg| arg.name().to_string()).collect())
229229
}
230230
})
231231
}
@@ -2331,8 +2331,8 @@ fn clean_middle_opaque_bounds<'tcx>(
23312331
bounds.insert(0, GenericBound::sized(cx));
23322332
}
23332333

2334-
if let Some(args) = cx.tcx.rendered_precise_capturing_args(impl_trait_def_id) {
2335-
bounds.push(GenericBound::Use(args.to_vec()));
2334+
if let Some(args) = cx.tcx.opt_precise_capturing_args(impl_trait_def_id) {
2335+
bounds.push(GenericBound::Use(args.iter().map(ToString::to_string).collect()));
23362336
}
23372337

23382338
ImplTrait(bounds)

src/librustdoc/clean/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,7 @@ pub(crate) enum GenericBound {
12601260
TraitBound(PolyTrait, hir::TraitBoundModifier),
12611261
Outlives(Lifetime),
12621262
/// `use<'a, T>` precise-capturing bound syntax
1263-
Use(Vec<Symbol>),
1263+
Use(Vec<String>),
12641264
}
12651265

12661266
impl GenericBound {

0 commit comments

Comments
 (0)