diff --git a/crates/assists/src/handlers/add_lifetime_to_type.rs b/crates/assists/src/handlers/add_lifetime_to_type.rs new file mode 100644 index 000000000000..c1603e9722a3 --- /dev/null +++ b/crates/assists/src/handlers/add_lifetime_to_type.rs @@ -0,0 +1,228 @@ +use ast::FieldList; +use syntax::ast::{self, AstNode, GenericParamsOwner, NameOwner, RefType, Type}; + +use crate::{AssistContext, AssistId, AssistKind, Assists}; + +// Assist: add_lifetime_to_type +// +// Adds a new lifetime to a struct, enum or union. +// +// ``` +// struct Point { +// x: &$0u32, +// y: u32, +// } +// ``` +// -> +// ``` +// struct Point<'a> { +// x: &'a u32, +// y: u32, +// } +// ``` +pub(crate) fn add_lifetime_to_type(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { + let ref_type_focused = ctx.find_node_at_offset::()?; + if ref_type_focused.lifetime().is_some() { + return None; + } + + let node = ctx.find_node_at_offset::()?; + let has_lifetime = node + .generic_param_list() + .map(|gen_list| gen_list.lifetime_params().count() > 0) + .unwrap_or_default(); + + if has_lifetime { + return None; + } + + let ref_types = fetch_borrowed_types(&node)?; + let target = node.syntax().text_range(); + + acc.add( + AssistId("add_lifetime_to_type", AssistKind::Generate), + "Add lifetime`", + target, + |builder| { + match node.generic_param_list() { + Some(gen_param) => { + if let Some(left_angle) = gen_param.l_angle_token() { + builder.insert(left_angle.text_range().end(), "'a, "); + } + } + None => { + if let Some(name) = node.name() { + builder.insert(name.syntax().text_range().end(), "<'a>"); + } + } + } + + for ref_type in ref_types { + if let Some(amp_token) = ref_type.amp_token() { + builder.insert(amp_token.text_range().end(), "'a "); + } + } + }, + ) +} + +fn fetch_borrowed_types(node: &ast::AdtDef) -> Option> { + let ref_types: Vec = match node { + ast::AdtDef::Enum(enum_) => { + let variant_list = enum_.variant_list()?; + variant_list + .variants() + .filter_map(|variant| { + let field_list = variant.field_list()?; + + find_ref_types_from_field_list(&field_list) + }) + .flatten() + .collect() + } + ast::AdtDef::Struct(strukt) => { + let field_list = strukt.field_list()?; + find_ref_types_from_field_list(&field_list)? + } + ast::AdtDef::Union(un) => { + let record_field_list = un.record_field_list()?; + record_field_list + .fields() + .filter_map(|r_field| { + if let Type::RefType(ref_type) = r_field.ty()? { + if ref_type.lifetime().is_none() { + return Some(ref_type); + } + } + + None + }) + .collect() + } + }; + + if ref_types.is_empty() { + None + } else { + Some(ref_types) + } +} + +fn find_ref_types_from_field_list(field_list: &FieldList) -> Option> { + let ref_types: Vec = match field_list { + ast::FieldList::RecordFieldList(record_list) => record_list + .fields() + .filter_map(|f| { + if let Type::RefType(ref_type) = f.ty()? { + if ref_type.lifetime().is_none() { + return Some(ref_type); + } + } + + None + }) + .collect(), + ast::FieldList::TupleFieldList(tuple_field_list) => tuple_field_list + .fields() + .filter_map(|f| { + if let Type::RefType(ref_type) = f.ty()? { + if ref_type.lifetime().is_none() { + return Some(ref_type); + } + } + + None + }) + .collect(), + }; + + if ref_types.is_empty() { + None + } else { + Some(ref_types) + } +} + +#[cfg(test)] +mod tests { + use crate::tests::{check_assist, check_assist_not_applicable}; + + use super::*; + + #[test] + fn add_lifetime_to_struct() { + check_assist( + add_lifetime_to_type, + "struct Foo { a: &$0i32 }", + "struct Foo<'a> { a: &'a i32 }", + ); + + check_assist( + add_lifetime_to_type, + "struct Foo { a: &$0i32, b: &usize }", + "struct Foo<'a> { a: &'a i32, b: &'a usize }", + ); + + check_assist( + add_lifetime_to_type, + "struct Foo { a: &$0i32, b: usize }", + "struct Foo<'a> { a: &'a i32, b: usize }", + ); + + check_assist( + add_lifetime_to_type, + "struct Foo { a: &$0T, b: usize }", + "struct Foo<'a, T> { a: &'a T, b: usize }", + ); + + check_assist_not_applicable(add_lifetime_to_type, "struct Foo<'a> { a: &$0'a i32 }"); + check_assist_not_applicable(add_lifetime_to_type, "struct Foo { a: &'a$0 i32 }"); + } + + #[test] + fn add_lifetime_to_enum() { + check_assist( + add_lifetime_to_type, + "enum Foo { Bar { a: i32 }, Other, Tuple(u32, &$0u32)}", + "enum Foo<'a> { Bar { a: i32 }, Other, Tuple(u32, &'a u32)}", + ); + + check_assist( + add_lifetime_to_type, + "enum Foo { Bar { a: &$0i32 }}", + "enum Foo<'a> { Bar { a: &'a i32 }}", + ); + + check_assist( + add_lifetime_to_type, + "enum Foo { Bar { a: &$0i32, b: &T }}", + "enum Foo<'a, T> { Bar { a: &'a i32, b: &'a T }}", + ); + + check_assist_not_applicable(add_lifetime_to_type, "enum Foo<'a> { Bar { a: &$0'a i32 }}"); + check_assist_not_applicable(add_lifetime_to_type, "enum Foo { Bar, $0Misc }"); + } + + #[test] + fn add_lifetime_to_union() { + check_assist( + add_lifetime_to_type, + "union Foo { a: &$0i32 }", + "union Foo<'a> { a: &'a i32 }", + ); + + check_assist( + add_lifetime_to_type, + "union Foo { a: &$0i32, b: &usize }", + "union Foo<'a> { a: &'a i32, b: &'a usize }", + ); + + check_assist( + add_lifetime_to_type, + "union Foo { a: &$0T, b: usize }", + "union Foo<'a, T> { a: &'a T, b: usize }", + ); + + check_assist_not_applicable(add_lifetime_to_type, "struct Foo<'a> { a: &'a $0i32 }"); + } +} diff --git a/crates/assists/src/lib.rs b/crates/assists/src/lib.rs index 14178a6510f3..559b9651e800 100644 --- a/crates/assists/src/lib.rs +++ b/crates/assists/src/lib.rs @@ -108,6 +108,7 @@ mod handlers { pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>; mod add_explicit_type; + mod add_lifetime_to_type; mod add_missing_impl_members; mod add_turbo_fish; mod apply_demorgan; @@ -164,6 +165,7 @@ pub(crate) fn all() -> &'static [Handler] { &[ // These are alphabetic for the foolish consistency add_explicit_type::add_explicit_type, + add_lifetime_to_type::add_lifetime_to_type, add_turbo_fish::add_turbo_fish, apply_demorgan::apply_demorgan, auto_import::auto_import, diff --git a/crates/assists/src/tests/generated.rs b/crates/assists/src/tests/generated.rs index d48d063b4076..9aa807f10a8f 100644 --- a/crates/assists/src/tests/generated.rs +++ b/crates/assists/src/tests/generated.rs @@ -103,6 +103,25 @@ fn foo(&self) -> u32 { ) } +#[test] +fn doctest_add_lifetime_to_type() { + check_doc_test( + "add_lifetime_to_type", + r#####" +struct Point { + x: &$0u32, + y: u32, +} +"#####, + r#####" +struct Point<'a> { + x: &'a u32, + y: u32, +} +"#####, + ) +} + #[test] fn doctest_add_turbo_fish() { check_doc_test( diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index aaa7013b66a6..c34a99d90bdf 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -281,7 +281,7 @@ pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module { /// Name of this module. pub fn name(self, db: &dyn HirDatabase) -> Option { - let def_map = db.crate_def_map(self.id.krate); + let def_map = self.id.def_map(db.upcast()); let parent = def_map[self.id.local_id].parent?; def_map[parent].children.iter().find_map(|(name, module_id)| { if *module_id == self.id.local_id { @@ -307,7 +307,7 @@ pub fn crate_root(self, db: &dyn HirDatabase) -> Module { /// Iterates over all child modules. pub fn children(self, db: &dyn HirDatabase) -> impl Iterator { - let def_map = db.crate_def_map(self.id.krate); + let def_map = self.id.def_map(db.upcast()); let children = def_map[self.id.local_id] .children .iter() @@ -318,7 +318,7 @@ pub fn children(self, db: &dyn HirDatabase) -> impl Iterator { /// Finds a parent module. pub fn parent(self, db: &dyn HirDatabase) -> Option { - let def_map = db.crate_def_map(self.id.krate); + let def_map = self.id.def_map(db.upcast()); let parent_id = def_map[self.id.local_id].parent?; Some(self.with_module_id(parent_id)) } @@ -339,7 +339,7 @@ pub fn scope( db: &dyn HirDatabase, visible_from: Option, ) -> Vec<(Name, ScopeDef)> { - db.crate_def_map(self.id.krate)[self.id.local_id] + self.id.def_map(db.upcast())[self.id.local_id] .scope .entries() .filter_map(|(name, def)| { @@ -362,14 +362,14 @@ pub fn scope( } pub fn visibility_of(self, db: &dyn HirDatabase, def: &ModuleDef) -> Option { - db.crate_def_map(self.id.krate)[self.id.local_id].scope.visibility_of(def.clone().into()) + self.id.def_map(db.upcast())[self.id.local_id].scope.visibility_of(def.clone().into()) } pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { let _p = profile::span("Module::diagnostics").detail(|| { format!("{:?}", self.name(db).map_or("".into(), |name| name.to_string())) }); - let crate_def_map = db.crate_def_map(self.id.krate); + let crate_def_map = self.id.def_map(db.upcast()); crate_def_map.add_diagnostics(db.upcast(), self.id.local_id, sink); for decl in self.declarations(db) { match decl { @@ -396,12 +396,12 @@ pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { } pub fn declarations(self, db: &dyn HirDatabase) -> Vec { - let def_map = db.crate_def_map(self.id.krate); + let def_map = self.id.def_map(db.upcast()); def_map[self.id.local_id].scope.declarations().map(ModuleDef::from).collect() } pub fn impl_defs(self, db: &dyn HirDatabase) -> Vec { - let def_map = db.crate_def_map(self.id.krate); + let def_map = self.id.def_map(db.upcast()); def_map[self.id.local_id].scope.impls().map(Impl::from).collect() } diff --git a/crates/hir/src/has_source.rs b/crates/hir/src/has_source.rs index 7c57d8378cca..2620026713c8 100644 --- a/crates/hir/src/has_source.rs +++ b/crates/hir/src/has_source.rs @@ -24,12 +24,12 @@ pub trait HasSource { impl Module { /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. pub fn definition_source(self, db: &dyn HirDatabase) -> InFile { - let def_map = db.crate_def_map(self.id.krate); + let def_map = self.id.def_map(db.upcast()); def_map[self.id.local_id].definition_source(db.upcast()) } pub fn is_mod_rs(self, db: &dyn HirDatabase) -> bool { - let def_map = db.crate_def_map(self.id.krate); + let def_map = self.id.def_map(db.upcast()); match def_map[self.id.local_id].origin { ModuleOrigin::File { is_mod_rs, .. } => is_mod_rs, _ => false, @@ -39,7 +39,7 @@ pub fn is_mod_rs(self, db: &dyn HirDatabase) -> bool { /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. /// `None` for the crate root. pub fn declaration_source(self, db: &dyn HirDatabase) -> Option> { - let def_map = db.crate_def_map(self.id.krate); + let def_map = self.id.def_map(db.upcast()); def_map[self.id.local_id].declaration_source(db.upcast()) } } diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs index 9bf60c72af80..775f7ec8b768 100644 --- a/crates/hir/src/semantics/source_to_def.rs +++ b/crates/hir/src/semantics/source_to_def.rs @@ -31,6 +31,7 @@ impl SourceToDefCtx<'_, '_> { pub(super) fn file_to_def(&mut self, file: FileId) -> Option { let _p = profile::span("SourceBinder::to_module_def"); let (krate, local_id) = self.db.relevant_crates(file).iter().find_map(|&crate_id| { + // FIXME: inner items let crate_def_map = self.db.crate_def_map(crate_id); let local_id = crate_def_map.modules_for_file(file).next()?; Some((crate_id, local_id)) @@ -60,7 +61,7 @@ pub(super) fn module_to_def(&mut self, src: InFile) -> Option Attrs { let raw_attrs = match def { AttrDefId::ModuleId(module) => { - let def_map = db.crate_def_map(module.krate); + let def_map = module.def_map(db); let mod_data = &def_map[module.local_id]; match mod_data.declaration_source(db) { Some(it) => { diff --git a/crates/hir_def/src/body.rs b/crates/hir_def/src/body.rs index 2c2c999dd0a7..d0c84ab0b590 100644 --- a/crates/hir_def/src/body.rs +++ b/crates/hir_def/src/body.rs @@ -86,7 +86,7 @@ pub(crate) fn new( module: ModuleId, ) -> Expander { let cfg_expander = CfgExpander::new(db, current_file_id, module.krate); - let crate_def_map = db.crate_def_map(module.krate); + let crate_def_map = module.def_map(db); let ast_id_map = db.ast_id_map(current_file_id); Expander { cfg_expander, diff --git a/crates/hir_def/src/child_by_source.rs b/crates/hir_def/src/child_by_source.rs index dcb00a1d9a0b..65d85c86a782 100644 --- a/crates/hir_def/src/child_by_source.rs +++ b/crates/hir_def/src/child_by_source.rs @@ -74,7 +74,7 @@ fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap { impl ChildBySource for ModuleId { fn child_by_source(&self, db: &dyn DefDatabase) -> DynMap { - let crate_def_map = db.crate_def_map(self.krate); + let crate_def_map = self.def_map(db); let module_data = &crate_def_map[self.local_id]; module_data.scope.child_by_source(db) } diff --git a/crates/hir_def/src/find_path.rs b/crates/hir_def/src/find_path.rs index db2d125aeac3..c01b6daf2c03 100644 --- a/crates/hir_def/src/find_path.rs +++ b/crates/hir_def/src/find_path.rs @@ -110,7 +110,7 @@ fn find_path_inner( // Base cases: // - if the item is already in scope, return the name under which it is - let def_map = db.crate_def_map(from.krate); + let def_map = from.def_map(db); let from_scope: &crate::item_scope::ItemScope = &def_map[from.local_id].scope; let scope_name = if let Some((name, _)) = from_scope.name_of(item) { Some(name.clone()) } else { None }; @@ -145,7 +145,7 @@ fn find_path_inner( // - if the item is in the prelude, return the name from there if let Some(prelude_module) = def_map.prelude() { - let prelude_def_map = db.crate_def_map(prelude_module.krate); + let prelude_def_map = prelude_module.def_map(db); let prelude_scope: &crate::item_scope::ItemScope = &prelude_def_map[prelude_module.local_id].scope; if let Some((name, vis)) = prelude_scope.name_of(item) { @@ -283,7 +283,7 @@ fn find_local_import_locations( // above `from` with any visibility. That means we do not need to descend into private siblings // of `from` (and similar). - let def_map = db.crate_def_map(from.krate); + let def_map = from.def_map(db); // Compute the initial worklist. We start with all direct child modules of `from` as well as all // of its (recursive) parent modules. @@ -312,7 +312,7 @@ fn find_local_import_locations( &def_map[module.local_id] } else { // The crate might reexport a module defined in another crate. - ext_def_map = db.crate_def_map(module.krate); + ext_def_map = module.def_map(db); &ext_def_map[module.local_id] }; @@ -375,7 +375,7 @@ fn check_found_path_(ra_fixture: &str, path: &str, prefix_kind: Option Arc { &def_map[module.local_id] } else { // The crate might reexport a module defined in another crate. - ext_def_map = db.crate_def_map(module.krate); + ext_def_map = module.def_map(db); &ext_def_map[module.local_id] }; diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index 08ed920c60a0..2f9261a7fe6b 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs @@ -50,7 +50,10 @@ macro_rules! eprintln { #[cfg(test)] mod test_db; -use std::hash::{Hash, Hasher}; +use std::{ + hash::{Hash, Hasher}, + sync::Arc, +}; use base_db::{impl_intern_key, salsa, CrateId}; use hir_expand::{ @@ -58,6 +61,7 @@ macro_rules! eprintln { MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, }; use la_arena::Idx; +use nameres::DefMap; use syntax::ast; use crate::builtin_type::BuiltinType; @@ -73,6 +77,12 @@ pub struct ModuleId { pub local_id: LocalModuleId, } +impl ModuleId { + pub fn def_map(&self, db: &dyn db::DefDatabase) -> Arc { + db.crate_def_map(self.krate) + } +} + /// An ID of a module, **local** to a specific crate pub type LocalModuleId = Idx; diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index cd68efbe6787..adfcf879a448 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -578,7 +578,7 @@ fn record_resolved_import(&mut self, directive: &ImportDirective) { } else if m.krate != self.def_map.krate { mark::hit!(glob_across_crates); // glob import from other crate => we can just import everything once - let item_map = self.db.crate_def_map(m.krate); + let item_map = m.def_map(self.db); let scope = &item_map[m.local_id].scope; // Module scoped macros is included diff --git a/crates/hir_def/src/nameres/path_resolution.rs b/crates/hir_def/src/nameres/path_resolution.rs index ec90f4e65054..82528b792062 100644 --- a/crates/hir_def/src/nameres/path_resolution.rs +++ b/crates/hir_def/src/nameres/path_resolution.rs @@ -243,7 +243,7 @@ pub(super) fn resolve_path_fp_with_macro_single( kind: PathKind::Super(0), }; log::debug!("resolving {:?} in other crate", path); - let defp_map = db.crate_def_map(module.krate); + let defp_map = module.def_map(db); let (def, s) = defp_map.resolve_path(db, module.local_id, &path, shadow); return ResolvePathResult::with( def, @@ -356,7 +356,7 @@ fn resolve_in_prelude(&self, db: &dyn DefDatabase, name: &Name) -> PerNs { self } else { // Extend lifetime - keep = db.crate_def_map(prelude.krate); + keep = prelude.def_map(db); &keep }; def_map[prelude.local_id].scope.get(name) diff --git a/crates/hir_def/src/resolver.rs b/crates/hir_def/src/resolver.rs index b2f5776497da..130c074f0887 100644 --- a/crates/hir_def/src/resolver.rs +++ b/crates/hir_def/src/resolver.rs @@ -430,7 +430,7 @@ pub fn traits_in_scope(&self, db: &dyn DefDatabase) -> FxHashSet { for scope in &self.scopes { if let Scope::ModuleScope(m) = scope { if let Some(prelude) = m.crate_def_map.prelude() { - let prelude_def_map = db.crate_def_map(prelude.krate); + let prelude_def_map = prelude.def_map(db); traits.extend(prelude_def_map[prelude.local_id].scope.traits()); } traits.extend(m.crate_def_map[m.module_id].scope.traits()); @@ -529,7 +529,7 @@ fn process_names(&self, db: &dyn DefDatabase, f: &mut dyn FnMut(Name, ScopeDef)) f(name.clone(), ScopeDef::PerNs(def)); }); if let Some(prelude) = m.crate_def_map.prelude() { - let prelude_def_map = db.crate_def_map(prelude.krate); + let prelude_def_map = prelude.def_map(db); prelude_def_map[prelude.local_id].scope.entries().for_each(|(name, def)| { let seen_tuple = (name.clone(), def); if !seen.contains(&seen_tuple) { @@ -633,7 +633,7 @@ pub trait HasResolver: Copy { impl HasResolver for ModuleId { fn resolver(self, db: &dyn DefDatabase) -> Resolver { - let def_map = db.crate_def_map(self.krate); + let def_map = self.def_map(db); Resolver::default().push_module_scope(def_map, self.local_id) } } diff --git a/crates/hir_def/src/visibility.rs b/crates/hir_def/src/visibility.rs index 3134fa43d023..e79a91102381 100644 --- a/crates/hir_def/src/visibility.rs +++ b/crates/hir_def/src/visibility.rs @@ -103,7 +103,7 @@ pub fn is_visible_from(self, db: &dyn DefDatabase, from_module: ModuleId) -> boo if from_module.krate != to_module.krate { return false; } - let def_map = db.crate_def_map(from_module.krate); + let def_map = from_module.def_map(db); self.is_visible_from_def_map(&def_map, from_module.local_id) } diff --git a/crates/hir_ty/src/tests.rs b/crates/hir_ty/src/tests.rs index 4a3fcea8dc41..25ee664d6f4c 100644 --- a/crates/hir_ty/src/tests.rs +++ b/crates/hir_ty/src/tests.rs @@ -188,10 +188,10 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String { }; let module = db.module_for_file(file_id); - let crate_def_map = db.crate_def_map(module.krate); + let def_map = module.def_map(&db); let mut defs: Vec = Vec::new(); - visit_module(&db, &crate_def_map, module.local_id, &mut |it| defs.push(it)); + visit_module(&db, &def_map, module.local_id, &mut |it| defs.push(it)); defs.sort_by_key(|def| match def { DefWithBodyId::FunctionId(it) => { let loc = it.lookup(&db); @@ -321,7 +321,7 @@ fn foo() -> i32 { { let events = db.log_executed(|| { let module = db.module_for_file(pos.file_id); - let crate_def_map = db.crate_def_map(module.krate); + let crate_def_map = module.def_map(&db); visit_module(&db, &crate_def_map, module.local_id, &mut |def| { db.infer(def); });