Skip to content

Commit dccbb38

Browse files
Less lifetines: derive SemanticsScope in place
1 parent db61d4e commit dccbb38

File tree

7 files changed

+49
-50
lines changed

7 files changed

+49
-50
lines changed

crates/hir/src/semantics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ fn find_root(node: &SyntaxNode) -> SyntaxNode {
774774
///
775775
/// Note that if you are wondering "what does this specific existing name mean?",
776776
/// you'd better use the `resolve_` family of methods.
777-
#[derive(Debug, Clone)]
777+
#[derive(Debug)]
778778
pub struct SemanticsScope<'a> {
779779
pub db: &'a dyn HirDatabase,
780780
file_id: HirFileId,

crates/ide_assists/src/handlers/auto_import.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
112112
Some(())
113113
}
114114

115-
pub(super) fn find_importable_node<'a>(
116-
ctx: &'a AssistContext,
117-
) -> Option<(ImportAssets<'a>, SyntaxNode)> {
115+
pub(super) fn find_importable_node(ctx: &AssistContext) -> Option<(ImportAssets, SyntaxNode)> {
118116
if let Some(path_under_caret) = ctx.find_node_at_offset_with_descend::<ast::Path>() {
119117
ImportAssets::for_exact_path(&path_under_caret, &ctx.sema)
120118
.zip(Some(path_under_caret.syntax().clone()))

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,20 @@ pub(crate) fn replace_derive_with_manual_impl(
6565
let current_module = ctx.sema.scope(annotated_name.syntax()).module()?;
6666
let current_crate = current_module.krate();
6767

68-
let found_traits = items_locator::with_for_exact_name(
69-
&ctx.sema,
70-
current_crate,
71-
trait_token.text().to_string(),
72-
)
73-
.into_iter()
74-
.filter_map(|item| match ModuleDef::from(item.as_module_def_id()?) {
75-
ModuleDef::Trait(trait_) => Some(trait_),
76-
_ => None,
77-
})
78-
.flat_map(|trait_| {
79-
current_module
80-
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_))
81-
.as_ref()
82-
.map(mod_path_to_ast)
83-
.zip(Some(trait_))
84-
});
68+
let found_traits =
69+
items_locator::with_exact_name(&ctx.sema, current_crate, trait_token.text().to_string())
70+
.into_iter()
71+
.filter_map(|item| match ModuleDef::from(item.as_module_def_id()?) {
72+
ModuleDef::Trait(trait_) => Some(trait_),
73+
_ => None,
74+
})
75+
.flat_map(|trait_| {
76+
current_module
77+
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_))
78+
.as_ref()
79+
.map(mod_path_to_ast)
80+
.zip(Some(trait_))
81+
});
8582

8683
let mut no_traits_found = true;
8784
for (trait_path, trait_) in found_traits.inspect(|_| no_traits_found = false) {

crates/ide_completion/src/completions/flyimport.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,23 +169,28 @@ pub(crate) fn position_for_import<'a>(
169169
})
170170
}
171171

