Skip to content
/ rust Public
forked from rust-lang/rust

Commit 1c84c06

Browse files
authored
Rollup merge of rust-lang#138128 - compiler-errors:precise-capturing-in-traits, r=oli-obk,traviscross
Stabilize `#![feature(precise_capturing_in_traits)]` # Precise capturing (`+ use<>` bounds) in traits - Stabilization Report Fixes rust-lang#130044. ## Stabilization summary This report proposes the stabilization of `use<>` precise capturing bounds in return-position impl traits in traits (RPITITs). This completes a missing part of [RFC 3617 "Precise capturing"]. Precise capturing in traits was not ready for stabilization when the first subset was proposed for stabilization (namely, RPITs on free and inherent functions - rust-lang#127672) since this feature has a slightly different implementation, and it hadn't yet been implemented or tested at the time. It is now complete, and the type system implications of this stabilization are detailed below. ## Motivation Currently, RPITITs capture all in-scope lifetimes, according to the decision made in the ["lifetime capture rules 2024" RFC](https://rust-lang.github.io/rfcs/3498-lifetime-capture-rules-2024.html#return-position-impl-trait-in-trait-rpitit). However, traits can be designed such that some lifetimes in arguments may not want to be captured. There is currently no way to express this. ## Major design decisions since the RFC No major decisions were made. This is simply an extension to the RFC that was understood as a follow-up from the original stabilization. ## What is stabilized? Users may write `+ use<'a, T>` bounds on their RPITITs. This conceptually modifies the desugaring of the RPITIT to omit the lifetimes that we would copy over from the method. For example, ```rust trait Foo { fn method<'a>(&'a self) -> impl Sized; // ... desugars to something like: type RPITIT_1<'a>: Sized; fn method_desugared<'a>(&'a self) -> Self::RPITIT_1<'a>; // ... whereas with precise capturing ... fn precise<'a>(&'a self) -> impl Sized + use<Self>; // ... desugars to something like: type RPITIT_2: Sized; fn precise_desugared<'a>(&'a self) -> Self::RPITIT_2; } ``` And thus the GAT doesn't name `'a`. In the compiler internals, it's not implemented exactly like this, but not in a way that users should expect to be able to observe. #### Limitations on what generics must be captured Currently, we require that all generics from the trait (including the `Self`) type are captured. This is because the generics from the trait are required to be *invariant* in order to do associated type normalization. And like regular precise capturing bounds, all type and const generics in scope must be captured. Thus, only the in-scope method lifetimes may be relaxed with this syntax today. ## What isn't stabilized? (a.k.a. potential future work) See section above. Relaxing the requirement to capture all type and const generics in scope may be relaxed when rust-lang#130043 is implemented, however it currently interacts with some underexplored corners of the type system (e.g. unconstrained type bivariance) so I don't expect it to come soon after. ## Implementation summary This functionality is implemented analogously to the way that *opaque type* precise capturing works. Namely, we currently use *variance* to model the capturedness of lifetimes. However, since RPITITs are anonymous GATs instead of opaque types, we instead modify the type relation of GATs to consider variances for RPITITs (along with opaque types which it has done since rust-lang#103491). https://github.com/rust-lang/rust/blob/30f168ef811aec63124eac677e14699baa9395bd/compiler/rustc_middle/src/ty/util.rs#L954-L976 https://github.com/rust-lang/rust/blob/30f168ef811aec63124eac677e14699baa9395bd/compiler/rustc_type_ir/src/relate.rs#L240-L244 Using variance to model capturedness is an implementation detail, and in the future it would be desirable if opaques and RPITITs simply did not include the uncaptured lifetimes in their generics. This can be changed in a forwards-compatible way, and almost certainly would not be observable by users (at least not negatively, since it may indeed fix some bugs along the way). ## Tests * Test that the lifetime isn't actually captured: `tests/ui/impl-trait/precise-capturing/rpitit.rs` and `tests/ui/impl-trait/precise-capturing/rpitit-outlives.rs` and `tests/ui/impl-trait/precise-capturing/rpitit-outlives-2.rs`. * Technical test for variance computation: `tests/ui/impl-trait/in-trait/variance.rs`. * Test that you must capture all trait generics: `tests/ui/impl-trait/precise-capturing/forgot-to-capture-type.rs`. * Test that you cannot capture more than what the trait specifies: `tests/ui/impl-trait/precise-capturing/rpitit-captures-more-method-lifetimes.rs` and `tests/ui/impl-trait/precise-capturing/rpitit-impl-captures-too-much.rs`. * Undercapturing (refinement) lint: `tests/ui/impl-trait/in-trait/refine-captures.rs`. ### What other unstable features may be exposed by this feature? I don't believe that this exposes any new unstable features indirectly. ## Remaining bugs and open issues Not aware of any open issues or bugs. ## Tooling support Rustfmt: ✅ Supports formatting `+ use<>` everywhere. Clippy: ✅ No support needed, unless specific clippy lints are impl'd to care for precise capturing itself. Rustdoc: ✅ Rendering `+ use<>` precise capturing bounds is supported. Rust-analyzer: ✅ Parser support, and then lifetime support isn't needed rust-lang#138128 (comment) (previous: ~~:question: There is parser support, but I am unsure of rust-analyzer's level of support for RPITITs in general.~~) ## History Tracking issue: rust-lang#130044 * rust-lang#131033 * rust-lang#132795 * rust-lang#136554
2 parents 43f0014 + eb3707e commit 1c84c06

