fix make HirDisplay format lifetimes first

This commit is contained in:
dfireBird 2024-03-13 21:07:20 +05:30
parent 8d08b337fa
commit a555e95c9a
No known key found for this signature in database
GPG Key ID: 26D522CA5FC2B93D
6 changed files with 32 additions and 13 deletions

View File

@ -3,6 +3,7 @@
//! purposes.
use std::{
cmp::Ordering,
fmt::{self, Debug},
mem::size_of,
};
@ -953,11 +954,17 @@ fn hir_fmt(
// `parameters` are in the order of fn's params (including impl traits),
// parent's params (those from enclosing impl or trait, if any).
let parameters = parameters.as_slice(Interner);
let fn_params_len =
self_param + type_params + const_params + lifetime_params;
let fn_params_len = self_param + type_params + const_params;
// This will give slice till last type or const
let fn_params = parameters.get(..fn_params_len);
let fn_lt_params =
parameters.get(fn_params_len..(fn_params_len + lifetime_params));
let parent_params = parameters.get(parameters.len() - parent_params..);
let params = parent_params.into_iter().chain(fn_params).flatten();
let params = parent_params
.into_iter()
.chain(fn_lt_params)
.chain(fn_params)
.flatten();
write!(f, "<")?;
f.write_joined(params, ", ")?;
write!(f, ">")?;
@ -1317,6 +1324,15 @@ fn hir_fmt_generics(
) -> Result<(), HirDisplayError> {
let db = f.db;
if parameters.len(Interner) > 0 {
let param_compare =
|a: &GenericArg, b: &GenericArg| match (a.data(Interner), b.data(Interner)) {
(crate::GenericArgData::Lifetime(_), crate::GenericArgData::Lifetime(_)) => {
Ordering::Equal
}
(crate::GenericArgData::Lifetime(_), _) => Ordering::Less,
(_, crate::GenericArgData::Lifetime(_)) => Ordering::Less,
(_, _) => Ordering::Equal,
};
let parameters_to_write = if f.display_target.is_source_code() || f.omit_verbose_types() {
match generic_def
.map(|generic_def_id| db.generic_defaults(generic_def_id))
@ -1367,6 +1383,9 @@ fn should_show(
} else {
parameters.as_slice(Interner)
};
//FIXME: Should handle when creating substitutions
let mut parameters_to_write = parameters_to_write.to_vec();
parameters_to_write.sort_by(param_compare);
if !parameters_to_write.is_empty() {
write!(f, "<")?;
let mut first = true;

View File

@ -896,13 +896,13 @@ fn flush(&self) {
"#,
expect![[r#"
123..127 'self': &Mutex<T>
150..152 '{}': MutexGuard<T, 'static>
150..152 '{}': MutexGuard<'static, T>
234..238 'self': &{unknown}
240..290 '{ ...()); }': ()
250..251 'w': &Mutex<BufWriter>
276..287 '*(w.lock())': BufWriter
278..279 'w': &Mutex<BufWriter>
278..286 'w.lock()': MutexGuard<BufWriter, 'static>
278..286 'w.lock()': MutexGuard<'static, BufWriter>
"#]],
);
}

View File

@ -5617,7 +5617,7 @@ fn func<T: Debug>(i: Struct<'_, T>) {
fun_name(i);
}
fn $0fun_name(i: Struct<T, 'static>) {
fn $0fun_name(i: Struct<'static, T>) {
foo(i);
}
"#,

View File

@ -614,7 +614,7 @@ struct Foo<'a, T> {
}
impl<'a, T> Foo<'a, T> {
$0fn bar(self, mut b: Vec<&'a Bar<T, 'a>>) -> &'a Bar<T, 'a> {
$0fn bar(self, mut b: Vec<&'a Bar<'a, T>>) -> &'a Bar<'a, T> {
self.field.bar(b)
}
}

View File

@ -19,7 +19,7 @@ struct Foo<'lt, T, const C: usize> where $0 {}
en Enum Enum
ma makro!() macro_rules! makro
md module
st Foo<> Foo<{unknown}, _, 'static>
st Foo<> Foo<'static, {unknown}, _>
st Record Record
st Tuple Tuple
st Unit Unit
@ -92,7 +92,7 @@ struct Foo<'lt, T, const C: usize> where for<'a> $0 {}
en Enum Enum
ma makro!() macro_rules! makro
md module
st Foo<> Foo<{unknown}, _, 'static>
st Foo<> Foo<'static, {unknown}, _>
st Record Record
st Tuple Tuple
st Unit Unit

View File

@ -20,8 +20,8 @@ struct Foo<'lt, T, const C: usize> {
en Enum Enum
ma makro!() macro_rules! makro
md module
sp Self Foo<{unknown}, _, 'static>
st Foo<> Foo<{unknown}, _, 'static>
sp Self Foo<'static, {unknown}, _>
st Foo<> Foo<'static, {unknown}, _>
st Record Record
st Tuple Tuple
st Unit Unit
@ -45,8 +45,8 @@ fn tuple_struct_field() {
en Enum Enum
ma makro!() macro_rules! makro
md module
sp Self Foo<{unknown}, _, 'static>
st Foo<> Foo<{unknown}, _, 'static>
sp Self Foo<'static, {unknown}, _>
st Foo<> Foo<'static, {unknown}, _>
st Record Record
st Tuple Tuple
st Unit Unit