Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

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

Lines changed: 0 additions & 3 deletions
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

Lines changed: 0 additions & 8 deletions
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

Lines changed: 0 additions & 22 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 6 deletions
This file was deleted.

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

Lines changed: 0 additions & 13 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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`

0 commit comments

Comments
 (0)