24 files changed

+22
-91
lines changed

compiler/rustc_ast_lowering/messages.ftl

-3
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,6 @@ ast_lowering_never_pattern_with_guard =
141141
142142
ast_lowering_no_precise_captures_on_apit = `use<...>` precise capturing syntax not allowed in argument-position `impl Trait`
143143
144-
ast_lowering_no_precise_captures_on_rpitit = `use<...>` precise capturing syntax is currently not allowed in return-position `impl Trait` in traits
145-
.note = currently, return-position `impl Trait` in traits and trait implementations capture all lifetimes in scope
146-
147144
ast_lowering_previously_used_here = previously used here
148145
149146
ast_lowering_register1 = register `{$reg1_name}`

compiler/rustc_ast_lowering/src/errors.rs

-8
Original file line numberDiff line numberDiff line change
@@ -444,14 +444,6 @@ pub(crate) struct NoPreciseCapturesOnApit {
444444
pub span: Span,
445445
}
446446

447-
#[derive(Diagnostic)]
448-
#[diag(ast_lowering_no_precise_captures_on_rpitit)]
449-
#[note]
450-
pub(crate) struct NoPreciseCapturesOnRpitit {
451-
#[primary_span]
452-
pub span: Span,
453-
}
454-
455447
#[derive(Diagnostic)]
456448
#[diag(ast_lowering_yield_in_closure)]
457449
pub(crate) struct YieldInClosure {

compiler/rustc_ast_lowering/src/lib.rs

-22
Original file line numberDiff line numberDiff line change
@@ -1440,28 +1440,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14401440
// frequently opened issues show.
14411441
let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::OpaqueTy, span, None);
14421442

1443-
// Feature gate for RPITIT + use<..>
1444-
match origin {
1445-
rustc_hir::OpaqueTyOrigin::FnReturn { in_trait_or_impl: Some(_), .. } => {
1446-
if !self.tcx.features().precise_capturing_in_traits()
1447-
&& let Some(span) = bounds.iter().find_map(|bound| match *bound {
1448-
ast::GenericBound::Use(_, span) => Some(span),
1449-
_ => None,
1450-
})
1451-
{
1452-
let mut diag =
1453-
self.tcx.dcx().create_err(errors::NoPreciseCapturesOnRpitit { span });
1454-
add_feature_diagnostics(
1455-
&mut diag,
1456-
self.tcx.sess,
1457-
sym::precise_capturing_in_traits,
1458-
);
1459-
diag.emit();
1460-
}
1461-
}
1462-
_ => {}
1463-
}
1464-
14651443
self.lower_opaque_inner(opaque_ty_node_id, origin, opaque_ty_span, |this| {
14661444
this.lower_param_bounds(bounds, itctx)
14671445
})

