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. //! purposes.
use std::{ use std::{
cmp::Ordering,
fmt::{self, Debug}, fmt::{self, Debug},
mem::size_of, mem::size_of,
}; };
@ -953,11 +954,17 @@ impl HirDisplay for Ty {
// `parameters` are in the order of fn's params (including impl traits), // `parameters` are in the order of fn's params (including impl traits),
// parent's params (those from enclosing impl or trait, if any). // parent's params (those from enclosing impl or trait, if any).
let parameters = parameters.as_slice(Interner); let parameters = parameters.as_slice(Interner);
let fn_params_len = let fn_params_len = self_param + type_params + const_params;
self_param + type_params + const_params + lifetime_params; // This will give slice till last type or const
let fn_params = parameters.get(..fn_params_len); 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 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, "<")?; write!(f, "<")?;
f.write_joined(params, ", ")?; f.write_joined(params, ", ")?;
write!(f, ">")?; write!(f, ">")?;
@ -1317,6 +1324,15 @@ fn hir_fmt_generics(
) -> Result<(), HirDisplayError> { ) -> Result<(), HirDisplayError> {
let db = f.db; let db = f.db;
if parameters.len(Interner) > 0 { 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() { let parameters_to_write = if f.display_target.is_source_code() || f.omit_verbose_types() {
match generic_def match generic_def
.map(|generic_def_id| db.generic_defaults(generic_def_id)) .map(|generic_def_id| db.generic_defaults(generic_def_id))
@ -1367,6 +1383,9 @@ fn hir_fmt_generics(
} else { } else {
parameters.as_slice(Interner) 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() { if !parameters_to_write.is_empty() {
write!(f, "<")?; write!(f, "<")?;
let mut first = true; let mut first = true;

View file

@ -896,13 +896,13 @@ fn flush(&self) {
"#, "#,
expect![[r#" expect![[r#"
123..127 'self': &Mutex<T> 123..127 'self': &Mutex<T>
150..152 '{}': MutexGuard<T, 'static> 150..152 '{}': MutexGuard<'static, T>
234..238 'self': &{unknown} 234..238 'self': &{unknown}
240..290 '{ ...()); }': () 240..290 '{ ...()); }': ()
250..251 'w': &Mutex<BufWriter> 250..251 'w': &Mutex<BufWriter>
276..287 '*(w.lock())': BufWriter 276..287 '*(w.lock())': BufWriter
278..279 'w': &Mutex<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); fun_name(i);
} }
fn $0fun_name(i: Struct<T, 'static>) { fn $0fun_name(i: Struct<'static, T>) {
foo(i); foo(i);
} }
"#, "#,

View file

@ -614,7 +614,7 @@ struct Foo<'a, T> {
} }
impl<'a, T> 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) self.field.bar(b)
} }
} }

View file

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

View file

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