Skip to content

Commit d091991

Browse files
committed
fix(completion): derive source scope from syntax node to be transformed
1 parent 008f506 commit d091991

File tree

2 files changed

+79
-12
lines changed

2 files changed

+79
-12
lines changed

crates/hir/src/semantics.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,6 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
483483
self.imp.scope_at_offset(node, offset)
484484
}
485485

486-
pub fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> {
487-
self.imp.scope_for_def(def)
488-
}
489-
490486
pub fn assert_contains_node(&self, node: &SyntaxNode) {
491487
self.imp.assert_contains_node(node)
492488
}
@@ -1307,12 +1303,6 @@ impl<'db> SemanticsImpl<'db> {
13071303
)
13081304
}
13091305

1310-
fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> {
1311-
let file_id = self.db.lookup_intern_trait(def.id).id.file_id();
1312-
let resolver = def.id.resolver(self.db.upcast());
1313-
SemanticsScope { db: self.db, file_id, resolver }
1314-
}
1315-
13161306
fn source<Def: HasSource>(&self, def: Def) -> Option<InFile<Def::Ast>>
13171307
where
13181308
Def::Ast: AstNode,

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

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,8 @@ fn get_transformed_assoc_item(
227227
assoc_item: ast::AssocItem,
228228
impl_def: hir::Impl,
229229
) -> Option<ast::AssocItem> {
230-
let assoc_item = assoc_item.clone_for_update();
231230
let trait_ = impl_def.trait_(ctx.db)?;
232-
let source_scope = &ctx.sema.scope_for_def(trait_);
231+
let source_scope = &ctx.sema.scope(assoc_item.syntax())?;
233232
let target_scope = &ctx.sema.scope(ctx.sema.source(impl_def)?.syntax().value)?;
234233
let transform = PathTransform::trait_impl(
235234
target_scope,
@@ -238,6 +237,9 @@ fn get_transformed_assoc_item(
238237
ctx.sema.source(impl_def)?.value,
239238
);
240239

240+
let assoc_item = assoc_item.clone_for_update();
241+
// FIXME: Paths in nested macros are not handled well. See
242+
// `macro_generated_assoc_item2` test.
241243
transform.apply(assoc_item.syntax());
242244
assoc_item.remove_attrs_and_docs();
243245
Some(assoc_item)
@@ -1220,6 +1222,81 @@ impl Foo for Test {
12201222
);
12211223
}
12221224

1225+
#[test]
1226+
fn macro_generated_assoc_item() {
1227+
check_edit(
1228+
"fn method",
1229+
r#"
1230+
macro_rules! ty { () => { i32 } }
1231+
trait SomeTrait { type Output; }
1232+
impl SomeTrait for i32 { type Output = i64; }
1233+
macro_rules! define_method {
1234+
() => {
1235+
fn method(&mut self, params: <ty!() as SomeTrait>::Output);
1236+
};
1237+
}
1238+
trait AnotherTrait { define_method!(); }
1239+
impl AnotherTrait for () {
1240+
$0
1241+
}
1242+
"#,
1243+
r#"
1244+
macro_rules! ty { () => { i32 } }
1245+
trait SomeTrait { type Output; }
1246+
impl SomeTrait for i32 { type Output = i64; }
1247+
macro_rules! define_method {
1248+
() => {
1249+
fn method(&mut self, params: <ty!() as SomeTrait>::Output);
1250+
};
1251+
}
1252+
trait AnotherTrait { define_method!(); }
1253+
impl AnotherTrait for () {
1254+
fn method(&mut self,params: <ty!()as SomeTrait>::Output) {
1255+
$0
1256+
}
1257+
}
1258+
"#,
1259+
);
1260+
}
1261+
1262+
// FIXME: `T` in `ty!(T)` should be replaced by `PathTransform`.
1263+
#[test]
1264+
fn macro_generated_assoc_item2() {
1265+
check_edit(
1266+
"fn method",
1267+
r#"
1268+
macro_rules! ty { ($me:ty) => { $me } }
1269+
trait SomeTrait { type Output; }
1270+
impl SomeTrait for i32 { type Output = i64; }
1271+
macro_rules! define_method {
1272+
($t:ty) => {
1273+
fn method(&mut self, params: <ty!($t) as SomeTrait>::Output);
1274+
};
1275+
}
1276+
trait AnotherTrait<T: SomeTrait> { define_method!(T); }
1277+
impl AnotherTrait<i32> for () {
1278+
$0
1279+
}
1280+
"#,
1281+
r#"
1282+
macro_rules! ty { ($me:ty) => { $me } }
1283+
trait SomeTrait { type Output; }
1284+
impl SomeTrait for i32 { type Output = i64; }
1285+
macro_rules! define_method {
1286+
($t:ty) => {
1287+
fn method(&mut self, params: <ty!($t) as SomeTrait>::Output);
1288+
};
1289+
}
1290+
trait AnotherTrait<T: SomeTrait> { define_method!(T); }
1291+
impl AnotherTrait<i32> for () {
1292+
fn method(&mut self,params: <ty!(T)as SomeTrait>::Output) {
1293+
$0
1294+
}
1295+
}
1296+
"#,
1297+
);
1298+
}
1299+
12231300
#[test]
12241301
fn includes_gat_generics() {
12251302
check_edit(

0 commit comments

Comments
 (0)