mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Merge #5153
5153: Make SemanticsScope non-generic r=matklad a=lnicola This slightly reduces the build times: ![image](https://user-images.githubusercontent.com/308347/86210975-3a809480-bb7e-11ea-8975-788457f6b353.png) (compare to https://github.com/rust-analyzer/rust-analyzer/issues/1987#issuecomment-652202248) Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
This commit is contained in:
commit
98ae447fa7
3 changed files with 15 additions and 19 deletions
|
@ -2,7 +2,6 @@
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
use hir::{HirDisplay, PathResolution, SemanticsScope};
|
use hir::{HirDisplay, PathResolution, SemanticsScope};
|
||||||
use ra_ide_db::RootDatabase;
|
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
algo::SyntaxRewriter,
|
algo::SyntaxRewriter,
|
||||||
ast::{self, AstNode},
|
ast::{self, AstNode},
|
||||||
|
@ -32,14 +31,14 @@ impl<'a> AstTransform<'a> for NullTransformer {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SubstituteTypeParams<'a> {
|
pub struct SubstituteTypeParams<'a> {
|
||||||
source_scope: &'a SemanticsScope<'a, RootDatabase>,
|
source_scope: &'a SemanticsScope<'a>,
|
||||||
substs: FxHashMap<hir::TypeParam, ast::TypeRef>,
|
substs: FxHashMap<hir::TypeParam, ast::TypeRef>,
|
||||||
previous: Box<dyn AstTransform<'a> + 'a>,
|
previous: Box<dyn AstTransform<'a> + 'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> SubstituteTypeParams<'a> {
|
impl<'a> SubstituteTypeParams<'a> {
|
||||||
pub fn for_trait_impl(
|
pub fn for_trait_impl(
|
||||||
source_scope: &'a SemanticsScope<'a, RootDatabase>,
|
source_scope: &'a SemanticsScope<'a>,
|
||||||
// FIXME: there's implicit invariant that `trait_` and `source_scope` match...
|
// FIXME: there's implicit invariant that `trait_` and `source_scope` match...
|
||||||
trait_: hir::Trait,
|
trait_: hir::Trait,
|
||||||
impl_def: ast::ImplDef,
|
impl_def: ast::ImplDef,
|
||||||
|
@ -126,16 +125,13 @@ impl<'a> AstTransform<'a> for SubstituteTypeParams<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct QualifyPaths<'a> {
|
pub struct QualifyPaths<'a> {
|
||||||
target_scope: &'a SemanticsScope<'a, RootDatabase>,
|
target_scope: &'a SemanticsScope<'a>,
|
||||||
source_scope: &'a SemanticsScope<'a, RootDatabase>,
|
source_scope: &'a SemanticsScope<'a>,
|
||||||
previous: Box<dyn AstTransform<'a> + 'a>,
|
previous: Box<dyn AstTransform<'a> + 'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> QualifyPaths<'a> {
|
impl<'a> QualifyPaths<'a> {
|
||||||
pub fn new(
|
pub fn new(target_scope: &'a SemanticsScope<'a>, source_scope: &'a SemanticsScope<'a>) -> Self {
|
||||||
target_scope: &'a SemanticsScope<'a, RootDatabase>,
|
|
||||||
source_scope: &'a SemanticsScope<'a, RootDatabase>,
|
|
||||||
) -> Self {
|
|
||||||
Self { target_scope, source_scope, previous: Box::new(NullTransformer) }
|
Self { target_scope, source_scope, previous: Box::new(NullTransformer) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,7 +152,7 @@ impl<'a> QualifyPaths<'a> {
|
||||||
let resolution = self.source_scope.resolve_hir_path(&hir_path?)?;
|
let resolution = self.source_scope.resolve_hir_path(&hir_path?)?;
|
||||||
match resolution {
|
match resolution {
|
||||||
PathResolution::Def(def) => {
|
PathResolution::Def(def) => {
|
||||||
let found_path = from.find_use_path(self.source_scope.db, def)?;
|
let found_path = from.find_use_path(self.source_scope.db.upcast(), def)?;
|
||||||
let mut path = path_to_ast(found_path);
|
let mut path = path_to_ast(found_path);
|
||||||
|
|
||||||
let type_args = p
|
let type_args = p
|
||||||
|
|
|
@ -297,19 +297,19 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
|
||||||
self.with_ctx(|ctx| ctx.file_to_def(file)).map(Module::from)
|
self.with_ctx(|ctx| ctx.file_to_def(file)).map(Module::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db, DB> {
|
pub fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> {
|
||||||
let node = self.find_file(node.clone());
|
let node = self.find_file(node.clone());
|
||||||
let resolver = self.analyze2(node.as_ref(), None).resolver;
|
let resolver = self.analyze2(node.as_ref(), None).resolver;
|
||||||
SemanticsScope { db: self.db, resolver }
|
SemanticsScope { db: self.db, resolver }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scope_at_offset(&self, node: &SyntaxNode, offset: TextSize) -> SemanticsScope<'db, DB> {
|
pub fn scope_at_offset(&self, node: &SyntaxNode, offset: TextSize) -> SemanticsScope<'db> {
|
||||||
let node = self.find_file(node.clone());
|
let node = self.find_file(node.clone());
|
||||||
let resolver = self.analyze2(node.as_ref(), Some(offset)).resolver;
|
let resolver = self.analyze2(node.as_ref(), Some(offset)).resolver;
|
||||||
SemanticsScope { db: self.db, resolver }
|
SemanticsScope { db: self.db, resolver }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db, DB> {
|
pub fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> {
|
||||||
let resolver = def.id.resolver(self.db);
|
let resolver = def.id.resolver(self.db);
|
||||||
SemanticsScope { db: self.db, resolver }
|
SemanticsScope { db: self.db, resolver }
|
||||||
}
|
}
|
||||||
|
@ -419,12 +419,12 @@ fn find_root(node: &SyntaxNode) -> SyntaxNode {
|
||||||
node.ancestors().last().unwrap()
|
node.ancestors().last().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SemanticsScope<'a, DB> {
|
pub struct SemanticsScope<'a> {
|
||||||
pub db: &'a DB,
|
pub db: &'a dyn HirDatabase,
|
||||||
resolver: Resolver,
|
resolver: Resolver,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, DB: HirDatabase> SemanticsScope<'a, DB> {
|
impl<'a> SemanticsScope<'a> {
|
||||||
pub fn module(&self) -> Option<Module> {
|
pub fn module(&self) -> Option<Module> {
|
||||||
Some(Module { id: self.resolver.module()? })
|
Some(Module { id: self.resolver.module()? })
|
||||||
}
|
}
|
||||||
|
@ -433,13 +433,13 @@ impl<'a, DB: HirDatabase> SemanticsScope<'a, DB> {
|
||||||
// FIXME: rename to visible_traits to not repeat scope?
|
// FIXME: rename to visible_traits to not repeat scope?
|
||||||
pub fn traits_in_scope(&self) -> FxHashSet<TraitId> {
|
pub fn traits_in_scope(&self) -> FxHashSet<TraitId> {
|
||||||
let resolver = &self.resolver;
|
let resolver = &self.resolver;
|
||||||
resolver.traits_in_scope(self.db)
|
resolver.traits_in_scope(self.db.upcast())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process_all_names(&self, f: &mut dyn FnMut(Name, ScopeDef)) {
|
pub fn process_all_names(&self, f: &mut dyn FnMut(Name, ScopeDef)) {
|
||||||
let resolver = &self.resolver;
|
let resolver = &self.resolver;
|
||||||
|
|
||||||
resolver.process_all_names(self.db, &mut |name, def| {
|
resolver.process_all_names(self.db.upcast(), &mut |name, def| {
|
||||||
let def = match def {
|
let def = match def {
|
||||||
resolver::ScopeDef::PerNs(it) => {
|
resolver::ScopeDef::PerNs(it) => {
|
||||||
let items = ScopeDef::all_items(it);
|
let items = ScopeDef::all_items(it);
|
||||||
|
|
|
@ -213,7 +213,7 @@ impl<'a> CompletionContext<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn scope(&self) -> SemanticsScope<'_, RootDatabase> {
|
pub(crate) fn scope(&self) -> SemanticsScope<'_> {
|
||||||
self.sema.scope_at_offset(&self.token.parent(), self.offset)
|
self.sema.scope_at_offset(&self.token.parent(), self.offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue