Skip to content

Commit

Permalink
Auto merge of rust-lang#123468 - compiler-errors:precise-capturing, r…
Browse files Browse the repository at this point in the history
…=oli-obk

Implement syntax for `impl Trait` to specify its captures explicitly (`feature(precise_capturing)`)

Implements `impl use<'a, 'b, T, U> Sized` syntax that allows users to explicitly list the captured parameters for an opaque, rather than inferring it from the opaque's bounds (or capturing *all* lifetimes under 2024-edition capture rules). This allows us to exclude some implicit captures, so this syntax may be used as a migration strategy for changes due to rust-lang#117587.

We represent this list of captured params as `PreciseCapturingArg` in AST and HIR, resolving them between `rustc_resolve` and `resolve_bound_vars`. Later on, we validate that the opaques only capture the parameters in this list.

We artificially limit the feature to *require* mentioning all type and const parameters, since we don't currently have support for non-lifetime bivariant generics. This can be relaxed in the future.

We also may need to limit this to require naming *all* lifetime parameters for RPITIT, since GATs have no variance. I have to investigate this. This can also be relaxed in the future.

r? `@oli-obk`

Tracking issue:

- rust-lang#123432
  • Loading branch information
bors committed Apr 16, 2024
2 parents dac1a22 + 9400b99 commit a568985
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,11 @@ impl Rewrite for ast::Ty {
rewrite_macro(mac, None, context, shape, MacroPosition::Expression)
}
ast::TyKind::ImplicitSelf => Some(String::from("")),
ast::TyKind::ImplTrait(_, ref it) => {
ast::TyKind::ImplTrait(_, ref it, ref captures) => {
// FIXME(precise_capturing): Implement formatting.
if captures.is_some() {
return None;
}
// Empty trait is not a parser error.
if it.is_empty() {
return Some("impl".to_owned());
Expand Down Expand Up @@ -1106,7 +1110,8 @@ fn join_bounds_inner(

pub(crate) fn opaque_ty(ty: &Option<ptr::P<ast::Ty>>) -> Option<&ast::GenericBounds> {
ty.as_ref().and_then(|t| match &t.kind {
ast::TyKind::ImplTrait(_, bounds) => Some(bounds),
// FIXME(precise_capturing): Implement support here
ast::TyKind::ImplTrait(_, bounds, _) => Some(bounds),
_ => None,
})
}
Expand Down

0 comments on commit a568985

Please sign in to comment.