Simplifications / cleanup from review

This commit is contained in:
Florian Diebold 2019-06-16 12:04:08 +02:00
parent 30647cd195
commit 96c2b9c41d
7 changed files with 31 additions and 38 deletions

View File

@ -788,8 +788,7 @@ pub fn associated_type_by_name(self, db: &impl DefDatabase, name: Name) -> Optio
TraitItem::TypeAlias(t) => Some(*t),
_ => None,
})
.filter(|t| t.name(db) == name)
.next()
.find(|t| t.name(db) == name)
}
pub(crate) fn trait_data(self, db: &impl DefDatabase) -> Arc<TraitData> {

View File

@ -185,11 +185,11 @@ fn implements(
goal: crate::ty::Canonical<crate::ty::TraitRef>,
) -> Option<crate::ty::traits::Solution>;
#[salsa::invoke(crate::ty::traits::normalize)]
#[salsa::invoke(crate::ty::traits::normalize_query)]
fn normalize(
&self,
krate: Crate,
goal: crate::ty::Canonical<crate::ty::traits::ProjectionPredicate>,
goal: crate::ty::Canonical<crate::ty::ProjectionPredicate>,
) -> Option<crate::ty::traits::Solution>;
}

View File

@ -1,10 +1,11 @@
use std::sync::Arc;
use rustc_hash::FxHashMap;
use ra_syntax::{SmolStr, ast::AttrsOwner};
use ra_syntax::{SmolStr, TreeArc, ast::AttrsOwner};
use crate::{
Crate, DefDatabase, Enum, Function, HirDatabase, ImplBlock, Module, Static, Struct, Trait, ModuleDef, AstDatabase, HasSource
Crate, DefDatabase, Enum, Function, HirDatabase, ImplBlock, Module,
Static, Struct, Trait, ModuleDef, AstDatabase, HasSource
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -93,39 +94,15 @@ fn collect_lang_items_recursive(
}
}
// FIXME make this nicer
for def in module.declarations(db) {
match def {
ModuleDef::Trait(trait_) => {
let node = trait_.source(db).ast;
if let Some(lang_item_name) = lang_item_name(&*node) {
self.items.entry(lang_item_name).or_insert(LangItemTarget::Trait(trait_));
}
}
ModuleDef::Enum(e) => {
let node = e.source(db).ast;
if let Some(lang_item_name) = lang_item_name(&*node) {
self.items.entry(lang_item_name).or_insert(LangItemTarget::Enum(e));
}
}
ModuleDef::Struct(s) => {
let node = s.source(db).ast;
if let Some(lang_item_name) = lang_item_name(&*node) {
self.items.entry(lang_item_name).or_insert(LangItemTarget::Struct(s));
}
}
ModuleDef::Function(f) => {
let node = f.source(db).ast;
if let Some(lang_item_name) = lang_item_name(&*node) {
self.items.entry(lang_item_name).or_insert(LangItemTarget::Function(f));
}
}
ModuleDef::Static(s) => {
let node = s.source(db).ast;
if let Some(lang_item_name) = lang_item_name(&*node) {
self.items.entry(lang_item_name).or_insert(LangItemTarget::Static(s));
}
self.collect_lang_item(db, trait_, LangItemTarget::Trait)
}
ModuleDef::Enum(e) => self.collect_lang_item(db, e, LangItemTarget::Enum),
ModuleDef::Struct(s) => self.collect_lang_item(db, s, LangItemTarget::Struct),
ModuleDef::Function(f) => self.collect_lang_item(db, f, LangItemTarget::Function),
ModuleDef::Static(s) => self.collect_lang_item(db, s, LangItemTarget::Static),
_ => {}
}
}
@ -135,6 +112,21 @@ fn collect_lang_items_recursive(
self.collect_lang_items_recursive(db, &child);
}
}
fn collect_lang_item<T, N>(
&mut self,
db: &(impl DefDatabase + AstDatabase),
item: T,
constructor: fn(T) -> LangItemTarget,
) where
T: Copy + HasSource<Ast = TreeArc<N>>,
N: AttrsOwner,
{
let node = item.source(db).ast;
if let Some(lang_item_name) = lang_item_name(&*node) {
self.items.entry(lang_item_name).or_insert(constructor(item));
}
}
}
fn lang_item_name<T: AttrsOwner>(node: &T) -> Option<SmolStr> {

View File

@ -23,6 +23,7 @@
pub(crate) use infer::{infer_query, InferenceResult, InferTy};
pub use lower::CallableDef;
pub(crate) use autoderef::autoderef;
pub(crate) use traits::ProjectionPredicate;
/// A type constructor or type name: this might be something like the primitive
/// type `bool`, a struct like `Vec`, or things like function pointers or

View File

@ -10,12 +10,14 @@
use crate::{HirDatabase, Name, Resolver, HasGenericParams};
use super::{traits::Solution, Ty, Canonical};
const AUTODEREF_RECURSION_LIMIT: usize = 10;
pub(crate) fn autoderef<'a>(
db: &'a impl HirDatabase,
resolver: &'a Resolver,
ty: Canonical<Ty>,
) -> impl Iterator<Item = Canonical<Ty>> + 'a {
successors(Some(ty), move |ty| deref(db, resolver, ty))
successors(Some(ty), move |ty| deref(db, resolver, ty)).take(AUTODEREF_RECURSION_LIMIT)
}
pub(crate) fn deref(

View File

@ -2768,7 +2768,6 @@ fn test(s: Arc<S>) {
#[test]
fn deref_trait_with_inference_var() {
// std::env::set_var("RUST_BACKTRACE", "1");
let t = type_at(
r#"
//- /main.rs

View File

@ -105,7 +105,7 @@ pub(crate) fn implements_query(
solution.map(|solution| solution_from_chalk(db, solution))
}
pub(crate) fn normalize(
pub(crate) fn normalize_query(
db: &impl HirDatabase,
krate: Crate,
projection: Canonical<ProjectionPredicate>,