Skip to content

Commit 189ac4a

Browse files
bors[bot]matklad
andauthored
Merge #4108
4108: Fully get rid of SyntaxNodePtr::range r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents d283f87 + 27dd008 commit 189ac4a

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

crates/ra_hir_ty/src/tests.rs

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@ use hir_def::{
1818
nameres::CrateDefMap,
1919
AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId,
2020
};
21-
use hir_expand::InFile;
21+
use hir_expand::{db::AstDatabase, InFile};
2222
use insta::assert_snapshot;
2323
use ra_db::{fixture::WithFixture, salsa::Database, FilePosition, SourceDatabase};
2424
use ra_syntax::{
2525
algo,
2626
ast::{self, AstNode},
27+
SyntaxNode,
2728
};
2829
use stdx::format_to;
2930

30-
use crate::{db::HirDatabase, display::HirDisplay, test_db::TestDB, InferenceResult};
31+
use crate::{
32+
db::HirDatabase, display::HirDisplay, infer::TypeMismatch, test_db::TestDB, InferenceResult, Ty,
33+
};
3134

3235
// These tests compare the inference results for all expressions in a file
3336
// against snapshots of the expected results using insta. Use cargo-insta to
@@ -67,43 +70,51 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
6770

6871
let mut infer_def = |inference_result: Arc<InferenceResult>,
6972
body_source_map: Arc<BodySourceMap>| {
70-
let mut types = Vec::new();
71-
let mut mismatches = Vec::new();
73+
let mut types: Vec<(InFile<SyntaxNode>, &Ty)> = Vec::new();
74+
let mut mismatches: Vec<(InFile<SyntaxNode>, &TypeMismatch)> = Vec::new();
7275

7376
for (pat, ty) in inference_result.type_of_pat.iter() {
7477
let syntax_ptr = match body_source_map.pat_syntax(pat) {
7578
Ok(sp) => {
76-
sp.map(|ast| ast.either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr()))
79+
let root = db.parse_or_expand(sp.file_id).unwrap();
80+
sp.map(|ptr| {
81+
ptr.either(
82+
|it| it.to_node(&root).syntax().clone(),
83+
|it| it.to_node(&root).syntax().clone(),
84+
)
85+
})
7786
}
7887
Err(SyntheticSyntax) => continue,
7988
};
8089
types.push((syntax_ptr, ty));
8190
}
8291

8392
for (expr, ty) in inference_result.type_of_expr.iter() {
84-
let syntax_ptr = match body_source_map.expr_syntax(expr) {
85-
Ok(sp) => sp.map(|ast| ast.syntax_node_ptr()),
93+
let node = match body_source_map.expr_syntax(expr) {
94+
Ok(sp) => {
95+
let root = db.parse_or_expand(sp.file_id).unwrap();
96+
sp.map(|ptr| ptr.to_node(&root).syntax().clone())
97+
}
8698
Err(SyntheticSyntax) => continue,
8799
};
88-
types.push((syntax_ptr.clone(), ty));
100+
types.push((node.clone(), ty));
89101
if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr) {
90-
mismatches.push((syntax_ptr, mismatch));
102+
mismatches.push((node, mismatch));
91103
}
92104
}
93105

94106
// sort ranges for consistency
95-
types.sort_by_key(|(src_ptr, _)| {
96-
(src_ptr.value.range().start(), src_ptr.value.range().end())
107+
types.sort_by_key(|(node, _)| {
108+
let range = node.value.text_range();
109+
(range.start(), range.end())
97110
});
98-
for (src_ptr, ty) in &types {
99-
let node = src_ptr.value.to_node(&src_ptr.file_syntax(&db));
100-
101-
let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.clone()) {
111+
for (node, ty) in &types {
112+
let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.value.clone()) {
102113
(self_param.self_token().unwrap().text_range(), "self".to_string())
103114
} else {
104-
(src_ptr.value.range(), node.text().to_string().replace("\n", " "))
115+
(node.value.text_range(), node.value.text().to_string().replace("\n", " "))
105116
};
106-
let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" };
117+
let macro_prefix = if node.file_id != file_id.into() { "!" } else { "" };
107118
format_to!(
108119
buf,
109120
"{}{} '{}': {}\n",
@@ -114,11 +125,12 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
114125
);
115126
}
116127
if include_mismatches {
117-
mismatches.sort_by_key(|(src_ptr, _)| {
118-
(src_ptr.value.range().start(), src_ptr.value.range().end())
128+
mismatches.sort_by_key(|(node, _)| {
129+
let range = node.value.text_range();
130+
(range.start(), range.end())
119131
});
120132
for (src_ptr, mismatch) in &mismatches {
121-
let range = src_ptr.value.range();
133+
let range = src_ptr.value.text_range();
122134
let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" };
123135
format_to!(
124136
buf,

crates/ra_syntax/src/ptr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ impl SyntaxNodePtr {
3030
.unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self))
3131
}
3232

33-
pub fn range(&self) -> TextRange {
34-
self.range
35-
}
33+
// pub fn range(&self) -> TextRange {
34+
// self.range
35+
// }
3636

3737
pub fn cast<N: AstNode>(self) -> Option<AstPtr<N>> {
3838
if !N::can_cast(self.kind) {

crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,13 @@ pub fn analysis_stats(
162162
let (_, sm) = db.body_with_source_map(f_id.into());
163163
let src = sm.expr_syntax(expr_id);
164164
if let Ok(src) = src {
165+
let node = {
166+
let root = db.parse_or_expand(src.file_id).unwrap();
167+
src.value.to_node(&root)
168+
};
165169
let original_file = src.file_id.original_file(db);
166170
let line_index = host.analysis().file_line_index(original_file).unwrap();
167-
let text_range = src.value.syntax_node_ptr().range();
171+
let text_range = node.syntax().text_range();
168172
let (start, end) = (
169173
line_index.line_col(text_range.start()),
170174
line_index.line_col(text_range.end()),

0 commit comments

Comments
 (0)