Skip to content

Commit bebc5c7

Browse files
bors[bot]matklad
andcommitted
Merge #1271
1271: make AstId untyped r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents d7a4ae4 + 549728b commit bebc5c7

File tree

11 files changed

+29
-24
lines changed

11 files changed

+29
-24
lines changed

crates/ra_hir/src/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{fmt, any::Any};
22

3-
use ra_syntax::{SyntaxNodePtr, TreeArc, AstPtr, TextRange, ast, SyntaxNode};
3+
use ra_syntax::{SyntaxNodePtr, TreeArc, AstPtr, TextRange, ast, SyntaxNode, AstNode};
44
use relative_path::RelativePathBuf;
55

66
use crate::{HirFileId, HirDatabase, Name};
@@ -30,7 +30,7 @@ pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
3030
impl dyn Diagnostic {
3131
pub fn syntax_node(&self, db: &impl HirDatabase) -> TreeArc<SyntaxNode> {
3232
let source_file = db.hir_parse(self.file());
33-
self.syntax_node_ptr().to_node(&source_file).to_owned()
33+
self.syntax_node_ptr().to_node(source_file.syntax()).to_owned()
3434
}
3535
pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> {
3636
self.as_any().downcast_ref()

crates/ra_hir/src/expr/validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
7676
let source_file = db.parse(file_id.original_file(db));
7777
if let Some(field_list_node) = source_map
7878
.expr_syntax(id)
79-
.map(|ptr| ptr.to_node(&source_file))
79+
.map(|ptr| ptr.to_node(source_file.syntax()))
8080
.and_then(StructLit::cast)
8181
.and_then(|lit| lit.named_field_list())
8282
{

crates/ra_hir/src/impl_block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl ImplSourceMap {
3434
ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(),
3535
};
3636

37-
self.map[impl_id].to_node(file).to_owned()
37+
self.map[impl_id].to_node(file.syntax()).to_owned()
3838
}
3939
}
4040

crates/ra_hir/src/nameres/raw.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ type ImportSource = Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>>
3939

4040
impl ImportSourcePtr {
4141
fn to_node(self, file: &SourceFile) -> ImportSource {
42-
self.map(|ptr| ptr.to_node(file).to_owned(), |ptr| ptr.to_node(file).to_owned())
42+
self.map(
43+
|ptr| ptr.to_node(file.syntax()).to_owned(),
44+
|ptr| ptr.to_node(file.syntax()).to_owned(),
45+
)
4346
}
4447
}
4548

crates/ra_hir/src/source_id.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{marker::PhantomData, sync::Arc, hash::{Hash, Hasher}};
22

33
use ra_arena::{Arena, RawId, impl_arena_id};
4-
use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, SourceFile, AstNode, ast};
4+
use ra_syntax::{SyntaxNodePtr, TreeArc, SyntaxNode, AstNode, ast};
55

66
use crate::{HirFileId, DefDatabase};
77