172-
fn import_assets<'a>(ctx: &'a CompletionContext, fuzzy_name: String) -> Option<ImportAssets<'a>> {
172+
fn import_assets(ctx: &CompletionContext, fuzzy_name: String) -> Option<ImportAssets> {
173173
let current_module = ctx.scope.module()?;
174174
if let Some(dot_receiver) = &ctx.dot_receiver {
175175
ImportAssets::for_fuzzy_method_call(
176176
current_module,
177177
ctx.sema.type_of_expr(dot_receiver)?,
178178
fuzzy_name,
179-
ctx.scope.clone(),
179+
dot_receiver.syntax().clone(),
180180
)
181181
} else {
182182
let fuzzy_name_length = fuzzy_name.len();
183+
let approximate_node = match current_module.definition_source(ctx.db).value {
184+
hir::ModuleSource::SourceFile(s) => s.syntax().clone(),
185+
hir::ModuleSource::Module(m) => m.syntax().clone(),
186+
hir::ModuleSource::BlockExpr(b) => b.syntax().clone(),
187+
};
183188
let assets_for_path = ImportAssets::for_fuzzy_path(
184189
current_module,
185190
ctx.path_qual.clone(),
186191
fuzzy_name,
187192
&ctx.sema,
188-
ctx.scope.clone(),
193+
approximate_node,
189194
)?;
190195

191196
if matches!(assets_for_path.import_candidate(), ImportCandidate::Path(_))

crates/ide_completion/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub fn resolve_completion_edits(
150150
let current_crate = current_module.krate();
151151

152152
let (import_path, item_to_import) =
153-
items_locator::with_for_exact_name(&ctx.sema, current_crate, imported_name)
153+
items_locator::with_exact_name(&ctx.sema, current_crate, imported_name)
154154
.into_iter()
155155
.filter_map(|candidate| {
156156
current_module

crates/ide_db/src/helpers/import_assets.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! Look up accessible paths for items.
22
use hir::{
33
AsAssocItem, AssocItem, AssocItemContainer, Crate, ItemInNs, MacroDef, ModPath, Module,
4-
ModuleDef, Name, PathResolution, PrefixKind, ScopeDef, Semantics, SemanticsScope, Type,
4+
ModuleDef, Name, PathResolution, PrefixKind, ScopeDef, Semantics, Type,
55
};
66
use itertools::Itertools;
77
use rustc_hash::FxHashSet;
8-
use syntax::{ast, AstNode};
8+
use syntax::{ast, AstNode, SyntaxNode};
99

1010
use crate::{
1111
items_locator::{self, AssocItemSearch, DEFAULT_QUERY_SEARCH_LIMIT},
@@ -62,38 +62,37 @@ impl NameToImport {
6262
}
6363

6464
#[derive(Debug)]
65-
pub struct ImportAssets<'a> {
65+
pub struct ImportAssets {
6666
import_candidate: ImportCandidate,
67+
candidate_node: SyntaxNode,
6768
module_with_candidate: Module,
68-
scope: SemanticsScope<'a>,
6969
}
7070

71-
impl<'a> ImportAssets<'a> {
71+
impl ImportAssets {
7272
pub fn for_method_call(
7373
method_call: &ast::MethodCallExpr,
74-
sema: &'a Semantics<RootDatabase>,
74+
sema: &Semantics<RootDatabase>,
7575
) -> Option<Self> {
76-
let scope = sema.scope(method_call.syntax());
76+
let candidate_node = method_call.syntax().clone();
7777
Some(Self {
7878
import_candidate: ImportCandidate::for_method_call(sema, method_call)?,
79-
module_with_candidate: scope.module()?,
80-
scope,
79+
module_with_candidate: sema.scope(&candidate_node).module()?,
80+
candidate_node,
8181
})
8282
}
8383

8484
pub fn for_exact_path(
8585
fully_qualified_path: &ast::Path,
86-
sema: &'a Semantics<RootDatabase>,
86+
sema: &Semantics<RootDatabase>,
8787
) -> Option<Self> {
88-
let syntax_under_caret = fully_qualified_path.syntax();
89-
if syntax_under_caret.ancestors().find_map(ast::Use::cast).is_some() {
88+
let candidate_node = fully_qualified_path.syntax().clone();
89+
if candidate_node.ancestors().find_map(ast::Use::cast).is_some() {
9090
return None;
9191
}
92-
let scope = sema.scope(syntax_under_caret);
9392
Some(Self {
9493
import_candidate: ImportCandidate::for_regular_path(sema, fully_qualified_path)?,
95-
module_with_candidate: scope.module()?,
96-
scope,
94+
module_with_candidate: sema.scope(&candidate_node).module()?,
95+
candidate_node,
9796
})
9897
}
9998

@@ -102,28 +101,28 @@ impl<'a> ImportAssets<'a> {
102101
qualifier: Option<ast::Path>,
103102
fuzzy_name: String,
104103
sema: &Semantics<RootDatabase>,
105-
scope: SemanticsScope<'a>,
104+
candidate_node: SyntaxNode,
106105
) -> Option<Self> {
107106
Some(Self {
108107
import_candidate: ImportCandidate::for_fuzzy_path(qualifier, fuzzy_name, sema)?,
109108
module_with_candidate,
110-
scope,
109+
candidate_node,
111110
})
112111
}
113112

114113
pub fn for_fuzzy_method_call(
115114
module_with_method_call: Module,
116115
receiver_ty: Type,
117116
fuzzy_method_name: String,
118-
scope: SemanticsScope<'a>,
117+
candidate_node: SyntaxNode,
119118
) -> Option<Self> {
120119
Some(Self {
121120
import_candidate: ImportCandidate::TraitMethod(TraitImportCandidate {
122121
receiver_ty,
123122
name: NameToImport::Fuzzy(fuzzy_method_name),
124123
}),
125124
module_with_candidate: module_with_method_call,
126-
scope,
125+
candidate_node,
127126
})
128127
}
129128
}
@@ -156,7 +155,7 @@ impl LocatedImport {
156155
}
157156
}
158157

159-
impl<'a> ImportAssets<'a> {
158+
impl ImportAssets {
160159
pub fn import_candidate(&self) -> &ImportCandidate {
161160
&self.import_candidate
162161
}
@@ -182,7 +181,7 @@ impl<'a> ImportAssets<'a> {
182181
prefixed: Option<PrefixKind>,
183182
) -> Vec<LocatedImport> {
184183
let items_with_candidate_name = match self.name_to_import() {
185-
NameToImport::Exact(exact_name) => items_locator::with_for_exact_name(
184+
NameToImport::Exact(exact_name) => items_locator::with_exact_name(
186185
sema,
187186
self.module_with_candidate.krate(),
188187
exact_name.clone(),
@@ -209,7 +208,7 @@ impl<'a> ImportAssets<'a> {
209208
}
210209
};
211210

212-
let scope_definitions = self.scope_definitions();
211+
let scope_definitions = self.scope_definitions(sema);
213212
self.applicable_defs(sema.db, prefixed, items_with_candidate_name)
214213
.into_iter()
215214
.filter(|import| import.import_path.len() > 1)
@@ -218,9 +217,9 @@ impl<'a> ImportAssets<'a> {
218217
.collect()
219218
}
220219

221-
fn scope_definitions(&self) -> FxHashSet<ScopeDef> {
220+
fn scope_definitions(&self, sema: &Semantics<RootDatabase>) -> FxHashSet<ScopeDef> {
222221
let mut scope_definitions = FxHashSet::default();
223-
self.scope.process_all_names(&mut |_, scope_def| {
222+
sema.scope(&self.candidate_node).process_all_names(&mut |_, scope_def| {
224223
scope_definitions.insert(scope_def);
225224
});
226225
scope_definitions

crates/ide_db/src/items_locator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_hash::FxHashSet;
1717

1818
pub(crate) const DEFAULT_QUERY_SEARCH_LIMIT: usize = 40;
1919

20-
pub fn with_for_exact_name(
20+
pub fn with_exact_name(
2121
sema: &Semantics<'_, RootDatabase>,
2222
krate: Crate,
2323
exact_name: String,

0 commit comments

Comments
 (0)