From 5953cbd7aefa90da67829fe8e92d066b460ee447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Wed, 1 Jul 2020 09:34:45 +0300 Subject: Make SemanticsScope non-generic --- crates/ra_hir/src/semantics.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 6a49c424a..810c49d6f 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -297,19 +297,19 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { 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 resolver = self.analyze2(node.as_ref(), None).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 resolver = self.analyze2(node.as_ref(), Some(offset)).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); SemanticsScope { db: self.db, resolver } } @@ -419,12 +419,12 @@ fn find_root(node: &SyntaxNode) -> SyntaxNode { node.ancestors().last().unwrap() } -pub struct SemanticsScope<'a, DB> { - pub db: &'a DB, +pub struct SemanticsScope<'a> { + pub db: &'a dyn HirDatabase, resolver: Resolver, } -impl<'a, DB: HirDatabase> SemanticsScope<'a, DB> { +impl<'a> SemanticsScope<'a> { pub fn module(&self) -> Option { 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? pub fn traits_in_scope(&self) -> FxHashSet { 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)) { 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 { resolver::ScopeDef::PerNs(it) => { let items = ScopeDef::all_items(it); -- cgit v1.2.3 From 80386ca5be78d8ea65483df3edeec1a89b09a5a3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 1 Jul 2020 10:03:07 +0200 Subject: Use Strings for display names --- crates/ra_hir/src/code_model.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index e86077dd6..e09eb77c2 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -31,7 +31,7 @@ use hir_ty::{ ApplicationTy, Canonical, GenericPredicate, InEnvironment, Substs, TraitEnvironment, Ty, TyDefId, TypeCtor, }; -use ra_db::{CrateId, CrateName, Edition, FileId}; +use ra_db::{CrateId, Edition, FileId}; use ra_prof::profile; use ra_syntax::ast::{self, AttrsOwner, NameOwner}; use rustc_hash::FxHashSet; @@ -94,8 +94,8 @@ impl Crate { db.crate_graph()[self.id].edition } - pub fn display_name(self, db: &dyn HirDatabase) -> Option { - db.crate_graph()[self.id].display_name.as_ref().cloned() + pub fn display_name(self, db: &dyn HirDatabase) -> Option { + db.crate_graph()[self.id].display_name.clone() } pub fn query_external_importables( -- cgit v1.2.3 From e8505f14d4bb8d393ca77ec234dd1c99826a2d79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Wed, 1 Jul 2020 12:43:36 +0300 Subject: Try to reduce Semantics monomorphisations --- crates/ra_hir/src/semantics.rs | 209 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 186 insertions(+), 23 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 810c49d6f..fbea9ad57 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -83,6 +83,11 @@ impl PathResolution { /// Primary API to get semantic information, like types, from syntax trees. pub struct Semantics<'db, DB> { pub db: &'db DB, + impl_: SemanticsImpl<'db>, +} + +pub struct SemanticsImpl<'db> { + pub db: &'db dyn HirDatabase, s2d_cache: RefCell, cache: RefCell>, } @@ -95,7 +100,166 @@ impl fmt::Debug for Semantics<'_, DB> { impl<'db, DB: HirDatabase> Semantics<'db, DB> { pub fn new(db: &DB) -> Semantics { - Semantics { db, s2d_cache: Default::default(), cache: Default::default() } + let impl_ = SemanticsImpl::new(db); + Semantics { db, impl_ } + } + + pub fn parse(&self, file_id: FileId) -> ast::SourceFile { + self.impl_.parse(file_id) + } + + pub fn ast(&self, d: &T) -> ::AST { + self.impl_.ast(d) + } + + pub fn expand(&self, macro_call: &ast::MacroCall) -> Option { + self.impl_.expand(macro_call) + } + + pub fn expand_hypothetical( + &self, + actual_macro_call: &ast::MacroCall, + hypothetical_args: &ast::TokenTree, + token_to_map: SyntaxToken, + ) -> Option<(SyntaxNode, SyntaxToken)> { + self.impl_.expand_hypothetical(actual_macro_call, hypothetical_args, token_to_map) + } + + pub fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken { + self.impl_.descend_into_macros(token) + } + + pub fn descend_node_at_offset( + &self, + node: &SyntaxNode, + offset: TextSize, + ) -> Option { + self.impl_.descend_node_at_offset(node, offset) + } + + pub fn original_range(&self, node: &SyntaxNode) -> FileRange { + self.impl_.original_range(node) + } + + pub fn diagnostics_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { + self.impl_.diagnostics_range(diagnostics) + } + + pub fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator + '_ { + self.impl_.ancestors_with_macros(node) + } + + pub fn ancestors_at_offset_with_macros( + &self, + node: &SyntaxNode, + offset: TextSize, + ) -> impl Iterator + '_ { + self.impl_.ancestors_at_offset_with_macros(node, offset) + } + + /// Find a AstNode by offset inside SyntaxNode, if it is inside *Macrofile*, + /// search up until it is of the target AstNode type + pub fn find_node_at_offset_with_macros( + &self, + node: &SyntaxNode, + offset: TextSize, + ) -> Option { + self.impl_.find_node_at_offset_with_macros(node, offset) + } + + /// Find a AstNode by offset inside SyntaxNode, if it is inside *MacroCall*, + /// descend it and find again + pub fn find_node_at_offset_with_descend( + &self, + node: &SyntaxNode, + offset: TextSize, + ) -> Option { + self.impl_.find_node_at_offset_with_descend(node, offset) + } + + pub fn type_of_expr(&self, expr: &ast::Expr) -> Option { + self.impl_.type_of_expr(expr) + } + + pub fn type_of_pat(&self, pat: &ast::Pat) -> Option { + self.impl_.type_of_pat(pat) + } + + pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option { + self.impl_.resolve_method_call(call) + } + + pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option { + self.impl_.resolve_field(field) + } + + pub fn resolve_record_field(&self, field: &ast::RecordField) -> Option<(Field, Option)> { + self.impl_.resolve_record_field(field) + } + + pub fn resolve_record_field_pat(&self, field: &ast::RecordFieldPat) -> Option { + self.impl_.resolve_record_field_pat(field) + } + + pub fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option { + self.impl_.resolve_macro_call(macro_call) + } + + pub fn resolve_path(&self, path: &ast::Path) -> Option { + self.impl_.resolve_path(path) + } + + pub fn resolve_variant(&self, record_lit: ast::RecordLit) -> Option { + self.impl_.resolve_variant(record_lit) + } + + pub fn lower_path(&self, path: &ast::Path) -> Option { + self.impl_.lower_path(path) + } + + pub fn resolve_bind_pat_to_const(&self, pat: &ast::BindPat) -> Option { + self.impl_.resolve_bind_pat_to_const(pat) + } + + // FIXME: use this instead? + // pub fn resolve_name_ref(&self, name_ref: &ast::NameRef) -> Option; + + pub fn record_literal_missing_fields(&self, literal: &ast::RecordLit) -> Vec<(Field, Type)> { + self.impl_.record_literal_missing_fields(literal) + } + + pub fn record_pattern_missing_fields(&self, pattern: &ast::RecordPat) -> Vec<(Field, Type)> { + self.impl_.record_pattern_missing_fields(pattern) + } + + pub fn to_def(&self, src: &T) -> Option { + self.impl_.to_def(src) + } + + pub fn to_module_def(&self, file: FileId) -> Option { + self.impl_.to_module_def(file) + } + + pub fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> { + self.impl_.scope(node) + } + + pub fn scope_at_offset(&self, node: &SyntaxNode, offset: TextSize) -> SemanticsScope<'db> { + self.impl_.scope_at_offset(node, offset) + } + + pub fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> { + self.impl_.scope_for_def(def) + } + + pub fn assert_contains_node(&self, node: &SyntaxNode) { + self.impl_.assert_contains_node(node) + } +} + +impl<'db> SemanticsImpl<'db> { + pub fn new(db: &'db dyn HirDatabase) -> Self { + Self { db, s2d_cache: Default::default(), cache: Default::default() } } pub fn parse(&self, file_id: FileId) -> ast::SourceFile { @@ -108,7 +272,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { let file_id = d.source().file_id; let root = self.db.parse_or_expand(file_id).unwrap(); self.cache(root, file_id); - d.ast(self.db) + d.ast(self.db.upcast()) } pub fn expand(&self, macro_call: &ast::MacroCall) -> Option { @@ -130,9 +294,15 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.find_file(actual_macro_call.syntax().clone()).with_value(actual_macro_call); let sa = self.analyze2(macro_call.map(|it| it.syntax()), None); let krate = sa.resolver.krate()?; - let macro_call_id = macro_call - .as_call_id(self.db, krate, |path| sa.resolver.resolve_path_as_macro(self.db, &path))?; - hir_expand::db::expand_hypothetical(self.db, macro_call_id, hypothetical_args, token_to_map) + let macro_call_id = macro_call.as_call_id(self.db.upcast(), krate, |path| { + sa.resolver.resolve_path_as_macro(self.db.upcast(), &path) + })?; + hir_expand::db::expand_hypothetical( + self.db.upcast(), + macro_call_id, + hypothetical_args, + token_to_map, + ) } pub fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken { @@ -147,7 +317,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { return None; } let file_id = sa.expand(self.db, token.with_value(¯o_call))?; - let token = file_id.expansion_info(self.db)?.map_token_down(token.as_ref())?; + let token = file_id.expansion_info(self.db.upcast())?.map_token_down(token.as_ref())?; self.cache(find_root(&token.value.parent()), token.file_id); @@ -184,7 +354,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { pub fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator + '_ { let node = self.find_file(node); - node.ancestors_with_macros(self.db).map(|it| it.value) + node.ancestors_with_macros(self.db.upcast()).map(|it| it.value) } pub fn ancestors_at_offset_with_macros( @@ -197,8 +367,6 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len()) } - /// Find a AstNode by offset inside SyntaxNode, if it is inside *Macrofile*, - /// search up until it is of the target AstNode type pub fn find_node_at_offset_with_macros( &self, node: &SyntaxNode, @@ -207,8 +375,6 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.ancestors_at_offset_with_macros(node, offset).find_map(N::cast) } - /// Find a AstNode by offset inside SyntaxNode, if it is inside *MacroCall*, - /// descend it and find again pub fn find_node_at_offset_with_descend( &self, node: &SyntaxNode, @@ -267,9 +433,6 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.analyze(pat.syntax()).resolve_bind_pat_to_const(self.db, pat) } - // FIXME: use this instead? - // pub fn resolve_name_ref(&self, name_ref: &ast::NameRef) -> Option; - pub fn record_literal_missing_fields(&self, literal: &ast::RecordLit) -> Vec<(Field, Type)> { self.analyze(literal.syntax()) .record_literal_missing_fields(self.db, literal) @@ -310,7 +473,7 @@ impl<'db, DB: HirDatabase> Semantics<'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.upcast()); SemanticsScope { db: self.db, resolver } } @@ -331,12 +494,12 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { ChildContainer::DefWithBodyId(def) => { return SourceAnalyzer::new_for_body(self.db, def, src, offset) } - ChildContainer::TraitId(it) => it.resolver(self.db), - ChildContainer::ImplId(it) => it.resolver(self.db), - ChildContainer::ModuleId(it) => it.resolver(self.db), - ChildContainer::EnumId(it) => it.resolver(self.db), - ChildContainer::VariantId(it) => it.resolver(self.db), - ChildContainer::GenericDefId(it) => it.resolver(self.db), + ChildContainer::TraitId(it) => it.resolver(self.db.upcast()), + ChildContainer::ImplId(it) => it.resolver(self.db.upcast()), + ChildContainer::ModuleId(it) => it.resolver(self.db.upcast()), + ChildContainer::EnumId(it) => it.resolver(self.db.upcast()), + ChildContainer::VariantId(it) => it.resolver(self.db.upcast()), + ChildContainer::GenericDefId(it) => it.resolver(self.db.upcast()), }; SourceAnalyzer::new_for_resolver(resolver, src) } @@ -382,14 +545,14 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { pub trait ToDef: AstNode + Clone { type Def; - fn to_def(sema: &Semantics, src: InFile) -> Option; + fn to_def(sema: &SemanticsImpl, src: InFile) -> Option; } macro_rules! to_def_impls { ($(($def:path, $ast:path, $meth:ident)),* ,) => {$( impl ToDef for $ast { type Def = $def; - fn to_def(sema: &Semantics, src: InFile) -> Option { + fn to_def(sema: &SemanticsImpl, src: InFile) -> Option { sema.with_ctx(|ctx| ctx.$meth(src)).map(<$def>::from) } } -- cgit v1.2.3 From d89827f9e01d855e9116a0d5276ffe1dad34ed3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Wed, 1 Jul 2020 14:32:18 +0300 Subject: Make less code generic --- crates/ra_hir/src/semantics.rs | 116 +++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 69 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index fbea9ad57..3d78f71c1 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -83,7 +83,7 @@ impl PathResolution { /// Primary API to get semantic information, like types, from syntax trees. pub struct Semantics<'db, DB> { pub db: &'db DB, - impl_: SemanticsImpl<'db>, + imp: SemanticsImpl<'db>, } pub struct SemanticsImpl<'db> { @@ -101,19 +101,22 @@ impl fmt::Debug for Semantics<'_, DB> { impl<'db, DB: HirDatabase> Semantics<'db, DB> { pub fn new(db: &DB) -> Semantics { let impl_ = SemanticsImpl::new(db); - Semantics { db, impl_ } + Semantics { db, imp: impl_ } } pub fn parse(&self, file_id: FileId) -> ast::SourceFile { - self.impl_.parse(file_id) + self.imp.parse(file_id) } pub fn ast(&self, d: &T) -> ::AST { - self.impl_.ast(d) + let file_id = d.source().file_id; + let root = self.db.parse_or_expand(file_id).unwrap(); + self.imp.cache(root, file_id); + d.ast(self.db.upcast()) } pub fn expand(&self, macro_call: &ast::MacroCall) -> Option { - self.impl_.expand(macro_call) + self.imp.expand(macro_call) } pub fn expand_hypothetical( @@ -122,11 +125,11 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { hypothetical_args: &ast::TokenTree, token_to_map: SyntaxToken, ) -> Option<(SyntaxNode, SyntaxToken)> { - self.impl_.expand_hypothetical(actual_macro_call, hypothetical_args, token_to_map) + self.imp.expand_hypothetical(actual_macro_call, hypothetical_args, token_to_map) } pub fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken { - self.impl_.descend_into_macros(token) + self.imp.descend_into_macros(token) } pub fn descend_node_at_offset( @@ -134,19 +137,19 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { node: &SyntaxNode, offset: TextSize, ) -> Option { - self.impl_.descend_node_at_offset(node, offset) + self.imp.descend_node_at_offset(node, offset).find_map(N::cast) } pub fn original_range(&self, node: &SyntaxNode) -> FileRange { - self.impl_.original_range(node) + self.imp.original_range(node) } pub fn diagnostics_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { - self.impl_.diagnostics_range(diagnostics) + self.imp.diagnostics_range(diagnostics) } pub fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator + '_ { - self.impl_.ancestors_with_macros(node) + self.imp.ancestors_with_macros(node) } pub fn ancestors_at_offset_with_macros( @@ -154,7 +157,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { node: &SyntaxNode, offset: TextSize, ) -> impl Iterator + '_ { - self.impl_.ancestors_at_offset_with_macros(node, offset) + self.imp.ancestors_at_offset_with_macros(node, offset) } /// Find a AstNode by offset inside SyntaxNode, if it is inside *Macrofile*, @@ -164,7 +167,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { node: &SyntaxNode, offset: TextSize, ) -> Option { - self.impl_.find_node_at_offset_with_macros(node, offset) + self.imp.ancestors_at_offset_with_macros(node, offset).find_map(N::cast) } /// Find a AstNode by offset inside SyntaxNode, if it is inside *MacroCall*, @@ -174,86 +177,91 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { node: &SyntaxNode, offset: TextSize, ) -> Option { - self.impl_.find_node_at_offset_with_descend(node, offset) + if let Some(it) = find_node_at_offset(&node, offset) { + return Some(it); + } + + self.imp.descend_node_at_offset(node, offset).find_map(N::cast) } pub fn type_of_expr(&self, expr: &ast::Expr) -> Option { - self.impl_.type_of_expr(expr) + self.imp.type_of_expr(expr) } pub fn type_of_pat(&self, pat: &ast::Pat) -> Option { - self.impl_.type_of_pat(pat) + self.imp.type_of_pat(pat) } pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option { - self.impl_.resolve_method_call(call) + self.imp.resolve_method_call(call) } pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option { - self.impl_.resolve_field(field) + self.imp.resolve_field(field) } pub fn resolve_record_field(&self, field: &ast::RecordField) -> Option<(Field, Option)> { - self.impl_.resolve_record_field(field) + self.imp.resolve_record_field(field) } pub fn resolve_record_field_pat(&self, field: &ast::RecordFieldPat) -> Option { - self.impl_.resolve_record_field_pat(field) + self.imp.resolve_record_field_pat(field) } pub fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option { - self.impl_.resolve_macro_call(macro_call) + self.imp.resolve_macro_call(macro_call) } pub fn resolve_path(&self, path: &ast::Path) -> Option { - self.impl_.resolve_path(path) + self.imp.resolve_path(path) } pub fn resolve_variant(&self, record_lit: ast::RecordLit) -> Option { - self.impl_.resolve_variant(record_lit) + self.imp.resolve_variant(record_lit) } pub fn lower_path(&self, path: &ast::Path) -> Option { - self.impl_.lower_path(path) + self.imp.lower_path(path) } pub fn resolve_bind_pat_to_const(&self, pat: &ast::BindPat) -> Option { - self.impl_.resolve_bind_pat_to_const(pat) + self.imp.resolve_bind_pat_to_const(pat) } // FIXME: use this instead? // pub fn resolve_name_ref(&self, name_ref: &ast::NameRef) -> Option; pub fn record_literal_missing_fields(&self, literal: &ast::RecordLit) -> Vec<(Field, Type)> { - self.impl_.record_literal_missing_fields(literal) + self.imp.record_literal_missing_fields(literal) } pub fn record_pattern_missing_fields(&self, pattern: &ast::RecordPat) -> Vec<(Field, Type)> { - self.impl_.record_pattern_missing_fields(pattern) + self.imp.record_pattern_missing_fields(pattern) } pub fn to_def(&self, src: &T) -> Option { - self.impl_.to_def(src) + let src = self.imp.find_file(src.syntax().clone()).with_value(src).cloned(); + T::to_def(&self.imp, src) } pub fn to_module_def(&self, file: FileId) -> Option { - self.impl_.to_module_def(file) + self.imp.to_module_def(file) } pub fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> { - self.impl_.scope(node) + self.imp.scope(node) } pub fn scope_at_offset(&self, node: &SyntaxNode, offset: TextSize) -> SemanticsScope<'db> { - self.impl_.scope_at_offset(node, offset) + self.imp.scope_at_offset(node, offset) } pub fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> { - self.impl_.scope_for_def(def) + self.imp.scope_for_def(def) } pub fn assert_contains_node(&self, node: &SyntaxNode) { - self.impl_.assert_contains_node(node) + self.imp.assert_contains_node(node) } } @@ -268,13 +276,6 @@ impl<'db> SemanticsImpl<'db> { tree } - pub fn ast(&self, d: &T) -> ::AST { - let file_id = d.source().file_id; - let root = self.db.parse_or_expand(file_id).unwrap(); - self.cache(root, file_id); - d.ast(self.db.upcast()) - } - pub fn expand(&self, macro_call: &ast::MacroCall) -> Option { let macro_call = self.find_file(macro_call.syntax().clone()).with_value(macro_call); let sa = self.analyze2(macro_call.map(|it| it.syntax()), None); @@ -329,15 +330,16 @@ impl<'db> SemanticsImpl<'db> { token.value } - pub fn descend_node_at_offset( + pub fn descend_node_at_offset( &self, node: &SyntaxNode, offset: TextSize, - ) -> Option { + ) -> impl Iterator + '_ { // Handle macro token cases node.token_at_offset(offset) .map(|token| self.descend_into_macros(token)) - .find_map(|it| self.ancestors_with_macros(it.parent()).find_map(N::cast)) + .map(|it| self.ancestors_with_macros(it.parent())) + .flatten() } pub fn original_range(&self, node: &SyntaxNode) -> FileRange { @@ -367,25 +369,6 @@ impl<'db> SemanticsImpl<'db> { .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len()) } - pub fn find_node_at_offset_with_macros( - &self, - node: &SyntaxNode, - offset: TextSize, - ) -> Option { - self.ancestors_at_offset_with_macros(node, offset).find_map(N::cast) - } - - pub fn find_node_at_offset_with_descend( - &self, - node: &SyntaxNode, - offset: TextSize, - ) -> Option { - if let Some(it) = find_node_at_offset(&node, offset) { - return Some(it); - } - self.descend_node_at_offset(&node, offset) - } - pub fn type_of_expr(&self, expr: &ast::Expr) -> Option { self.analyze(expr.syntax()).type_of(self.db, &expr) } @@ -445,11 +428,6 @@ impl<'db> SemanticsImpl<'db> { .unwrap_or_default() } - pub fn to_def(&self, src: &T) -> Option { - let src = self.find_file(src.syntax().clone()).with_value(src).cloned(); - T::to_def(self, src) - } - fn with_ctx T, T>(&self, f: F) -> T { let mut cache = self.s2d_cache.borrow_mut(); let mut ctx = SourceToDefCtx { db: self.db, cache: &mut *cache }; @@ -504,7 +482,7 @@ impl<'db> SemanticsImpl<'db> { SourceAnalyzer::new_for_resolver(resolver, src) } - fn cache(&self, root_node: SyntaxNode, file_id: HirFileId) { + pub fn cache(&self, root_node: SyntaxNode, file_id: HirFileId) { assert!(root_node.parent().is_none()); let mut cache = self.cache.borrow_mut(); let prev = cache.insert(root_node, file_id); @@ -520,7 +498,7 @@ impl<'db> SemanticsImpl<'db> { cache.get(root_node).copied() } - fn find_file(&self, node: SyntaxNode) -> InFile { + pub fn find_file(&self, node: SyntaxNode) -> InFile { let root_node = find_root(&node); let file_id = self.lookup(&root_node).unwrap_or_else(|| { panic!( -- cgit v1.2.3 From 6bde542a39fe63298a31b838e59705797ed8a2cf Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 1 Jul 2020 17:15:20 +0200 Subject: Split `CrateImplDefs` in inherent and trait impls This makes the intention of inherent vs. trait impls somewhat more clear and also fixes (?) an issue where trait impls with an unresolved trait were added as inherent impls instead (hence the test changes). --- crates/ra_hir/src/code_model.rs | 16 +++++++++------- crates/ra_hir/src/db.rs | 8 ++++---- 2 files changed, 13 insertions(+), 11 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index e09eb77c2..cc72964ff 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -1053,12 +1053,14 @@ pub struct ImplDef { impl ImplDef { pub fn all_in_crate(db: &dyn HirDatabase, krate: Crate) -> Vec { - let impls = db.impls_in_crate(krate.id); - impls.all_impls().map(Self::from).collect() + let inherent = db.inherent_impls_in_crate(krate.id); + let trait_ = db.trait_impls_in_crate(krate.id); + + inherent.all_impls().chain(trait_.all_impls()).map(Self::from).collect() } pub fn for_trait(db: &dyn HirDatabase, krate: Crate, trait_: Trait) -> Vec { - let impls = db.impls_in_crate(krate.id); - impls.lookup_impl_defs_for_trait(trait_.id).map(Self::from).collect() + let impls = db.trait_impls_in_crate(krate.id); + impls.for_trait(trait_.id).map(Self::from).collect() } pub fn target_trait(self, db: &dyn HirDatabase) -> Option { @@ -1303,10 +1305,10 @@ impl Type { mut callback: impl FnMut(AssocItem) -> Option, ) -> Option { for krate in self.ty.value.def_crates(db, krate.id)? { - let impls = db.impls_in_crate(krate); + let impls = db.inherent_impls_in_crate(krate); - for impl_def in impls.lookup_impl_defs(&self.ty.value) { - for &item in db.impl_data(impl_def).items.iter() { + for impl_def in impls.for_self_ty(&self.ty.value) { + for &item in db.impl_data(*impl_def).items.iter() { if let Some(result) = callback(item.into()) { return Some(result); } diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index bb67952de..cb48ca065 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -16,10 +16,10 @@ pub use hir_expand::db::{ pub use hir_ty::db::{ AssociatedTyDataQuery, AssociatedTyValueQuery, CallableItemSignatureQuery, FieldTypesQuery, GenericDefaultsQuery, GenericPredicatesForParamQuery, GenericPredicatesQuery, HirDatabase, - HirDatabaseStorage, ImplDatumQuery, ImplSelfTyQuery, ImplTraitQuery, ImplsFromDepsQuery, - ImplsInCrateQuery, InferQueryQuery, InternAssocTyValueQuery, InternChalkImplQuery, - InternTypeCtorQuery, InternTypeParamIdQuery, ReturnTypeImplTraitsQuery, StructDatumQuery, - TraitDatumQuery, TraitSolveQuery, TyQuery, ValueTyQuery, + HirDatabaseStorage, ImplDatumQuery, ImplSelfTyQuery, ImplTraitQuery, InferQueryQuery, + InherentImplsInCrateQuery, InternAssocTyValueQuery, InternChalkImplQuery, InternTypeCtorQuery, + InternTypeParamIdQuery, ReturnTypeImplTraitsQuery, StructDatumQuery, TraitDatumQuery, + TraitImplsInCrateQuery, TraitImplsInDepsQuery, TraitSolveQuery, TyQuery, ValueTyQuery, }; #[test] -- cgit v1.2.3 From d5d485ef9289589332893f2c0ad96cb366afe9d6 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 28 Jun 2020 21:17:27 +0200 Subject: Implement Chalk variable kinds This means we need to keep track of the kinds (general/int/float) of variables in `Canonical`, which requires some more ceremony. (It also exposes some places where we're not really dealing with canonicalization correctly -- another thing to be cleaned up when we switch to using Chalk's types directly.) Should fix the last remaining issue of #2534. --- crates/ra_hir/src/code_model.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index e86077dd6..479c82fa4 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -1187,7 +1187,7 @@ impl Type { None => return false, }; - let canonical_ty = Canonical { value: self.ty.value.clone(), num_vars: 0 }; + let canonical_ty = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; method_resolution::implements_trait( &canonical_ty, db, @@ -1211,7 +1211,7 @@ impl Type { self.ty.environment.clone(), hir_ty::Obligation::Trait(trait_ref), ), - num_vars: 0, + kinds: Arc::new([]), }; db.trait_solve(self.krate, goal).is_some() @@ -1286,7 +1286,7 @@ impl Type { pub fn autoderef<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator + 'a { // There should be no inference vars in types passed here // FIXME check that? - let canonical = Canonical { value: self.ty.value.clone(), num_vars: 0 }; + let canonical = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; let environment = self.ty.environment.clone(); let ty = InEnvironment { value: canonical, environment }; autoderef(db, Some(self.krate), ty) @@ -1327,7 +1327,7 @@ impl Type { // There should be no inference vars in types passed here // FIXME check that? // FIXME replace Unknown by bound vars here - let canonical = Canonical { value: self.ty.value.clone(), num_vars: 0 }; + let canonical = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; let env = self.ty.environment.clone(); let krate = krate.id; @@ -1358,7 +1358,7 @@ impl Type { // There should be no inference vars in types passed here // FIXME check that? // FIXME replace Unknown by bound vars here - let canonical = Canonical { value: self.ty.value.clone(), num_vars: 0 }; + let canonical = Canonical { value: self.ty.value.clone(), kinds: Arc::new([]) }; let env = self.ty.environment.clone(); let krate = krate.id; -- cgit v1.2.3 From 63ce2c7b5fd96e6688796f2ddd1cd7316df8d11d Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 8 Jul 2020 19:58:45 +0200 Subject: Add argument count mismatch diagnostic --- crates/ra_hir/src/diagnostics.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/diagnostics.rs b/crates/ra_hir/src/diagnostics.rs index c82883d0c..11a0ecb8b 100644 --- a/crates/ra_hir/src/diagnostics.rs +++ b/crates/ra_hir/src/diagnostics.rs @@ -1,4 +1,6 @@ //! FIXME: write short doc here pub use hir_def::diagnostics::UnresolvedModule; pub use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink}; -pub use hir_ty::diagnostics::{MissingFields, MissingMatchArms, MissingOkInTailExpr, NoSuchField}; +pub use hir_ty::diagnostics::{ + MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkInTailExpr, NoSuchField, +}; -- cgit v1.2.3 From b85042601d69d1c592b731430ef0e445c39fda56 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 10 Jul 2020 14:08:35 +0200 Subject: Goto type definition works for self --- crates/ra_hir/src/semantics.rs | 9 +++++++++ crates/ra_hir/src/source_analyzer.rs | 12 ++++++++++++ 2 files changed, 21 insertions(+) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 3d78f71c1..6b6c5e494 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -192,6 +192,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.type_of_pat(pat) } + pub fn type_of_self(&self, param: &ast::SelfParam) -> Option { + self.imp.type_of_self(param) + } + pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option { self.imp.resolve_method_call(call) } @@ -216,6 +220,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.resolve_path(path) } + // TODO: id pub fn resolve_variant(&self, record_lit: ast::RecordLit) -> Option { self.imp.resolve_variant(record_lit) } @@ -377,6 +382,10 @@ impl<'db> SemanticsImpl<'db> { self.analyze(pat.syntax()).type_of_pat(self.db, &pat) } + pub fn type_of_self(&self, param: &ast::SelfParam) -> Option { + self.analyze(param.syntax()).type_of_self(self.db, ¶m) + } + pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option { self.analyze(call.syntax()).resolve_method_call(self.db, call) } diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index 1d6c47103..1f1bdc0d8 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs @@ -115,6 +115,7 @@ impl SourceAnalyzer { Some(res) } + // TODO: rename pub(crate) fn type_of(&self, db: &dyn HirDatabase, expr: &ast::Expr) -> Option { let expr_id = self.expr_id(db, expr)?; let ty = self.infer.as_ref()?[expr_id].clone(); @@ -127,6 +128,17 @@ impl SourceAnalyzer { Type::new_with_resolver(db, &self.resolver, ty) } + pub(crate) fn type_of_self( + &self, + db: &dyn HirDatabase, + param: &ast::SelfParam, + ) -> Option { + let src = InFile { file_id: self.file_id, value: param }; + let pat_id = self.body_source_map.as_ref()?.node_self_param(src)?; + let ty = self.infer.as_ref()?[pat_id].clone(); + Type::new_with_resolver(db, &self.resolver, ty) + } + pub(crate) fn resolve_method_call( &self, db: &dyn HirDatabase, -- cgit v1.2.3 From 9c54537ecf4694d02f45c7ca0a3828c08c0a0efd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 10 Jul 2020 14:09:31 +0200 Subject: Rename --- crates/ra_hir/src/semantics.rs | 2 +- crates/ra_hir/src/source_analyzer.rs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 6b6c5e494..4677eb561 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -375,7 +375,7 @@ impl<'db> SemanticsImpl<'db> { } pub fn type_of_expr(&self, expr: &ast::Expr) -> Option { - self.analyze(expr.syntax()).type_of(self.db, &expr) + self.analyze(expr.syntax()).type_of_expr(self.db, &expr) } pub fn type_of_pat(&self, pat: &ast::Pat) -> Option { diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index 1f1bdc0d8..f74b78b23 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs @@ -115,8 +115,7 @@ impl SourceAnalyzer { Some(res) } - // TODO: rename - pub(crate) fn type_of(&self, db: &dyn HirDatabase, expr: &ast::Expr) -> Option { + pub(crate) fn type_of_expr(&self, db: &dyn HirDatabase, expr: &ast::Expr) -> Option { let expr_id = self.expr_id(db, expr)?; let ty = self.infer.as_ref()?[expr_id].clone(); Type::new_with_resolver(db, &self.resolver, ty) -- cgit v1.2.3 From f4147f6a341bda8f4a181a096b7dd726fc2d6b31 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 10 Jul 2020 14:11:31 +0200 Subject: Dont expose ID --- crates/ra_hir/src/semantics.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 4677eb561..0d877e44e 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -25,7 +25,7 @@ use crate::{ semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, source_analyzer::{resolve_hir_path, resolve_hir_path_qualifier, SourceAnalyzer}, AssocItem, Field, Function, HirFileId, ImplDef, InFile, Local, MacroDef, Module, ModuleDef, - Name, Origin, Path, ScopeDef, Trait, Type, TypeAlias, TypeParam, + Name, Origin, Path, ScopeDef, Trait, Type, TypeAlias, TypeParam, VariantDef, }; use resolver::TypeNs; @@ -220,9 +220,8 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.resolve_path(path) } - // TODO: id - pub fn resolve_variant(&self, record_lit: ast::RecordLit) -> Option { - self.imp.resolve_variant(record_lit) + pub fn resolve_variant(&self, record_lit: ast::RecordLit) -> Option { + self.imp.resolve_variant(record_lit).map(VariantDef::from) } pub fn lower_path(&self, path: &ast::Path) -> Option { -- cgit v1.2.3 From a36ff4a100c2c321eec898f2cfee25c0be85ffc6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Jul 2020 01:26:24 +0200 Subject: Speed up completion --- crates/ra_hir/src/semantics.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 0d877e44e..4a16ac566 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -568,6 +568,7 @@ fn find_root(node: &SyntaxNode) -> SyntaxNode { node.ancestors().last().unwrap() } +#[derive(Debug)] pub struct SemanticsScope<'a> { pub db: &'a dyn HirDatabase, resolver: Resolver, -- cgit v1.2.3 From 3fc4916b53894c63320e31855e3c62b974dfcc95 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Jul 2020 12:31:50 +0200 Subject: Reduce visibility --- crates/ra_hir/src/semantics.rs | 62 +++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 31 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 4a16ac566..97125b32a 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -270,17 +270,17 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { } impl<'db> SemanticsImpl<'db> { - pub fn new(db: &'db dyn HirDatabase) -> Self { + fn new(db: &'db dyn HirDatabase) -> Self { Self { db, s2d_cache: Default::default(), cache: Default::default() } } - pub fn parse(&self, file_id: FileId) -> ast::SourceFile { + fn parse(&self, file_id: FileId) -> ast::SourceFile { let tree = self.db.parse(file_id).tree(); self.cache(tree.syntax().clone(), file_id.into()); tree } - pub fn expand(&self, macro_call: &ast::MacroCall) -> Option { + fn expand(&self, macro_call: &ast::MacroCall) -> Option { let macro_call = self.find_file(macro_call.syntax().clone()).with_value(macro_call); let sa = self.analyze2(macro_call.map(|it| it.syntax()), None); let file_id = sa.expand(self.db, macro_call)?; @@ -289,7 +289,7 @@ impl<'db> SemanticsImpl<'db> { Some(node) } - pub fn expand_hypothetical( + fn expand_hypothetical( &self, actual_macro_call: &ast::MacroCall, hypothetical_args: &ast::TokenTree, @@ -310,7 +310,7 @@ impl<'db> SemanticsImpl<'db> { ) } - pub fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken { + fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken { let parent = token.parent(); let parent = self.find_file(parent); let sa = self.analyze2(parent.as_ref(), None); @@ -334,7 +334,7 @@ impl<'db> SemanticsImpl<'db> { token.value } - pub fn descend_node_at_offset( + fn descend_node_at_offset( &self, node: &SyntaxNode, offset: TextSize, @@ -346,24 +346,24 @@ impl<'db> SemanticsImpl<'db> { .flatten() } - pub fn original_range(&self, node: &SyntaxNode) -> FileRange { + fn original_range(&self, node: &SyntaxNode) -> FileRange { let node = self.find_file(node.clone()); original_range(self.db, node.as_ref()) } - pub fn diagnostics_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { + fn diagnostics_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { let src = diagnostics.source(); let root = self.db.parse_or_expand(src.file_id).unwrap(); let node = src.value.to_node(&root); original_range(self.db, src.with_value(&node)) } - pub fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator + '_ { + fn ancestors_with_macros(&self, node: SyntaxNode) -> impl Iterator + '_ { let node = self.find_file(node); node.ancestors_with_macros(self.db.upcast()).map(|it| it.value) } - pub fn ancestors_at_offset_with_macros( + fn ancestors_at_offset_with_macros( &self, node: &SyntaxNode, offset: TextSize, @@ -373,64 +373,64 @@ impl<'db> SemanticsImpl<'db> { .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len()) } - pub fn type_of_expr(&self, expr: &ast::Expr) -> Option { + fn type_of_expr(&self, expr: &ast::Expr) -> Option { self.analyze(expr.syntax()).type_of_expr(self.db, &expr) } - pub fn type_of_pat(&self, pat: &ast::Pat) -> Option { + fn type_of_pat(&self, pat: &ast::Pat) -> Option { self.analyze(pat.syntax()).type_of_pat(self.db, &pat) } - pub fn type_of_self(&self, param: &ast::SelfParam) -> Option { + fn type_of_self(&self, param: &ast::SelfParam) -> Option { self.analyze(param.syntax()).type_of_self(self.db, ¶m) } - pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option { + fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option { self.analyze(call.syntax()).resolve_method_call(self.db, call) } - pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option { + fn resolve_field(&self, field: &ast::FieldExpr) -> Option { self.analyze(field.syntax()).resolve_field(self.db, field) } - pub fn resolve_record_field(&self, field: &ast::RecordField) -> Option<(Field, Option)> { + fn resolve_record_field(&self, field: &ast::RecordField) -> Option<(Field, Option)> { self.analyze(field.syntax()).resolve_record_field(self.db, field) } - pub fn resolve_record_field_pat(&self, field: &ast::RecordFieldPat) -> Option { + fn resolve_record_field_pat(&self, field: &ast::RecordFieldPat) -> Option { self.analyze(field.syntax()).resolve_record_field_pat(self.db, field) } - pub fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option { + fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option { let sa = self.analyze(macro_call.syntax()); let macro_call = self.find_file(macro_call.syntax().clone()).with_value(macro_call); sa.resolve_macro_call(self.db, macro_call) } - pub fn resolve_path(&self, path: &ast::Path) -> Option { + fn resolve_path(&self, path: &ast::Path) -> Option { self.analyze(path.syntax()).resolve_path(self.db, path) } - pub fn resolve_variant(&self, record_lit: ast::RecordLit) -> Option { + fn resolve_variant(&self, record_lit: ast::RecordLit) -> Option { self.analyze(record_lit.syntax()).resolve_variant(self.db, record_lit) } - pub fn lower_path(&self, path: &ast::Path) -> Option { + fn lower_path(&self, path: &ast::Path) -> Option { let src = self.find_file(path.syntax().clone()); Path::from_src(path.clone(), &Hygiene::new(self.db.upcast(), src.file_id.into())) } - pub fn resolve_bind_pat_to_const(&self, pat: &ast::BindPat) -> Option { + fn resolve_bind_pat_to_const(&self, pat: &ast::BindPat) -> Option { self.analyze(pat.syntax()).resolve_bind_pat_to_const(self.db, pat) } - pub fn record_literal_missing_fields(&self, literal: &ast::RecordLit) -> Vec<(Field, Type)> { + fn record_literal_missing_fields(&self, literal: &ast::RecordLit) -> Vec<(Field, Type)> { self.analyze(literal.syntax()) .record_literal_missing_fields(self.db, literal) .unwrap_or_default() } - pub fn record_pattern_missing_fields(&self, pattern: &ast::RecordPat) -> Vec<(Field, Type)> { + fn record_pattern_missing_fields(&self, pattern: &ast::RecordPat) -> Vec<(Field, Type)> { self.analyze(pattern.syntax()) .record_pattern_missing_fields(self.db, pattern) .unwrap_or_default() @@ -442,23 +442,23 @@ impl<'db> SemanticsImpl<'db> { f(&mut ctx) } - pub fn to_module_def(&self, file: FileId) -> Option { + fn to_module_def(&self, file: FileId) -> Option { self.with_ctx(|ctx| ctx.file_to_def(file)).map(Module::from) } - pub fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> { + fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> { let node = self.find_file(node.clone()); let resolver = self.analyze2(node.as_ref(), None).resolver; SemanticsScope { db: self.db, resolver } } - pub fn scope_at_offset(&self, node: &SyntaxNode, offset: TextSize) -> SemanticsScope<'db> { + fn scope_at_offset(&self, node: &SyntaxNode, offset: TextSize) -> SemanticsScope<'db> { let node = self.find_file(node.clone()); let resolver = self.analyze2(node.as_ref(), Some(offset)).resolver; SemanticsScope { db: self.db, resolver } } - pub fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> { + fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> { let resolver = def.id.resolver(self.db.upcast()); SemanticsScope { db: self.db, resolver } } @@ -490,14 +490,14 @@ impl<'db> SemanticsImpl<'db> { SourceAnalyzer::new_for_resolver(resolver, src) } - pub fn cache(&self, root_node: SyntaxNode, file_id: HirFileId) { + fn cache(&self, root_node: SyntaxNode, file_id: HirFileId) { assert!(root_node.parent().is_none()); let mut cache = self.cache.borrow_mut(); let prev = cache.insert(root_node, file_id); assert!(prev == None || prev == Some(file_id)) } - pub fn assert_contains_node(&self, node: &SyntaxNode) { + fn assert_contains_node(&self, node: &SyntaxNode) { self.find_file(node.clone()); } @@ -506,7 +506,7 @@ impl<'db> SemanticsImpl<'db> { cache.get(root_node).copied() } - pub fn find_file(&self, node: SyntaxNode) -> InFile { + fn find_file(&self, node: SyntaxNode) -> InFile { let root_node = find_root(&node); let file_id = self.lookup(&root_node).unwrap_or_else(|| { panic!( -- cgit v1.2.3 From 8c4919c9fdfb2333fd798cd7d531f4264c939322 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Jul 2020 12:45:30 +0200 Subject: Fix goto definition for type alias type parameters closes https://github.com/rust-analyzer/rust-analyzer/issues/5042 --- crates/ra_hir/src/semantics.rs | 1 + crates/ra_hir/src/semantics/source_to_def.rs | 7 +++++++ 2 files changed, 8 insertions(+) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 97125b32a..155b666d7 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -485,6 +485,7 @@ impl<'db> SemanticsImpl<'db> { ChildContainer::ModuleId(it) => it.resolver(self.db.upcast()), ChildContainer::EnumId(it) => it.resolver(self.db.upcast()), ChildContainer::VariantId(it) => it.resolver(self.db.upcast()), + ChildContainer::TypeAliasId(it) => it.resolver(self.db.upcast()), ChildContainer::GenericDefId(it) => it.resolver(self.db.upcast()), }; SourceAnalyzer::new_for_resolver(resolver, src) diff --git a/crates/ra_hir/src/semantics/source_to_def.rs b/crates/ra_hir/src/semantics/source_to_def.rs index 8af64fdc1..0e1d92fb3 100644 --- a/crates/ra_hir/src/semantics/source_to_def.rs +++ b/crates/ra_hir/src/semantics/source_to_def.rs @@ -194,6 +194,10 @@ impl SourceToDefCtx<'_, '_> { let def = self.const_to_def(container.with_value(it))?; DefWithBodyId::from(def).into() }, + ast::TypeAliasDef(it) => { + let def = self.type_alias_to_def(container.with_value(it))?; + def.into() + }, _ => continue, } }; @@ -246,6 +250,7 @@ pub(crate) enum ChildContainer { ImplId(ImplId), EnumId(EnumId), VariantId(VariantId), + TypeAliasId(TypeAliasId), /// XXX: this might be the same def as, for example an `EnumId`. However, /// here the children generic parameters, and not, eg enum variants. GenericDefId(GenericDefId), @@ -258,6 +263,7 @@ impl_froms! { ImplId, EnumId, VariantId, + TypeAliasId, GenericDefId } @@ -271,6 +277,7 @@ impl ChildContainer { ChildContainer::ImplId(it) => it.child_by_source(db), ChildContainer::EnumId(it) => it.child_by_source(db), ChildContainer::VariantId(it) => it.child_by_source(db), + ChildContainer::TypeAliasId(_) => DynMap::default(), ChildContainer::GenericDefId(it) => it.child_by_source(db), } } -- cgit v1.2.3 From e1d6b7f7c48d82c3c03550bc702e64cd7d079c99 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Jul 2020 14:50:00 +0200 Subject: Use dedicated semantic highlight tag for parameters closes #5106 --- crates/ra_hir/src/code_model.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 1b3525011..04fd335fe 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -33,7 +33,10 @@ use hir_ty::{ }; use ra_db::{CrateId, Edition, FileId}; use ra_prof::profile; -use ra_syntax::ast::{self, AttrsOwner, NameOwner}; +use ra_syntax::{ + ast::{self, AttrsOwner, NameOwner}, + AstNode, +}; use rustc_hash::FxHashSet; use crate::{ @@ -955,6 +958,16 @@ pub struct Local { } impl Local { + pub fn is_param(self, db: &dyn HirDatabase) -> bool { + let src = self.source(db); + match src.value { + Either::Left(bind_pat) => { + bind_pat.syntax().ancestors().any(|it| ast::Param::can_cast(it.kind())) + } + Either::Right(_self_param) => true, + } + } + // FIXME: why is this an option? It shouldn't be? pub fn name(self, db: &dyn HirDatabase) -> Option { let body = db.body(self.parent.into()); -- cgit v1.2.3 From 693ac892f2db5db1ce7cf86db7bf6207b3515c42 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 13 Jul 2020 16:16:53 +0200 Subject: Don't copy-paste `impl_froms` into every crate --- crates/ra_hir/src/code_model.rs | 23 +++++++++++++---------- crates/ra_hir/src/lib.rs | 19 ------------------- crates/ra_hir/src/semantics/source_to_def.rs | 5 +++-- 3 files changed, 16 insertions(+), 31 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 04fd335fe..9222009fe 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -38,6 +38,7 @@ use ra_syntax::{ AstNode, }; use rustc_hash::FxHashSet; +use stdx::impl_from; use crate::{ db::{DefDatabase, HirDatabase}, @@ -142,8 +143,8 @@ pub enum ModuleDef { TypeAlias(TypeAlias), BuiltinType(BuiltinType), } -impl_froms!( - ModuleDef: Module, +impl_from!( + Module, Function, Adt(Struct, Enum, Union), EnumVariant, @@ -152,6 +153,7 @@ impl_froms!( Trait, TypeAlias, BuiltinType + for ModuleDef ); impl ModuleDef { @@ -541,7 +543,7 @@ pub enum Adt { Union(Union), Enum(Enum), } -impl_froms!(Adt: Struct, Union, Enum); +impl_from!(Struct, Union, Enum for Adt); impl Adt { pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { @@ -584,7 +586,7 @@ pub enum VariantDef { Union(Union), EnumVariant(EnumVariant), } -impl_froms!(VariantDef: Struct, Union, EnumVariant); +impl_from!(Struct, Union, EnumVariant for VariantDef); impl VariantDef { pub fn fields(self, db: &dyn HirDatabase) -> Vec { @@ -627,8 +629,7 @@ pub enum DefWithBody { Static(Static), Const(Const), } - -impl_froms!(DefWithBody: Function, Const, Static); +impl_from!(Function, Const, Static for DefWithBody); impl DefWithBody { pub fn module(self, db: &dyn HirDatabase) -> Module { @@ -930,14 +931,15 @@ pub enum GenericDef { // consts can have type parameters from their parents (i.e. associated consts of traits) Const(Const), } -impl_froms!( - GenericDef: Function, +impl_from!( + Function, Adt(Struct, Enum, Union), Trait, TypeAlias, ImplDef, EnumVariant, Const + for GenericDef ); impl GenericDef { @@ -1578,8 +1580,8 @@ pub enum AttrDef { MacroDef(MacroDef), } -impl_froms!( - AttrDef: Module, +impl_from!( + Module, Field, Adt(Struct, Enum, Union), EnumVariant, @@ -1589,6 +1591,7 @@ impl_froms!( Trait, TypeAlias, MacroDef + for AttrDef ); pub trait HasAttrs { diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 3364a822f..7d9b174b4 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -19,25 +19,6 @@ #![recursion_limit = "512"] -macro_rules! impl_froms { - ($e:ident: $($v:ident $(($($sv:ident),*))?),*$(,)?) => { - $( - impl From<$v> for $e { - fn from(it: $v) -> $e { - $e::$v(it) - } - } - $($( - impl From<$sv> for $e { - fn from(it: $sv) -> $e { - $e::$v($v::$sv(it)) - } - } - )*)? - )* - } -} - mod semantics; pub mod db; mod source_analyzer; diff --git a/crates/ra_hir/src/semantics/source_to_def.rs b/crates/ra_hir/src/semantics/source_to_def.rs index 0e1d92fb3..42e5a1bdb 100644 --- a/crates/ra_hir/src/semantics/source_to_def.rs +++ b/crates/ra_hir/src/semantics/source_to_def.rs @@ -16,6 +16,7 @@ use ra_syntax::{ match_ast, AstNode, SyntaxNode, }; use rustc_hash::FxHashMap; +use stdx::impl_from; use crate::{db::HirDatabase, InFile, MacroDefId}; @@ -255,8 +256,7 @@ pub(crate) enum ChildContainer { /// here the children generic parameters, and not, eg enum variants. GenericDefId(GenericDefId), } -impl_froms! { - ChildContainer: +impl_from! { DefWithBodyId, ModuleId, TraitId, @@ -265,6 +265,7 @@ impl_froms! { VariantId, TypeAliasId, GenericDefId + for ChildContainer } impl ChildContainer { -- cgit v1.2.3 From 1fdbf81181356854b692fe0407bac75aba6ea942 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 14 Jul 2020 10:18:08 +0200 Subject: Consolidate hir diagnostics code in one place --- crates/ra_hir/src/code_model.rs | 8 +++----- crates/ra_hir/src/source_analyzer.rs | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 9222009fe..dbda25d1d 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -24,12 +24,10 @@ use hir_expand::{ }; use hir_ty::{ autoderef, + diagnostics::{expr::ExprValidator, unsafe_check::UnsafeValidator}, display::{HirDisplayError, HirFormatter}, - expr::ExprValidator, - method_resolution, - unsafe_validation::UnsafeValidator, - ApplicationTy, Canonical, GenericPredicate, InEnvironment, Substs, TraitEnvironment, Ty, - TyDefId, TypeCtor, + method_resolution, ApplicationTy, Canonical, GenericPredicate, InEnvironment, Substs, + TraitEnvironment, Ty, TyDefId, TypeCtor, }; use ra_db::{CrateId, Edition, FileId}; use ra_prof::profile; diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index f74b78b23..bfa543e5b 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs @@ -18,7 +18,7 @@ use hir_def::{ }; use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; use hir_ty::{ - expr::{record_literal_missing_fields, record_pattern_missing_fields}, + diagnostics::expr::{record_literal_missing_fields, record_pattern_missing_fields}, InferenceResult, Substs, Ty, }; use ra_syntax::{ -- cgit v1.2.3 From 19450534cf308eff30ea7de1a40ab77dca4e6014 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 14 Jul 2020 10:28:55 +0200 Subject: Cleanup hir diagnostics API --- crates/ra_hir/src/code_model.rs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index dbda25d1d..42c9ca189 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -24,7 +24,6 @@ use hir_expand::{ }; use hir_ty::{ autoderef, - diagnostics::{expr::ExprValidator, unsafe_check::UnsafeValidator}, display::{HirDisplayError, HirFormatter}, method_resolution, ApplicationTy, Canonical, GenericPredicate, InEnvironment, Substs, TraitEnvironment, Ty, TyDefId, TypeCtor, @@ -678,13 +677,7 @@ impl Function { } pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { - let _p = profile("Function::diagnostics"); - let infer = db.infer(self.id.into()); - infer.add_diagnostics(db, self.id, sink); - let mut validator = ExprValidator::new(self.id, infer.clone(), sink); - validator.validate_body(db); - let mut validator = UnsafeValidator::new(self.id, infer, sink); - validator.validate_body(db); + hir_ty::diagnostics::validate_body(db, self.id.into(), sink) } } -- cgit v1.2.3 From b2390f10fa55d983cd4439f1bfb07a3fa9a63e75 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 14 Jul 2020 10:52:18 +0200 Subject: Cleanup visibility --- crates/ra_hir/src/source_analyzer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index bfa543e5b..d76345525 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs @@ -18,7 +18,7 @@ use hir_def::{ }; use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; use hir_ty::{ - diagnostics::expr::{record_literal_missing_fields, record_pattern_missing_fields}, + diagnostics::{record_literal_missing_fields, record_pattern_missing_fields}, InferenceResult, Substs, Ty, }; use ra_syntax::{ -- cgit v1.2.3 From 6b9c72567363edcff5f4e703646b7246fdf5c671 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 15 Jul 2020 17:18:19 +0200 Subject: Cap macro expansion depth for IDE features closes #4453 --- crates/ra_hir/src/source_analyzer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index d76345525..ecb54f653 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs @@ -352,7 +352,7 @@ impl SourceAnalyzer { let macro_call_id = macro_call.as_call_id(db.upcast(), krate, |path| { self.resolver.resolve_path_as_macro(db.upcast(), &path) })?; - Some(macro_call_id.as_file()) + Some(macro_call_id.as_file()).filter(|it| it.expansion_level(db.upcast()) < 64) } pub(crate) fn resolve_variant( -- cgit v1.2.3 From a48843a16a2306399f2f6a78c69d9192a6480c88 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 12 Jul 2020 15:26:02 +0200 Subject: Use Chalk closure support --- crates/ra_hir/src/db.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index cb48ca065..3a9973abf 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -17,9 +17,9 @@ pub use hir_ty::db::{ AssociatedTyDataQuery, AssociatedTyValueQuery, CallableItemSignatureQuery, FieldTypesQuery, GenericDefaultsQuery, GenericPredicatesForParamQuery, GenericPredicatesQuery, HirDatabase, HirDatabaseStorage, ImplDatumQuery, ImplSelfTyQuery, ImplTraitQuery, InferQueryQuery, - InherentImplsInCrateQuery, InternAssocTyValueQuery, InternChalkImplQuery, InternTypeCtorQuery, - InternTypeParamIdQuery, ReturnTypeImplTraitsQuery, StructDatumQuery, TraitDatumQuery, - TraitImplsInCrateQuery, TraitImplsInDepsQuery, TraitSolveQuery, TyQuery, ValueTyQuery, + InherentImplsInCrateQuery, InternTypeCtorQuery, InternTypeParamIdQuery, + ReturnTypeImplTraitsQuery, StructDatumQuery, TraitDatumQuery, TraitImplsInCrateQuery, + TraitImplsInDepsQuery, TraitSolveQuery, TyQuery, ValueTyQuery, }; #[test] -- cgit v1.2.3 From 20770044631fd0c21caa12f9bc87489ea6c848ee Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 15 Jul 2020 21:47:45 +0200 Subject: Remove TypeCtor interning Our TypeCtor and Chalk's TypeName match now! --- crates/ra_hir/src/db.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 3a9973abf..1ad92a1f8 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -17,9 +17,9 @@ pub use hir_ty::db::{ AssociatedTyDataQuery, AssociatedTyValueQuery, CallableItemSignatureQuery, FieldTypesQuery, GenericDefaultsQuery, GenericPredicatesForParamQuery, GenericPredicatesQuery, HirDatabase, HirDatabaseStorage, ImplDatumQuery, ImplSelfTyQuery, ImplTraitQuery, InferQueryQuery, - InherentImplsInCrateQuery, InternTypeCtorQuery, InternTypeParamIdQuery, - ReturnTypeImplTraitsQuery, StructDatumQuery, TraitDatumQuery, TraitImplsInCrateQuery, - TraitImplsInDepsQuery, TraitSolveQuery, TyQuery, ValueTyQuery, + InherentImplsInCrateQuery, InternTypeParamIdQuery, ReturnTypeImplTraitsQuery, StructDatumQuery, + TraitDatumQuery, TraitImplsInCrateQuery, TraitImplsInDepsQuery, TraitSolveQuery, TyQuery, + ValueTyQuery, }; #[test] -- cgit v1.2.3 From b5ce84b17023d27f4e96ec7911aca712db0e000b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jul 2020 13:15:00 +0200 Subject: Align CallableDefId naming with other ids --- crates/ra_hir/src/code_model.rs | 4 ++-- crates/ra_hir/src/lib.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 42c9ca189..9891b0785 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -40,7 +40,7 @@ use stdx::impl_from; use crate::{ db::{DefDatabase, HirDatabase}, has_source::HasSource, - CallableDef, HirDisplay, InFile, Name, + CallableDefId, HirDisplay, InFile, Name, }; /// hir::Crate describes a single crate. It's the main interface with which @@ -1226,7 +1226,7 @@ impl Type { } // FIXME: this method is broken, as it doesn't take closures into account. - pub fn as_callable(&self) -> Option { + pub fn as_callable(&self) -> Option { Some(self.ty.value.as_callable()?.0) } diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 7d9b174b4..cf7134923 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -55,4 +55,4 @@ pub use hir_expand::{ hygiene::Hygiene, name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile, Origin, }; -pub use hir_ty::{display::HirDisplay, CallableDef}; +pub use hir_ty::{display::HirDisplay, CallableDefId}; -- cgit v1.2.3 From ff0312fa32715ce42f134fd9f049c4df5956d042 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jul 2020 13:00:56 +0200 Subject: Semantical call info --- crates/ra_hir/src/code_model.rs | 76 ++++++++++++++++++++++++++++++++---- crates/ra_hir/src/lib.rs | 13 +++--- crates/ra_hir/src/semantics.rs | 25 +++++++++--- crates/ra_hir/src/source_analyzer.rs | 6 +-- 4 files changed, 99 insertions(+), 21 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 9891b0785..057dfb82a 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -1,5 +1,5 @@ //! FIXME: write short doc here -use std::sync::Arc; +use std::{iter, sync::Arc}; use arrayvec::ArrayVec; use either::Either; @@ -12,6 +12,7 @@ use hir_def::{ import_map, per_ns::PerNs, resolver::{HasResolver, Resolver}, + src::HasSource as _, type_ref::{Mutability, TypeRef}, AdtId, AssocContainerId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LocalEnumVariantId, LocalFieldId, LocalModuleId, Lookup, ModuleId, StaticId, StructId, @@ -25,8 +26,8 @@ use hir_expand::{ use hir_ty::{ autoderef, display::{HirDisplayError, HirFormatter}, - method_resolution, ApplicationTy, Canonical, GenericPredicate, InEnvironment, Substs, - TraitEnvironment, Ty, TyDefId, TypeCtor, + method_resolution, ApplicationTy, CallableDefId, Canonical, FnSig, GenericPredicate, + InEnvironment, Substs, TraitEnvironment, Ty, TyDefId, TypeCtor, }; use ra_db::{CrateId, Edition, FileId}; use ra_prof::profile; @@ -40,7 +41,7 @@ use stdx::impl_from; use crate::{ db::{DefDatabase, HirDatabase}, has_source::HasSource, - CallableDefId, HirDisplay, InFile, Name, + HirDisplay, InFile, Name, }; /// hir::Crate describes a single crate. It's the main interface with which @@ -1168,6 +1169,12 @@ impl Type { Type::new(db, krate, def, ty) } + pub fn is_unit(&self) -> bool { + matches!( + self.ty.value, + Ty::Apply(ApplicationTy { ctor: TypeCtor::Tuple { cardinality: 0 }, .. }) + ) + } pub fn is_bool(&self) -> bool { matches!(self.ty.value, Ty::Apply(ApplicationTy { ctor: TypeCtor::Bool, .. })) } @@ -1225,9 +1232,10 @@ impl Type { db.trait_solve(self.krate, goal).is_some() } - // FIXME: this method is broken, as it doesn't take closures into account. - pub fn as_callable(&self) -> Option { - Some(self.ty.value.as_callable()?.0) + pub fn as_callable(&self, db: &dyn HirDatabase) -> Option { + let (id, substs) = self.ty.value.as_callable()?; + let sig = db.callable_item_signature(id).subst(substs); + Some(Callable { ty: self.clone(), sig, id, is_bound_method: false }) } pub fn is_closure(&self) -> bool { @@ -1512,6 +1520,60 @@ impl HirDisplay for Type { } } +// FIXME: closures +#[derive(Debug)] +pub struct Callable { + ty: Type, + sig: FnSig, + id: CallableDefId, + pub(crate) is_bound_method: bool, +} + +pub enum CallableKind { + Function(Function), + TupleStruct(Struct), + TupleEnumVariant(EnumVariant), +} + +impl Callable { + pub fn kind(&self) -> CallableKind { + match self.id { + CallableDefId::FunctionId(it) => CallableKind::Function(it.into()), + CallableDefId::StructId(it) => CallableKind::TupleStruct(it.into()), + CallableDefId::EnumVariantId(it) => CallableKind::TupleEnumVariant(it.into()), + } + } + pub fn receiver_param(&self, db: &dyn HirDatabase) -> Option { + let func = match self.id { + CallableDefId::FunctionId(it) if self.is_bound_method => it, + _ => return None, + }; + let src = func.lookup(db.upcast()).source(db.upcast()); + let param_list = src.value.param_list()?; + param_list.self_param() + } + pub fn params(&self, db: &dyn HirDatabase) -> Vec<(Option, Type)> { + let types = self + .sig + .params() + .iter() + .skip(if self.is_bound_method { 1 } else { 0 }) + .map(|ty| self.ty.derived(ty.clone())); + let patterns = match self.id { + CallableDefId::FunctionId(func) => { + let src = func.lookup(db.upcast()).source(db.upcast()); + src.value.param_list().map(|it| it.params().map(|it| it.pat())) + } + CallableDefId::StructId(_) => None, + CallableDefId::EnumVariantId(_) => None, + }; + patterns.into_iter().flatten().chain(iter::repeat(None)).zip(types).collect() + } + pub fn return_type(&self) -> Type { + self.ty.derived(self.sig.ret().clone()) + } +} + /// For IDE only #[derive(Debug)] pub enum ScopeDef { diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index cf7134923..31f3241c9 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -32,10 +32,10 @@ mod has_source; pub use crate::{ code_model::{ - Adt, AsAssocItem, AssocItem, AssocItemContainer, AttrDef, Const, Crate, CrateDependency, - DefWithBody, Docs, Enum, EnumVariant, Field, FieldSource, Function, GenericDef, HasAttrs, - HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, - Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, + Adt, AsAssocItem, AssocItem, AssocItemContainer, AttrDef, Callable, CallableKind, Const, + Crate, CrateDependency, DefWithBody, Docs, Enum, EnumVariant, Field, FieldSource, Function, + GenericDef, HasAttrs, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, + Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility, }, has_source::HasSource, semantics::{original_range, PathResolution, Semantics, SemanticsScope}, @@ -52,7 +52,8 @@ pub use hir_def::{ type_ref::Mutability, }; pub use hir_expand::{ - hygiene::Hygiene, name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, + hygiene::Hygiene, name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, + MacroDefId, /* FIXME */ MacroFile, Origin, }; -pub use hir_ty::{display::HirDisplay, CallableDefId}; +pub use hir_ty::display::HirDisplay; diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 155b666d7..f5283ab22 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -6,7 +6,7 @@ use std::{cell::RefCell, fmt, iter::successors}; use hir_def::{ resolver::{self, HasResolver, Resolver}, - AsMacroCall, TraitId, VariantId, + AsMacroCall, FunctionId, TraitId, VariantId, }; use hir_expand::{diagnostics::AstDiagnostic, hygiene::Hygiene, ExpansionInfo}; use hir_ty::associated_type_shorthand_candidates; @@ -24,8 +24,8 @@ use crate::{ diagnostics::Diagnostic, semantics::source_to_def::{ChildContainer, SourceToDefCache, SourceToDefCtx}, source_analyzer::{resolve_hir_path, resolve_hir_path_qualifier, SourceAnalyzer}, - AssocItem, Field, Function, HirFileId, ImplDef, InFile, Local, MacroDef, Module, ModuleDef, - Name, Origin, Path, ScopeDef, Trait, Type, TypeAlias, TypeParam, VariantDef, + AssocItem, Callable, Field, Function, HirFileId, ImplDef, InFile, Local, MacroDef, Module, + ModuleDef, Name, Origin, Path, ScopeDef, Trait, Type, TypeAlias, TypeParam, VariantDef, }; use resolver::TypeNs; @@ -197,7 +197,11 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { } pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option { - self.imp.resolve_method_call(call) + self.imp.resolve_method_call(call).map(Function::from) + } + + pub fn resolve_method_call_as_callable(&self, call: &ast::MethodCallExpr) -> Option { + self.imp.resolve_method_call_as_callable(call) } pub fn resolve_field(&self, field: &ast::FieldExpr) -> Option { @@ -385,10 +389,21 @@ impl<'db> SemanticsImpl<'db> { self.analyze(param.syntax()).type_of_self(self.db, ¶m) } - fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option { + fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option { self.analyze(call.syntax()).resolve_method_call(self.db, call) } + fn resolve_method_call_as_callable(&self, call: &ast::MethodCallExpr) -> Option { + // FIXME: this erases Substs + let func = self.resolve_method_call(call)?; + let ty = self.db.value_ty(func.into()); + let resolver = self.analyze(call.syntax()).resolver; + let ty = Type::new_with_resolver(self.db, &resolver, ty.value)?; + let mut res = ty.as_callable(self.db)?; + res.is_bound_method = true; + Some(res) + } + fn resolve_field(&self, field: &ast::FieldExpr) -> Option { self.analyze(field.syntax()).resolve_field(self.db, field) } diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index ecb54f653..86a47a9e5 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs @@ -14,7 +14,7 @@ use hir_def::{ }, expr::{ExprId, Pat, PatId}, resolver::{resolver_for_scope, Resolver, TypeNs, ValueNs}, - AsMacroCall, DefWithBodyId, FieldId, LocalFieldId, VariantId, + AsMacroCall, DefWithBodyId, FieldId, FunctionId, LocalFieldId, VariantId, }; use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; use hir_ty::{ @@ -142,9 +142,9 @@ impl SourceAnalyzer { &self, db: &dyn HirDatabase, call: &ast::MethodCallExpr, - ) -> Option { + ) -> Option { let expr_id = self.expr_id(db, &call.clone().into())?; - self.infer.as_ref()?.method_resolution(expr_id).map(Function::from) + self.infer.as_ref()?.method_resolution(expr_id) } pub(crate) fn resolve_field( -- cgit v1.2.3 From 6da22ed9752b239fcd4e7c75673907ceb1ac6b65 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jul 2020 18:24:26 +0200 Subject: Redner self as param for call infor for assoc fn call --- crates/ra_hir/src/code_model.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 057dfb82a..eb6a14eda 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -1552,7 +1552,10 @@ impl Callable { let param_list = src.value.param_list()?; param_list.self_param() } - pub fn params(&self, db: &dyn HirDatabase) -> Vec<(Option, Type)> { + pub fn params( + &self, + db: &dyn HirDatabase, + ) -> Vec<(Option>, Type)> { let types = self .sig .params() @@ -1562,7 +1565,14 @@ impl Callable { let patterns = match self.id { CallableDefId::FunctionId(func) => { let src = func.lookup(db.upcast()).source(db.upcast()); - src.value.param_list().map(|it| it.params().map(|it| it.pat())) + src.value.param_list().map(|param_list| { + param_list + .self_param() + .map(|it| Some(Either::Left(it))) + .filter(|_| !self.is_bound_method) + .into_iter() + .chain(param_list.params().map(|it| it.pat().map(Either::Right))) + }) } CallableDefId::StructId(_) => None, CallableDefId::EnumVariantId(_) => None, -- cgit v1.2.3 From a5ae8b8b92748e1b876002799d160136a7836212 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jul 2020 21:51:44 +0200 Subject: Inlay hints use callables --- crates/ra_hir/src/code_model.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index eb6a14eda..6cbcc3850 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -1552,6 +1552,9 @@ impl Callable { let param_list = src.value.param_list()?; param_list.self_param() } + pub fn n_params(&self) -> usize { + self.sig.params().len() + } pub fn params( &self, db: &dyn HirDatabase, -- cgit v1.2.3 From 3823c2dc1995ec261e36435662b8802c714e23d4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jul 2020 22:05:43 +0200 Subject: Remove FunctionSignature --- crates/ra_hir/src/code_model.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 6cbcc3850..0f6953158 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -1553,7 +1553,7 @@ impl Callable { param_list.self_param() } pub fn n_params(&self) -> usize { - self.sig.params().len() + self.sig.params().len() - if self.is_bound_method { 1 } else { 0 } } pub fn params( &self, -- cgit v1.2.3 From 371c5aec1c4ad18f37e96b4bf85c49563fc4a01d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 17 Jul 2020 10:57:49 +0200 Subject: call_info works with closures --- crates/ra_hir/src/code_model.rs | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 0f6953158..859bdfb3b 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -1233,9 +1233,13 @@ impl Type { } pub fn as_callable(&self, db: &dyn HirDatabase) -> Option { - let (id, substs) = self.ty.value.as_callable()?; - let sig = db.callable_item_signature(id).subst(substs); - Some(Callable { ty: self.clone(), sig, id, is_bound_method: false }) + let def = match self.ty.value { + Ty::Apply(ApplicationTy { ctor: TypeCtor::FnDef(def), parameters: _ }) => Some(def), + _ => None, + }; + + let sig = self.ty.value.callable_sig(db)?; + Some(Callable { ty: self.clone(), sig, def, is_bound_method: false }) } pub fn is_closure(&self) -> bool { @@ -1525,7 +1529,7 @@ impl HirDisplay for Type { pub struct Callable { ty: Type, sig: FnSig, - id: CallableDefId, + def: Option, pub(crate) is_bound_method: bool, } @@ -1533,19 +1537,21 @@ pub enum CallableKind { Function(Function), TupleStruct(Struct), TupleEnumVariant(EnumVariant), + Closure, } impl Callable { pub fn kind(&self) -> CallableKind { - match self.id { - CallableDefId::FunctionId(it) => CallableKind::Function(it.into()), - CallableDefId::StructId(it) => CallableKind::TupleStruct(it.into()), - CallableDefId::EnumVariantId(it) => CallableKind::TupleEnumVariant(it.into()), + match self.def { + Some(CallableDefId::FunctionId(it)) => CallableKind::Function(it.into()), + Some(CallableDefId::StructId(it)) => CallableKind::TupleStruct(it.into()), + Some(CallableDefId::EnumVariantId(it)) => CallableKind::TupleEnumVariant(it.into()), + None => CallableKind::Closure, } } pub fn receiver_param(&self, db: &dyn HirDatabase) -> Option { - let func = match self.id { - CallableDefId::FunctionId(it) if self.is_bound_method => it, + let func = match self.def { + Some(CallableDefId::FunctionId(it)) if self.is_bound_method => it, _ => return None, }; let src = func.lookup(db.upcast()).source(db.upcast()); @@ -1565,8 +1571,8 @@ impl Callable { .iter() .skip(if self.is_bound_method { 1 } else { 0 }) .map(|ty| self.ty.derived(ty.clone())); - let patterns = match self.id { - CallableDefId::FunctionId(func) => { + let patterns = match self.def { + Some(CallableDefId::FunctionId(func)) => { let src = func.lookup(db.upcast()).source(db.upcast()); src.value.param_list().map(|param_list| { param_list @@ -1577,8 +1583,7 @@ impl Callable { .chain(param_list.params().map(|it| it.pat().map(Either::Right))) }) } - CallableDefId::StructId(_) => None, - CallableDefId::EnumVariantId(_) => None, + _ => None, }; patterns.into_iter().flatten().chain(iter::repeat(None)).zip(types).collect() } -- cgit v1.2.3 From cb958cf5fec8b051d16833ac0890cace379ad765 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Wed, 22 Jul 2020 21:50:37 +0300 Subject: Store macro invocation parameters as text instead of tt --- crates/ra_hir/src/db.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 1ad92a1f8..a2b9f3e35 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -11,7 +11,7 @@ pub use hir_def::db::{ }; pub use hir_expand::db::{ AstDatabase, AstDatabaseStorage, AstIdMapQuery, InternEagerExpansionQuery, InternMacroQuery, - MacroArgQuery, MacroDefQuery, MacroExpandQuery, ParseMacroQuery, + MacroArgTextQuery, MacroDefQuery, MacroExpandQuery, ParseMacroQuery, }; pub use hir_ty::db::{ AssociatedTyDataQuery, AssociatedTyValueQuery, CallableItemSignatureQuery, FieldTypesQuery, -- cgit v1.2.3 From c0d8921148d068d088589d545916801c60b6341e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 24 Jul 2020 13:13:36 +0200 Subject: Add profiling call --- crates/ra_hir/src/semantics.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index f5283ab22..1dbc095ff 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -315,6 +315,7 @@ impl<'db> SemanticsImpl<'db> { } fn descend_into_macros(&self, token: SyntaxToken) -> SyntaxToken { + let _p = profile("descend_into_macros"); let parent = token.parent(); let parent = self.find_file(parent); let sa = self.analyze2(parent.as_ref(), None); -- cgit v1.2.3 From b9ef6cf29518fb59d3a05b1d4b45999f08ad0aeb Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 24 Jul 2020 13:51:27 +0200 Subject: Add missing cancellation point --- crates/ra_hir/src/semantics.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 1dbc095ff..02d83e0d9 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -321,6 +321,7 @@ impl<'db> SemanticsImpl<'db> { let sa = self.analyze2(parent.as_ref(), None); let token = successors(Some(parent.with_value(token)), |token| { + self.db.check_canceled(); let macro_call = token.value.ancestors().find_map(ast::MacroCall::cast)?; let tt = macro_call.token_tree()?; if !tt.syntax().text_range().contains_range(token.value.text_range()) { -- cgit v1.2.3 From a432f87d66bdfb8e9eef4f6d38d3d5efc2488c1f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 24 Jul 2020 14:12:13 +0200 Subject: Cache macro expansion in semantics #5497 accidentally made syntax highlighting quadratic, due to repeated tokentreeizing of macros. --- crates/ra_hir/src/semantics.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 02d83e0d9..1436b1afe 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -89,6 +89,7 @@ pub struct Semantics<'db, DB> { pub struct SemanticsImpl<'db> { pub db: &'db dyn HirDatabase, s2d_cache: RefCell, + expansion_info_cache: RefCell>>, cache: RefCell>, } @@ -275,7 +276,12 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { impl<'db> SemanticsImpl<'db> { fn new(db: &'db dyn HirDatabase) -> Self { - Self { db, s2d_cache: Default::default(), cache: Default::default() } + SemanticsImpl { + db, + s2d_cache: Default::default(), + cache: Default::default(), + expansion_info_cache: Default::default(), + } } fn parse(&self, file_id: FileId) -> ast::SourceFile { @@ -328,7 +334,13 @@ impl<'db> SemanticsImpl<'db> { return None; } let file_id = sa.expand(self.db, token.with_value(¯o_call))?; - let token = file_id.expansion_info(self.db.upcast())?.map_token_down(token.as_ref())?; + let token = self + .expansion_info_cache + .borrow_mut() + .entry(file_id) + .or_insert_with(|| file_id.expansion_info(self.db.upcast())) + .as_ref()? + .map_token_down(token.as_ref())?; self.cache(find_root(&token.value.parent()), token.file_id); -- cgit v1.2.3 From 6f02befee4249618a2a7858d27649fa389888ea8 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 24 Jul 2020 16:30:12 +0200 Subject: Add a builder for DiagnosticSink --- crates/ra_hir/src/diagnostics.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/diagnostics.rs b/crates/ra_hir/src/diagnostics.rs index 11a0ecb8b..266b513dc 100644 --- a/crates/ra_hir/src/diagnostics.rs +++ b/crates/ra_hir/src/diagnostics.rs @@ -1,6 +1,8 @@ //! FIXME: write short doc here pub use hir_def::diagnostics::UnresolvedModule; -pub use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink}; +pub use hir_expand::diagnostics::{ + AstDiagnostic, Diagnostic, DiagnosticSink, DiagnosticSinkBuilder, +}; pub use hir_ty::diagnostics::{ MismatchedArgCount, MissingFields, MissingMatchArms, MissingOkInTailExpr, NoSuchField, }; -- cgit v1.2.3 From 1142112c70b705f59b7d559d9d72cdc831865158 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 14:51:08 +0200 Subject: Rename FnDef -> Fn --- crates/ra_hir/src/has_source.rs | 4 ++-- crates/ra_hir/src/semantics.rs | 2 +- crates/ra_hir/src/semantics/source_to_def.rs | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs index 76c32fc17..f32b8cbb9 100644 --- a/crates/ra_hir/src/has_source.rs +++ b/crates/ra_hir/src/has_source.rs @@ -81,8 +81,8 @@ impl HasSource for EnumVariant { } } impl HasSource for Function { - type Ast = ast::FnDef; - fn source(self, db: &dyn HirDatabase) -> InFile { + type Ast = ast::Fn; + fn source(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } } diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 1436b1afe..0b634f23e 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -585,7 +585,7 @@ to_def_impls![ (crate::TypeAlias, ast::TypeAliasDef, type_alias_to_def), (crate::Const, ast::ConstDef, const_to_def), (crate::Static, ast::StaticDef, static_to_def), - (crate::Function, ast::FnDef, fn_to_def), + (crate::Function, ast::Fn, fn_to_def), (crate::Field, ast::RecordFieldDef, record_field_to_def), (crate::Field, ast::TupleFieldDef, tuple_field_to_def), (crate::EnumVariant, ast::EnumVariant, enum_variant_to_def), diff --git a/crates/ra_hir/src/semantics/source_to_def.rs b/crates/ra_hir/src/semantics/source_to_def.rs index 42e5a1bdb..4118de2e2 100644 --- a/crates/ra_hir/src/semantics/source_to_def.rs +++ b/crates/ra_hir/src/semantics/source_to_def.rs @@ -71,7 +71,7 @@ impl SourceToDefCtx<'_, '_> { pub(super) fn impl_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::IMPL) } - pub(super) fn fn_to_def(&mut self, src: InFile) -> Option { + pub(super) fn fn_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::FUNCTION) } pub(super) fn struct_to_def(&mut self, src: InFile) -> Option { @@ -171,7 +171,7 @@ impl SourceToDefCtx<'_, '_> { let def = self.impl_to_def(container.with_value(it))?; def.into() }, - ast::FnDef(it) => { + ast::Fn(it) => { let def = self.fn_to_def(container.with_value(it))?; DefWithBodyId::from(def).into() }, @@ -213,7 +213,7 @@ impl SourceToDefCtx<'_, '_> { for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) { let res: GenericDefId = match_ast! { match (container.value) { - ast::FnDef(it) => self.fn_to_def(container.with_value(it))?.into(), + ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(), ast::StructDef(it) => self.struct_to_def(container.with_value(it))?.into(), ast::EnumDef(it) => self.enum_to_def(container.with_value(it))?.into(), ast::TraitDef(it) => self.trait_to_def(container.with_value(it))?.into(), @@ -233,7 +233,7 @@ impl SourceToDefCtx<'_, '_> { match (container.value) { ast::ConstDef(it) => self.const_to_def(container.with_value(it))?.into(), ast::StaticDef(it) => self.static_to_def(container.with_value(it))?.into(), - ast::FnDef(it) => self.fn_to_def(container.with_value(it))?.into(), + ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(), _ => continue, } }; -- cgit v1.2.3 From eb2f8063444b11257111f4f8ade990ec810e0361 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 15:25:46 +0200 Subject: Rename TypeAliasDef -> TypeAlias --- crates/ra_hir/src/has_source.rs | 4 ++-- crates/ra_hir/src/semantics.rs | 2 +- crates/ra_hir/src/semantics/source_to_def.rs | 9 +++------ 3 files changed, 6 insertions(+), 9 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs index f32b8cbb9..1557b7c83 100644 --- a/crates/ra_hir/src/has_source.rs +++ b/crates/ra_hir/src/has_source.rs @@ -105,8 +105,8 @@ impl HasSource for Trait { } } impl HasSource for TypeAlias { - type Ast = ast::TypeAliasDef; - fn source(self, db: &dyn HirDatabase) -> InFile { + type Ast = ast::TypeAlias; + fn source(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } } diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 0b634f23e..054405966 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -582,7 +582,7 @@ to_def_impls![ (crate::Union, ast::UnionDef, union_to_def), (crate::Trait, ast::TraitDef, trait_to_def), (crate::ImplDef, ast::ImplDef, impl_to_def), - (crate::TypeAlias, ast::TypeAliasDef, type_alias_to_def), + (crate::TypeAlias, ast::TypeAlias, type_alias_to_def), (crate::Const, ast::ConstDef, const_to_def), (crate::Static, ast::StaticDef, static_to_def), (crate::Function, ast::Fn, fn_to_def), diff --git a/crates/ra_hir/src/semantics/source_to_def.rs b/crates/ra_hir/src/semantics/source_to_def.rs index 4118de2e2..d23a1974b 100644 --- a/crates/ra_hir/src/semantics/source_to_def.rs +++ b/crates/ra_hir/src/semantics/source_to_def.rs @@ -89,10 +89,7 @@ impl SourceToDefCtx<'_, '_> { pub(super) fn const_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::CONST) } - pub(super) fn type_alias_to_def( - &mut self, - src: InFile, - ) -> Option { + pub(super) fn type_alias_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::TYPE_ALIAS) } pub(super) fn record_field_to_def( @@ -195,7 +192,7 @@ impl SourceToDefCtx<'_, '_> { let def = self.const_to_def(container.with_value(it))?; DefWithBodyId::from(def).into() }, - ast::TypeAliasDef(it) => { + ast::TypeAlias(it) => { let def = self.type_alias_to_def(container.with_value(it))?; def.into() }, @@ -217,7 +214,7 @@ impl SourceToDefCtx<'_, '_> { ast::StructDef(it) => self.struct_to_def(container.with_value(it))?.into(), ast::EnumDef(it) => self.enum_to_def(container.with_value(it))?.into(), ast::TraitDef(it) => self.trait_to_def(container.with_value(it))?.into(), - ast::TypeAliasDef(it) => self.type_alias_to_def(container.with_value(it))?.into(), + ast::TypeAlias(it) => self.type_alias_to_def(container.with_value(it))?.into(), ast::ImplDef(it) => self.impl_to_def(container.with_value(it))?.into(), _ => continue, } -- cgit v1.2.3 From 6f8aa75329d0a4e588e58b8f22f7932bf3d3a706 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 16:21:30 +0200 Subject: Rename RecordLit -> RecordExpr --- crates/ra_hir/src/semantics.rs | 15 +++++++++------ crates/ra_hir/src/source_analyzer.rs | 8 ++++---- 2 files changed, 13 insertions(+), 10 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 054405966..a654c618f 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -209,7 +209,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.resolve_field(field) } - pub fn resolve_record_field(&self, field: &ast::RecordField) -> Option<(Field, Option)> { + pub fn resolve_record_field( + &self, + field: &ast::RecordExprField, + ) -> Option<(Field, Option)> { self.imp.resolve_record_field(field) } @@ -225,7 +228,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.resolve_path(path) } - pub fn resolve_variant(&self, record_lit: ast::RecordLit) -> Option { + pub fn resolve_variant(&self, record_lit: ast::RecordExpr) -> Option { self.imp.resolve_variant(record_lit).map(VariantDef::from) } @@ -240,7 +243,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { // FIXME: use this instead? // pub fn resolve_name_ref(&self, name_ref: &ast::NameRef) -> Option; - pub fn record_literal_missing_fields(&self, literal: &ast::RecordLit) -> Vec<(Field, Type)> { + pub fn record_literal_missing_fields(&self, literal: &ast::RecordExpr) -> Vec<(Field, Type)> { self.imp.record_literal_missing_fields(literal) } @@ -422,7 +425,7 @@ impl<'db> SemanticsImpl<'db> { self.analyze(field.syntax()).resolve_field(self.db, field) } - fn resolve_record_field(&self, field: &ast::RecordField) -> Option<(Field, Option)> { + fn resolve_record_field(&self, field: &ast::RecordExprField) -> Option<(Field, Option)> { self.analyze(field.syntax()).resolve_record_field(self.db, field) } @@ -440,7 +443,7 @@ impl<'db> SemanticsImpl<'db> { self.analyze(path.syntax()).resolve_path(self.db, path) } - fn resolve_variant(&self, record_lit: ast::RecordLit) -> Option { + fn resolve_variant(&self, record_lit: ast::RecordExpr) -> Option { self.analyze(record_lit.syntax()).resolve_variant(self.db, record_lit) } @@ -453,7 +456,7 @@ impl<'db> SemanticsImpl<'db> { self.analyze(pat.syntax()).resolve_bind_pat_to_const(self.db, pat) } - fn record_literal_missing_fields(&self, literal: &ast::RecordLit) -> Vec<(Field, Type)> { + fn record_literal_missing_fields(&self, literal: &ast::RecordExpr) -> Vec<(Field, Type)> { self.analyze(literal.syntax()) .record_literal_missing_fields(self.db, literal) .unwrap_or_default() diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index 86a47a9e5..8f438bba0 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs @@ -159,7 +159,7 @@ impl SourceAnalyzer { pub(crate) fn resolve_record_field( &self, db: &dyn HirDatabase, - field: &ast::RecordField, + field: &ast::RecordExprField, ) -> Option<(Field, Option)> { let expr = field.expr()?; let expr_id = self.expr_id(db, &expr)?; @@ -246,7 +246,7 @@ impl SourceAnalyzer { } } - if let Some(rec_lit) = path.syntax().parent().and_then(ast::RecordLit::cast) { + if let Some(rec_lit) = path.syntax().parent().and_then(ast::RecordExpr::cast) { let expr_id = self.expr_id(db, &rec_lit.into())?; if let Some(VariantId::EnumVariantId(variant)) = self.infer.as_ref()?.variant_resolution_for_expr(expr_id) @@ -284,7 +284,7 @@ impl SourceAnalyzer { pub(crate) fn record_literal_missing_fields( &self, db: &dyn HirDatabase, - literal: &ast::RecordLit, + literal: &ast::RecordExpr, ) -> Option> { let krate = self.resolver.krate()?; let body = self.body.as_ref()?; @@ -358,7 +358,7 @@ impl SourceAnalyzer { pub(crate) fn resolve_variant( &self, db: &dyn HirDatabase, - record_lit: ast::RecordLit, + record_lit: ast::RecordExpr, ) -> Option { let infer = self.infer.as_ref()?; let expr_id = self.expr_id(db, &record_lit.into())?; -- cgit v1.2.3 From 0a9e3ccc262fbcbd4cdaab30384f8cb71584544b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 16:49:13 +0200 Subject: Rename FieldDef -> Field --- crates/ra_hir/src/code_model.rs | 4 ++-- crates/ra_hir/src/semantics.rs | 4 ++-- crates/ra_hir/src/semantics/source_to_def.rs | 10 ++-------- 3 files changed, 6 insertions(+), 12 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 859bdfb3b..36c0bdc9e 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -361,8 +361,8 @@ pub struct Field { #[derive(Debug, PartialEq, Eq)] pub enum FieldSource { - Named(ast::RecordFieldDef), - Pos(ast::TupleFieldDef), + Named(ast::RecordField), + Pos(ast::TupleField), } impl Field { diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index a654c618f..3e9c54a39 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -589,8 +589,8 @@ to_def_impls![ (crate::Const, ast::ConstDef, const_to_def), (crate::Static, ast::StaticDef, static_to_def), (crate::Function, ast::Fn, fn_to_def), - (crate::Field, ast::RecordFieldDef, record_field_to_def), - (crate::Field, ast::TupleFieldDef, tuple_field_to_def), + (crate::Field, ast::RecordField, record_field_to_def), + (crate::Field, ast::TupleField, tuple_field_to_def), (crate::EnumVariant, ast::EnumVariant, enum_variant_to_def), (crate::TypeParam, ast::TypeParam, type_param_to_def), (crate::MacroDef, ast::MacroCall, macro_call_to_def), // this one is dubious, not all calls are macros diff --git a/crates/ra_hir/src/semantics/source_to_def.rs b/crates/ra_hir/src/semantics/source_to_def.rs index d23a1974b..4f90f588e 100644 --- a/crates/ra_hir/src/semantics/source_to_def.rs +++ b/crates/ra_hir/src/semantics/source_to_def.rs @@ -92,16 +92,10 @@ impl SourceToDefCtx<'_, '_> { pub(super) fn type_alias_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::TYPE_ALIAS) } - pub(super) fn record_field_to_def( - &mut self, - src: InFile, - ) -> Option { + pub(super) fn record_field_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::RECORD_FIELD) } - pub(super) fn tuple_field_to_def( - &mut self, - src: InFile, - ) -> Option { + pub(super) fn tuple_field_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::TUPLE_FIELD) } pub(super) fn enum_variant_to_def( -- cgit v1.2.3 From 1ae4721c9cfea746fce59a816b1c266bf373d6cf Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 17:36:46 +0200 Subject: Finalize union grammar --- crates/ra_hir/src/has_source.rs | 4 ++-- crates/ra_hir/src/semantics.rs | 2 +- crates/ra_hir/src/semantics/source_to_def.rs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs index 1557b7c83..3a3d82109 100644 --- a/crates/ra_hir/src/has_source.rs +++ b/crates/ra_hir/src/has_source.rs @@ -63,8 +63,8 @@ impl HasSource for Struct { } } impl HasSource for Union { - type Ast = ast::UnionDef; - fn source(self, db: &dyn HirDatabase) -> InFile { + type Ast = ast::Union; + fn source(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } } diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 3e9c54a39..6c8775402 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -582,7 +582,7 @@ to_def_impls![ (crate::Module, ast::Module, module_to_def), (crate::Struct, ast::StructDef, struct_to_def), (crate::Enum, ast::EnumDef, enum_to_def), - (crate::Union, ast::UnionDef, union_to_def), + (crate::Union, ast::Union, union_to_def), (crate::Trait, ast::TraitDef, trait_to_def), (crate::ImplDef, ast::ImplDef, impl_to_def), (crate::TypeAlias, ast::TypeAlias, type_alias_to_def), diff --git a/crates/ra_hir/src/semantics/source_to_def.rs b/crates/ra_hir/src/semantics/source_to_def.rs index 4f90f588e..0093a8671 100644 --- a/crates/ra_hir/src/semantics/source_to_def.rs +++ b/crates/ra_hir/src/semantics/source_to_def.rs @@ -80,7 +80,7 @@ impl SourceToDefCtx<'_, '_> { pub(super) fn enum_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::ENUM) } - pub(super) fn union_to_def(&mut self, src: InFile) -> Option { + pub(super) fn union_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::UNION) } pub(super) fn static_to_def(&mut self, src: InFile) -> Option { @@ -174,7 +174,7 @@ impl SourceToDefCtx<'_, '_> { let def = self.enum_to_def(container.with_value(it))?; def.into() }, - ast::UnionDef(it) => { + ast::Union(it) => { let def = self.union_to_def(container.with_value(it))?; VariantId::from(def).into() }, -- cgit v1.2.3 From 216a5344c8ef3c3e430d2761dc8b1a7b60250a15 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 17:50:40 +0200 Subject: Rename StructDef -> Struct --- crates/ra_hir/src/has_source.rs | 4 ++-- crates/ra_hir/src/semantics.rs | 2 +- crates/ra_hir/src/semantics/source_to_def.rs | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs index 3a3d82109..811a12e00 100644 --- a/crates/ra_hir/src/has_source.rs +++ b/crates/ra_hir/src/has_source.rs @@ -57,8 +57,8 @@ impl HasSource for Field { } } impl HasSource for Struct { - type Ast = ast::StructDef; - fn source(self, db: &dyn HirDatabase) -> InFile { + type Ast = ast::Struct; + fn source(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } } diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 6c8775402..7df018b05 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -580,7 +580,7 @@ macro_rules! to_def_impls { to_def_impls![ (crate::Module, ast::Module, module_to_def), - (crate::Struct, ast::StructDef, struct_to_def), + (crate::Struct, ast::Struct, struct_to_def), (crate::Enum, ast::EnumDef, enum_to_def), (crate::Union, ast::Union, union_to_def), (crate::Trait, ast::TraitDef, trait_to_def), diff --git a/crates/ra_hir/src/semantics/source_to_def.rs b/crates/ra_hir/src/semantics/source_to_def.rs index 0093a8671..75b773352 100644 --- a/crates/ra_hir/src/semantics/source_to_def.rs +++ b/crates/ra_hir/src/semantics/source_to_def.rs @@ -74,7 +74,7 @@ impl SourceToDefCtx<'_, '_> { pub(super) fn fn_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::FUNCTION) } - pub(super) fn struct_to_def(&mut self, src: InFile) -> Option { + pub(super) fn struct_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::STRUCT) } pub(super) fn enum_to_def(&mut self, src: InFile) -> Option { @@ -166,7 +166,7 @@ impl SourceToDefCtx<'_, '_> { let def = self.fn_to_def(container.with_value(it))?; DefWithBodyId::from(def).into() }, - ast::StructDef(it) => { + ast::Struct(it) => { let def = self.struct_to_def(container.with_value(it))?; VariantId::from(def).into() }, @@ -205,7 +205,7 @@ impl SourceToDefCtx<'_, '_> { let res: GenericDefId = match_ast! { match (container.value) { ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(), - ast::StructDef(it) => self.struct_to_def(container.with_value(it))?.into(), + ast::Struct(it) => self.struct_to_def(container.with_value(it))?.into(), ast::EnumDef(it) => self.enum_to_def(container.with_value(it))?.into(), ast::TraitDef(it) => self.trait_to_def(container.with_value(it))?.into(), ast::TypeAlias(it) => self.type_alias_to_def(container.with_value(it))?.into(), -- cgit v1.2.3 From 609680ef97dbf82c07b6b06e21aa366423892304 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 17:52:53 +0200 Subject: Rename EnumDef -> Enum --- crates/ra_hir/src/has_source.rs | 4 ++-- crates/ra_hir/src/semantics.rs | 2 +- crates/ra_hir/src/semantics/source_to_def.rs | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs index 811a12e00..88399f724 100644 --- a/crates/ra_hir/src/has_source.rs +++ b/crates/ra_hir/src/has_source.rs @@ -69,8 +69,8 @@ impl HasSource for Union { } } impl HasSource for Enum { - type Ast = ast::EnumDef; - fn source(self, db: &dyn HirDatabase) -> InFile { + type Ast = ast::Enum; + fn source(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } } diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 7df018b05..f8e70fe27 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -581,7 +581,7 @@ macro_rules! to_def_impls { to_def_impls![ (crate::Module, ast::Module, module_to_def), (crate::Struct, ast::Struct, struct_to_def), - (crate::Enum, ast::EnumDef, enum_to_def), + (crate::Enum, ast::Enum, enum_to_def), (crate::Union, ast::Union, union_to_def), (crate::Trait, ast::TraitDef, trait_to_def), (crate::ImplDef, ast::ImplDef, impl_to_def), diff --git a/crates/ra_hir/src/semantics/source_to_def.rs b/crates/ra_hir/src/semantics/source_to_def.rs index 75b773352..9f81b952f 100644 --- a/crates/ra_hir/src/semantics/source_to_def.rs +++ b/crates/ra_hir/src/semantics/source_to_def.rs @@ -77,7 +77,7 @@ impl SourceToDefCtx<'_, '_> { pub(super) fn struct_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::STRUCT) } - pub(super) fn enum_to_def(&mut self, src: InFile) -> Option { + pub(super) fn enum_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::ENUM) } pub(super) fn union_to_def(&mut self, src: InFile) -> Option { @@ -170,7 +170,7 @@ impl SourceToDefCtx<'_, '_> { let def = self.struct_to_def(container.with_value(it))?; VariantId::from(def).into() }, - ast::EnumDef(it) => { + ast::Enum(it) => { let def = self.enum_to_def(container.with_value(it))?; def.into() }, @@ -206,7 +206,7 @@ impl SourceToDefCtx<'_, '_> { match (container.value) { ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(), ast::Struct(it) => self.struct_to_def(container.with_value(it))?.into(), - ast::EnumDef(it) => self.enum_to_def(container.with_value(it))?.into(), + ast::Enum(it) => self.enum_to_def(container.with_value(it))?.into(), ast::TraitDef(it) => self.trait_to_def(container.with_value(it))?.into(), ast::TypeAlias(it) => self.type_alias_to_def(container.with_value(it))?.into(), ast::ImplDef(it) => self.impl_to_def(container.with_value(it))?.into(), -- cgit v1.2.3 From 1766aae145c6925a33e427f2fe6ef2a56c301665 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 17:56:53 +0200 Subject: Rename EnumVariant -> Variant --- crates/ra_hir/src/has_source.rs | 4 ++-- crates/ra_hir/src/semantics.rs | 2 +- crates/ra_hir/src/semantics/source_to_def.rs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs index 88399f724..1c5747581 100644 --- a/crates/ra_hir/src/has_source.rs +++ b/crates/ra_hir/src/has_source.rs @@ -75,8 +75,8 @@ impl HasSource for Enum { } } impl HasSource for EnumVariant { - type Ast = ast::EnumVariant; - fn source(self, db: &dyn HirDatabase) -> InFile { + type Ast = ast::Variant; + fn source(self, db: &dyn HirDatabase) -> InFile { self.parent.id.child_source(db.upcast()).map(|map| map[self.id].clone()) } } diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index f8e70fe27..e18df2537 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -591,7 +591,7 @@ to_def_impls![ (crate::Function, ast::Fn, fn_to_def), (crate::Field, ast::RecordField, record_field_to_def), (crate::Field, ast::TupleField, tuple_field_to_def), - (crate::EnumVariant, ast::EnumVariant, enum_variant_to_def), + (crate::EnumVariant, ast::Variant, enum_variant_to_def), (crate::TypeParam, ast::TypeParam, type_param_to_def), (crate::MacroDef, ast::MacroCall, macro_call_to_def), // this one is dubious, not all calls are macros (crate::Local, ast::BindPat, bind_pat_to_def), diff --git a/crates/ra_hir/src/semantics/source_to_def.rs b/crates/ra_hir/src/semantics/source_to_def.rs index 9f81b952f..b85a12680 100644 --- a/crates/ra_hir/src/semantics/source_to_def.rs +++ b/crates/ra_hir/src/semantics/source_to_def.rs @@ -100,9 +100,9 @@ impl SourceToDefCtx<'_, '_> { } pub(super) fn enum_variant_to_def( &mut self, - src: InFile, + src: InFile, ) -> Option { - self.to_def(src, keys::ENUM_VARIANT) + self.to_def(src, keys::VARIANT) } pub(super) fn bind_pat_to_def( &mut self, -- cgit v1.2.3 From 3cd4112bdc924c132cb0eab9d064511a215421ec Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 18:02:20 +0200 Subject: Finalize const&static grammar --- crates/ra_hir/src/has_source.rs | 8 ++++---- crates/ra_hir/src/semantics.rs | 4 ++-- crates/ra_hir/src/semantics/source_to_def.rs | 12 ++++++------ 3 files changed, 12 insertions(+), 12 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs index 1c5747581..9581552e5 100644 --- a/crates/ra_hir/src/has_source.rs +++ b/crates/ra_hir/src/has_source.rs @@ -87,14 +87,14 @@ impl HasSource for Function { } } impl HasSource for Const { - type Ast = ast::ConstDef; - fn source(self, db: &dyn HirDatabase) -> InFile { + type Ast = ast::Const; + fn source(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } } impl HasSource for Static { - type Ast = ast::StaticDef; - fn source(self, db: &dyn HirDatabase) -> InFile { + type Ast = ast::Static; + fn source(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } } diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index e18df2537..32a60b789 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -586,8 +586,8 @@ to_def_impls![ (crate::Trait, ast::TraitDef, trait_to_def), (crate::ImplDef, ast::ImplDef, impl_to_def), (crate::TypeAlias, ast::TypeAlias, type_alias_to_def), - (crate::Const, ast::ConstDef, const_to_def), - (crate::Static, ast::StaticDef, static_to_def), + (crate::Const, ast::Const, const_to_def), + (crate::Static, ast::Static, static_to_def), (crate::Function, ast::Fn, fn_to_def), (crate::Field, ast::RecordField, record_field_to_def), (crate::Field, ast::TupleField, tuple_field_to_def), diff --git a/crates/ra_hir/src/semantics/source_to_def.rs b/crates/ra_hir/src/semantics/source_to_def.rs index b85a12680..782a03f9e 100644 --- a/crates/ra_hir/src/semantics/source_to_def.rs +++ b/crates/ra_hir/src/semantics/source_to_def.rs @@ -83,10 +83,10 @@ impl SourceToDefCtx<'_, '_> { pub(super) fn union_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::UNION) } - pub(super) fn static_to_def(&mut self, src: InFile) -> Option { + pub(super) fn static_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::STATIC) } - pub(super) fn const_to_def(&mut self, src: InFile) -> Option { + pub(super) fn const_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::CONST) } pub(super) fn type_alias_to_def(&mut self, src: InFile) -> Option { @@ -178,11 +178,11 @@ impl SourceToDefCtx<'_, '_> { let def = self.union_to_def(container.with_value(it))?; VariantId::from(def).into() }, - ast::StaticDef(it) => { + ast::Static(it) => { let def = self.static_to_def(container.with_value(it))?; DefWithBodyId::from(def).into() }, - ast::ConstDef(it) => { + ast::Const(it) => { let def = self.const_to_def(container.with_value(it))?; DefWithBodyId::from(def).into() }, @@ -222,8 +222,8 @@ impl SourceToDefCtx<'_, '_> { for container in src.cloned().ancestors_with_macros(self.db.upcast()).skip(1) { let res: DefWithBodyId = match_ast! { match (container.value) { - ast::ConstDef(it) => self.const_to_def(container.with_value(it))?.into(), - ast::StaticDef(it) => self.static_to_def(container.with_value(it))?.into(), + ast::Const(it) => self.const_to_def(container.with_value(it))?.into(), + ast::Static(it) => self.static_to_def(container.with_value(it))?.into(), ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(), _ => continue, } -- cgit v1.2.3 From c83467796b6c7365ea4f41900d74444384a9e618 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 18:17:28 +0200 Subject: Finalize Trait grammar --- crates/ra_hir/src/has_source.rs | 6 +++--- crates/ra_hir/src/semantics.rs | 2 +- crates/ra_hir/src/semantics/source_to_def.rs | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs index 9581552e5..057f6e32f 100644 --- a/crates/ra_hir/src/has_source.rs +++ b/crates/ra_hir/src/has_source.rs @@ -99,8 +99,8 @@ impl HasSource for Static { } } impl HasSource for Trait { - type Ast = ast::TraitDef; - fn source(self, db: &dyn HirDatabase) -> InFile { + type Ast = ast::Trait; + fn source(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } } @@ -127,7 +127,7 @@ impl HasSource for ImplDef { } impl HasSource for TypeParam { - type Ast = Either; + type Ast = Either; fn source(self, db: &dyn HirDatabase) -> InFile { let child_source = self.id.parent.child_source(db.upcast()); child_source.map(|it| it[self.id.local_id].clone()) diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 32a60b789..5f5104dab 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -583,7 +583,7 @@ to_def_impls![ (crate::Struct, ast::Struct, struct_to_def), (crate::Enum, ast::Enum, enum_to_def), (crate::Union, ast::Union, union_to_def), - (crate::Trait, ast::TraitDef, trait_to_def), + (crate::Trait, ast::Trait, trait_to_def), (crate::ImplDef, ast::ImplDef, impl_to_def), (crate::TypeAlias, ast::TypeAlias, type_alias_to_def), (crate::Const, ast::Const, const_to_def), diff --git a/crates/ra_hir/src/semantics/source_to_def.rs b/crates/ra_hir/src/semantics/source_to_def.rs index 782a03f9e..ae41d3ddf 100644 --- a/crates/ra_hir/src/semantics/source_to_def.rs +++ b/crates/ra_hir/src/semantics/source_to_def.rs @@ -65,7 +65,7 @@ impl SourceToDefCtx<'_, '_> { Some(ModuleId { krate: parent_module.krate, local_id: child_id }) } - pub(super) fn trait_to_def(&mut self, src: InFile) -> Option { + pub(super) fn trait_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::TRAIT) } pub(super) fn impl_to_def(&mut self, src: InFile) -> Option { @@ -154,7 +154,7 @@ impl SourceToDefCtx<'_, '_> { let def = self.module_to_def(container.with_value(it))?; def.into() }, - ast::TraitDef(it) => { + ast::Trait(it) => { let def = self.trait_to_def(container.with_value(it))?; def.into() }, @@ -207,7 +207,7 @@ impl SourceToDefCtx<'_, '_> { ast::Fn(it) => self.fn_to_def(container.with_value(it))?.into(), ast::Struct(it) => self.struct_to_def(container.with_value(it))?.into(), ast::Enum(it) => self.enum_to_def(container.with_value(it))?.into(), - ast::TraitDef(it) => self.trait_to_def(container.with_value(it))?.into(), + ast::Trait(it) => self.trait_to_def(container.with_value(it))?.into(), ast::TypeAlias(it) => self.type_alias_to_def(container.with_value(it))?.into(), ast::ImplDef(it) => self.impl_to_def(container.with_value(it))?.into(), _ => continue, -- cgit v1.2.3 From c5798c4d75aa807aec47208a49101bdec3affcca Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 18:28:28 +0200 Subject: Finalize impl Grammar --- crates/ra_hir/src/has_source.rs | 4 ++-- crates/ra_hir/src/semantics.rs | 2 +- crates/ra_hir/src/semantics/source_to_def.rs | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs index 057f6e32f..1c691d961 100644 --- a/crates/ra_hir/src/has_source.rs +++ b/crates/ra_hir/src/has_source.rs @@ -120,8 +120,8 @@ impl HasSource for MacroDef { } } impl HasSource for ImplDef { - type Ast = ast::ImplDef; - fn source(self, db: &dyn HirDatabase) -> InFile { + type Ast = ast::Impl; + fn source(self, db: &dyn HirDatabase) -> InFile { self.id.lookup(db.upcast()).source(db.upcast()) } } diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index 5f5104dab..6f3b3dc9a 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -584,7 +584,7 @@ to_def_impls![ (crate::Enum, ast::Enum, enum_to_def), (crate::Union, ast::Union, union_to_def), (crate::Trait, ast::Trait, trait_to_def), - (crate::ImplDef, ast::ImplDef, impl_to_def), + (crate::ImplDef, ast::Impl, impl_to_def), (crate::TypeAlias, ast::TypeAlias, type_alias_to_def), (crate::Const, ast::Const, const_to_def), (crate::Static, ast::Static, static_to_def), diff --git a/crates/ra_hir/src/semantics/source_to_def.rs b/crates/ra_hir/src/semantics/source_to_def.rs index ae41d3ddf..d1994e2e7 100644 --- a/crates/ra_hir/src/semantics/source_to_def.rs +++ b/crates/ra_hir/src/semantics/source_to_def.rs @@ -68,7 +68,7 @@ impl SourceToDefCtx<'_, '_> { pub(super) fn trait_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::TRAIT) } - pub(super) fn impl_to_def(&mut self, src: InFile) -> Option { + pub(super) fn impl_to_def(&mut self, src: InFile) -> Option { self.to_def(src, keys::IMPL) } pub(super) fn fn_to_def(&mut self, src: InFile) -> Option { @@ -158,7 +158,7 @@ impl SourceToDefCtx<'_, '_> { let def = self.trait_to_def(container.with_value(it))?; def.into() }, - ast::ImplDef(it) => { + ast::Impl(it) => { let def = self.impl_to_def(container.with_value(it))?; def.into() }, @@ -209,7 +209,7 @@ impl SourceToDefCtx<'_, '_> { ast::Enum(it) => self.enum_to_def(container.with_value(it))?.into(), ast::Trait(it) => self.trait_to_def(container.with_value(it))?.into(), ast::TypeAlias(it) => self.type_alias_to_def(container.with_value(it))?.into(), - ast::ImplDef(it) => self.impl_to_def(container.with_value(it))?.into(), + ast::Impl(it) => self.impl_to_def(container.with_value(it))?.into(), _ => continue, } }; -- cgit v1.2.3 From 848f446a5abdc1c5512782267427f72c217b1556 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Jul 2020 22:56:11 +0200 Subject: simplify --- crates/ra_hir/src/source_analyzer.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/source_analyzer.rs b/crates/ra_hir/src/source_analyzer.rs index 8f438bba0..f2e630ef1 100644 --- a/crates/ra_hir/src/source_analyzer.rs +++ b/crates/ra_hir/src/source_analyzer.rs @@ -405,8 +405,7 @@ fn scope_for_offset( ) }) .map(|(expr_range, scope)| { - adjust(db, scopes, source_map, expr_range, offset.file_id, offset.value) - .unwrap_or(*scope) + adjust(db, scopes, source_map, expr_range, offset).unwrap_or(*scope) }) } @@ -417,8 +416,7 @@ fn adjust( scopes: &ExprScopes, source_map: &BodySourceMap, expr_range: TextRange, - file_id: HirFileId, - offset: TextSize, + offset: InFile, ) -> Option { let child_scopes = scopes .scope_by_expr() @@ -426,7 +424,7 @@ fn adjust( .filter_map(|(id, scope)| { let source = source_map.expr_syntax(*id).ok()?; // FIXME: correctly handle macro expansion - if source.file_id != file_id { + if source.file_id != offset.file_id { return None; } let root = source.file_syntax(db.upcast()); @@ -434,7 +432,7 @@ fn adjust( Some((node.syntax().text_range(), scope)) }) .filter(|&(range, _)| { - range.start() <= offset && expr_range.contains_range(range) && range != expr_range + range.start() <= offset.value && expr_range.contains_range(range) && range != expr_range }); child_scopes -- cgit v1.2.3