Support non-lifetime binders
This commit is contained in:
parent
c6d39a2259
commit
2db13f448c
@ -12,7 +12,7 @@ use crate::overflow::OverflowableItem;
|
|||||||
use crate::rewrite::{Rewrite, RewriteContext};
|
use crate::rewrite::{Rewrite, RewriteContext};
|
||||||
use crate::shape::Shape;
|
use crate::shape::Shape;
|
||||||
use crate::source_map::SpanUtils;
|
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};
|
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:
|
// 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()
|
"for<> ".to_owned()
|
||||||
}
|
}
|
||||||
ast::ClosureBinder::For { generic_params, .. } => {
|
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}> ")
|
format!("for<{lifetime_str}> ")
|
||||||
}
|
}
|
||||||
ast::ClosureBinder::NotPresent => "".to_owned(),
|
ast::ClosureBinder::NotPresent => "".to_owned(),
|
||||||
|
19
src/types.rs
19
src/types.rs
@ -426,10 +426,10 @@ impl Rewrite for ast::WherePredicate {
|
|||||||
}) => {
|
}) => {
|
||||||
let type_str = bounded_ty.rewrite(context, shape)?;
|
let type_str = bounded_ty.rewrite(context, shape)?;
|
||||||
let colon = type_bound_colon(context).trim_end();
|
let colon = type_bound_colon(context).trim_end();
|
||||||
let lhs = if let Some(lifetime_str) =
|
let lhs = if let Some(binder_str) =
|
||||||
rewrite_lifetime_param(context, shape, bound_generic_params)
|
rewrite_bound_params(context, shape, bound_generic_params)
|
||||||
{
|
{
|
||||||
format!("for<{}> {}{}", lifetime_str, type_str, colon)
|
format!("for<{}> {}{}", binder_str, type_str, colon)
|
||||||
} else {
|
} else {
|
||||||
format!("{}{}", type_str, colon)
|
format!("{}{}", type_str, colon)
|
||||||
};
|
};
|
||||||
@ -657,8 +657,7 @@ impl Rewrite for ast::GenericParam {
|
|||||||
|
|
||||||
impl Rewrite for ast::PolyTraitRef {
|
impl Rewrite for ast::PolyTraitRef {
|
||||||
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
|
||||||
if let Some(lifetime_str) =
|
if let Some(lifetime_str) = rewrite_bound_params(context, shape, &self.bound_generic_params)
|
||||||
rewrite_lifetime_param(context, shape, &self.bound_generic_params)
|
|
||||||
{
|
{
|
||||||
// 6 is "for<> ".len()
|
// 6 is "for<> ".len()
|
||||||
let extra_offset = lifetime_str.len() + 6;
|
let extra_offset = lifetime_str.len() + 6;
|
||||||
@ -881,8 +880,7 @@ fn rewrite_bare_fn(
|
|||||||
|
|
||||||
let mut result = String::with_capacity(128);
|
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<");
|
result.push_str("for<");
|
||||||
// 6 = "for<> ".len(), 4 = "for<".
|
// 6 = "for<> ".len(), 4 = "for<".
|
||||||
// This doesn't work out so nicely for multiline situation with lots of
|
// 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.
|
/// Returns `None` if there is no `GenericParam` in the list
|
||||||
pub(crate) fn rewrite_lifetime_param(
|
pub(crate) fn rewrite_bound_params(
|
||||||
context: &RewriteContext<'_>,
|
context: &RewriteContext<'_>,
|
||||||
shape: Shape,
|
shape: Shape,
|
||||||
generic_params: &[ast::GenericParam],
|
generic_params: &[ast::GenericParam],
|
||||||
) -> Option<String> {
|
) -> Option<String> {
|
||||||
let result = generic_params
|
let result = generic_params
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|p| matches!(p.kind, ast::GenericParamKind::Lifetime))
|
.map(|param| param.rewrite(context, shape))
|
||||||
.map(|lt| lt.rewrite(context, shape))
|
|
||||||
.collect::<Option<Vec<_>>>()?
|
.collect::<Option<Vec<_>>>()?
|
||||||
.join(", ");
|
.join(", ");
|
||||||
if result.is_empty() {
|
if result.is_empty() {
|
||||||
|
8
tests/source/issue_5721.rs
Normal file
8
tests/source/issue_5721.rs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#![feature(non_lifetime_binders)]
|
||||||
|
#![allow(incomplete_features)]
|
||||||
|
|
||||||
|
trait Other<U: ?Sized> {}
|
||||||
|
|
||||||
|
trait Trait<U>
|
||||||
|
where
|
||||||
|
for<T> U: Other<T> {}
|
10
tests/source/non-lifetime-binders.rs
Normal file
10
tests/source/non-lifetime-binders.rs
Normal file
@ -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
tests/target/issue_5721.rs
Normal file
10
tests/target/issue_5721.rs
Normal file
@ -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
tests/target/non-lifetime-binders.rs
Normal file
10
tests/target/non-lifetime-binders.rs
Normal file
@ -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);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user