mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-01 23:12:02 +00:00
Reorganize opaque lowering code
This commit is contained in:
parent
33d21e62d0
commit
72a32583d1
@ -1539,9 +1539,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
|||||||
);
|
);
|
||||||
debug!(?opaque_ty_def_id);
|
debug!(?opaque_ty_def_id);
|
||||||
|
|
||||||
// Contains the new lifetime definitions created for the TAIT (if any).
|
|
||||||
let mut collected_lifetimes = Vec::new();
|
|
||||||
|
|
||||||
// If this came from a TAIT (as opposed to a function that returns an RPIT), we only want
|
// If this came from a TAIT (as opposed to a function that returns an RPIT), we only want
|
||||||
// to capture the lifetimes that appear in the bounds. So visit the bounds to find out
|
// to capture the lifetimes that appear in the bounds. So visit the bounds to find out
|
||||||
// exactly which ones those are.
|
// exactly which ones those are.
|
||||||
@ -1558,20 +1555,29 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
|||||||
};
|
};
|
||||||
debug!(?lifetimes_to_remap);
|
debug!(?lifetimes_to_remap);
|
||||||
|
|
||||||
|
let mut new_remapping = FxHashMap::default();
|
||||||
|
|
||||||
|
// Contains the new lifetime definitions created for the TAIT (if any).
|
||||||
|
// If this opaque type is only capturing a subset of the lifetimes (those that appear in
|
||||||
|
// bounds), then create the new lifetime parameters required and create a mapping from the
|
||||||
|
// old `'a` (on the function) to the new `'a` (on the opaque type).
|
||||||
|
let collected_lifetimes =
|
||||||
|
self.create_lifetime_defs(opaque_ty_def_id, &lifetimes_to_remap, &mut new_remapping);
|
||||||
|
debug!(?collected_lifetimes);
|
||||||
|
debug!(?new_remapping);
|
||||||
|
|
||||||
|
// This creates HIR lifetime arguments as `hir::GenericArg`, in the given example `type
|
||||||
|
// TestReturn<'a, T, 'x> = impl Debug + 'x`, it creates a collection containing `&['x]`.
|
||||||
|
let lifetimes: Vec<_> = collected_lifetimes
|
||||||
|
.iter()
|
||||||
|
.map(|(_, lifetime)| {
|
||||||
|
let id = self.next_node_id();
|
||||||
|
self.new_named_lifetime(lifetime.id, id, lifetime.ident)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
debug!(?lifetimes);
|
||||||
|
|
||||||
self.with_hir_id_owner(opaque_ty_node_id, |lctx| {
|
self.with_hir_id_owner(opaque_ty_node_id, |lctx| {
|
||||||
let mut new_remapping = FxHashMap::default();
|
|
||||||
|
|
||||||
// If this opaque type is only capturing a subset of the lifetimes (those that appear
|
|
||||||
// in bounds), then create the new lifetime parameters required and create a mapping
|
|
||||||
// from the old `'a` (on the function) to the new `'a` (on the opaque type).
|
|
||||||
collected_lifetimes = lctx.create_lifetime_defs(
|
|
||||||
opaque_ty_def_id,
|
|
||||||
&lifetimes_to_remap,
|
|
||||||
&mut new_remapping,
|
|
||||||
);
|
|
||||||
debug!(?collected_lifetimes);
|
|
||||||
debug!(?new_remapping);
|
|
||||||
|
|
||||||
// Install the remapping from old to new (if any):
|
// Install the remapping from old to new (if any):
|
||||||
lctx.with_remapping(new_remapping, |lctx| {
|
lctx.with_remapping(new_remapping, |lctx| {
|
||||||
// This creates HIR lifetime definitions as `hir::GenericParam`, in the given
|
// This creates HIR lifetime definitions as `hir::GenericParam`, in the given
|
||||||
@ -1630,12 +1636,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
|||||||
|
|
||||||
// This creates HIR lifetime arguments as `hir::GenericArg`, in the given example `type
|
// This creates HIR lifetime arguments as `hir::GenericArg`, in the given example `type
|
||||||
// TestReturn<'a, T, 'x> = impl Debug + 'x`, it creates a collection containing `&['x]`.
|
// TestReturn<'a, T, 'x> = impl Debug + 'x`, it creates a collection containing `&['x]`.
|
||||||
let lifetimes =
|
let lifetimes = self.arena.alloc_from_iter(
|
||||||
self.arena.alloc_from_iter(collected_lifetimes.into_iter().map(|(_, lifetime)| {
|
lifetimes.into_iter().map(|lifetime| hir::GenericArg::Lifetime(lifetime)),
|
||||||
let id = self.next_node_id();
|
);
|
||||||
let l = self.new_named_lifetime(lifetime.id, id, lifetime.ident);
|
|
||||||
hir::GenericArg::Lifetime(l)
|
|
||||||
}));
|
|
||||||
debug!(?lifetimes);
|
debug!(?lifetimes);
|
||||||
|
|
||||||
// `impl Trait` now just becomes `Foo<'a, 'b, ..>`.
|
// `impl Trait` now just becomes `Foo<'a, 'b, ..>`.
|
||||||
@ -1993,22 +1996,32 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
|||||||
let lifetimes_to_remap = lifetime_collector::lifetimes_in_ret_ty(&self.resolver, output);
|
let lifetimes_to_remap = lifetime_collector::lifetimes_in_ret_ty(&self.resolver, output);
|
||||||
debug!(?lifetimes_to_remap);
|
debug!(?lifetimes_to_remap);
|
||||||
|
|
||||||
self.with_hir_id_owner(opaque_ty_node_id, |this| {
|
// If this opaque type is only capturing a subset of the lifetimes (those that appear in
|
||||||
// If this opaque type is only capturing a subset of the lifetimes (those that appear
|
// bounds), then create the new lifetime parameters required and create a mapping from the
|
||||||
// in bounds), then create the new lifetime parameters required and create a mapping
|
// old `'a` (on the function) to the new `'a` (on the opaque type).
|
||||||
// from the old `'a` (on the function) to the new `'a` (on the opaque type).
|
collected_lifetimes.extend(
|
||||||
collected_lifetimes.extend(
|
self.create_lifetime_defs(opaque_ty_def_id, &lifetimes_to_remap, &mut new_remapping)
|
||||||
this.create_lifetime_defs(
|
|
||||||
opaque_ty_def_id,
|
|
||||||
&lifetimes_to_remap,
|
|
||||||
&mut new_remapping,
|
|
||||||
)
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(new_node_id, lifetime)| (new_node_id, lifetime, None)),
|
.map(|(new_node_id, lifetime)| (new_node_id, lifetime, None)),
|
||||||
);
|
);
|
||||||
debug!(?collected_lifetimes);
|
debug!(?collected_lifetimes);
|
||||||
debug!(?new_remapping);
|
debug!(?new_remapping);
|
||||||
|
|
||||||
|
// This creates HIR lifetime arguments as `hir::GenericArg`, in the given example `type
|
||||||
|
// TestReturn<'a, T, 'x> = impl Debug + 'x`, it creates a collection containing `&['x]`.
|
||||||
|
let lifetimes: Vec<_> = collected_lifetimes
|
||||||
|
.iter()
|
||||||
|
.map(|(_, lifetime, res)| {
|
||||||
|
let id = self.next_node_id();
|
||||||
|
let res = res.unwrap_or(
|
||||||
|
self.resolver.get_lifetime_res(lifetime.id).unwrap_or(LifetimeRes::Error),
|
||||||
|
);
|
||||||
|
self.new_named_lifetime_with_res(id, lifetime.ident, res)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
debug!(?lifetimes);
|
||||||
|
|
||||||
|
self.with_hir_id_owner(opaque_ty_node_id, |this| {
|
||||||
// Install the remapping from old to new (if any):
|
// Install the remapping from old to new (if any):
|
||||||
this.with_remapping(new_remapping, |this| {
|
this.with_remapping(new_remapping, |this| {
|
||||||
// We have to be careful to get elision right here. The
|
// We have to be careful to get elision right here. The
|
||||||
@ -2096,15 +2109,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
|||||||
//
|
//
|
||||||
// For the "output" lifetime parameters, we just want to
|
// For the "output" lifetime parameters, we just want to
|
||||||
// generate `'_`.
|
// generate `'_`.
|
||||||
let generic_args = self.arena.alloc_from_iter(collected_lifetimes.into_iter().map(
|
let generic_args = self
|
||||||
|(_, lifetime, res)| {
|
.arena
|
||||||
let id = self.next_node_id();
|
.alloc_from_iter(lifetimes.iter().map(|lifetime| hir::GenericArg::Lifetime(*lifetime)));
|
||||||
let res = res.unwrap_or(
|
|
||||||
self.resolver.get_lifetime_res(lifetime.id).unwrap_or(LifetimeRes::Error),
|
|
||||||
);
|
|
||||||
hir::GenericArg::Lifetime(self.new_named_lifetime_with_res(id, lifetime.ident, res))
|
|
||||||
},
|
|
||||||
));
|
|
||||||
|
|
||||||
// Create the `Foo<...>` reference itself. Note that the `type
|
// Create the `Foo<...>` reference itself. Note that the `type
|
||||||
// Foo = impl Trait` is, internally, created as a child of the
|
// Foo = impl Trait` is, internally, created as a child of the
|
||||||
|
Loading…
Reference in New Issue
Block a user