Skip to content

Commit f304ce3

Browse files
bors[bot]matklad
andauthored
Merge #6821
6821: Improve code structure r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 15a644d + 4015ff0 commit f304ce3

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

crates/hir/src/semantics.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,8 @@ impl<'db> SemanticsImpl<'db> {
294294
}
295295

296296
fn expand(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> {
297-
let macro_call = self.find_file(macro_call.syntax().clone()).with_value(macro_call);
298-
let sa = self.analyze2(macro_call.map(|it| it.syntax()), None);
299-
let file_id = sa.expand(self.db, macro_call)?;
297+
let sa = self.analyze(macro_call.syntax());
298+
let file_id = sa.expand(self.db, InFile::new(sa.file_id, macro_call))?;
300299
let node = self.db.parse_or_expand(file_id)?;
301300
self.cache(node.clone(), file_id);
302301
Some(node)
@@ -308,9 +307,8 @@ impl<'db> SemanticsImpl<'db> {
308307
hypothetical_args: &ast::TokenTree,
309308
token_to_map: SyntaxToken,
310309
) -> Option<(SyntaxNode, SyntaxToken)> {
311-
let macro_call =
312-
self.find_file(actual_macro_call.syntax().clone()).with_value(actual_macro_call);
313-
let sa = self.analyze2(macro_call.map(|it| it.syntax()), None);
310+
let sa = self.analyze(actual_macro_call.syntax());
311+
let macro_call = InFile::new(sa.file_id, actual_macro_call);
314312
let krate = sa.resolver.krate()?;
315313
let macro_call_id = macro_call.as_call_id(self.db.upcast(), krate, |path| {
316314
sa.resolver.resolve_path_as_macro(self.db.upcast(), &path)
@@ -326,10 +324,9 @@ impl<'db> SemanticsImpl<'db> {
326324
fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken {
327325
let _p = profile::span("descend_into_macros");
328326
let parent = token.parent();
329-
let parent = self.find_file(parent);
330-
let sa = self.analyze2(parent.as_ref(), None);
327+
let sa = self.analyze(&parent);
331328

332-
let token = successors(Some(parent.with_value(token)), |token| {
329+
let token = successors(Some(InFile::new(sa.file_id, token)), |token| {
333330
self.db.check_canceled();
334331
let macro_call = token.value.ancestors().find_map(ast::MacroCall::cast)?;
335332
let tt = macro_call.token_tree()?;
@@ -486,15 +483,13 @@ impl<'db> SemanticsImpl<'db> {
486483
}
487484

488485
fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> {
489-
let node = self.find_file(node.clone());
490-
let resolver = self.analyze2(node.as_ref(), None).resolver;
491-
SemanticsScope { db: self.db, file_id: node.file_id, resolver }
486+
let sa = self.analyze(node);
487+
SemanticsScope { db: self.db, file_id: sa.file_id, resolver: sa.resolver }
492488
}
493489

494490
fn scope_at_offset(&self, node: &SyntaxNode, offset: TextSize) -> SemanticsScope<'db> {
495-
let node = self.find_file(node.clone());
496-
let resolver = self.analyze2(node.as_ref(), Some(offset)).resolver;
497-
SemanticsScope { db: self.db, file_id: node.file_id, resolver }
491+
let sa = self.analyze_with_offset(node, offset);
492+
SemanticsScope { db: self.db, file_id: sa.file_id, resolver: sa.resolver }
498493
}
499494

500495
fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> {
@@ -504,21 +499,24 @@ impl<'db> SemanticsImpl<'db> {
504499
}
505500

506501
fn analyze(&self, node: &SyntaxNode) -> SourceAnalyzer {
507-
let src = self.find_file(node.clone());
508-
self.analyze2(src.as_ref(), None)
502+
self.analyze_impl(node, None)
509503
}
504+
fn analyze_with_offset(&self, node: &SyntaxNode, offset: TextSize) -> SourceAnalyzer {
505+
self.analyze_impl(node, Some(offset))
506+
}
507+
fn analyze_impl(&self, node: &SyntaxNode, offset: Option<TextSize>) -> SourceAnalyzer {
508+
let _p = profile::span("Semantics::analyze_impl");
509+
let node = self.find_file(node.clone());
510+
let node = node.as_ref();
510511

511-
fn analyze2(&self, src: InFile<&SyntaxNode>, offset: Option<TextSize>) -> SourceAnalyzer {
512-
let _p = profile::span("Semantics::analyze2");
513-
514-
let container = match self.with_ctx(|ctx| ctx.find_container(src)) {
512+
let container = match self.with_ctx(|ctx| ctx.find_container(node)) {
515513
Some(it) => it,
516-
None => return SourceAnalyzer::new_for_resolver(Resolver::default(), src),
514+
None => return SourceAnalyzer::new_for_resolver(Resolver::default(), node),
517515
};
518516

519517
let resolver = match container {
520518
ChildContainer::DefWithBodyId(def) => {
521-
return SourceAnalyzer::new_for_body(self.db, def, src, offset)
519+
return SourceAnalyzer::new_for_body(self.db, def, node, offset)
522520
}
523521
ChildContainer::TraitId(it) => it.resolver(self.db.upcast()),
524522
ChildContainer::ImplId(it) => it.resolver(self.db.upcast()),
@@ -528,7 +526,7 @@ impl<'db> SemanticsImpl<'db> {
528526
ChildContainer::TypeAliasId(it) => it.resolver(self.db.upcast()),
529527
ChildContainer::GenericDefId(it) => it.resolver(self.db.upcast()),
530528
};
531-
SourceAnalyzer::new_for_resolver(resolver, src)
529+
SourceAnalyzer::new_for_resolver(resolver, node)
532530
}
533531

534532
fn cache(&self, root_node: SyntaxNode, file_id: HirFileId) {

crates/hir/src/source_analyzer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use base_db::CrateId;
3737
/// original source files. It should not be used inside the HIR itself.
3838
#[derive(Debug)]
3939
pub(crate) struct SourceAnalyzer {
40-
file_id: HirFileId,
40+
pub(crate) file_id: HirFileId,
4141
pub(crate) resolver: Resolver,
4242
body: Option<Arc<Body>>,
4343
body_source_map: Option<Arc<BodySourceMap>>,

0 commit comments

Comments
 (0)