Skip to content

Commit

Permalink
Encode precise capturing args in their type system representation
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Oct 3, 2024
1 parent ba24715 commit d4f016d
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 16 deletions.
30 changes: 24 additions & 6 deletions compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use rustc_data_structures::unord::UnordMap;
use rustc_errors::{
Applicability, Diag, DiagCtxtHandle, E0228, ErrorGuaranteed, StashKey, struct_span_code_err,
};
use rustc_hir::def::DefKind;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::{self, Visitor, walk_generics};
use rustc_hir::{self as hir, GenericParamKind, Node};
Expand Down Expand Up @@ -85,7 +85,7 @@ pub fn provide(providers: &mut Providers) {
coroutine_kind,
coroutine_for_closure,
opaque_ty_origin,
rendered_precise_capturing_args,
opt_precise_capturing_args,
..*providers
};
}
Expand Down Expand Up @@ -1865,20 +1865,38 @@ fn opaque_ty_origin<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> hir::OpaqueT
}
}

fn rendered_precise_capturing_args<'tcx>(
fn opt_precise_capturing_args<'tcx>(
tcx: TyCtxt<'tcx>,
def_id: LocalDefId,
) -> Option<&'tcx [Symbol]> {
) -> Option<ty::EarlyBinder<'tcx, &'tcx [ty::GenericArg<'tcx>]>> {
if let Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) =
tcx.opt_rpitit_info(def_id.to_def_id())
{
return tcx.rendered_precise_capturing_args(opaque_def_id);
return tcx.opt_precise_capturing_args(opaque_def_id);
}

let icx = ItemCtxt::new(tcx, def_id);

tcx.hir_node_by_def_id(def_id).expect_item().expect_opaque_ty().bounds.iter().find_map(
|bound| match bound {
hir::GenericBound::Use(args, ..) => {
Some(&*tcx.arena.alloc_from_iter(args.iter().map(|arg| arg.name())))
Some(ty::EarlyBinder::bind(&*tcx.arena.alloc_from_iter(args.iter().map(|arg| {
match arg {
hir::PreciseCapturingArg::Lifetime(lt) => icx
.lowerer()
.lower_lifetime(*lt, RegionInferReason::OutlivesBound)
.into(),
hir::PreciseCapturingArg::Param(arg) => match arg.res {
Res::Def(DefKind::TyParam, _) => {
icx.lowerer().lower_ty_param(arg.hir_id).into()
}
Res::Def(DefKind::ConstParam, _) => {
icx.lowerer().lower_const_param(arg.hir_id).into()
}
_ => unreachable!(),
},
}
}))))
}
_ => None,
},
Expand Down
16 changes: 15 additions & 1 deletion compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>>
}
}

impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>>
ProcessQueryValue<'tcx, Option<ty::EarlyBinder<'tcx, &'tcx [T]>>>
for Option<DecodeIterator<'a, 'tcx, T>>
{
#[inline(always)]
fn process_decoded(
self,
tcx: TyCtxt<'tcx>,
_err: impl Fn() -> !,
) -> Option<ty::EarlyBinder<'tcx, &'tcx [T]>> {
self.map(|iter| ty::EarlyBinder::bind(&*tcx.arena.alloc_from_iter(iter)))
}
}

impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>>
ProcessQueryValue<'tcx, Option<&'tcx [T]>> for Option<DecodeIterator<'a, 'tcx, T>>
{
Expand Down Expand Up @@ -285,7 +299,7 @@ provide! { tcx, def_id, other, cdata,
.process_decoded(tcx, || panic!("{def_id:?} does not have coerce_unsized_info"))) }
mir_const_qualif => { table }
rendered_const => { table }
rendered_precise_capturing_args => { table }
opt_precise_capturing_args => { table }
asyncness => { table_direct }
fn_arg_names => { table }
coroutine_kind => { table_direct }
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1671,11 +1671,14 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}

fn encode_precise_capturing_args(&mut self, def_id: DefId) {
let Some(precise_capturing_args) = self.tcx.rendered_precise_capturing_args(def_id) else {
let Some(precise_capturing_args) = self.tcx.opt_precise_capturing_args(def_id) else {
return;
};

record_array!(self.tables.rendered_precise_capturing_args[def_id] <- precise_capturing_args);
record_array!(
self.tables.opt_precise_capturing_args[def_id]
<- precise_capturing_args.skip_binder()
);
}

fn encode_mir(&mut self) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ define_tables! {
coerce_unsized_info: Table<DefIndex, LazyValue<ty::adjustment::CoerceUnsizedInfo>>,
mir_const_qualif: Table<DefIndex, LazyValue<mir::ConstQualifs>>,
rendered_const: Table<DefIndex, LazyValue<String>>,
rendered_precise_capturing_args: Table<DefIndex, LazyArray<Symbol>>,
opt_precise_capturing_args: Table<DefIndex, LazyArray<ty::GenericArg<'static>>>,
asyncness: Table<DefIndex, ty::Asyncness>,
fn_arg_names: Table<DefIndex, LazyArray<Ident>>,
coroutine_kind: Table<DefIndex, hir::CoroutineKind>,
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/query/erase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ impl<T> EraseType for Option<&'_ [T]> {
type Result = [u8; size_of::<Option<&'static [()]>>()];
}

impl<T> EraseType for Option<ty::EarlyBinder<'_, &'_ [T]>> {
type Result = [u8; size_of::<Option<ty::EarlyBinder<'static, &'static [()]>>>()];
}

impl EraseType for Option<mir::DestructuredConstant<'_>> {
type Result = [u8; size_of::<Option<mir::DestructuredConstant<'static>>>()];
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,8 @@ rustc_queries! {
}

/// Gets the rendered precise capturing args for an opaque for use in rustdoc.
query rendered_precise_capturing_args(def_id: DefId) -> Option<&'tcx [Symbol]> {
query opt_precise_capturing_args(def_id: DefId)
-> Option<ty::EarlyBinder<'tcx, &'tcx [ty::GenericArg<'tcx>]>> {
desc { |tcx| "rendering precise capturing args for `{}`", tcx.def_path_str(def_id) }
separate_provide_extern
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_middle/src/ty/parameterized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,6 @@ parameterized_over_tcx! {
ty::Predicate,
ty::Clause,
ty::ClauseKind,
ty::ImplTraitHeader
ty::ImplTraitHeader,
ty::GenericArg,
}
6 changes: 3 additions & 3 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ fn clean_generic_bound<'tcx>(
GenericBound::TraitBound(clean_poly_trait_ref(t, cx), modifier)
}
hir::GenericBound::Use(args, ..) => {
GenericBound::Use(args.iter().map(|arg| arg.name()).collect())
GenericBound::Use(args.iter().map(|arg| arg.name().to_string()).collect())
}
})
}
Expand Down Expand Up @@ -2331,8 +2331,8 @@ fn clean_middle_opaque_bounds<'tcx>(
bounds.insert(0, GenericBound::sized(cx));
}

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

ImplTrait(bounds)
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,7 @@ pub(crate) enum GenericBound {
TraitBound(PolyTrait, hir::TraitBoundModifier),
Outlives(Lifetime),
/// `use<'a, T>` precise-capturing bound syntax
Use(Vec<Symbol>),
Use(Vec<String>),
}

impl GenericBound {
Expand Down

0 comments on commit d4f016d

Please sign in to comment.