@@ -89,7 +89,7 @@ pub struct AstIdMap {
8989
impl AstIdMap {
9090
pub(crate) fn ast_id_map_query(db: &impl DefDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
9191
let source_file = db.hir_parse(file_id);
92-
Arc::new(AstIdMap::from_source_file(&source_file))
92+
Arc::new(AstIdMap::from_source(source_file.syntax()))
9393
}
9494

9595
pub(crate) fn file_item_query(
@@ -98,7 +98,7 @@ impl AstIdMap {
9898
ast_id: ErasedFileAstId,
9999
) -> TreeArc<SyntaxNode> {
100100
let source_file = db.hir_parse(file_id);
101-
db.ast_id_map(file_id).arena[ast_id].to_node(&source_file).to_owned()
101+
db.ast_id_map(file_id).arena[ast_id].to_node(source_file.syntax()).to_owned()
102102
}
103103

104104
pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> {
@@ -115,13 +115,14 @@ impl AstIdMap {
115115
FileAstId { raw, _ty: PhantomData }
116116
}
117117

118-
fn from_source_file(source_file: &SourceFile) -> AstIdMap {
118+
fn from_source(node: &SyntaxNode) -> AstIdMap {
119+
assert!(node.parent().is_none());
119120
let mut res = AstIdMap { arena: Arena::default() };
120121
// By walking the tree in bread-first order we make sure that parents
121122
// get lower ids then children. That is, adding a new child does not
122123
// change parent's id. This means that, say, adding a new function to a
123124
// trait does not change ids of top-level items, which helps caching.
124-
bfs(source_file.syntax(), |it| {
125+
bfs(node, |it| {
125126
if let Some(module_item) = ast::ModuleItem::cast(it) {
126127
res.alloc(module_item.syntax());
127128
} else if let Some(macro_call) = ast::MacroCall::cast(it) {

crates/ra_hir/src/ty/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2715,7 +2715,7 @@ fn infer(content: &str) -> String {
27152715
// sort ranges for consistency
27162716
types.sort_by_key(|(ptr, _)| (ptr.range().start(), ptr.range().end()));
27172717
for (syntax_ptr, ty) in &types {
2718-
let node = syntax_ptr.to_node(&source_file);
2718+
let node = syntax_ptr.to_node(source_file.syntax());
27192719
let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node) {
27202720
(self_param.self_kw_token().range(), "self".to_string())
27212721
} else {

crates/ra_ide_api/src/diagnostics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
5454
let file_id = d.file().original_file(db);
5555
let source_file = db.parse(file_id);
5656
let syntax_node = d.syntax_node_ptr();
57-
let node = NamedFieldList::cast(syntax_node.to_node(&source_file)).unwrap();
57+
let node = NamedFieldList::cast(syntax_node.to_node(source_file.syntax())).unwrap();
5858
let mut ast_editor = AstEditor::new(node);
5959
for f in d.missed_fields.iter() {
6060
ast_editor.append_field(&AstBuilder::<NamedField>::from_name(f));
@@ -281,7 +281,7 @@ mod tests {
281281
one: i32,
282282
two: i64,
283283
}
284-
284+
285285
fn test_fn() {
286286
let one = 1;
287287
let s = TestStruct{ one, two: 2 };
@@ -298,7 +298,7 @@ mod tests {
298298
one: i32,
299299
two: i64,
300300
}
301-
301+
302302
fn test_fn() {
303303
let one = 1;
304304
let s = TestStruct{ ..a };

crates/ra_ide_api/src/display/navigation_target.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl NavigationTarget {
8181
) -> NavigationTarget {
8282
let file = db.parse(file_id);
8383
let (name, full_range) = match pat {
84-
Either::A(pat) => match pat.to_node(&file).kind() {
84+
Either::A(pat) => match pat.to_node(file.syntax()).kind() {
8585
ast::PatKind::BindPat(pat) => {
8686
return NavigationTarget::from_bind_pat(file_id, &pat)
8787
}

crates/ra_ide_api/src/references.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub(crate) fn find_all_refs(
8686
let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None);
8787
let resolved = analyzer.resolve_local_name(name_ref)?;
8888
if let Either::A(ptr) = resolved.ptr() {
89-
if let ast::PatKind::BindPat(binding) = ptr.to_node(source_file).kind() {
89+
if let ast::PatKind::BindPat(binding) = ptr.to_node(source_file.syntax()).kind() {
9090
return Some((binding, analyzer));
9191
}
9292
}

crates/ra_syntax/src/ptr.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
iter::successors,
44
};
55
use crate::{
6-
AstNode, SourceFile, SyntaxKind, SyntaxNode, TextRange,
6+
AstNode, SyntaxKind, SyntaxNode, TextRange,
77
};
88

99
/// A pointer to a syntax node inside a file. It can be used to remember a
@@ -19,8 +19,9 @@ impl SyntaxNodePtr {
1919
SyntaxNodePtr { range: node.range(), kind: node.kind() }
2020
}
2121

22-
pub fn to_node(self, source_file: &SourceFile) -> &SyntaxNode {
23-
successors(Some(source_file.syntax()), |&node| {
22+
pub fn to_node(self, root: &SyntaxNode) -> &SyntaxNode {
23+
assert!(root.parent().is_none());
24+
successors(Some(root), |&node| {
2425
node.children().find(|it| self.range.is_subrange(&it.range()))
2526
})
2627
.find(|it| it.range() == self.range && it.kind() == self.kind)
@@ -55,8 +56,8 @@ impl<N: AstNode> AstPtr<N> {
5556
AstPtr { raw: SyntaxNodePtr::new(node.syntax()), _ty: PhantomData }
5657
}
5758

58-
pub fn to_node(self, source_file: &SourceFile) -> &N {
59-
let syntax_node = self.raw.to_node(source_file);
59+
pub fn to_node(self, root: &SyntaxNode) -> &N {
60+
let syntax_node = self.raw.to_node(root);
6061
N::cast(syntax_node).unwrap()
6162
}
6263

@@ -73,11 +74,11 @@ impl<N: AstNode> From<AstPtr<N>> for SyntaxNodePtr {
7374

7475
#[test]
7576
fn test_local_syntax_ptr() {
76-
use crate::{ast, AstNode};
77+
use crate::{ast, AstNode, SourceFile};
7778

7879
let file = SourceFile::parse("struct Foo { f: u32, }");
7980
let field = file.syntax().descendants().find_map(ast::NamedFieldDef::cast).unwrap();
8081
let ptr = SyntaxNodePtr::new(field.syntax());
81-
let field_syntax = ptr.to_node(&file);
82+
let field_syntax = ptr.to_node(file.syntax());
8283
assert_eq!(field.syntax(), &*field_syntax);
8384
}

crates/ra_syntax/src/syntax_node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ impl SyntaxNode {
392392
// `range` private afterwards
393393
let mut ptr = SyntaxNodePtr::new(self);
394394
ptr.range = TextRange::offset_len(ptr.range().start(), len);
395-
return ptr.to_node(&file).to_owned();
395+
return ptr.to_node(file.syntax()).to_owned();
396396
}
397397

398398
fn position_of_child(&self, child: SyntaxElement) -> usize {

0 commit comments

Comments
 (0)