Skip to content

Commit feac2ec

Browse files
committed
Auto merge of #94088 - oli-obk:revert, r=jackh726
Revert #91403 fixes #94004 r? `@pnkfelix` `@cjgillot`
2 parents f838a42 + 86d17b9 commit feac2ec

34 files changed

+281
-227
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+50-19
Original file line numberDiff line numberDiff line change
@@ -1659,12 +1659,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16591659

16601660
// When we create the opaque type for this async fn, it is going to have
16611661
// to capture all the lifetimes involved in the signature (including in the
1662-
// return type). This is done by:
1662+
// return type). This is done by introducing lifetime parameters for:
16631663
//
1664-
// - making the opaque type inherit all lifetime parameters from its parent;
1665-
// - make all the elided lifetimes in the fn arguments into parameters;
1666-
// - manually introducing parameters on the opaque type for elided
1667-
// lifetimes in the return type.
1664+
// - all the explicitly declared lifetimes from the impl and function itself;
1665+
// - all the elided lifetimes in the fn arguments;
1666+
// - all the elided lifetimes in the return type.
16681667
//
16691668
// So for example in this snippet:
16701669
//
@@ -1680,22 +1679,44 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16801679
// we would create an opaque type like:
16811680
//
16821681
// ```
1683-
// type Foo<'a>::bar<'b, '0, '1>::Bar<'2> = impl Future<Output = &'2 u32>;
1682+
// type Bar<'a, 'b, '0, '1, '2> = impl Future<Output = &'2 u32>;
16841683
// ```
16851684
//
16861685
// and we would then desugar `bar` to the equivalent of:
16871686
//
16881687
// ```rust
16891688
// impl<'a> Foo<'a> {
1690-
// fn bar<'b, '0, '1>(&'0 self, x: &'b Vec<f64>, y: &'1 str) -> Bar<'_>
1689+
// fn bar<'b, '0, '1>(&'0 self, x: &'b Vec<f64>, y: &'1 str) -> Bar<'a, 'b, '0, '1, '_>
16911690
// }
16921691
// ```
16931692
//
16941693
// Note that the final parameter to `Bar` is `'_`, not `'2` --
16951694
// this is because the elided lifetimes from the return type
16961695
// should be figured out using the ordinary elision rules, and
16971696
// this desugaring achieves that.
1698-
let mut lifetime_params = Vec::new();
1697+
1698+
debug!("lower_async_fn_ret_ty: in_scope_lifetimes={:#?}", self.in_scope_lifetimes);
1699+
debug!("lower_async_fn_ret_ty: lifetimes_to_define={:#?}", self.lifetimes_to_define);
1700+
1701+
// Calculate all the lifetimes that should be captured
1702+
// by the opaque type. This should include all in-scope
1703+
// lifetime parameters, including those defined in-band.
1704+
//
1705+
// `lifetime_params` is a vector of tuple (span, parameter name, lifetime name).
1706+
1707+
// Input lifetime like `'a` or `'1`:
1708+
let mut lifetime_params: Vec<_> = self
1709+
.in_scope_lifetimes
1710+
.iter()
1711+
.cloned()
1712+
.map(|name| (name.ident().span, name, hir::LifetimeName::Param(name)))
1713+
.chain(
1714+
self.lifetimes_to_define
1715+
.iter()
1716+
.map(|&(span, name)| (span, name, hir::LifetimeName::Param(name))),
1717+
)
1718+
.collect();
1719+
16991720
self.with_hir_id_owner(opaque_ty_node_id, |this| {
17001721
// We have to be careful to get elision right here. The
17011722
// idea is that we create a lifetime parameter for each
@@ -1714,12 +1735,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17141735
debug!("lower_async_fn_ret_ty: future_bound={:#?}", future_bound);
17151736
debug!("lower_async_fn_ret_ty: lifetimes_to_define={:#?}", lifetimes_to_define);
17161737

1717-
// Output lifetime like `'_`:
1718-
lifetime_params = lifetimes_to_define;
1738+
lifetime_params.extend(
1739+
// Output lifetime like `'_`:
1740+
lifetimes_to_define
1741+
.into_iter()
1742+
.map(|(span, name)| (span, name, hir::LifetimeName::Implicit(false))),
1743+
);
17191744
debug!("lower_async_fn_ret_ty: lifetime_params={:#?}", lifetime_params);
17201745

17211746
let generic_params =
1722-
this.arena.alloc_from_iter(lifetime_params.iter().map(|&(span, hir_name)| {
1747+
this.arena.alloc_from_iter(lifetime_params.iter().map(|&(span, hir_name, _)| {
17231748
this.lifetime_to_generic_param(span, hir_name, opaque_ty_def_id)
17241749
}));
17251750

@@ -1737,22 +1762,28 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17371762
this.generate_opaque_type(opaque_ty_def_id, opaque_ty_item, span, opaque_ty_span)
17381763
});
17391764

1740-
// We need to create the lifetime arguments to our opaque type.
1741-
// Continuing with our example, we're creating the type arguments
1742-
// for the return type:
1765+
// As documented above on the variable
1766+
// `input_lifetimes_count`, we need to create the lifetime
1767+
// arguments to our opaque type. Continuing with our example,
1768+
// we're creating the type arguments for the return type:
17431769
//
17441770
// ```
1745-
// For<'a>::bar<'b, '0, '1>::Bar<'_>
1771+
// Bar<'a, 'b, '0, '1, '_>
17461772
// ```
17471773
//
1748-
// For the "input" lifetime parameters are inherited automatically.
1749-
// For the "output" lifetime parameters, we just want to generate `'_`.
1774+
// For the "input" lifetime parameters, we wish to create
1775+
// references to the parameters themselves, including the
1776+
// "implicit" ones created from parameter types (`'a`, `'b`,
1777+
// '`0`, `'1`).
1778+
//
1779+
// For the "output" lifetime parameters, we just want to
1780+
// generate `'_`.
17501781
let generic_args =
1751-
self.arena.alloc_from_iter(lifetime_params.into_iter().map(|(span, _)| {
1782+
self.arena.alloc_from_iter(lifetime_params.into_iter().map(|(span, _, name)| {
17521783
GenericArg::Lifetime(hir::Lifetime {
17531784
hir_id: self.next_id(),
17541785
span: self.lower_span(span),
1755-
name: hir::LifetimeName::Implicit(false),
1786+
name,
17561787
})
17571788
}));
17581789

compiler/rustc_borrowck/src/region_infer/mod.rs

-18
Original file line numberDiff line numberDiff line change
@@ -2156,24 +2156,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
21562156
}
21572157
}
21582158

2159-
// When in async fn, prefer errors that come from inside the closure.
2160-
if !categorized_path[i].from_closure {
2161-
let span = categorized_path.iter().find_map(|p| {
2162-
if p.from_closure
2163-
&& p.category == categorized_path[i].category
2164-
&& categorized_path[i].cause.span.contains(p.cause.span)
2165-
{
2166-
Some(p.cause.span)
2167-
} else {
2168-
None
2169-
}
2170-
});
2171-
2172-
if let Some(span) = span {
2173-
categorized_path[i].cause.span = span;
2174-
}
2175-
}
2176-
21772159
return categorized_path[i].clone();
21782160
}
21792161

compiler/rustc_error_codes/src/error_codes/E0760.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
`impl trait` return type cannot contain a projection
1+
`async fn`/`impl trait` return type cannot contain a projection
22
or `Self` that references lifetimes from a parent scope.
33

44
Erroneous code example:
@@ -7,7 +7,7 @@ Erroneous code example:
77
struct S<'a>(&'a i32);
88
99
impl<'a> S<'a> {
10-
fn new(i: &'a i32) -> impl Into<Self> {
10+
async fn new(i: &'a i32) -> Self {
1111
S(&22)
1212
}
1313
}
@@ -19,7 +19,7 @@ To fix this error we need to spell out `Self` to `S<'a>`:
1919
struct S<'a>(&'a i32);
2020
2121
impl<'a> S<'a> {
22-
fn new(i: &'a i32) -> impl Into<S<'a>> {
22+
async fn new(i: &'a i32) -> S<'a> {
2323
S(&22)
2424
}
2525
}

compiler/rustc_infer/src/infer/opaque_types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
276276
debug!(?concrete_ty);
277277

278278
let first_own_region = match opaque_defn.origin {
279-
hir::OpaqueTyOrigin::FnReturn(..) => {
279+
hir::OpaqueTyOrigin::FnReturn(..) | hir::OpaqueTyOrigin::AsyncFn(..) => {
280280
// We lower
281281
//
282282
// fn foo<'l0..'ln>() -> impl Trait<'l0..'lm>
@@ -291,7 +291,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
291291
}
292292
// These opaque type inherit all lifetime parameters from their
293293
// parent, so we have to check them all.
294-
hir::OpaqueTyOrigin::AsyncFn(..) | hir::OpaqueTyOrigin::TyAlias => 0,
294+
hir::OpaqueTyOrigin::TyAlias => 0,
295295
};
296296

297297
// For a case like `impl Foo<'a, 'b>`, we would generate a constraint

compiler/rustc_resolve/src/late/lifetimes.rs

+8-26
Original file line numberDiff line numberDiff line change
@@ -729,16 +729,9 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
729729
match item.kind {
730730
hir::ItemKind::Fn(ref sig, ref generics, _) => {
731731
self.missing_named_lifetime_spots.push(generics.into());
732-
self.visit_early_late(
733-
None,
734-
item.hir_id(),
735-
&sig.decl,
736-
generics,
737-
sig.header.asyncness,
738-
|this| {
739-
intravisit::walk_item(this, item);
740-
},
741-
);
732+
self.visit_early_late(None, item.hir_id(), &sig.decl, generics, |this| {
733+
intravisit::walk_item(this, item);
734+
});
742735
self.missing_named_lifetime_spots.pop();
743736
}
744737

@@ -856,16 +849,11 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
856849

857850
fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem<'tcx>) {
858851
match item.kind {
859-
hir::ForeignItemKind::Fn(ref decl, _, ref generics) => self.visit_early_late(
860-
None,
861-
item.hir_id(),
862-
decl,
863-
generics,
864-
hir::IsAsync::NotAsync,
865-
|this| {
852+
hir::ForeignItemKind::Fn(ref decl, _, ref generics) => {
853+
self.visit_early_late(None, item.hir_id(), decl, generics, |this| {
866854
intravisit::walk_foreign_item(this, item);
867-
},
868-
),
855+
})
856+
}
869857
hir::ForeignItemKind::Static(..) => {
870858
intravisit::walk_foreign_item(self, item);
871859
}
@@ -1142,7 +1130,6 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
11421130
trait_item.hir_id(),
11431131
&sig.decl,
11441132
&trait_item.generics,
1145-
sig.header.asyncness,
11461133
|this| intravisit::walk_trait_item(this, trait_item),
11471134
);
11481135
self.missing_named_lifetime_spots.pop();
@@ -1212,7 +1199,6 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
12121199
impl_item.hir_id(),
12131200
&sig.decl,
12141201
&impl_item.generics,
1215-
sig.header.asyncness,
12161202
|this| intravisit::walk_impl_item(this, impl_item),
12171203
);
12181204
self.missing_named_lifetime_spots.pop();
@@ -2173,15 +2159,11 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
21732159
hir_id: hir::HirId,
21742160
decl: &'tcx hir::FnDecl<'tcx>,
21752161
generics: &'tcx hir::Generics<'tcx>,
2176-
asyncness: hir::IsAsync,
21772162
walk: F,
21782163
) where
21792164
F: for<'b, 'c> FnOnce(&'b mut LifetimeContext<'c, 'tcx>),
21802165
{
2181-
// Async fns need all their lifetime parameters to be early bound.
2182-
if asyncness != hir::IsAsync::Async {
2183-
insert_late_bound_lifetimes(self.map, decl, generics);
2184-
}
2166+
insert_late_bound_lifetimes(self.map, decl, generics);
21852167

21862168
// Find the start of nested early scopes, e.g., in methods.
21872169
let mut next_early_index = 0;

compiler/rustc_typeck/src/astconv/mod.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -2409,11 +2409,16 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24092409
let def_id = item_id.def_id.to_def_id();
24102410

24112411
match opaque_ty.kind {
2412-
hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) => {
2413-
let replace_parent_lifetimes =
2414-
matches!(origin, hir::OpaqueTyOrigin::FnReturn(..));
2415-
self.impl_trait_ty_to_ty(def_id, lifetimes, replace_parent_lifetimes)
2416-
}
2412+
hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) => self
2413+
.impl_trait_ty_to_ty(
2414+
def_id,
2415+
lifetimes,
2416+
matches!(
2417+
origin,
2418+
hir::OpaqueTyOrigin::FnReturn(..)
2419+
| hir::OpaqueTyOrigin::AsyncFn(..)
2420+
),
2421+
),
24172422
ref i => bug!("`impl Trait` pointed to non-opaque type?? {:#?}", i),
24182423
}
24192424
}

compiler/rustc_typeck/src/check/check.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,10 @@ pub(super) fn check_opaque_for_inheriting_lifetimes<'tcx>(
545545
}
546546
}
547547

548-
if let ItemKind::OpaqueTy(hir::OpaqueTy { origin: hir::OpaqueTyOrigin::FnReturn(..), .. }) =
549-
item.kind
548+
if let ItemKind::OpaqueTy(hir::OpaqueTy {
549+
origin: hir::OpaqueTyOrigin::AsyncFn(..) | hir::OpaqueTyOrigin::FnReturn(..),
550+
..
551+
}) = item.kind
550552
{
551553
let mut visitor = ProhibitOpaqueVisitor {
552554
opaque_identity_ty: tcx.mk_opaque(
@@ -568,13 +570,20 @@ pub(super) fn check_opaque_for_inheriting_lifetimes<'tcx>(
568570

569571
if let Some(ty) = prohibit_opaque.break_value() {
570572
visitor.visit_item(&item);
573+
let is_async = match item.kind {
574+
ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) => {
575+
matches!(origin, hir::OpaqueTyOrigin::AsyncFn(..))
576+
}
577+
_ => unreachable!(),
578+
};
571579

572580
let mut err = struct_span_err!(
573581
tcx.sess,
574582
span,
575583
E0760,
576-
"`impl Trait` return type cannot contain a projection or `Self` that references lifetimes from \
584+
"`{}` return type cannot contain a projection or `Self` that references lifetimes from \
577585
a parent scope",
586+
if is_async { "async fn" } else { "impl Trait" },
578587
);
579588

580589
for (span, name) in visitor.selftys {

compiler/rustc_typeck/src/collect.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2157,7 +2157,8 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
21572157
generics
21582158
}
21592159
ItemKind::OpaqueTy(OpaqueTy {
2160-
origin: hir::OpaqueTyOrigin::FnReturn(..), ..
2160+
origin: hir::OpaqueTyOrigin::AsyncFn(..) | hir::OpaqueTyOrigin::FnReturn(..),
2161+
..
21612162
}) => {
21622163
// return-position impl trait
21632164
//
@@ -2177,7 +2178,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
21772178
}
21782179
ItemKind::OpaqueTy(OpaqueTy {
21792180
ref generics,
2180-
origin: hir::OpaqueTyOrigin::AsyncFn(..) | hir::OpaqueTyOrigin::TyAlias,
2181+
origin: hir::OpaqueTyOrigin::TyAlias,
21812182
..
21822183
}) => {
21832184
// type-alias impl trait

src/librustdoc/clean/mod.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -585,12 +585,7 @@ fn clean_ty_generics(
585585
.params
586586
.iter()
587587
.filter_map(|param| match param.kind {
588-
ty::GenericParamDefKind::Lifetime => {
589-
if param.name == kw::UnderscoreLifetime {
590-
return None;
591-
}
592-
Some(param.clean(cx))
593-
}
588+
ty::GenericParamDefKind::Lifetime => Some(param.clean(cx)),
594589
ty::GenericParamDefKind::Type { synthetic, .. } => {
595590
if param.name == kw::SelfUpper {
596591
assert_eq!(param.index, 0);

src/test/ui/async-await/issue-61949-self-return-type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub struct Foo<'a> {
88

99
impl<'a> Foo<'a> {
1010
pub async fn new(_bar: &'a i32) -> Self {
11+
//~^ ERROR `async fn` return type cannot contain a projection or `Self` that references lifetimes from a parent scope
1112
Foo {
1213
bar: &22
1314
}
@@ -18,7 +19,6 @@ async fn foo() {
1819
let x = {
1920
let bar = 22;
2021
Foo::new(&bar).await
21-
//~^ ERROR `bar` does not live long enough [E0597]
2222
};
2323
drop(x);
2424
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
error[E0597]: `bar` does not live long enough
2-
--> $DIR/issue-61949-self-return-type.rs:20:18
1+
error[E0760]: `async fn` return type cannot contain a projection or `Self` that references lifetimes from a parent scope
2+
--> $DIR/issue-61949-self-return-type.rs:10:40
33
|
4-
LL | let x = {
5-
| - borrow later stored here
6-
LL | let bar = 22;
7-
LL | Foo::new(&bar).await
8-
| ^^^^ borrowed value does not live long enough
9-
LL |
10-
LL | };
11-
| - `bar` dropped here while still borrowed
4+
LL | pub async fn new(_bar: &'a i32) -> Self {
5+
| ^^^^ help: consider spelling out the type instead: `Foo<'a>`
126

137
error: aborting due to previous error
148

15-
For more information about this error, try `rustc --explain E0597`.
9+
For more information about this error, try `rustc --explain E0760`.

0 commit comments

Comments
 (0)