compiler/rustc_feature/src/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ declare_features! (
331331
(accepted, pattern_parentheses, "1.31.0", Some(51087)),
332332
/// Allows `use<'a, 'b, A, B>` in `impl Trait + use<...>` for precise capture of generic args.
333333
(accepted, precise_capturing, "1.82.0", Some(123432)),
334+
/// Allows `use<..>` precise capturign on impl Trait in traits.
335+
(accepted, precise_capturing_in_traits, "CURRENT_RUSTC_VERSION", Some(130044)),
334336
/// Allows procedural macros in `proc-macro` crates.
335337
(accepted, proc_macro, "1.29.0", Some(38356)),
336338
/// Allows multi-segment paths in attributes and derives.

compiler/rustc_feature/src/unstable.rs

-2
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,6 @@ declare_features! (
600600
(incomplete, pin_ergonomics, "1.83.0", Some(130494)),
601601
/// Allows postfix match `expr.match { ... }`
602602
(unstable, postfix_match, "1.79.0", Some(121618)),
603-
/// Allows `use<..>` precise capturign on impl Trait in traits.
604-
(unstable, precise_capturing_in_traits, "1.83.0", Some(130044)),
605603
/// Allows macro attributes on expressions, statements and non-inline modules.
606604
(unstable, proc_macro_hygiene, "1.30.0", Some(54727)),
607605
/// Allows the use of raw-dylibs on ELF platforms

tests/ui/feature-gates/feature-gate-precise_capturing_in_traits.rs

-6
This file was deleted.

tests/ui/feature-gates/feature-gate-precise_capturing_in_traits.stderr

-13
This file was deleted.

tests/ui/impl-trait/in-trait/dump.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ compile-flags: -Zverbose-internals
22

3-
#![feature(precise_capturing_in_traits, rustc_attrs)]
3+
#![feature(rustc_attrs)]
44
#![rustc_hidden_type_of_opaques]
55

66
trait Foo {

tests/ui/impl-trait/in-trait/refine-captures.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(precise_capturing_in_traits)]
2-
31
trait LifetimeParam<'a> {
42
fn test() -> impl Sized;
53
}

tests/ui/impl-trait/in-trait/refine-captures.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: impl trait in impl method captures fewer lifetimes than in trait
2-
--> $DIR/refine-captures.rs:8:31
2+
--> $DIR/refine-captures.rs:6:31
33
|
44
LL | fn test() -> impl Sized + use<> {}
55
| ^^^^^
@@ -13,7 +13,7 @@ LL | fn test() -> impl Sized + use<'a> {}
1313
| ++
1414

1515
warning: impl trait in impl method captures fewer lifetimes than in trait
16-
--> $DIR/refine-captures.rs:22:31
16+
--> $DIR/refine-captures.rs:20:31
1717
|
1818
LL | fn test() -> impl Sized + use<> {}
1919
| ^^^^^
@@ -26,7 +26,7 @@ LL | fn test() -> impl Sized + use<'a> {}
2626
| ++
2727

2828
warning: impl trait in impl method captures fewer lifetimes than in trait
29-
--> $DIR/refine-captures.rs:27:31
29+
--> $DIR/refine-captures.rs:25:31
3030
|
3131
LL | fn test() -> impl Sized + use<'b> {}
3232
| ^^^^^^^
@@ -39,7 +39,7 @@ LL | fn test() -> impl Sized + use<'a, 'b> {}
3939
| ++++
4040

4141
error: `impl Trait` must mention all type parameters in scope in `use<...>`
42-
--> $DIR/refine-captures.rs:32:18
42+
--> $DIR/refine-captures.rs:30:18
4343
|
4444
LL | impl<T> TypeParam<T> for u64 {
4545
| - type parameter is implicitly captured by this `impl Trait`

tests/ui/impl-trait/in-trait/variance.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(rustc_attrs, precise_capturing_in_traits)]
1+
#![feature(rustc_attrs)]
22
#![allow(internal_features)]
33
#![rustc_variance_of_opaques]
44

tests/ui/impl-trait/precise-capturing/forgot-to-capture-type.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(precise_capturing_in_traits)]
2-
31
fn type_param<T>() -> impl Sized + use<> {}
42
//~^ ERROR `impl Trait` must mention all type parameters in scope
53

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: `impl Trait` must mention all type parameters in scope in `use<...>`
2-
--> $DIR/forgot-to-capture-type.rs:3:23
2+
--> $DIR/forgot-to-capture-type.rs:1:23
33
|
44
LL | fn type_param<T>() -> impl Sized + use<> {}
55
| - ^^^^^^^^^^^^^^^^^^
@@ -9,7 +9,7 @@ LL | fn type_param<T>() -> impl Sized + use<> {}
99
= note: currently, all type parameters are required to be mentioned in the precise captures list
1010

