Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support non-lifetime binders #5848

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(),
Expand Down
19 changes: 8 additions & 11 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
Expand Down Expand Up @@ -657,8 +657,7 @@ impl Rewrite for ast::GenericParam {

impl Rewrite for ast::PolyTraitRef {
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String> {
let result = generic_params
.iter()
.filter(|p| matches!(p.kind, ast::GenericParamKind::Lifetime))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this filter line is the only non-name-tweaking change.

.map(|lt| lt.rewrite(context, shape))
.map(|param| param.rewrite(context, shape))
.collect::<Option<Vec<_>>>()?
.join(", ");
if result.is_empty() {
Expand Down
8 changes: 8 additions & 0 deletions tests/source/issue_5721.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(non_lifetime_binders)]
#![allow(incomplete_features)]

trait Other<U: ?Sized> {}

trait Trait<U>
where
for<T> U: Other<T> {}
Comment on lines +1 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No real issue keeping the source file, but I just want to point out that the target file is enough to show that the input is idempotent, and that rustfmt no longer remove the for<T>.

10 changes: 10 additions & 0 deletions tests/source/non-lifetime-binders.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn main() where for<'a, T: Sized + 'a, const C: usize> [&'a T; C]: Sized {
let x = for<T>
|| {};

let y: dyn
for<T> Into<T>;

let z: for<T>
fn(T);
}
10 changes: 10 additions & 0 deletions tests/target/issue_5721.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![feature(non_lifetime_binders)]
#![allow(incomplete_features)]

trait Other<U: ?Sized> {}

trait Trait<U>
where
for<T> U: Other<T>,
{
}
10 changes: 10 additions & 0 deletions tests/target/non-lifetime-binders.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fn main()
where
for<'a, T: Sized + 'a, const C: usize> [&'a T; C]: Sized,
{
let x = for<T> || {};

let y: dyn for<T> Into<T>;

let z: for<T> fn(T);
}