Use FamousDefs for shorten_iterator hint

This commit is contained in:
Lukas Wirth
2020-10-06 21:05:57 +02:00
parent aaa3905fdd
commit c6f1de6ac5
2 changed files with 85 additions and 69 deletions

View File

@@ -274,24 +274,54 @@ impl TryEnum {
/// somewhat similar to the known paths infra inside hir, but it different; We
/// want to make sure that IDE specific paths don't become interesting inside
/// the compiler itself as well.
pub(crate) struct FamousDefs<'a, 'b>(pub(crate) &'a Semantics<'b, RootDatabase>, pub(crate) Crate);
pub struct FamousDefs<'a, 'b>(pub &'a Semantics<'b, RootDatabase>, pub Crate);
#[allow(non_snake_case)]
impl FamousDefs<'_, '_> {
#[cfg(test)]
pub(crate) const FIXTURE: &'static str = r#"//- /libcore.rs crate:core
pub const FIXTURE: &'static str = r#"//- /libcore.rs crate:core
pub mod convert {
pub trait From<T> {
fn from(T) -> Self;
}
}
pub mod iter {
pub use self::traits::iterator::Iterator;
mod traits { mod iterator {
use crate::option::Option;
pub trait Iterator {
type Item;
fn next(&mut self) -> Option<Self::Item>;
}
} }
pub use self::sources::*;
mod sources {
use super::Iterator;
pub struct Repeat<A> {
element: A,
}
pub fn repeat<T: Clone>(elt: T) -> Repeat<T> {
Repeat { element: elt }
}
impl<A: Clone> Iterator for Repeat<A> {
type Item = A;
fn next(&mut self) -> Option<A> {
Some(self.element.clone())
}
}
}
}
pub mod option {
pub enum Option<T> { None, Some(T)}
}
pub mod prelude {
pub use crate::{convert::From, option::Option::{self, *}};
pub use crate::{convert::From, iter::Iterator, option::Option::{self, *}};
}
#[prelude_import]
pub use prelude::*;
@@ -305,6 +335,10 @@ pub use prelude::*;
self.find_enum("core:option:Option")
}
pub fn core_iter_Iterator(&self) -> Option<Trait> {
self.find_trait("core:iter:traits:iterator:Iterator")
}
fn find_trait(&self, path: &str) -> Option<Trait> {
match self.find_def(path)? {
hir::ScopeDef::ModuleDef(hir::ModuleDef::Trait(it)) => Some(it),
@@ -324,18 +358,21 @@ pub use prelude::*;
let mut path = path.split(':');
let trait_ = path.next_back()?;
let std_crate = path.next()?;
let std_crate = self
let std_crate = if self
.1
.dependencies(db)
.into_iter()
.find(|dep| &dep.name.to_string() == std_crate)?
.krate;
.declaration_name(db)
.map(|name| name.to_string() == std_crate)
.unwrap_or(false)
{
self.1
} else {
self.1.dependencies(db).into_iter().find(|dep| dep.name.to_string() == std_crate)?.krate
};
let mut module = std_crate.root_module(db);
for segment in path {
module = module.children(db).find_map(|child| {
let name = child.name(db)?;
if &name.to_string() == segment {
if name.to_string() == segment {
Some(child)
} else {
None
@@ -343,7 +380,7 @@ pub use prelude::*;
})?;
}
let def =
module.scope(db, None).into_iter().find(|(name, _def)| &name.to_string() == trait_)?.1;
module.scope(db, None).into_iter().find(|(name, _def)| name.to_string() == trait_)?.1;
Some(def)
}
}