Auto merge of #116952 - compiler-errors:lifetime_capture_rules_2024, r=TaKO8Ki
Implement 2024-edition lifetime capture rules RFC Implements rust-lang/rfcs#3498.
This commit is contained in:
commit
b9068315db
11 changed files with 125 additions and 8 deletions
|
@ -1574,8 +1574,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
hir::OpaqueTyOrigin::FnReturn(..) => {
|
hir::OpaqueTyOrigin::FnReturn(..) => {
|
||||||
if let FnDeclKind::Impl | FnDeclKind::Trait =
|
if matches!(
|
||||||
fn_kind.expect("expected RPITs to be lowered with a FnKind")
|
fn_kind.expect("expected RPITs to be lowered with a FnKind"),
|
||||||
|
FnDeclKind::Impl | FnDeclKind::Trait
|
||||||
|
) || self.tcx.features().lifetime_capture_rules_2024
|
||||||
|
|| span.at_least_rust_2024()
|
||||||
{
|
{
|
||||||
// return-position impl trait in trait was decided to capture all
|
// return-position impl trait in trait was decided to capture all
|
||||||
// in-scope lifetimes, which we collect for all opaques during resolution.
|
// in-scope lifetimes, which we collect for all opaques during resolution.
|
||||||
|
|
|
@ -206,6 +206,8 @@ declare_features! (
|
||||||
(internal, intrinsics, "1.0.0", None, None),
|
(internal, intrinsics, "1.0.0", None, None),
|
||||||
/// Allows using `#[lang = ".."]` attribute for linking items to special compiler logic.
|
/// Allows using `#[lang = ".."]` attribute for linking items to special compiler logic.
|
||||||
(internal, lang_items, "1.0.0", None, None),
|
(internal, lang_items, "1.0.0", None, None),
|
||||||
|
/// Changes `impl Trait` to capture all lifetimes in scope.
|
||||||
|
(unstable, lifetime_capture_rules_2024, "CURRENT_RUSTC_VERSION", None, None),
|
||||||
/// Allows `#[link(..., cfg(..))]`; perma-unstable per #37406
|
/// Allows `#[link(..., cfg(..))]`; perma-unstable per #37406
|
||||||
(unstable, link_cfg, "1.14.0", None, None),
|
(unstable, link_cfg, "1.14.0", None, None),
|
||||||
/// Allows the `multiple_supertrait_upcastable` lint.
|
/// Allows the `multiple_supertrait_upcastable` lint.
|
||||||
|
|
|
@ -933,6 +933,7 @@ symbols! {
|
||||||
lib,
|
lib,
|
||||||
libc,
|
libc,
|
||||||
lifetime,
|
lifetime,
|
||||||
|
lifetime_capture_rules_2024,
|
||||||
lifetimes,
|
lifetimes,
|
||||||
likely,
|
likely,
|
||||||
line,
|
line,
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
fn foo(x: &Vec<i32>) -> impl Sized {
|
||||||
|
x
|
||||||
|
//~^ ERROR hidden type for `impl Sized` captures lifetime that does not appear in bounds
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
|
@ -0,0 +1,18 @@
|
||||||
|
error[E0700]: hidden type for `impl Sized` captures lifetime that does not appear in bounds
|
||||||
|
--> $DIR/feature-gate-lifetime-capture-rules-2024.rs:2:5
|
||||||
|
|
|
||||||
|
LL | fn foo(x: &Vec<i32>) -> impl Sized {
|
||||||
|
| --------- ---------- opaque type defined here
|
||||||
|
| |
|
||||||
|
| hidden type `&Vec<i32>` captures the anonymous lifetime defined here
|
||||||
|
LL | x
|
||||||
|
| ^
|
||||||
|
|
|
||||||
|
help: to declare that `impl Sized` captures `'_`, you can add an explicit `'_` lifetime bound
|
||||||
|
|
|
||||||
|
LL | fn foo(x: &Vec<i32>) -> impl Sized + '_ {
|
||||||
|
| ++++
|
||||||
|
|
||||||
|
error: aborting due to 1 previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0700`.
|
14
tests/ui/impl-trait/implicit-capture-late.rs
Normal file
14
tests/ui/impl-trait/implicit-capture-late.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
// known-bug: #117647
|
||||||
|
|
||||||
|
#![feature(lifetime_capture_rules_2024)]
|
||||||
|
#![feature(rustc_attrs)]
|
||||||
|
#![allow(internal_features)]
|
||||||
|
#![rustc_variance_of_opaques]
|
||||||
|
|
||||||
|
use std::ops::Deref;
|
||||||
|
|
||||||
|
fn foo(x: Vec<i32>) -> Box<dyn for<'a> Deref<Target = impl ?Sized>> {
|
||||||
|
Box::new(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
9
tests/ui/impl-trait/implicit-capture-late.stderr
Normal file
9
tests/ui/impl-trait/implicit-capture-late.stderr
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
error[E0657]: `impl Trait` can only capture lifetimes bound at the fn or impl level
|
||||||
|
--> $DIR/implicit-capture-late.rs:10:36
|
||||||
|
|
|
||||||
|
LL | fn foo(x: Vec<i32>) -> Box<dyn for<'a> Deref<Target = impl ?Sized>> {
|
||||||
|
| ^^
|
||||||
|
|
||||||
|
error: aborting due to 1 previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0657`.
|
26
tests/ui/impl-trait/variance.e2024.stderr
Normal file
26
tests/ui/impl-trait/variance.e2024.stderr
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
error: [*, o]
|
||||||
|
--> $DIR/variance.rs:14:36
|
||||||
|
|
|
||||||
|
LL | fn not_captured_early<'a: 'a>() -> impl Sized {}
|
||||||
|
| ^^^^^^^^^^
|
||||||
|
|
||||||
|
error: [*, o]
|
||||||
|
--> $DIR/variance.rs:19:32
|
||||||
|
|
|
||||||
|
LL | fn captured_early<'a: 'a>() -> impl Sized + Captures<'a> {}
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: [o]
|
||||||
|
--> $DIR/variance.rs:21:40
|
||||||
|
|
|
||||||
|
LL | fn not_captured_late<'a>(_: &'a ()) -> impl Sized {}
|
||||||
|
| ^^^^^^^^^^
|
||||||
|
|
||||||
|
error: [o]
|
||||||
|
--> $DIR/variance.rs:26:36
|
||||||
|
|
|
||||||
|
LL | fn captured_late<'a>(_: &'a ()) -> impl Sized + Captures<'a> {}
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: aborting due to 4 previous errors
|
||||||
|
|
26
tests/ui/impl-trait/variance.new.stderr
Normal file
26
tests/ui/impl-trait/variance.new.stderr
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
error: [*, o]
|
||||||
|
--> $DIR/variance.rs:14:36
|
||||||
|
|
|
||||||
|
LL | fn not_captured_early<'a: 'a>() -> impl Sized {}
|
||||||
|
| ^^^^^^^^^^
|
||||||
|
|
||||||
|
error: [*, o]
|
||||||
|
--> $DIR/variance.rs:19:32
|
||||||
|
|
|
||||||
|
LL | fn captured_early<'a: 'a>() -> impl Sized + Captures<'a> {}
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: [o]
|
||||||
|
--> $DIR/variance.rs:21:40
|
||||||
|
|
|
||||||
|
LL | fn not_captured_late<'a>(_: &'a ()) -> impl Sized {}
|
||||||
|
| ^^^^^^^^^^
|
||||||
|
|
||||||
|
error: [o]
|
||||||
|
--> $DIR/variance.rs:26:36
|
||||||
|
|
|
||||||
|
LL | fn captured_late<'a>(_: &'a ()) -> impl Sized + Captures<'a> {}
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: aborting due to 4 previous errors
|
||||||
|
|
|
@ -1,23 +1,23 @@
|
||||||
error: [*]
|
error: [*]
|
||||||
--> $DIR/variance.rs:8:36
|
--> $DIR/variance.rs:14:36
|
||||||
|
|
|
|
||||||
LL | fn not_captured_early<'a: 'a>() -> impl Sized {}
|
LL | fn not_captured_early<'a: 'a>() -> impl Sized {}
|
||||||
| ^^^^^^^^^^
|
| ^^^^^^^^^^
|
||||||
|
|
||||||
error: [*, o]
|
error: [*, o]
|
||||||
--> $DIR/variance.rs:10:32
|
--> $DIR/variance.rs:19:32
|
||||||
|
|
|
|
||||||
LL | fn captured_early<'a: 'a>() -> impl Sized + Captures<'a> {}
|
LL | fn captured_early<'a: 'a>() -> impl Sized + Captures<'a> {}
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: []
|
error: []
|
||||||
--> $DIR/variance.rs:12:40
|
--> $DIR/variance.rs:21:40
|
||||||
|
|
|
|
||||||
LL | fn not_captured_late<'a>(_: &'a ()) -> impl Sized {}
|
LL | fn not_captured_late<'a>(_: &'a ()) -> impl Sized {}
|
||||||
| ^^^^^^^^^^
|
| ^^^^^^^^^^
|
||||||
|
|
||||||
error: [o]
|
error: [o]
|
||||||
--> $DIR/variance.rs:14:36
|
--> $DIR/variance.rs:26:36
|
||||||
|
|
|
|
||||||
LL | fn captured_late<'a>(_: &'a ()) -> impl Sized + Captures<'a> {}
|
LL | fn captured_late<'a>(_: &'a ()) -> impl Sized + Captures<'a> {}
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
@ -1,3 +1,9 @@
|
||||||
|
// revisions: old new e2024
|
||||||
|
//[e2024] edition: 2024
|
||||||
|
//[e2024] compile-flags: -Z unstable-options
|
||||||
|
|
||||||
|
#![cfg_attr(new, feature(lifetime_capture_rules_2024))]
|
||||||
|
|
||||||
#![feature(rustc_attrs)]
|
#![feature(rustc_attrs)]
|
||||||
#![allow(internal_features)]
|
#![allow(internal_features)]
|
||||||
#![rustc_variance_of_opaques]
|
#![rustc_variance_of_opaques]
|
||||||
|
@ -5,11 +11,17 @@
|
||||||
trait Captures<'a> {}
|
trait Captures<'a> {}
|
||||||
impl<T> Captures<'_> for T {}
|
impl<T> Captures<'_> for T {}
|
||||||
|
|
||||||
fn not_captured_early<'a: 'a>() -> impl Sized {} //~ [*]
|
fn not_captured_early<'a: 'a>() -> impl Sized {}
|
||||||
|
//[old]~^ [*]
|
||||||
|
//[new]~^^ [*, o]
|
||||||
|
//[e2024]~^^^ [*, o]
|
||||||
|
|
||||||
fn captured_early<'a: 'a>() -> impl Sized + Captures<'a> {} //~ [*, o]
|
fn captured_early<'a: 'a>() -> impl Sized + Captures<'a> {} //~ [*, o]
|
||||||
|
|
||||||
fn not_captured_late<'a>(_: &'a ()) -> impl Sized {} //~ []
|
fn not_captured_late<'a>(_: &'a ()) -> impl Sized {}
|
||||||
|
//[old]~^ []
|
||||||
|
//[new]~^^ [o]
|
||||||
|
//[e2024]~^^^ [o]
|
||||||
|
|
||||||
fn captured_late<'a>(_: &'a ()) -> impl Sized + Captures<'a> {} //~ [o]
|
fn captured_late<'a>(_: &'a ()) -> impl Sized + Captures<'a> {} //~ [o]
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue