Better strip turbofishes

This commit is contained in:
Kirill Bulatov 2021-03-08 14:59:54 +02:00
parent 5168ab16e1
commit 778deb38fe
5 changed files with 52 additions and 8 deletions

View File

@ -44,10 +44,6 @@ pub enum ImportAlias {
} }
impl ModPath { impl ModPath {
pub fn from_src_unhygienic(path: ast::Path) -> Option<ModPath> {
lower::lower_path(path, &Hygiene::new_unhygienic()).map(|it| it.mod_path)
}
pub fn from_src(path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> { pub fn from_src(path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> {
lower::lower_path(path, hygiene).map(|it| it.mod_path) lower::lower_path(path, hygiene).map(|it| it.mod_path)
} }

View File

@ -5,7 +5,7 @@ use hir::{
}; };
use itertools::Itertools; use itertools::Itertools;
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use syntax::{ast, AstNode, SyntaxNode}; use syntax::{ast, utils::path_to_string_stripping_turbo_fish, AstNode, SyntaxNode};
use crate::{ use crate::{
items_locator::{self, AssocItemSearch, DEFAULT_QUERY_SEARCH_LIMIT}, items_locator::{self, AssocItemSearch, DEFAULT_QUERY_SEARCH_LIMIT},
@ -57,7 +57,7 @@ pub struct PathImportCandidate {
#[derive(Debug)] #[derive(Debug)]
pub struct FirstSegmentUnresolved { pub struct FirstSegmentUnresolved {
fist_segment: ast::NameRef, fist_segment: ast::NameRef,
full_qualifier: ModPath, full_qualifier: ast::Path,
} }
/// A name that will be used during item lookups. /// A name that will be used during item lookups.
@ -310,7 +310,7 @@ fn path_applicable_imports(
} }
Some(first_segment_unresolved) => ( Some(first_segment_unresolved) => (
first_segment_unresolved.fist_segment.to_string(), first_segment_unresolved.fist_segment.to_string(),
first_segment_unresolved.full_qualifier.to_string(), path_to_string_stripping_turbo_fish(&first_segment_unresolved.full_qualifier),
), ),
}; };
@ -583,7 +583,7 @@ fn path_import_candidate(
ImportCandidate::Path(PathImportCandidate { ImportCandidate::Path(PathImportCandidate {
qualifier: Some(FirstSegmentUnresolved { qualifier: Some(FirstSegmentUnresolved {
fist_segment: qualifier_start, fist_segment: qualifier_start,
full_qualifier: ModPath::from_src_unhygienic(qualifier)?, full_qualifier: qualifier,
}), }),
name, name,
}) })

View File

@ -91,6 +91,10 @@ pub fn path_from_segments(
}) })
} }
pub fn path_from_text(text: &str) -> ast::Path {
ast_from_text(&format!("fn main() {{ let test = {}; }}", text))
}
pub fn glob_use_tree() -> ast::UseTree { pub fn glob_use_tree() -> ast::UseTree {
ast_from_text("use *;") ast_from_text("use *;")
} }

View File

@ -37,6 +37,7 @@ pub mod algo;
pub mod ast; pub mod ast;
#[doc(hidden)] #[doc(hidden)]
pub mod fuzz; pub mod fuzz;
pub mod utils;
use std::{marker::PhantomData, sync::Arc}; use std::{marker::PhantomData, sync::Arc};

View File

@ -0,0 +1,43 @@
//! A set of utils methods to reuse on other abstraction levels
use itertools::Itertools;
use crate::{ast, match_ast, AstNode};
pub fn path_to_string_stripping_turbo_fish(path: &ast::Path) -> String {
path.syntax()
.children()
.filter_map(|node| {
match_ast! {
match node {
ast::PathSegment(it) => {
Some(it.name_ref()?.to_string())
},
ast::Path(it) => {
Some(path_to_string_stripping_turbo_fish(&it))
},
_ => None,
}
}
})
.join("::")
}
#[cfg(test)]
mod tests {
use super::path_to_string_stripping_turbo_fish;
use crate::ast::make;
#[test]
fn turbofishes_are_stripped() {
assert_eq!("Vec", path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::<i32>")),);
assert_eq!(
"Vec::new",
path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::<i32>::new")),
);
assert_eq!(
"Vec::new",
path_to_string_stripping_turbo_fish(&make::path_from_text("Vec::new()")),
);
}
}