diff --git a/src/closures.rs b/src/closures.rs index c95e9a97b43..cf128752919 100644 --- a/src/closures.rs +++ b/src/closures.rs @@ -12,7 +12,7 @@ use crate::overflow::OverflowableItem; use crate::rewrite::{Rewrite, RewriteContext}; use crate::shape::Shape; use crate::source_map::SpanUtils; -use crate::types::rewrite_lifetime_param; +use crate::types::rewrite_bound_params; use crate::utils::{last_line_width, left_most_sub_expr, stmt_expr, NodeIdExt}; // This module is pretty messy because of the rules around closures and blocks: @@ -246,7 +246,7 @@ fn rewrite_closure_fn_decl( "for<> ".to_owned() } ast::ClosureBinder::For { generic_params, .. } => { - let lifetime_str = rewrite_lifetime_param(context, shape, generic_params)?; + let lifetime_str = rewrite_bound_params(context, shape, generic_params)?; format!("for<{lifetime_str}> ") } ast::ClosureBinder::NotPresent => "".to_owned(), diff --git a/src/types.rs b/src/types.rs index 18a08f17ba0..bb236a386e4 100644 --- a/src/types.rs +++ b/src/types.rs @@ -426,10 +426,10 @@ impl Rewrite for ast::WherePredicate { }) => { let type_str = bounded_ty.rewrite(context, shape)?; let colon = type_bound_colon(context).trim_end(); - let lhs = if let Some(lifetime_str) = - rewrite_lifetime_param(context, shape, bound_generic_params) + let lhs = if let Some(binder_str) = + rewrite_bound_params(context, shape, bound_generic_params) { - format!("for<{}> {}{}", lifetime_str, type_str, colon) + format!("for<{}> {}{}", binder_str, type_str, colon) } else { format!("{}{}", type_str, colon) }; @@ -657,8 +657,7 @@ impl Rewrite for ast::GenericParam { impl Rewrite for ast::PolyTraitRef { fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option { - if let Some(lifetime_str) = - rewrite_lifetime_param(context, shape, &self.bound_generic_params) + if let Some(lifetime_str) = rewrite_bound_params(context, shape, &self.bound_generic_params) { // 6 is "for<> ".len() let extra_offset = lifetime_str.len() + 6; @@ -881,8 +880,7 @@ fn rewrite_bare_fn( let mut result = String::with_capacity(128); - if let Some(ref lifetime_str) = rewrite_lifetime_param(context, shape, &bare_fn.generic_params) - { + if let Some(ref lifetime_str) = rewrite_bound_params(context, shape, &bare_fn.generic_params) { result.push_str("for<"); // 6 = "for<> ".len(), 4 = "for<". // This doesn't work out so nicely for multiline situation with lots of @@ -1122,16 +1120,15 @@ pub(crate) fn can_be_overflowed_type( } } -/// Returns `None` if there is no `LifetimeDef` in the given generic parameters. -pub(crate) fn rewrite_lifetime_param( +/// Returns `None` if there is no `GenericParam` in the list +pub(crate) fn rewrite_bound_params( context: &RewriteContext<'_>, shape: Shape, generic_params: &[ast::GenericParam], ) -> Option { let result = generic_params .iter() - .filter(|p| matches!(p.kind, ast::GenericParamKind::Lifetime)) - .map(|lt| lt.rewrite(context, shape)) + .map(|param| param.rewrite(context, shape)) .collect::>>()? .join(", "); if result.is_empty() { diff --git a/tests/source/issue_5721.rs b/tests/source/issue_5721.rs new file mode 100644 index 00000000000..e5ae9612c98 --- /dev/null +++ b/tests/source/issue_5721.rs @@ -0,0 +1,8 @@ +#![feature(non_lifetime_binders)] +#![allow(incomplete_features)] + +trait Other {} + +trait Trait +where + for U: Other {} diff --git a/tests/source/non-lifetime-binders.rs b/tests/source/non-lifetime-binders.rs new file mode 100644 index 00000000000..c26393c8f89 --- /dev/null +++ b/tests/source/non-lifetime-binders.rs @@ -0,0 +1,10 @@ +fn main() where for<'a, T: Sized + 'a, const C: usize> [&'a T; C]: Sized { + let x = for + || {}; + + let y: dyn + for Into; + + let z: for + fn(T); +} diff --git a/tests/target/issue_5721.rs b/tests/target/issue_5721.rs new file mode 100644 index 00000000000..d073b09cac2 --- /dev/null +++ b/tests/target/issue_5721.rs @@ -0,0 +1,10 @@ +#![feature(non_lifetime_binders)] +#![allow(incomplete_features)] + +trait Other {} + +trait Trait +where + for U: Other, +{ +} diff --git a/tests/target/non-lifetime-binders.rs b/tests/target/non-lifetime-binders.rs new file mode 100644 index 00000000000..ca6941a0c00 --- /dev/null +++ b/tests/target/non-lifetime-binders.rs @@ -0,0 +1,10 @@ +fn main() +where + for<'a, T: Sized + 'a, const C: usize> [&'a T; C]: Sized, +{ + let x = for || {}; + + let y: dyn for Into; + + let z: for fn(T); +}