From 549728bba87ed8f4375f27bb9a77223bf8f65452 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 13 May 2019 19:39:06 +0300 Subject: make AstId untyped --- crates/ra_hir/src/diagnostics.rs | 4 ++-- crates/ra_hir/src/expr/validation.rs | 2 +- crates/ra_hir/src/impl_block.rs | 2 +- crates/ra_hir/src/nameres/raw.rs | 5 ++++- crates/ra_hir/src/source_id.rs | 11 ++++++----- crates/ra_hir/src/ty/tests.rs | 2 +- crates/ra_ide_api/src/diagnostics.rs | 6 +++--- crates/ra_ide_api/src/display/navigation_target.rs | 2 +- crates/ra_ide_api/src/references.rs | 2 +- crates/ra_syntax/src/ptr.rs | 15 ++++++++------- crates/ra_syntax/src/syntax_node.rs | 2 +- 11 files changed, 29 insertions(+), 24 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/diagnostics.rs b/crates/ra_hir/src/diagnostics.rs index 61cd9d6b1..d41525779 100644 --- a/crates/ra_hir/src/diagnostics.rs +++ b/crates/ra_hir/src/diagnostics.rs @@ -1,6 +1,6 @@ use std::{fmt, any::Any}; -use ra_syntax::{SyntaxNodePtr, TreeArc, AstPtr, TextRange, ast, SyntaxNode}; +use ra_syntax::{SyntaxNodePtr, TreeArc, AstPtr, TextRange, ast, SyntaxNode, AstNode}; use relative_path::RelativePathBuf; use crate::{HirFileId, HirDatabase, Name}; @@ -30,7 +30,7 @@ pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { impl dyn Diagnostic { pub fn syntax_node(&self, db: &impl HirDatabase) -> TreeArc { let source_file = db.hir_parse(self.file()); - self.syntax_node_ptr().to_node(&source_file).to_owned() + self.syntax_node_ptr().to_node(source_file.syntax()).to_owned() } pub fn downcast_ref(&self) -> Option<&D> { self.as_any().downcast_ref() diff --git a/crates/ra_hir/src/expr/validation.rs b/crates/ra_hir/src/expr/validation.rs index fd4907313..aebed6788 100644 --- a/crates/ra_hir/src/expr/validation.rs +++ b/crates/ra_hir/src/expr/validation.rs @@ -76,7 +76,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> { let source_file = db.parse(file_id.original_file(db)); if let Some(field_list_node) = source_map .expr_syntax(id) - .map(|ptr| ptr.to_node(&source_file)) + .map(|ptr| ptr.to_node(source_file.syntax())) .and_then(StructLit::cast) .and_then(|lit| lit.named_field_list()) { diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index b7dd775f1..51fa491c3 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs @@ -34,7 +34,7 @@ impl ImplSourceMap { ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), }; - self.map[impl_id].to_node(file).to_owned() + self.map[impl_id].to_node(file.syntax()).to_owned() } } diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs index 43c97a0bf..211e02068 100644 --- a/crates/ra_hir/src/nameres/raw.rs +++ b/crates/ra_hir/src/nameres/raw.rs @@ -39,7 +39,10 @@ type ImportSource = Either, TreeArc> impl ImportSourcePtr { fn to_node(self, file: &SourceFile) -> ImportSource { - self.map(|ptr| ptr.to_node(file).to_owned(), |ptr| ptr.to_node(file).to_owned()) + self.map( + |ptr| ptr.to_node(file.syntax()).to_owned(), + |ptr| ptr.to_node(file.syntax()).to_owned(), + ) } } diff --git a/crates/ra_hir/src/source_id.rs b/crates/ra_hir/src/source_id.rs index 0a8fb6d32..7a39be779 100644 --- a/crates/ra_hir/src/source_id.rs +++ b/crates/ra_hir/src/source_id.rs @@ -1,7 +1,7 @@ use std::{marker::PhantomData, sync::Arc, hash::{Hash, Hasher}}; use ra_arena::{Arena, RawId, impl_arena_id}; -use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast}; +use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, AstNode, ast}; use crate::{HirFileId, DefDatabase}; @@ -89,7 +89,7 @@ pub struct AstIdMap { impl AstIdMap { pub(crate) fn ast_id_map_query(db: &impl DefDatabase, file_id: HirFileId) -> Arc { let source_file = db.hir_parse(file_id); - Arc::new(AstIdMap::from_source_file(&source_file)) + Arc::new(AstIdMap::from_source(source_file.syntax())) } pub(crate) fn file_item_query( @@ -98,7 +98,7 @@ impl AstIdMap { ast_id: ErasedFileAstId, ) -> TreeArc { let source_file = db.hir_parse(file_id); - db.ast_id_map(file_id).arena[ast_id].to_node(&source_file).to_owned() + db.ast_id_map(file_id).arena[ast_id].to_node(source_file.syntax()).to_owned() } pub(crate) fn ast_id(&self, item: &N) -> FileAstId { @@ -115,13 +115,14 @@ impl AstIdMap { FileAstId { raw, _ty: PhantomData } } - fn from_source_file(source_file: &SourceFile) -> AstIdMap { + fn from_source(node: &SyntaxNode) -> AstIdMap { + assert!(node.parent().is_none()); let mut res = AstIdMap { arena: Arena::default() }; // By walking the tree in bread-first order we make sure that parents // get lower ids then children. That is, adding a new child does not // change parent's id. This means that, say, adding a new function to a // trait does not change ids of top-level items, which helps caching. - bfs(source_file.syntax(), |it| { + bfs(node, |it| { if let Some(module_item) = ast::ModuleItem::cast(it) { res.alloc(module_item.syntax()); } else if let Some(macro_call) = ast::MacroCall::cast(it) { diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 978cc2587..f8364203d 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -2715,7 +2715,7 @@ fn infer(content: &str) -> String { // sort ranges for consistency types.sort_by_key(|(ptr, _)| (ptr.range().start(), ptr.range().end())); for (syntax_ptr, ty) in &types { - let node = syntax_ptr.to_node(&source_file); + let node = syntax_ptr.to_node(source_file.syntax()); let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node) { (self_param.self_kw_token().range(), "self".to_string()) } else { diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index 855a3ff0f..e23d178b0 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -54,7 +54,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec let file_id = d.file().original_file(db); let source_file = db.parse(file_id); let syntax_node = d.syntax_node_ptr(); - let node = NamedFieldList::cast(syntax_node.to_node(&source_file)).unwrap(); + let node = NamedFieldList::cast(syntax_node.to_node(source_file.syntax())).unwrap(); let mut ast_editor = AstEditor::new(node); for f in d.missed_fields.iter() { ast_editor.append_field(&AstBuilder::::from_name(f)); @@ -281,7 +281,7 @@ mod tests { one: i32, two: i64, } - + fn test_fn() { let one = 1; let s = TestStruct{ one, two: 2 }; @@ -298,7 +298,7 @@ mod tests { one: i32, two: i64, } - + fn test_fn() { let one = 1; let s = TestStruct{ ..a }; diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index 765cf883b..7ea336c50 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -81,7 +81,7 @@ impl NavigationTarget { ) -> NavigationTarget { let file = db.parse(file_id); let (name, full_range) = match pat { - Either::A(pat) => match pat.to_node(&file).kind() { + Either::A(pat) => match pat.to_node(file.syntax()).kind() { ast::PatKind::BindPat(pat) => { return NavigationTarget::from_bind_pat(file_id, &pat) } diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs index 9f655d83c..d5c2b08ca 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs @@ -86,7 +86,7 @@ pub(crate) fn find_all_refs( let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None); let resolved = analyzer.resolve_local_name(name_ref)?; if let Either::A(ptr) = resolved.ptr() { - if let ast::PatKind::BindPat(binding) = ptr.to_node(source_file).kind() { + if let ast::PatKind::BindPat(binding) = ptr.to_node(source_file.syntax()).kind() { return Some((binding, analyzer)); } } diff --git a/crates/ra_syntax/src/ptr.rs b/crates/ra_syntax/src/ptr.rs index b0816b135..cee9503ca 100644 --- a/crates/ra_syntax/src/ptr.rs +++ b/crates/ra_syntax/src/ptr.rs @@ -3,7 +3,7 @@ use std::{ iter::successors, }; use crate::{ - AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange, + AstNode, SyntaxKind, SyntaxNode, TextRange, }; /// A pointer to a syntax node inside a file. It can be used to remember a @@ -19,8 +19,9 @@ impl SyntaxNodePtr { SyntaxNodePtr { range: node.range(), kind: node.kind() } } - pub fn to_node(self, source_file: &SourceFile) -> &SyntaxNode { - successors(Some(source_file.syntax()), |&node| { + pub fn to_node(self, root: &SyntaxNode) -> &SyntaxNode { + assert!(root.parent().is_none()); + successors(Some(root), |&node| { node.children().find(|it| self.range.is_subrange(&it.range())) }) .find(|it| it.range() == self.range && it.kind() == self.kind) @@ -55,8 +56,8 @@ impl AstPtr { AstPtr { raw: SyntaxNodePtr::new(node.syntax()), _ty: PhantomData } } - pub fn to_node(self, source_file: &SourceFile) -> &N { - let syntax_node = self.raw.to_node(source_file); + pub fn to_node(self, root: &SyntaxNode) -> &N { + let syntax_node = self.raw.to_node(root); N::cast(syntax_node).unwrap() } @@ -73,11 +74,11 @@ impl From> for SyntaxNodePtr { #[test] fn test_local_syntax_ptr() { - use crate::{ast, AstNode}; + use crate::{ast, AstNode, SourceFile}; let file = SourceFile::parse("struct Foo { f: u32, }"); let field = file.syntax().descendants().find_map(ast::NamedFieldDef::cast).unwrap(); let ptr = SyntaxNodePtr::new(field.syntax()); - let field_syntax = ptr.to_node(&file); + let field_syntax = ptr.to_node(file.syntax()); assert_eq!(field.syntax(), &*field_syntax); } diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index 92c15234e..80054f529 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs @@ -392,7 +392,7 @@ impl SyntaxNode { // `range` private afterwards let mut ptr = SyntaxNodePtr::new(self); ptr.range = TextRange::offset_len(ptr.range().start(), len); - return ptr.to_node(&file).to_owned(); + return ptr.to_node(file.syntax()).to_owned(); } fn position_of_child(&self, child: SyntaxElement) -> usize { -- cgit v1.2.3