1111
error: `impl Trait` must mention the `Self` type of the trait in `use<...>`
12-
--> $DIR/forgot-to-capture-type.rs:7:17
12+
--> $DIR/forgot-to-capture-type.rs:5:17
1313
|
1414
LL | trait Foo {
1515
| --------- `Self` type parameter is implicitly captured by this `impl Trait`

tests/ui/impl-trait/precise-capturing/redundant.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//@ edition: 2024
22

3-
#![feature(precise_capturing_in_traits)]
43
#![deny(impl_trait_redundant_captures)]
54

65
fn hello<'a>() -> impl Sized + use<'a> {}

tests/ui/impl-trait/precise-capturing/redundant.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
error: all possible in-scope parameters are already captured, so `use<...>` syntax is redundant
2-
--> $DIR/redundant.rs:6:19
2+
--> $DIR/redundant.rs:5:19
33
|
44
LL | fn hello<'a>() -> impl Sized + use<'a> {}
55
| ^^^^^^^^^^^^^-------
66
| |
77
| help: remove the `use<...>` syntax
88
|
99
note: the lint level is defined here
10-
--> $DIR/redundant.rs:4:9
10+
--> $DIR/redundant.rs:3:9
1111
|
1212
LL | #![deny(impl_trait_redundant_captures)]
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

1515
error: all possible in-scope parameters are already captured, so `use<...>` syntax is redundant
16-
--> $DIR/redundant.rs:11:27
16+
--> $DIR/redundant.rs:10:27
1717
|
1818
LL | fn inherent(&self) -> impl Sized + use<'_> {}
1919
| ^^^^^^^^^^^^^-------
2020
| |
2121
| help: remove the `use<...>` syntax
2222

2323
error: all possible in-scope parameters are already captured, so `use<...>` syntax is redundant
24-
--> $DIR/redundant.rs:16:22
24+
--> $DIR/redundant.rs:15:22
2525
|
2626
LL | fn in_trait() -> impl Sized + use<'a, Self>;
2727
| ^^^^^^^^^^^^^-------------
2828
| |
2929
| help: remove the `use<...>` syntax
3030

3131
error: all possible in-scope parameters are already captured, so `use<...>` syntax is redundant
32-
--> $DIR/redundant.rs:20:22
32+
--> $DIR/redundant.rs:19:22
3333
|
3434
LL | fn in_trait() -> impl Sized + use<'a> {}
3535
| ^^^^^^^^^^^^^-------

tests/ui/impl-trait/precise-capturing/rpitit-captures-more-method-lifetimes.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// trait definition, which is not allowed. Due to the default lifetime capture
33
// rules of RPITITs, this is only doable if we use precise capturing.
44

5-
#![feature(precise_capturing_in_traits)]
6-
75
pub trait Foo {
86
fn bar<'tr: 'tr>(&'tr mut self) -> impl Sized + use<Self>;
97
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error: return type captures more lifetimes than trait definition
2-
--> $DIR/rpitit-captures-more-method-lifetimes.rs:12:40
2+
--> $DIR/rpitit-captures-more-method-lifetimes.rs:10:40
33
|
44
LL | fn bar<'im: 'im>(&'im mut self) -> impl Sized + use<'im> {}
55
| --- ^^^^^^^^^^^^^^^^^^^^^
66
| |
77
| this lifetime was captured
88
|
99
note: hidden type must only reference lifetimes captured by this impl trait
10-
--> $DIR/rpitit-captures-more-method-lifetimes.rs:8:40
10+
--> $DIR/rpitit-captures-more-method-lifetimes.rs:6:40
1111
|
1212
LL | fn bar<'tr: 'tr>(&'tr mut self) -> impl Sized + use<Self>;
1313
| ^^^^^^^^^^^^^^^^^^^^^^

tests/ui/impl-trait/precise-capturing/rpitit-impl-captures-too-much.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(precise_capturing_in_traits)]
2-
31
struct Invariant<'a>(&'a mut &'a mut ());
42

53
trait Trait {

tests/ui/impl-trait/precise-capturing/rpitit-impl-captures-too-much.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: return type captures more lifetimes than trait definition
2-
--> $DIR/rpitit-impl-captures-too-much.rs:10:39
2+
--> $DIR/rpitit-impl-captures-too-much.rs:8:39
33
|
44
LL | fn hello(self_: Invariant<'_>) -> impl Sized + use<Self>;
55
| -- this lifetime was captured
@@ -8,7 +8,7 @@ LL | fn hello(self_: Invariant<'_>) -> impl Sized + use<'_> {}
88
| ^^^^^^^^^^^^^^^^^^^^
99
|
1010
note: hidden type must only reference lifetimes captured by this impl trait
11-
--> $DIR/rpitit-impl-captures-too-much.rs:6:39
11+
--> $DIR/rpitit-impl-captures-too-much.rs:4:39
1212
|
1313
LL | fn hello(self_: Invariant<'_>) -> impl Sized + use<Self>;
1414
| ^^^^^^^^^^^^^^^^^^^^^^

tests/ui/impl-trait/precise-capturing/rpitit-outlives-2.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
// Ensure that we skip uncaptured args from RPITITs when comptuing outlives.
44

5-
#![feature(precise_capturing_in_traits)]
6-
75
struct Invariant<T>(*mut T);
86

97
trait Foo {

tests/ui/impl-trait/precise-capturing/rpitit-outlives.rs

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// Ensure that we skip uncaptured args from RPITITs when collecting the regions
44
// to enforce member constraints in opaque type inference.
55

6-
#![feature(precise_capturing_in_traits)]
7-
86
struct Invariant<T>(*mut T);
97

108
trait Foo {

tests/ui/impl-trait/precise-capturing/rpitit.rs

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// To fix this soundly, we need to make sure that all the trait header args
44
// remain captured, since they affect trait selection.
55

6-
#![feature(precise_capturing_in_traits)]
7-
86
fn eq_types<T>(_: T, _: T) {}
97

108
trait TraitLt<'a: 'a> {

tests/ui/impl-trait/precise-capturing/rpitit.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error: `impl Trait` captures lifetime parameter, but it is not mentioned in `use<...>` precise captures list
2-
--> $DIR/rpitit.rs:11:19
2+
--> $DIR/rpitit.rs:9:19
33
|
44
LL | trait TraitLt<'a: 'a> {
55
| -- all lifetime parameters originating from a trait are captured implicitly
66
LL | fn hello() -> impl Sized + use<Self>;
77
| ^^^^^^^^^^^^^^^^^^^^^^
88

99
error: lifetime may not live long enough
10-
--> $DIR/rpitit.rs:15:5
10+
--> $DIR/rpitit.rs:13:5
1111
|
1212
LL | fn trait_lt<'a, 'b, T: for<'r> TraitLt<'r>> () {
1313
| -- -- lifetime `'b` defined here
@@ -24,7 +24,7 @@ LL | | );
2424
= help: consider adding the following bound: `'a: 'b`
2525

2626
error: lifetime may not live long enough
27-
--> $DIR/rpitit.rs:15:5
27+
--> $DIR/rpitit.rs:13:5
2828
|
2929
LL | fn trait_lt<'a, 'b, T: for<'r> TraitLt<'r>> () {
3030
| -- -- lifetime `'b` defined here

tests/ui/impl-trait/precise-capturing/self-capture.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//@ check-pass
22

3-
#![feature(precise_capturing_in_traits)]
4-
53
trait Foo {
64
fn bar<'a>() -> impl Sized + use<Self>;
75
}

0 commit comments

Comments
 (0)