Skip to content

Commit 2db13f4

Browse files
compiler-errorscalebcartwright
authored andcommitted
Support non-lifetime binders
1 parent c6d39a2 commit 2db13f4

File tree

6 files changed

+48
-13
lines changed

6 files changed

+48
-13
lines changed

src/closures.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::overflow::OverflowableItem;
1212
use crate::rewrite::{Rewrite, RewriteContext};
1313
use crate::shape::Shape;
1414
use crate::source_map::SpanUtils;
15-
use crate::types::rewrite_lifetime_param;
15+
use crate::types::rewrite_bound_params;
1616
use crate::utils::{last_line_width, left_most_sub_expr, stmt_expr, NodeIdExt};
1717

1818
// This module is pretty messy because of the rules around closures and blocks:
@@ -246,7 +246,7 @@ fn rewrite_closure_fn_decl(
246246
"for<> ".to_owned()
247247
}
248248
ast::ClosureBinder::For { generic_params, .. } => {
249-
let lifetime_str = rewrite_lifetime_param(context, shape, generic_params)?;
249+
let lifetime_str = rewrite_bound_params(context, shape, generic_params)?;
250250
format!("for<{lifetime_str}> ")
251251
}
252252
ast::ClosureBinder::NotPresent => "".to_owned(),

src/types.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,10 @@ impl Rewrite for ast::WherePredicate {
426426
}) => {
427427
let type_str = bounded_ty.rewrite(context, shape)?;
428428
let colon = type_bound_colon(context).trim_end();
429-
let lhs = if let Some(lifetime_str) =
430-
rewrite_lifetime_param(context, shape, bound_generic_params)
429+
let lhs = if let Some(binder_str) =
430+
rewrite_bound_params(context, shape, bound_generic_params)
431431
{
432-
format!("for<{}> {}{}", lifetime_str, type_str, colon)
432+
format!("for<{}> {}{}", binder_str, type_str, colon)
433433
} else {
434434
format!("{}{}", type_str, colon)
435435
};
@@ -657,8 +657,7 @@ impl Rewrite for ast::GenericParam {
657657

658658
impl Rewrite for ast::PolyTraitRef {
659659
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
660-
if let Some(lifetime_str) =
661-
rewrite_lifetime_param(context, shape, &self.bound_generic_params)
660+
if let Some(lifetime_str) = rewrite_bound_params(context, shape, &self.bound_generic_params)
662661
{
663662
// 6 is "for<> ".len()
664663
let extra_offset = lifetime_str.len() + 6;
@@ -881,8 +880,7 @@ fn rewrite_bare_fn(
881880

882881
let mut result = String::with_capacity(128);
883882

884-
if let Some(ref lifetime_str) = rewrite_lifetime_param(context, shape, &bare_fn.generic_params)
885-
{
883+
if let Some(ref lifetime_str) = rewrite_bound_params(context, shape, &bare_fn.generic_params) {
886884
result.push_str("for<");
887885
// 6 = "for<> ".len(), 4 = "for<".
888886
// This doesn't work out so nicely for multiline situation with lots of
@@ -1122,16 +1120,15 @@ pub(crate) fn can_be_overflowed_type(
11221120
}
11231121
}
11241122

1125-
/// Returns `None` if there is no `LifetimeDef` in the given generic parameters.
1126-
pub(crate) fn rewrite_lifetime_param(
1123+
/// Returns `None` if there is no `GenericParam` in the list
1124+
pub(crate) fn rewrite_bound_params(
11271125
context: &RewriteContext<'_>,
11281126
shape: Shape,
11291127
generic_params: &[ast::GenericParam],
11301128
) -> Option<String> {
11311129
let result = generic_params
11321130
.iter()
1133-
.filter(|p| matches!(p.kind, ast::GenericParamKind::Lifetime))
1134-
.map(|lt| lt.rewrite(context, shape))
1131+
.map(|param| param.rewrite(context, shape))
11351132
.collect::<Option<Vec<_>>>()?
11361133
.join(", ");
11371134
if result.is_empty() {

tests/source/issue_5721.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![feature(non_lifetime_binders)]
2+
#![allow(incomplete_features)]
3+
4+
trait Other<U: ?Sized> {}
5+
6+
trait Trait<U>
7+
where
8+
for<T> U: Other<T> {}

tests/source/non-lifetime-binders.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn main() where for<'a, T: Sized + 'a, const C: usize> [&'a T; C]: Sized {
2+
let x = for<T>
3+
|| {};
4+
5+
let y: dyn
6+
for<T> Into<T>;
7+
8+
let z: for<T>
9+
fn(T);
10+
}

tests/target/issue_5721.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(non_lifetime_binders)]
2+
#![allow(incomplete_features)]
3+
4+
trait Other<U: ?Sized> {}
5+
6+
trait Trait<U>
7+
where
8+
for<T> U: Other<T>,
9+
{
10+
}

tests/target/non-lifetime-binders.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn main()
2+
where
3+
for<'a, T: Sized + 'a, const C: usize> [&'a T; C]: Sized,
4+
{
5+
let x = for<T> || {};
6+
7+
let y: dyn for<T> Into<T>;
8+
9+
let z: for<T> fn(T);
10+
}

0 commit comments

Comments
 (0)