Skip to content

Commit 7f45ccc

Browse files
committed
lifetime transformation: refactoring & a new test
1 parent fe8f862 commit 7f45ccc

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

crates/hir/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2637,18 +2637,21 @@ impl GenericDef {
26372637
Either::Right(x) => GenericParam::TypeParam(x),
26382638
}
26392639
});
2640-
self.lifetime_params(db).into_iter().chain(ty_params).collect()
2640+
self.lifetime_params(db)
2641+
.into_iter()
2642+
.map(GenericParam::LifetimeParam)
2643+
.chain(ty_params)
2644+
.collect()
26412645
}
26422646

2643-
pub fn lifetime_params(self, db: &dyn HirDatabase) -> Vec<GenericParam> {
2647+
pub fn lifetime_params(self, db: &dyn HirDatabase) -> Vec<LifetimeParam> {
26442648
let generics = db.generic_params(self.into());
26452649
generics
26462650
.lifetimes
26472651
.iter()
26482652
.map(|(local_id, _)| LifetimeParam {
26492653
id: LifetimeParamId { parent: self.into(), local_id },
26502654
})
2651-
.map(GenericParam::LifetimeParam)
26522655
.collect()
26532656
}
26542657

crates/ide-completion/src/completions/item_list/trait_impl.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,33 @@ impl Test for () {
833833
);
834834
}
835835

836+
#[test]
837+
fn fn_with_lifetimes() {
838+
check_edit(
839+
"fn foo",
840+
r#"
841+
trait Test<'a, 'b, T> {
842+
fn foo(&self, a: &'a T, b: &'b T) -> &'a T;
843+
}
844+
845+
impl<'x, 'y, A> Test<'x, 'y, A> for () {
846+
t$0
847+
}
848+
"#,
849+
r#"
850+
trait Test<'a, 'b, T> {
851+
fn foo(&self, a: &'a T, b: &'b T) -> &'a T;
852+
}
853+
854+
impl<'x, 'y, A> Test<'x, 'y, A> for () {
855+
fn foo(&self, a: &'x A, b: &'y A) -> &'x A {
856+
$0
857+
}
858+
}
859+
"#,
860+
);
861+
}
862+
836863
#[test]
837864
fn complete_without_name() {
838865
let test = |completion: &str, hint: &str, completed: &str, next_sibling: &str| {

crates/ide-db/src/path_transform.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use syntax::{
1010
};
1111

1212
#[derive(Default)]
13-
struct Substs {
13+
struct AstSubsts {
1414
types: Vec<ast::TypeArg>,
1515
lifetimes: Vec<ast::LifetimeArg>,
1616
}
@@ -42,7 +42,7 @@ type LifetimeName = String;
4242
/// ```
4343
pub struct PathTransform<'a> {
4444
generic_def: Option<hir::GenericDef>,
45-
substs: Substs,
45+
substs: AstSubsts,
4646
target_scope: &'a SemanticsScope<'a>,
4747
source_scope: &'a SemanticsScope<'a>,
4848
}
@@ -80,7 +80,12 @@ impl<'a> PathTransform<'a> {
8080
target_scope: &'a SemanticsScope<'a>,
8181
source_scope: &'a SemanticsScope<'a>,
8282
) -> PathTransform<'a> {
83-
PathTransform { source_scope, target_scope, generic_def: None, substs: Substs::default() }
83+
PathTransform {
84+
source_scope,
85+
target_scope,
86+
generic_def: None,
87+
substs: AstSubsts::default(),
88+
}
8489
}
8590

8691
pub fn apply(&self, syntax: &SyntaxNode) {
@@ -134,7 +139,7 @@ impl<'a> PathTransform<'a> {
134139
.into_iter()
135140
.flat_map(|it| it.lifetime_params(db))
136141
.zip(self.substs.lifetimes.clone())
137-
.filter_map(|(k, v)| Some((k.name(db).to_string(), v.lifetime()?)))
142+
.filter_map(|(k, v)| Some((k.name(db).display(db.upcast()).to_string(), v.lifetime()?)))
138143
.collect();
139144
Ctx { type_substs, lifetime_substs, target_module, source_scope: self.source_scope }
140145
}
@@ -279,7 +284,7 @@ impl<'a> Ctx<'a> {
279284

280285
// FIXME: It would probably be nicer if we could get this via HIR (i.e. get the
281286
// trait ref, and then go from the types in the substs back to the syntax).
282-
fn get_syntactic_substs(impl_def: ast::Impl) -> Option<Substs> {
287+
fn get_syntactic_substs(impl_def: ast::Impl) -> Option<AstSubsts> {
283288
let target_trait = impl_def.trait_()?;
284289
let path_type = match target_trait {
285290
ast::Type::PathType(path) => path,
@@ -290,8 +295,8 @@ fn get_syntactic_substs(impl_def: ast::Impl) -> Option<Substs> {
290295
get_type_args_from_arg_list(generic_arg_list)
291296
}
292297

293-
fn get_type_args_from_arg_list(generic_arg_list: ast::GenericArgList) -> Option<Substs> {
294-
let mut result = Substs::default();
298+
fn get_type_args_from_arg_list(generic_arg_list: ast::GenericArgList) -> Option<AstSubsts> {
299+
let mut result = AstSubsts::default();
295300
generic_arg_list.generic_args().for_each(|generic_arg| match generic_arg {
296301
ast::GenericArg::TypeArg(type_arg) => result.types.push(type_arg),
297302
ast::GenericArg::LifetimeArg(l_arg) => result.lifetimes.push(l_arg),

0 commit comments

Comments
 (0)