Skip to content

Commit 08c2ee2

Browse files
committed
Simplifications / cleanup from review
1 parent 30647cd commit 08c2ee2

File tree

7 files changed

+31
-39
lines changed

7 files changed

+31
-39
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,8 +788,7 @@ impl Trait {
788788
TraitItem::TypeAlias(t) => Some(*t),
789789
_ => None,
790790
})
791-
.filter(|t| t.name(db) == name)
792-
.next()
791+
.find(|t| t.name(db) == name)
793792
}
794793

795794
pub(crate) fn trait_data(self, db: &impl DefDatabase) -> Arc<TraitData> {

crates/ra_hir/src/db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,11 @@ pub trait HirDatabase: DefDatabase + AstDatabase {
185185
goal: crate::ty::Canonical<crate::ty::TraitRef>,
186186
) -> Option<crate::ty::traits::Solution>;
187187

188-
#[salsa::invoke(crate::ty::traits::normalize)]
188+
#[salsa::invoke(crate::ty::traits::normalize_query)]
189189
fn normalize(
190190
&self,
191191
krate: Crate,
192-
goal: crate::ty::Canonical<crate::ty::traits::ProjectionPredicate>,
192+
goal: crate::ty::Canonical<crate::ty::ProjectionPredicate>,
193193
) -> Option<crate::ty::traits::Solution>;
194194
}
195195

crates/ra_hir/src/lang_item.rs

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::sync::Arc;
22
use rustc_hash::FxHashMap;
33

4-
use ra_syntax::{SmolStr, ast::AttrsOwner};
4+
use ra_syntax::{SmolStr, TreeArc, ast::AttrsOwner};
55

66
use crate::{
7-
Crate, DefDatabase, Enum, Function, HirDatabase, ImplBlock, Module, Static, Struct, Trait, ModuleDef, AstDatabase, HasSource
7+
Crate, DefDatabase, Enum, Function, HirDatabase, ImplBlock, Module,
8+
Static, Struct, Trait, ModuleDef, AstDatabase, HasSource
89
};
910

1011
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -93,39 +94,13 @@ impl LangItems {
9394
}
9495
}
9596

96-
// FIXME make this nicer
9797
for def in module.declarations(db) {
9898
match def {
99-
ModuleDef::Trait(trait_) => {
100-
let node = trait_.source(db).ast;
101-
if let Some(lang_item_name) = lang_item_name(&*node) {
102-
self.items.entry(lang_item_name).or_insert(LangItemTarget::Trait(trait_));
103-
}
104-
}
105-
ModuleDef::Enum(e) => {
106-
let node = e.source(db).ast;
107-
if let Some(lang_item_name) = lang_item_name(&*node) {
108-
self.items.entry(lang_item_name).or_insert(LangItemTarget::Enum(e));
109-
}
110-
}
111-
ModuleDef::Struct(s) => {
112-
let node = s.source(db).ast;
113-
if let Some(lang_item_name) = lang_item_name(&*node) {
114-
self.items.entry(lang_item_name).or_insert(LangItemTarget::Struct(s));
115-
}
116-
}
117-
ModuleDef::Function(f) => {
118-
let node = f.source(db).ast;
119-
if let Some(lang_item_name) = lang_item_name(&*node) {
120-
self.items.entry(lang_item_name).or_insert(LangItemTarget::Function(f));
121-
}
122-
}
123-
ModuleDef::Static(s) => {
124-
let node = s.source(db).ast;
125-
if let Some(lang_item_name) = lang_item_name(&*node) {
126-
self.items.entry(lang_item_name).or_insert(LangItemTarget::Static(s));
127-
}
128-
}
99+
ModuleDef::Trait(trait_) => self.collect_lang_item(db, trait_, LangItemTarget::Trait),
100+
ModuleDef::Enum(e) => self.collect_lang_item(db, e, LangItemTarget::Enum),
101+
ModuleDef::Struct(s) => self.collect_lang_item(db, s, LangItemTarget::Struct),
102+
ModuleDef::Function(f) => self.collect_lang_item(db, f, LangItemTarget::Function),
103+
ModuleDef::Static(s) => self.collect_lang_item(db, s, LangItemTarget::Static),
129104
_ => {}
130105
}
131106
}
@@ -135,6 +110,21 @@ impl LangItems {
135110
self.collect_lang_items_recursive(db, &child);
136111
}
137112
}
113+
114+
fn collect_lang_item<T, N>(
115+
&mut self,
116+
db: &(impl DefDatabase + AstDatabase),
117+
item: T,
118+
constructor: fn(T) -> LangItemTarget,
119+
) where
120+
T: Copy + HasSource<Ast = TreeArc<N>>,
121+
N: AttrsOwner,
122+
{
123+
let node = item.source(db).ast;
124+
if let Some(lang_item_name) = lang_item_name(&*node) {
125+
self.items.entry(lang_item_name).or_insert(constructor(item));
126+
}
127+
}
138128
}
139129

140130
fn lang_item_name<T: AttrsOwner>(node: &T) -> Option<SmolStr> {

crates/ra_hir/src/ty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub(crate) use lower::{TypableDef, type_for_def, type_for_field, callable_item_s
2323
pub(crate) use infer::{infer_query, InferenceResult, InferTy};
2424
pub use lower::CallableDef;
2525
pub(crate) use autoderef::autoderef;
26+
pub(crate) use traits::ProjectionPredicate;
2627

2728
/// A type constructor or type name: this might be something like the primitive
2829
/// type `bool`, a struct like `Vec`, or things like function pointers or

crates/ra_hir/src/ty/autoderef.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ use log::{info, warn};
1010
use crate::{HirDatabase, Name, Resolver, HasGenericParams};
1111
use super::{traits::Solution, Ty, Canonical};
1212

13+
const AUTODEREF_RECURSION_LIMIT: usize = 10;
14+
1315
pub(crate) fn autoderef<'a>(
1416
db: &'a impl HirDatabase,
1517
resolver: &'a Resolver,
1618
ty: Canonical<Ty>,
1719
) -> impl Iterator<Item = Canonical<Ty>> + 'a {
1820
successors(Some(ty), move |ty| deref(db, resolver, ty))
21+
.take(AUTODEREF_RECURSION_LIMIT)
1922
}
2023

2124
pub(crate) fn deref(

crates/ra_hir/src/ty/tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2768,7 +2768,6 @@ fn test(s: Arc<S>) {
27682768

27692769
#[test]
27702770
fn deref_trait_with_inference_var() {
2771-
// std::env::set_var("RUST_BACKTRACE", "1");
27722771
let t = type_at(
27732772
r#"
27742773
//- /main.rs

crates/ra_hir/src/ty/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub(crate) fn implements_query(
105105
solution.map(|solution| solution_from_chalk(db, solution))
106106
}
107107

108-
pub(crate) fn normalize(
108+
pub(crate) fn normalize_query(
109109
db: &impl HirDatabase,
110110
krate: Crate,
111111
projection: Canonical<ProjectionPredicate>,

0 commit comments

Comments
 (0)