Skip to content

Commit b7297ac

Browse files
compiler-errorsworkingjubilee
authored andcommitted
Add gate for precise capturing in traits
1 parent 8d94e06 commit b7297ac

File tree

10 files changed

+54
-5
lines changed

10 files changed

+54
-5
lines changed

Diff for: compiler/rustc_ast_lowering/src/lib.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -1573,11 +1573,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15731573
// Feature gate for RPITIT + use<..>
15741574
match origin {
15751575
rustc_hir::OpaqueTyOrigin::FnReturn { in_trait_or_impl: Some(_), .. } => {
1576-
if let Some(span) = bounds.iter().find_map(|bound| match *bound {
1577-
ast::GenericBound::Use(_, span) => Some(span),
1578-
_ => None,
1579-
}) {
1580-
self.tcx.dcx().emit_err(errors::NoPreciseCapturesOnRpitit { span });
1576+
if !self.tcx.features().precise_capturing_in_traits
1577+
&& let Some(span) = bounds.iter().find_map(|bound| match *bound {
1578+
ast::GenericBound::Use(_, span) => Some(span),
1579+
_ => None,
1580+
})
1581+
{
1582+
let mut diag =
1583+
self.tcx.dcx().create_err(errors::NoPreciseCapturesOnRpitit { span });
1584+
add_feature_diagnostics(
1585+
&mut diag,
1586+
self.tcx.sess,
1587+
sym::precise_capturing_in_traits,
1588+
);
1589+
diag.emit();
15811590
}
15821591
}
15831592
_ => {}

Diff for: compiler/rustc_feature/src/unstable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,8 @@ declare_features! (
565565
(incomplete, pin_ergonomics, "CURRENT_RUSTC_VERSION", Some(130494)),
566566
/// Allows postfix match `expr.match { ... }`
567567
(unstable, postfix_match, "1.79.0", Some(121618)),
568+
/// Allows `use<..>` precise capturign on impl Trait in traits.
569+
(unstable, precise_capturing_in_traits, "CURRENT_RUSTC_VERSION", Some(130044)),
568570
/// Allows macro attributes on expressions, statements and non-inline modules.
569571
(unstable, proc_macro_hygiene, "1.30.0", Some(54727)),
570572
/// Makes `&` and `&mut` patterns eat only one layer of references in Rust 2024.

Diff for: compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1479,6 +1479,7 @@ symbols! {
14791479
powif64,
14801480
pre_dash_lto: "pre-lto",
14811481
precise_capturing,
1482+
precise_capturing_in_traits,
14821483
precise_pointer_size_matching,
14831484
pref_align_of,
14841485
prefetch_read_data,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
trait Foo {
2+
fn test() -> impl Sized + use<Self>;
3+
//~^ ERROR `use<...>` precise capturing syntax is currently not allowed in return-position
4+
}
5+
6+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: `use<...>` precise capturing syntax is currently not allowed in return-position `impl Trait` in traits
2+
--> $DIR/feature-gate-precise_capturing_in_traits.rs:2:31
3+
|
4+
LL | fn test() -> impl Sized + use<Self>;
5+
| ^^^^^^^^^
6+
|
7+
= note: currently, return-position `impl Trait` in traits and trait implementations capture all lifetimes in scope
8+
= note: see issue #130044 <https://github.com/rust-lang/rust/issues/130044> for more information
9+
= help: add `#![feature(precise_capturing_in_traits)]` to the crate attributes to enable
10+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
11+
12+
error: aborting due to 1 previous error
13+

Diff for: tests/ui/impl-trait/precise-capturing/forgot-to-capture-type.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ LL | fn bar() -> impl Sized + use<>;
55
| ^^^^^
66
|
77
= note: currently, return-position `impl Trait` in traits and trait implementations capture all lifetimes in scope
8+
= note: see issue #130044 <https://github.com/rust-lang/rust/issues/130044> for more information
9+
= help: add `#![feature(precise_capturing_in_traits)]` to the crate attributes to enable
10+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
811

912
error: `impl Trait` must mention all type parameters in scope in `use<...>`
1013
--> $DIR/forgot-to-capture-type.rs:1:23

Diff for: tests/ui/impl-trait/precise-capturing/redundant.rpitit.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ LL | fn in_trait() -> impl Sized + use<'a, Self>;
55
| ^^^^^^^^^^^^^
66
|
77
= note: currently, return-position `impl Trait` in traits and trait implementations capture all lifetimes in scope
8+
= note: see issue #130044 <https://github.com/rust-lang/rust/issues/130044> for more information
9+
= help: add `#![feature(precise_capturing_in_traits)]` to the crate attributes to enable
10+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
811

912
error: `use<...>` precise capturing syntax is currently not allowed in return-position `impl Trait` in traits
1013
--> $DIR/redundant.rs:21:35
@@ -13,6 +16,9 @@ LL | fn in_trait() -> impl Sized + use<'a> {}
1316
| ^^^^^^^
1417
|
1518
= note: currently, return-position `impl Trait` in traits and trait implementations capture all lifetimes in scope
19+
= note: see issue #130044 <https://github.com/rust-lang/rust/issues/130044> for more information
20+
= help: add `#![feature(precise_capturing_in_traits)]` to the crate attributes to enable
21+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1622

1723
error: aborting due to 2 previous errors
1824

Diff for: tests/ui/impl-trait/precise-capturing/rpitit-captures-more-method-lifetimes.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ LL | fn bar<'tr: 'tr>(&'tr mut self) -> impl Sized + use<Self>;
55
| ^^^^^^^^^
66
|
77
= note: currently, return-position `impl Trait` in traits and trait implementations capture all lifetimes in scope
8+
= note: see issue #130044 <https://github.com/rust-lang/rust/issues/130044> for more information
9+
= help: add `#![feature(precise_capturing_in_traits)]` to the crate attributes to enable
10+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
811

912
error: return type captures more lifetimes than trait definition
1013
--> $DIR/rpitit-captures-more-method-lifetimes.rs:11:40

Diff for: tests/ui/impl-trait/precise-capturing/rpitit.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ LL | fn hello() -> impl PartialEq + use<Self>;
55
| ^^^^^^^^^
66
|
77
= note: currently, return-position `impl Trait` in traits and trait implementations capture all lifetimes in scope
8+
= note: see issue #130044 <https://github.com/rust-lang/rust/issues/130044> for more information
9+
= help: add `#![feature(precise_capturing_in_traits)]` to the crate attributes to enable
10+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
811

912
error: `impl Trait` captures lifetime parameter, but it is not mentioned in `use<...>` precise captures list
1013
--> $DIR/rpitit.rs:9:19

Diff for: tests/ui/impl-trait/precise-capturing/self-capture.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ LL | fn bar<'a>() -> impl Sized + use<Self>;
55
| ^^^^^^^^^
66
|
77
= note: currently, return-position `impl Trait` in traits and trait implementations capture all lifetimes in scope
8+
= note: see issue #130044 <https://github.com/rust-lang/rust/issues/130044> for more information
9+
= help: add `#![feature(precise_capturing_in_traits)]` to the crate attributes to enable
10+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
811

912
error: aborting due to 1 previous error
1013

0 commit comments

Comments
 (0)