Skip to content

Commit 778deb3

Browse files
Better strip turbofishes
1 parent 5168ab1 commit 778deb3

File tree

5 files changed

+52
-8
lines changed

5 files changed

+52
-8
lines changed

crates/hir_def/src/path.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ pub enum ImportAlias {
4444
}
4545

4646
impl ModPath {
47-
pub fn from_src_unhygienic(path: ast::Path) -> Option<ModPath> {
48-
lower::lower_path(path, &Hygiene::new_unhygienic()).map(|it| it.mod_path)
49-
}
50-
5147
pub fn from_src(path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> {
5248
lower::lower_path(path, hygiene).map(|it| it.mod_path)
5349
}

crates/ide_db/src/helpers/import_assets.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use hir::{
55
};
66
use itertools::Itertools;
77
use rustc_hash::FxHashSet;
8-
use syntax::{ast, AstNode, SyntaxNode};
8+
use syntax::{ast, utils::path_to_string_stripping_turbo_fish, AstNode, SyntaxNode};
99

1010
use crate::{
1111
items_locator::{self, AssocItemSearch, DEFAULT_QUERY_SEARCH_LIMIT},
@@ -57,7 +57,7 @@ pub struct PathImportCandidate {
5757
#[derive(Debug)]
5858
pub struct FirstSegmentUnresolved {
5959
fist_segment: ast::NameRef,
60-
full_qualifier: ModPath,
60+
full_qualifier: ast::Path,
6161
}
6262

6363
/// A name that will be used during item lookups.
@@ -310,7 +310,7 @@ fn path_applicable_imports(
310310
}
311311
Some(first_segment_unresolved) => (
312312
first_segment_unresolved.fist_segment.to_string(),
313-
first_segment_unresolved.full_qualifier.to_string(),
313+
path_to_string_stripping_turbo_fish(&first_segment_unresolved.full_qualifier),
314314
),
315315
};
316316

@@ -583,7 +583,7 @@ fn path_import_candidate(
583583
ImportCandidate::Path(PathImportCandidate {
584584
qualifier: Some(FirstSegmentUnresolved {
585585
fist_segment: qualifier_start,
586-
full_qualifier: ModPath::from_src_unhygienic(qualifier)?,
586+
full_qualifier: qualifier,
587587
}),
588588
name,
589589
})

crates/syntax/src/ast/make.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ pub fn path_from_segments(
9191
})
9292
}
9393

94+
pub fn path_from_text(text: &str) -> ast::Path {
95+
ast_from_text(&format!("fn main() {{ let test = {}; }}", text))
96+
}
97+
9498
pub fn glob_use_tree() -> ast::UseTree {
9599
ast_from_text("use *;")
96100
}

crates/syntax/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub mod algo;
3737
pub mod ast;
3838
#[doc(hidden)]
3939
pub mod fuzz;
40+
pub mod utils;
4041

4142
use std::{marker::PhantomData, sync::Arc};
4243

crates/syntax/src/utils.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//! A set of utils methods to reuse on other abstraction levels
2+
3+
use itertools::Itertools;
4+
5+
use crate::{ast, match_ast, AstNode};
6+
7+
pub fn path_to_string_stripping_turbo_fish(path: &ast::Path) -> String {
8+
path.syntax()
9+
.children()
10+
.filter_map(|node| {
11+
match_ast! {
12+
match node {
13+
ast::PathSegment(it) => {
14+
Some(it.name_ref()?.to_string())
15+
},
16+
ast::Path(it) => {
17+
Some(path_to_string_stripping_turbo_fish(&it))
18+
},
19+
_ => None,
20+
}
21+
}
22+
})
23+
.join("::")
24+
}
25+
26+
#[cfg(test)]
27+
mod tests {
28+
use super::path_to_string_stripping_turbo_fish;
29+
use crate::ast::make;
30+
31+
#[test]
32+
fn turbofishes_are_stripped() {
33+
assert_eq!("Vec", path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::<i32>")),);
34+
assert_eq!(
35+
"Vec::new",
36+
path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::<i32>::new")),
37+
);
38+
assert_eq!(
39+
"Vec::new",
40+
path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::new()")),
41+
);
42+
}
43+
}

0 commit comments

Comments
 (0)