2019-12-24 09:44:32 -06:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2019-08-22 13:31:21 -05:00
|
|
|
use hir::{db::HirDatabase, HasSource};
|
|
|
|
use ra_syntax::{
|
2019-09-28 11:50:16 -05:00
|
|
|
ast::{self, edit, make, AstNode, NameOwner},
|
2019-08-22 13:31:21 -05:00
|
|
|
SmolStr,
|
|
|
|
};
|
|
|
|
|
2019-09-28 12:09:57 -05:00
|
|
|
use crate::{Assist, AssistCtx, AssistId};
|
2019-03-06 16:45:11 -06:00
|
|
|
|
2019-07-03 08:17:18 -05:00
|
|
|
#[derive(PartialEq)]
|
2019-03-23 10:06:25 -05:00
|
|
|
enum AddMissingImplMembersMode {
|
|
|
|
DefaultMethodsOnly,
|
|
|
|
NoDefaultMethods,
|
|
|
|
}
|
|
|
|
|
2019-10-25 15:38:15 -05:00
|
|
|
// Assist: add_impl_missing_members
|
2019-10-26 09:27:47 -05:00
|
|
|
//
|
2019-10-26 11:08:13 -05:00
|
|
|
// Adds scaffold for required impl members.
|
2019-10-26 09:27:47 -05:00
|
|
|
//
|
2019-10-25 15:38:15 -05:00
|
|
|
// ```
|
2019-12-24 09:44:32 -06:00
|
|
|
// trait Trait<T> {
|
2019-10-25 15:38:15 -05:00
|
|
|
// Type X;
|
2019-12-24 09:44:32 -06:00
|
|
|
// fn foo(&self) -> T;
|
2019-10-25 15:38:15 -05:00
|
|
|
// fn bar(&self) {}
|
|
|
|
// }
|
|
|
|
//
|
2019-12-24 09:44:32 -06:00
|
|
|
// impl Trait<u32> for () {<|>
|
2019-10-25 15:38:15 -05:00
|
|
|
//
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
2019-12-24 09:44:32 -06:00
|
|
|
// trait Trait<T> {
|
2019-10-25 15:38:15 -05:00
|
|
|
// Type X;
|
2019-12-24 09:44:32 -06:00
|
|
|
// fn foo(&self) -> T;
|
2019-10-25 15:38:15 -05:00
|
|
|
// fn bar(&self) {}
|
|
|
|
// }
|
|
|
|
//
|
2019-12-24 09:44:32 -06:00
|
|
|
// impl Trait<u32> for () {
|
|
|
|
// fn foo(&self) -> u32 { unimplemented!() }
|
2019-10-25 15:38:15 -05:00
|
|
|
//
|
|
|
|
// }
|
|
|
|
// ```
|
2019-03-23 10:06:25 -05:00
|
|
|
pub(crate) fn add_missing_impl_members(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
|
|
|
add_missing_impl_members_inner(
|
|
|
|
ctx,
|
|
|
|
AddMissingImplMembersMode::NoDefaultMethods,
|
|
|
|
"add_impl_missing_members",
|
|
|
|
"add missing impl members",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-10-25 15:38:15 -05:00
|
|
|
// Assist: add_impl_default_members
|
2019-10-26 11:08:13 -05:00
|
|
|
//
|
|
|
|
// Adds scaffold for overriding default impl members.
|
|
|
|
//
|
2019-10-25 15:38:15 -05:00
|
|
|
// ```
|
2019-12-24 09:44:32 -06:00
|
|
|
// trait Trait {
|
2019-10-25 15:38:15 -05:00
|
|
|
// Type X;
|
|
|
|
// fn foo(&self);
|
|
|
|
// fn bar(&self) {}
|
|
|
|
// }
|
|
|
|
//
|
2019-12-24 09:44:32 -06:00
|
|
|
// impl Trait for () {
|
2019-10-25 15:38:15 -05:00
|
|
|
// Type X = ();
|
|
|
|
// fn foo(&self) {}<|>
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
2019-12-24 09:44:32 -06:00
|
|
|
// trait Trait {
|
2019-10-25 15:38:15 -05:00
|
|
|
// Type X;
|
|
|
|
// fn foo(&self);
|
|
|
|
// fn bar(&self) {}
|
|
|
|
// }
|
|
|
|
//
|
2019-12-24 09:44:32 -06:00
|
|
|
// impl Trait for () {
|
2019-10-25 15:38:15 -05:00
|
|
|
// Type X = ();
|
|
|
|
// fn foo(&self) {}
|
|
|
|
// fn bar(&self) {}
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
// ```
|
2019-03-23 10:06:25 -05:00
|
|
|
pub(crate) fn add_missing_default_members(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
|
|
|
add_missing_impl_members_inner(
|
|
|
|
ctx,
|
|
|
|
AddMissingImplMembersMode::DefaultMethodsOnly,
|
|
|
|
"add_impl_default_members",
|
|
|
|
"add impl default members",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_missing_impl_members_inner(
|
2019-10-27 09:35:37 -05:00
|
|
|
ctx: AssistCtx<impl HirDatabase>,
|
2019-03-23 10:06:25 -05:00
|
|
|
mode: AddMissingImplMembersMode,
|
|
|
|
assist_id: &'static str,
|
|
|
|
label: &'static str,
|
|
|
|
) -> Option<Assist> {
|
2019-10-27 03:48:40 -05:00
|
|
|
let impl_node = ctx.find_node_at_offset::<ast::ImplBlock>()?;
|
2019-03-06 18:48:31 -06:00
|
|
|
let impl_item_list = impl_node.item_list()?;
|
2019-03-06 16:45:11 -06:00
|
|
|
|
2019-12-24 09:44:32 -06:00
|
|
|
let (trait_, trait_def) = {
|
2019-11-15 16:14:56 -06:00
|
|
|
let analyzer = ctx.source_analyzer(impl_node.syntax(), None);
|
2019-03-06 16:45:11 -06:00
|
|
|
|
2019-07-19 03:24:41 -05:00
|
|
|
resolve_target_trait_def(ctx.db, &analyzer, &impl_node)?
|
2019-03-06 16:45:11 -06:00
|
|
|
};
|
|
|
|
|
2019-08-19 06:11:09 -05:00
|
|
|
let def_name = |item: &ast::ImplItem| -> Option<SmolStr> {
|
|
|
|
match item {
|
|
|
|
ast::ImplItem::FnDef(def) => def.name(),
|
|
|
|
ast::ImplItem::TypeAliasDef(def) => def.name(),
|
|
|
|
ast::ImplItem::ConstDef(def) => def.name(),
|
2019-07-03 08:17:18 -05:00
|
|
|
}
|
2019-07-19 03:24:41 -05:00
|
|
|
.map(|it| it.text().clone())
|
2019-03-07 09:20:37 -06:00
|
|
|
};
|
2019-07-03 08:17:18 -05:00
|
|
|
|
|
|
|
let trait_items = trait_def.item_list()?.impl_items();
|
|
|
|
let impl_items = impl_item_list.impl_items().collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let missing_items: Vec<_> = trait_items
|
2019-08-19 06:11:09 -05:00
|
|
|
.filter(|t| def_name(t).is_some())
|
|
|
|
.filter(|t| match t {
|
|
|
|
ast::ImplItem::FnDef(def) => match mode {
|
2019-07-03 08:17:18 -05:00
|
|
|
AddMissingImplMembersMode::DefaultMethodsOnly => def.body().is_some(),
|
|
|
|
AddMissingImplMembersMode::NoDefaultMethods => def.body().is_none(),
|
|
|
|
},
|
|
|
|
_ => mode == AddMissingImplMembersMode::NoDefaultMethods,
|
|
|
|
})
|
2019-08-19 06:11:09 -05:00
|
|
|
.filter(|t| impl_items.iter().all(|i| def_name(i) != def_name(t)))
|
2019-07-03 08:17:18 -05:00
|
|
|
.collect();
|
|
|
|
if missing_items.is_empty() {
|
2019-03-06 18:48:31 -06:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-12-24 09:44:32 -06:00
|
|
|
let file_id = ctx.frange.file_id;
|
|
|
|
let db = ctx.db;
|
|
|
|
|
2019-10-27 09:35:37 -05:00
|
|
|
ctx.add_assist(AssistId(assist_id), label, |edit| {
|
2019-04-22 05:01:33 -05:00
|
|
|
let n_existing_items = impl_item_list.impl_items().count();
|
2019-12-31 09:17:08 -06:00
|
|
|
let module = hir::SourceAnalyzer::new(
|
|
|
|
db,
|
|
|
|
hir::InFile::new(file_id.into(), impl_node.syntax()),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.module();
|
2019-12-24 09:44:32 -06:00
|
|
|
let substs = get_syntactic_substs(impl_node).unwrap_or_default();
|
|
|
|
let generic_def: hir::GenericDef = trait_.into();
|
|
|
|
let substs_by_param: HashMap<_, _> = generic_def
|
|
|
|
.params(db)
|
|
|
|
.into_iter()
|
|
|
|
// this is a trait impl, so we need to skip the first type parameter -- this is a bit hacky
|
|
|
|
.skip(1)
|
|
|
|
.zip(substs.into_iter())
|
|
|
|
.collect();
|
2019-09-30 02:08:28 -05:00
|
|
|
let items = missing_items
|
|
|
|
.into_iter()
|
2019-12-31 09:17:08 -06:00
|
|
|
.map(|it| match module {
|
|
|
|
Some(module) => qualify_paths(db, hir::InFile::new(file_id.into(), it), module),
|
|
|
|
None => it,
|
|
|
|
})
|
2019-12-24 09:44:32 -06:00
|
|
|
.map(|it| {
|
|
|
|
substitute_type_params(db, hir::InFile::new(file_id.into(), it), &substs_by_param)
|
|
|
|
})
|
2019-09-30 02:08:28 -05:00
|
|
|
.map(|it| match it {
|
|
|
|
ast::ImplItem::FnDef(def) => ast::ImplItem::FnDef(add_body(def)),
|
|
|
|
_ => it,
|
|
|
|
})
|
|
|
|
.map(|it| edit::strip_attrs_and_docs(&it));
|
2019-09-28 12:09:57 -05:00
|
|
|
let new_impl_item_list = impl_item_list.append_items(items);
|
|
|
|
let cursor_position = {
|
|
|
|
let first_new_item = new_impl_item_list.impl_items().nth(n_existing_items).unwrap();
|
|
|
|
first_new_item.syntax().text_range().start()
|
|
|
|
};
|
2019-04-22 05:01:33 -05:00
|
|
|
|
2019-09-28 12:09:57 -05:00
|
|
|
edit.replace_ast(impl_item_list, new_impl_item_list);
|
2019-07-10 13:53:44 -05:00
|
|
|
edit.set_cursor(cursor_position);
|
2019-10-27 09:35:37 -05:00
|
|
|
})
|
2019-03-06 07:41:22 -06:00
|
|
|
}
|
|
|
|
|
2019-07-19 03:24:41 -05:00
|
|
|
fn add_body(fn_def: ast::FnDef) -> ast::FnDef {
|
2019-04-22 05:01:33 -05:00
|
|
|
if fn_def.body().is_none() {
|
2019-09-26 14:08:44 -05:00
|
|
|
fn_def.with_body(make::block_from_expr(make::expr_unimplemented()))
|
|
|
|
} else {
|
|
|
|
fn_def
|
2019-04-22 05:01:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-24 09:44:32 -06:00
|
|
|
// FIXME: It would probably be nicer if we could get this via HIR (i.e. get the
|
|
|
|
// trait ref, and then go from the types in the substs back to the syntax)
|
|
|
|
// FIXME: This should be a general utility (not even just for assists)
|
|
|
|
fn get_syntactic_substs(impl_block: ast::ImplBlock) -> Option<Vec<ast::TypeRef>> {
|
|
|
|
let target_trait = impl_block.target_trait()?;
|
|
|
|
let path_type = match target_trait {
|
|
|
|
ast::TypeRef::PathType(path) => path,
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
let type_arg_list = path_type.path()?.segment()?.type_arg_list()?;
|
|
|
|
let mut result = Vec::new();
|
|
|
|
for type_arg in type_arg_list.type_args() {
|
|
|
|
let type_arg: ast::TypeArg = type_arg;
|
|
|
|
result.push(type_arg.type_ref()?);
|
|
|
|
}
|
|
|
|
Some(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: This should be a general utility (not even just for assists)
|
|
|
|
fn substitute_type_params<N: AstNode>(
|
|
|
|
db: &impl HirDatabase,
|
|
|
|
node: hir::InFile<N>,
|
|
|
|
substs: &HashMap<hir::TypeParam, ast::TypeRef>,
|
|
|
|
) -> N {
|
|
|
|
let type_param_replacements = node
|
|
|
|
.value
|
|
|
|
.syntax()
|
|
|
|
.descendants()
|
|
|
|
.filter_map(ast::TypeRef::cast)
|
|
|
|
.filter_map(|n| {
|
|
|
|
let path = match &n {
|
|
|
|
ast::TypeRef::PathType(path_type) => path_type.path()?,
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
let analyzer = hir::SourceAnalyzer::new(db, node.with_value(n.syntax()), None);
|
|
|
|
let resolution = analyzer.resolve_path(db, &path)?;
|
|
|
|
match resolution {
|
|
|
|
hir::PathResolution::TypeParam(tp) => Some((n, substs.get(&tp)?.clone())),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
if type_param_replacements.is_empty() {
|
|
|
|
node.value
|
|
|
|
} else {
|
|
|
|
edit::replace_descendants(&node.value, type_param_replacements.into_iter())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 09:17:08 -06:00
|
|
|
use hir::PathResolution;
|
|
|
|
|
2020-01-01 16:08:22 -06:00
|
|
|
// TODO handle generic args
|
|
|
|
// TODO handle associated item paths
|
2019-12-31 09:17:08 -06:00
|
|
|
// TODO handle value ns?
|
|
|
|
|
2020-01-01 16:08:22 -06:00
|
|
|
// FIXME extract this to a general utility as well
|
2019-12-31 09:17:08 -06:00
|
|
|
fn qualify_paths<N: AstNode>(db: &impl HirDatabase, node: hir::InFile<N>, from: hir::Module) -> N {
|
|
|
|
let path_replacements = node
|
|
|
|
.value
|
|
|
|
.syntax()
|
|
|
|
.descendants()
|
|
|
|
.filter_map(ast::Path::cast)
|
|
|
|
.filter_map(|p| {
|
2020-01-01 16:08:22 -06:00
|
|
|
if p.segment().and_then(|s| s.param_list()).is_some() {
|
|
|
|
// don't try to qualify `Fn(Foo) -> Bar` paths, they are in prelude anyway
|
|
|
|
return None;
|
|
|
|
}
|
2019-12-31 09:17:08 -06:00
|
|
|
let analyzer = hir::SourceAnalyzer::new(db, node.with_value(p.syntax()), None);
|
|
|
|
let resolution = analyzer.resolve_path(db, &p)?;
|
|
|
|
match resolution {
|
|
|
|
PathResolution::Def(def) => {
|
|
|
|
let found_path = from.find_path(db, def)?;
|
|
|
|
Some((p, found_path.to_ast()))
|
|
|
|
}
|
|
|
|
PathResolution::Local(_)
|
|
|
|
| PathResolution::TypeParam(_)
|
|
|
|
| PathResolution::SelfType(_) => None,
|
|
|
|
PathResolution::Macro(_) => None,
|
|
|
|
PathResolution::AssocItem(_) => None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
if path_replacements.is_empty() {
|
|
|
|
node.value
|
|
|
|
} else {
|
|
|
|
edit::replace_descendants(&node.value, path_replacements.into_iter())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 17:24:17 -05:00
|
|
|
/// Given an `ast::ImplBlock`, resolves the target trait (the one being
|
|
|
|
/// implemented) to a `ast::TraitDef`.
|
|
|
|
fn resolve_target_trait_def(
|
|
|
|
db: &impl HirDatabase,
|
2019-04-11 07:51:02 -05:00
|
|
|
analyzer: &hir::SourceAnalyzer,
|
2019-03-16 17:24:17 -05:00
|
|
|
impl_block: &ast::ImplBlock,
|
2019-12-24 09:44:32 -06:00
|
|
|
) -> Option<(hir::Trait, ast::TraitDef)> {
|
2019-07-19 03:24:41 -05:00
|
|
|
let ast_path = impl_block
|
|
|
|
.target_trait()
|
|
|
|
.map(|it| it.syntax().clone())
|
|
|
|
.and_then(ast::PathType::cast)?
|
|
|
|
.path()?;
|
2019-03-16 17:24:17 -05:00
|
|
|
|
2019-04-11 07:51:02 -05:00
|
|
|
match analyzer.resolve_path(db, &ast_path) {
|
2019-12-24 09:44:32 -06:00
|
|
|
Some(hir::PathResolution::Def(hir::ModuleDef::Trait(def))) => {
|
|
|
|
Some((def, def.source(db).value))
|
|
|
|
}
|
2019-03-16 17:24:17 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 07:41:22 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-03-07 06:21:56 -06:00
|
|
|
use crate::helpers::{check_assist, check_assist_not_applicable};
|
2019-03-06 07:41:22 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_add_missing_impl_members() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
trait Foo {
|
2019-07-03 08:17:18 -05:00
|
|
|
type Output;
|
|
|
|
|
|
|
|
const CONST: usize = 42;
|
|
|
|
|
2019-03-06 07:41:22 -06:00
|
|
|
fn foo(&self);
|
2019-03-06 18:48:31 -06:00
|
|
|
fn bar(&self);
|
2019-03-07 05:02:53 -06:00
|
|
|
fn baz(&self);
|
2019-03-06 07:41:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
|
|
|
|
impl Foo for S {
|
2019-03-06 18:48:31 -06:00
|
|
|
fn bar(&self) {}
|
2019-04-22 05:01:33 -05:00
|
|
|
<|>
|
2019-03-06 07:41:22 -06:00
|
|
|
}",
|
|
|
|
"
|
|
|
|
trait Foo {
|
2019-07-03 08:17:18 -05:00
|
|
|
type Output;
|
|
|
|
|
|
|
|
const CONST: usize = 42;
|
|
|
|
|
2019-03-06 07:41:22 -06:00
|
|
|
fn foo(&self);
|
2019-03-06 18:48:31 -06:00
|
|
|
fn bar(&self);
|
2019-03-07 05:02:53 -06:00
|
|
|
fn baz(&self);
|
2019-03-06 07:41:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
|
|
|
|
impl Foo for S {
|
2019-03-06 18:48:31 -06:00
|
|
|
fn bar(&self) {}
|
2019-07-03 08:17:18 -05:00
|
|
|
<|>type Output;
|
|
|
|
const CONST: usize = 42;
|
|
|
|
fn foo(&self) { unimplemented!() }
|
2019-04-22 05:01:33 -05:00
|
|
|
fn baz(&self) { unimplemented!() }
|
|
|
|
|
2019-03-06 07:41:22 -06:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
2019-03-07 06:21:56 -06:00
|
|
|
|
2019-03-07 06:44:01 -06:00
|
|
|
#[test]
|
|
|
|
fn test_copied_overriden_members() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
trait Foo {
|
|
|
|
fn foo(&self);
|
|
|
|
fn bar(&self) -> bool { true }
|
|
|
|
fn baz(&self) -> u32 { 42 }
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
|
|
|
|
impl Foo for S {
|
|
|
|
fn bar(&self) {}
|
2019-04-22 05:01:33 -05:00
|
|
|
<|>
|
2019-03-07 06:44:01 -06:00
|
|
|
}",
|
|
|
|
"
|
|
|
|
trait Foo {
|
|
|
|
fn foo(&self);
|
|
|
|
fn bar(&self) -> bool { true }
|
|
|
|
fn baz(&self) -> u32 { 42 }
|
|
|
|
}
|
|
|
|
|
|
|
|
struct S;
|
|
|
|
|
|
|
|
impl Foo for S {
|
|
|
|
fn bar(&self) {}
|
2019-04-22 05:01:33 -05:00
|
|
|
<|>fn foo(&self) { unimplemented!() }
|
|
|
|
|
2019-03-07 06:44:01 -06:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-07 06:21:56 -06:00
|
|
|
#[test]
|
|
|
|
fn test_empty_impl_block() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
trait Foo { fn foo(&self); }
|
|
|
|
struct S;
|
2019-03-23 09:49:20 -05:00
|
|
|
impl Foo for S { <|> }",
|
2019-03-07 06:21:56 -06:00
|
|
|
"
|
|
|
|
trait Foo { fn foo(&self); }
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {
|
2019-04-22 05:01:33 -05:00
|
|
|
<|>fn foo(&self) { unimplemented!() }
|
2019-03-07 06:21:56 -06:00
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-24 09:44:32 -06:00
|
|
|
#[test]
|
|
|
|
fn fill_in_type_params_1() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
trait Foo<T> { fn foo(&self, t: T) -> &T; }
|
|
|
|
struct S;
|
|
|
|
impl Foo<u32> for S { <|> }",
|
|
|
|
"
|
|
|
|
trait Foo<T> { fn foo(&self, t: T) -> &T; }
|
|
|
|
struct S;
|
|
|
|
impl Foo<u32> for S {
|
|
|
|
<|>fn foo(&self, t: u32) -> &u32 { unimplemented!() }
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fill_in_type_params_2() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
trait Foo<T> { fn foo(&self, t: T) -> &T; }
|
|
|
|
struct S;
|
|
|
|
impl<U> Foo<U> for S { <|> }",
|
|
|
|
"
|
|
|
|
trait Foo<T> { fn foo(&self, t: T) -> &T; }
|
|
|
|
struct S;
|
|
|
|
impl<U> Foo<U> for S {
|
|
|
|
<|>fn foo(&self, t: U) -> &U { unimplemented!() }
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-07 06:21:56 -06:00
|
|
|
#[test]
|
|
|
|
fn test_cursor_after_empty_impl_block() {
|
2019-03-16 17:19:14 -05:00
|
|
|
check_assist(
|
2019-03-07 06:21:56 -06:00
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
trait Foo { fn foo(&self); }
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {}<|>",
|
2019-03-16 17:19:14 -05:00
|
|
|
"
|
|
|
|
trait Foo { fn foo(&self); }
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {
|
2019-04-22 05:01:33 -05:00
|
|
|
<|>fn foo(&self) { unimplemented!() }
|
2019-03-16 17:19:14 -05:00
|
|
|
}",
|
2019-03-07 06:21:56 -06:00
|
|
|
)
|
|
|
|
}
|
2019-03-07 06:44:01 -06:00
|
|
|
|
2019-12-30 06:53:43 -06:00
|
|
|
#[test]
|
|
|
|
fn test_qualify_path_1() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
mod foo {
|
2019-12-31 09:17:08 -06:00
|
|
|
pub struct Bar;
|
2019-12-30 06:53:43 -06:00
|
|
|
trait Foo { fn foo(&self, bar: Bar); }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo for S { <|> }",
|
|
|
|
"
|
|
|
|
mod foo {
|
2019-12-31 09:17:08 -06:00
|
|
|
pub struct Bar;
|
2019-12-30 06:53:43 -06:00
|
|
|
trait Foo { fn foo(&self, bar: Bar); }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo for S {
|
|
|
|
<|>fn foo(&self, bar: foo::Bar) { unimplemented!() }
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-01 16:08:22 -06:00
|
|
|
#[test]
|
|
|
|
fn test_qualify_path_generic() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar<T>;
|
|
|
|
trait Foo { fn foo(&self, bar: Bar<u32>); }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo for S { <|> }",
|
|
|
|
"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar<T>;
|
|
|
|
trait Foo { fn foo(&self, bar: Bar<u32>); }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo for S {
|
|
|
|
<|>fn foo(&self, bar: foo::Bar<u32>) { unimplemented!() }
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_qualify_path_and_substitute_param() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar<T>;
|
|
|
|
trait Foo<T> { fn foo(&self, bar: Bar<T>); }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo<u32> for S { <|> }",
|
|
|
|
"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar<T>;
|
|
|
|
trait Foo<T> { fn foo(&self, bar: Bar<T>); }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo<u32> for S {
|
|
|
|
<|>fn foo(&self, bar: foo::Bar<u32>) { unimplemented!() }
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_qualify_path_associated_item() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar<T>;
|
|
|
|
impl Bar<T> { type Assoc = u32; }
|
|
|
|
trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo for S { <|> }",
|
|
|
|
"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar<T>;
|
|
|
|
impl Bar { type Assoc = u32; }
|
|
|
|
trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo for S {
|
|
|
|
<|>fn foo(&self, bar: foo::Bar<u32>::Assoc) { unimplemented!() }
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_qualify_path_nested() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar<T>;
|
|
|
|
pub struct Baz;
|
|
|
|
trait Foo { fn foo(&self, bar: Bar<Baz>); }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo for S { <|> }",
|
|
|
|
"
|
|
|
|
mod foo {
|
|
|
|
pub struct Bar<T>;
|
|
|
|
pub struct Baz;
|
|
|
|
trait Foo { fn foo(&self, bar: Bar<Baz>); }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo for S {
|
|
|
|
<|>fn foo(&self, bar: foo::Bar<foo::Baz>) { unimplemented!() }
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_qualify_path_fn_trait_notation() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
mod foo {
|
|
|
|
pub trait Fn<Args> { type Output; }
|
|
|
|
trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo for S { <|> }",
|
|
|
|
"
|
|
|
|
mod foo {
|
|
|
|
pub trait Fn<Args> { type Output; }
|
|
|
|
trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl foo::Foo for S {
|
|
|
|
<|>fn foo(&self, bar: dyn Fn(u32) -> i32) { unimplemented!() }
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-07 06:44:01 -06:00
|
|
|
#[test]
|
|
|
|
fn test_empty_trait() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
trait Foo;
|
|
|
|
struct S;
|
|
|
|
impl Foo for S { <|> }",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-03-23 09:49:20 -05:00
|
|
|
fn test_ignore_unnamed_trait_members_and_default_methods() {
|
|
|
|
check_assist_not_applicable(
|
2019-03-07 06:44:01 -06:00
|
|
|
add_missing_impl_members,
|
|
|
|
"
|
|
|
|
trait Foo {
|
|
|
|
fn (arg: u32);
|
|
|
|
fn valid(some: u32) -> bool { false }
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl Foo for S { <|> }",
|
2019-03-07 09:10:21 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-23 09:49:20 -05:00
|
|
|
#[test]
|
|
|
|
fn test_with_docstring_and_attrs() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_impl_members,
|
|
|
|
r#"
|
|
|
|
#[doc(alias = "test alias")]
|
|
|
|
trait Foo {
|
|
|
|
/// doc string
|
2019-07-03 08:17:18 -05:00
|
|
|
type Output;
|
|
|
|
|
2019-03-23 09:49:20 -05:00
|
|
|
#[must_use]
|
|
|
|
fn foo(&self);
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {}<|>"#,
|
|
|
|
r#"
|
|
|
|
#[doc(alias = "test alias")]
|
|
|
|
trait Foo {
|
|
|
|
/// doc string
|
2019-07-03 08:17:18 -05:00
|
|
|
type Output;
|
|
|
|
|
2019-03-23 09:49:20 -05:00
|
|
|
#[must_use]
|
|
|
|
fn foo(&self);
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {
|
2019-07-03 08:17:18 -05:00
|
|
|
<|>type Output;
|
|
|
|
fn foo(&self) { unimplemented!() }
|
2019-03-23 09:49:20 -05:00
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
2019-03-23 10:06:25 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_default_methods() {
|
|
|
|
check_assist(
|
|
|
|
add_missing_default_members,
|
|
|
|
"
|
|
|
|
trait Foo {
|
2019-07-03 08:17:18 -05:00
|
|
|
type Output;
|
|
|
|
|
|
|
|
const CONST: usize = 42;
|
|
|
|
|
2019-03-23 10:06:25 -05:00
|
|
|
fn valid(some: u32) -> bool { false }
|
|
|
|
fn foo(some: u32) -> bool;
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl Foo for S { <|> }",
|
|
|
|
"
|
|
|
|
trait Foo {
|
2019-07-03 08:17:18 -05:00
|
|
|
type Output;
|
|
|
|
|
|
|
|
const CONST: usize = 42;
|
|
|
|
|
2019-03-23 10:06:25 -05:00
|
|
|
fn valid(some: u32) -> bool { false }
|
|
|
|
fn foo(some: u32) -> bool;
|
|
|
|
}
|
|
|
|
struct S;
|
|
|
|
impl Foo for S {
|
2019-04-22 05:01:33 -05:00
|
|
|
<|>fn valid(some: u32) -> bool { false }
|
2019-03-23 10:06:25 -05:00
|
|
|
}",
|
|
|
|
)
|
|
|
|
}
|
2019-03-06 07:41:22 -06:00
|
|
|
}
|