forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#131789 - compiler-errors:capture-more, r=fmease Make sure that outer opaques capture inner opaques's lifetimes even with precise capturing syntax When lowering an opaque, we must capture and duplicate all of the lifetimes in the opaque's bounds to correctly lower the opaque's bounds. We do this *even if* the lifetime is not captured according to the `+ use<>` precise capturing bound; in that case, we will later reject that captured lifetime. For example, Given an opaque like `impl Sized + 'a + use<>`, we will still duplicate `'a` but later error that it is not mentioned in the `use<>` bound. The current heuristic was not properly handling cases like: ``` //@ edition: 2024 fn foo<'a>() -> impl Trait<Assoc = impl Trait2> + use<> {} ``` Which forces the outer `impl Trait` to capture `'a` since `impl Trait2` *implicitly* captures `'a` due to the new lifetime capture rules for edition 2024. We were only capturing lifetimes syntactically mentioned in the bounds. (Note that this still is an error; we just need to capture `'a` so it is handled later in the compiler correctly -- hence the ICE in rust-lang#131769 where a late-bound lifetime was being referenced outside of its binder). This PR reworks the way we collect lifetimes to capture and duplicate in AST lowering to fix this. Fixes rust-lang#131769
- Loading branch information
Showing
5 changed files
with
102 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
tests/ui/impl-trait/precise-capturing/capturing-implicit.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//@ edition: 2024 | ||
//@ compile-flags: -Zunstable-options | ||
|
||
#![feature(rustc_attrs)] | ||
#![feature(type_alias_impl_trait)] | ||
#![rustc_variance_of_opaques] | ||
|
||
fn foo(x: &()) -> impl IntoIterator<Item = impl Sized> + use<> { | ||
//~^ ERROR ['_: o] | ||
//~| ERROR ['_: o] | ||
//~| ERROR `impl Trait` captures lifetime parameter | ||
[*x] | ||
} | ||
|
||
fn main() {} |
22 changes: 22 additions & 0 deletions
22
tests/ui/impl-trait/precise-capturing/capturing-implicit.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error: `impl Trait` captures lifetime parameter, but it is not mentioned in `use<...>` precise captures list | ||
--> $DIR/capturing-implicit.rs:8:11 | ||
| | ||
LL | fn foo(x: &()) -> impl IntoIterator<Item = impl Sized> + use<> { | ||
| ^ -------------------------------------------- lifetime captured due to being mentioned in the bounds of the `impl Trait` | ||
| | | ||
| this lifetime parameter is captured | ||
|
||
error: ['_: o] | ||
--> $DIR/capturing-implicit.rs:8:19 | ||
| | ||
LL | fn foo(x: &()) -> impl IntoIterator<Item = impl Sized> + use<> { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: ['_: o] | ||
--> $DIR/capturing-implicit.rs:8:44 | ||
| | ||
LL | fn foo(x: &()) -> impl IntoIterator<Item = impl Sized> + use<> { | ||
| ^^^^^^^^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|