Skip to content

Commit 45d4465

Browse files
committed
Account for late-bound depth when capturing all opaque lifetimes.
1 parent 145f9cf commit 45d4465

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -571,17 +571,23 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
571571
// We list scopes outwards, this causes us to see lifetime parameters in reverse
572572
// declaration order. In order to make it consistent with what `generics_of` might
573573
// give, we will reverse the IndexMap after early captures.
574+
let mut late_depth = 0;
574575
let mut scope = self.scope;
575576
let mut opaque_capture_scopes = vec![(opaque.def_id, &captures)];
576577
loop {
577578
match *scope {
578-
Scope::Binder { ref bound_vars, s, .. } => {
579+
Scope::Binder { ref bound_vars, scope_type, s, .. } => {
579580
for (&original_lifetime, &def) in bound_vars.iter().rev() {
580581
if let DefKind::LifetimeParam = self.tcx.def_kind(original_lifetime) {
582+
let def = def.shifted(late_depth);
581583
let ident = lifetime_ident(original_lifetime);
582584
self.remap_opaque_captures(&opaque_capture_scopes, def, ident);
583585
}
584586
}
587+
match scope_type {
588+
BinderScopeType::Normal => late_depth += 1,
589+
BinderScopeType::Concatenating => {}
590+
}
585591
scope = s;
586592
}
587593

@@ -602,6 +608,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
602608

603609
Scope::Opaque { captures, def_id, s } => {
604610
opaque_capture_scopes.push((def_id, captures));
611+
late_depth = 0;
605612
scope = s;
606613
}
607614

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Test for issue #132429
2+
//@compile-flags: -Zunstable-options --edition=2024
3+
4+
trait ThreeCellFragment {
5+
fn ext_cells<'a>(
6+
&'a self,
7+
) -> dyn core::future::Future<Output = impl IntoIterator<Item = u32>> + 'a {
8+
//~^ ERROR mismatched types
9+
//~| ERROR return type cannot have an unboxed trait object
10+
}
11+
}
12+
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/late-bound-in-object-assocty.rs:7:80
3+
|
4+
LL | ) -> dyn core::future::Future<Output = impl IntoIterator<Item = u32>> + 'a {
5+
| ________________________________________________________________________________^
6+
LL | |
7+
LL | |
8+
LL | | }
9+
| |_____^ expected `dyn Future`, found `()`
10+
|
11+
= note: expected trait object `(dyn Future<Output = _> + 'a)`
12+
found unit type `()`
13+
14+
error[E0746]: return type cannot have an unboxed trait object
15+
--> $DIR/late-bound-in-object-assocty.rs:7:10
16+
|
17+
LL | ) -> dyn core::future::Future<Output = impl IntoIterator<Item = u32>> + 'a {
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
19+
|
20+
help: consider returning an `impl Trait` instead of a `dyn Trait`
21+
|
22+
LL | ) -> impl core::future::Future<Output = impl IntoIterator<Item = u32>> + 'a {
23+
| ~~~~
24+
help: alternatively, box the return type, and wrap all of the returned values in `Box::new`
25+
|
26+
LL | ) -> Box<dyn core::future::Future<Output = impl IntoIterator<Item = u32>> + 'a> {
27+
| ++++ +
28+
29+
error: aborting due to 2 previous errors
30+
31+
Some errors have detailed explanations: E0308, E0746.
32+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)