Skip to content

Commit 7d088a1

Browse files
bors[bot]matklad
andauthored
Merge #2421
2421: Cleanup r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents d770f22 + bed6869 commit 7d088a1

File tree

9 files changed

+118
-113
lines changed

9 files changed

+118
-113
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ use crate::{
2828
expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId},
2929
ty::display::HirFormatter,
3030
ty::{
31-
self, InEnvironment, InferenceResult, TraitEnvironment, TraitRef, Ty, TypeCtor, TypeWalk,
31+
self, InEnvironment, InferenceResult, TraitEnvironment, TraitRef, Ty, TyDefId, TypeCtor,
32+
TypeWalk,
3233
},
3334
CallableDef, Either, HirDisplay, Name, Source,
3435
};
@@ -498,12 +499,9 @@ impl Adt {
498499
let subst = db.generic_defaults(self.into());
499500
subst.iter().any(|ty| ty == &Ty::Unknown)
500501
}
501-
pub fn ty(self, db: &impl HirDatabase) -> Ty {
502-
match self {
503-
Adt::Struct(it) => it.ty(db),
504-
Adt::Union(it) => it.ty(db),
505-
Adt::Enum(it) => it.ty(db),
506-
}
502+
pub fn ty(self, db: &impl HirDatabase) -> Type {
503+
let id = AdtId::from(self);
504+
Type::from_def(db, id.module(db).krate, id)
507505
}
508506

509507
pub fn module(self, db: &impl DefDatabase) -> Module {
@@ -795,8 +793,8 @@ impl TypeAlias {
795793
db.type_alias_data(self.id).type_ref.clone()
796794
}
797795

798-
pub fn ty(self, db: &impl HirDatabase) -> Ty {
799-
db.ty(self.id.into())
796+
pub fn ty(self, db: &impl HirDatabase) -> Type {
797+
Type::from_def(db, self.id.lookup(db).module(db).krate, self.id)
800798
}
801799

802800
pub fn name(self, db: &impl DefDatabase) -> Name {
@@ -945,7 +943,7 @@ impl ImplBlock {
945943
}
946944
pub fn for_trait(db: &impl HirDatabase, krate: Crate, trait_: Trait) -> Vec<ImplBlock> {
947945
let impls = db.impls_in_crate(krate.crate_id);
948-
impls.lookup_impl_blocks_for_trait(trait_).map(Self::from).collect()
946+
impls.lookup_impl_blocks_for_trait(trait_.id).map(Self::from).collect()
949947
}
950948

951949
pub fn target_trait(&self, db: &impl DefDatabase) -> Option<TypeRef> {
@@ -989,6 +987,17 @@ pub struct Type {
989987
}
990988

991989
impl Type {
990+
fn from_def(
991+
db: &impl HirDatabase,
992+
krate: CrateId,
993+
def: impl HasResolver + Into<TyDefId>,
994+
) -> Type {
995+
let resolver = def.resolver(db);
996+
let environment = TraitEnvironment::lower(db, &resolver);
997+
let ty = db.ty(def.into());
998+
Type { krate, ty: InEnvironment { value: ty, environment } }
999+
}
1000+
9921001
pub fn is_bool(&self) -> bool {
9931002
match &self.ty.value {
9941003
Ty::Apply(a_ty) => match a_ty.ctor {
@@ -1097,6 +1106,28 @@ impl Type {
10971106
.map(move |ty| self.derived(ty))
10981107
}
10991108

1109+
// This would be nicer if it just returned an iterator, but that runs into
1110+
// lifetime problems, because we need to borrow temp `CrateImplBlocks`.
1111+
pub fn iterate_impl_items<T>(
1112+
self,
1113+
db: &impl HirDatabase,
1114+
krate: Crate,
1115+
mut callback: impl FnMut(AssocItem) -> Option<T>,
1116+
) -> Option<T> {
1117+
for krate in self.ty.value.def_crates(db, krate.crate_id)? {
1118+
let impls = db.impls_in_crate(krate);
1119+
1120+
for impl_block in impls.lookup_impl_blocks(&self.ty.value) {
1121+
for &item in db.impl_data(impl_block).items.iter() {
1122+
if let Some(result) = callback(item.into()) {
1123+
return Some(result);
1124+
}
1125+
}
1126+
}
1127+
}
1128+
None
1129+
}
1130+
11001131
// FIXME: remove
11011132
pub fn into_ty(self) -> Ty {
11021133
self.ty.value

crates/ra_hir/src/db.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::sync::Arc;
44

5+
use hir_def::{GenericDefId, LocalStructFieldId, TraitId, VariantId};
56
use ra_arena::map::ArenaMap;
67
use ra_db::{salsa, CrateId};
78

@@ -12,19 +13,15 @@ use crate::{
1213
CallableDef, FnSig, GenericPredicate, InferenceResult, Substs, Ty, TyDefId, TypeCtor,
1314
ValueTyDefId,
1415
},
15-
Crate, DefWithBody, ImplBlock, Trait,
16+
Crate, DefWithBody, ImplBlock,
1617
};
1718

18-
pub use hir_def::{
19-
db::{
20-
BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, CrateDefMapQuery, CrateLangItemsQuery,
21-
DefDatabase, DefDatabaseStorage, DocumentationQuery, EnumDataQuery, ExprScopesQuery,
22-
FunctionDataQuery, GenericParamsQuery, ImplDataQuery, InternDatabase,
23-
InternDatabaseStorage, LangItemQuery, ModuleLangItemsQuery, RawItemsQuery,
24-
RawItemsWithSourceMapQuery, StaticDataQuery, StructDataQuery, TraitDataQuery,
25-
TypeAliasDataQuery,
26-
},
27-
GenericDefId, LocalStructFieldId, VariantId,
19+
pub use hir_def::db::{
20+
BodyQuery, BodyWithSourceMapQuery, ConstDataQuery, CrateDefMapQuery, CrateLangItemsQuery,
21+
DefDatabase, DefDatabaseStorage, DocumentationQuery, EnumDataQuery, ExprScopesQuery,
22+
FunctionDataQuery, GenericParamsQuery, ImplDataQuery, InternDatabase, InternDatabaseStorage,
23+
LangItemQuery, ModuleLangItemsQuery, RawItemsQuery, RawItemsWithSourceMapQuery,
24+
StaticDataQuery, StructDataQuery, TraitDataQuery, TypeAliasDataQuery,
2825
};
2926
pub use hir_expand::db::{
3027
AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery,
@@ -66,7 +63,7 @@ pub trait HirDatabase: DefDatabase {
6663
fn impls_in_crate(&self, krate: CrateId) -> Arc<CrateImplBlocks>;
6764

6865
#[salsa::invoke(crate::ty::traits::impls_for_trait_query)]
69-
fn impls_for_trait(&self, krate: Crate, trait_: Trait) -> Arc<[ImplBlock]>;
66+
fn impls_for_trait(&self, krate: CrateId, trait_: TraitId) -> Arc<[ImplBlock]>;
7067

7168
/// This provides the Chalk trait solver instance. Because Chalk always
7269
/// works from a specific crate, this query is keyed on the crate; and

crates/ra_hir/src/source_binder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,14 +389,14 @@ impl SourceAnalyzer {
389389
pub fn iterate_path_candidates<T>(
390390
&self,
391391
db: &impl HirDatabase,
392-
ty: Ty,
392+
ty: &Type,
393393
name: Option<&Name>,
394394
callback: impl FnMut(&Ty, AssocItem) -> Option<T>,
395395
) -> Option<T> {
396396
// There should be no inference vars in types passed here
397397
// FIXME check that?
398398
// FIXME replace Unknown by bound vars here
399-
let canonical = crate::ty::Canonical { value: ty, num_vars: 0 };
399+
let canonical = crate::ty::Canonical { value: ty.ty.value.clone(), num_vars: 0 };
400400
method_resolution::iterate_method_candidates(
401401
&canonical,
402402
db,

crates/ra_hir/src/ty/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl Ty {
189189
Ty::Param { idx, name }
190190
}
191191
TypeNs::SelfType(impl_block) => ImplBlock::from(impl_block).target_ty(db),
192-
TypeNs::AdtSelfType(adt) => Adt::from(adt).ty(db),
192+
TypeNs::AdtSelfType(adt) => db.ty(adt.into()),
193193

194194
TypeNs::AdtId(it) => Ty::from_hir_path_inner(db, resolver, resolved_segment, it.into()),
195195
TypeNs::BuiltinType(it) => {

crates/ra_hir/src/ty/method_resolution.rs

Lines changed: 53 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use std::sync::Arc;
66

77
use arrayvec::ArrayVec;
88
use hir_def::{
9-
lang_item::LangItemTarget, resolver::HasResolver, resolver::Resolver, AssocItemId, AstItemDef,
10-
HasModule, ImplId, TraitId,
9+
lang_item::LangItemTarget, resolver::HasResolver, resolver::Resolver, type_ref::Mutability,
10+
AssocItemId, AstItemDef, HasModule, ImplId, TraitId,
1111
};
12+
use hir_expand::name::Name;
1213
use ra_db::CrateId;
1314
use ra_prof::profile;
1415
use rustc_hash::FxHashMap;
@@ -17,7 +18,7 @@ use crate::{
1718
db::HirDatabase,
1819
ty::primitive::{FloatBitness, Uncertain},
1920
ty::{utils::all_super_traits, Ty, TypeCtor},
20-
AssocItem, Crate, Function, Mutability, Name, Trait,
21+
AssocItem, Function,
2122
};
2223

2324
use super::{autoderef, Canonical, InEnvironment, TraitEnvironment, TraitRef};
@@ -87,60 +88,65 @@ impl CrateImplBlocks {
8788
fingerprint.and_then(|f| self.impls.get(&f)).into_iter().flatten().copied()
8889
}
8990

90-
pub fn lookup_impl_blocks_for_trait(&self, tr: Trait) -> impl Iterator<Item = ImplId> + '_ {
91-
self.impls_by_trait.get(&tr.id).into_iter().flatten().copied()
91+
pub fn lookup_impl_blocks_for_trait(&self, tr: TraitId) -> impl Iterator<Item = ImplId> + '_ {
92+
self.impls_by_trait.get(&tr).into_iter().flatten().copied()
9293
}
9394

9495
pub fn all_impls<'a>(&'a self) -> impl Iterator<Item = ImplId> + 'a {
9596
self.impls.values().chain(self.impls_by_trait.values()).flatten().copied()
9697
}
9798
}
9899

99-
fn def_crates(db: &impl HirDatabase, cur_crate: Crate, ty: &Ty) -> Option<ArrayVec<[Crate; 2]>> {
100-
// Types like slice can have inherent impls in several crates, (core and alloc).
101-
// The corresponding impls are marked with lang items, so we can use them to find the required crates.
102-
macro_rules! lang_item_crate {
100+
impl Ty {
101+
pub(crate) fn def_crates(
102+
&self,
103+
db: &impl HirDatabase,
104+
cur_crate: CrateId,
105+
) -> Option<ArrayVec<[CrateId; 2]>> {
106+
// Types like slice can have inherent impls in several crates, (core and alloc).
107+
// The corresponding impls are marked with lang items, so we can use them to find the required crates.
108+
macro_rules! lang_item_crate {
103109
($($name:expr),+ $(,)?) => {{
104110
let mut v = ArrayVec::<[LangItemTarget; 2]>::new();
105111
$(
106-
v.extend(db.lang_item(cur_crate.crate_id, $name.into()));
112+
v.extend(db.lang_item(cur_crate, $name.into()));
107113
)+
108114
v
109115
}};
110116
}
111117

112-
let lang_item_targets = match ty {
113-
Ty::Apply(a_ty) => match a_ty.ctor {
114-
TypeCtor::Adt(def_id) => {
115-
return Some(std::iter::once(def_id.module(db).krate.into()).collect())
116-
}
117-
TypeCtor::Bool => lang_item_crate!("bool"),
118-
TypeCtor::Char => lang_item_crate!("char"),
119-
TypeCtor::Float(Uncertain::Known(f)) => match f.bitness {
120-
// There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime)
121-
FloatBitness::X32 => lang_item_crate!("f32", "f32_runtime"),
122-
FloatBitness::X64 => lang_item_crate!("f64", "f64_runtime"),
118+
let lang_item_targets = match self {
119+
Ty::Apply(a_ty) => match a_ty.ctor {
120+
TypeCtor::Adt(def_id) => {
121+
return Some(std::iter::once(def_id.module(db).krate).collect())
122+
}
123+
TypeCtor::Bool => lang_item_crate!("bool"),
124+
TypeCtor::Char => lang_item_crate!("char"),
125+
TypeCtor::Float(Uncertain::Known(f)) => match f.bitness {
126+
// There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime)
127+
FloatBitness::X32 => lang_item_crate!("f32", "f32_runtime"),
128+
FloatBitness::X64 => lang_item_crate!("f64", "f64_runtime"),
129+
},
130+
TypeCtor::Int(Uncertain::Known(i)) => lang_item_crate!(i.ty_to_string()),
131+
TypeCtor::Str => lang_item_crate!("str_alloc", "str"),
132+
TypeCtor::Slice => lang_item_crate!("slice_alloc", "slice"),
133+
TypeCtor::RawPtr(Mutability::Shared) => lang_item_crate!("const_ptr"),
134+
TypeCtor::RawPtr(Mutability::Mut) => lang_item_crate!("mut_ptr"),
135+
_ => return None,
123136
},
124-
TypeCtor::Int(Uncertain::Known(i)) => lang_item_crate!(i.ty_to_string()),
125-
TypeCtor::Str => lang_item_crate!("str_alloc", "str"),
126-
TypeCtor::Slice => lang_item_crate!("slice_alloc", "slice"),
127-
TypeCtor::RawPtr(Mutability::Shared) => lang_item_crate!("const_ptr"),
128-
TypeCtor::RawPtr(Mutability::Mut) => lang_item_crate!("mut_ptr"),
129137
_ => return None,
130-
},
131-
_ => return None,
132-
};
133-
let res = lang_item_targets
134-
.into_iter()
135-
.filter_map(|it| match it {
136-
LangItemTarget::ImplBlockId(it) => Some(it),
137-
_ => None,
138-
})
139-
.map(|it| it.module(db).krate.into())
140-
.collect();
141-
Some(res)
138+
};
139+
let res = lang_item_targets
140+
.into_iter()
141+
.filter_map(|it| match it {
142+
LangItemTarget::ImplBlockId(it) => Some(it),
143+
_ => None,
144+
})
145+
.map(|it| it.module(db).krate)
146+
.collect();
147+
Some(res)
148+
}
142149
}
143-
144150
/// Look up the method with the given name, returning the actual autoderefed
145151
/// receiver type (but without autoref applied yet).
146152
pub(crate) fn lookup_method(
@@ -193,14 +199,9 @@ pub(crate) fn iterate_method_candidates<T>(
193199
let environment = TraitEnvironment::lower(db, resolver);
194200
let ty = InEnvironment { value: ty.clone(), environment };
195201
for derefed_ty in autoderef::autoderef(db, resolver.krate(), ty) {
196-
if let Some(result) = iterate_inherent_methods(
197-
&derefed_ty,
198-
db,
199-
name,
200-
mode,
201-
krate.into(),
202-
&mut callback,
203-
) {
202+
if let Some(result) =
203+
iterate_inherent_methods(&derefed_ty, db, name, mode, krate, &mut callback)
204+
{
204205
return Some(result);
205206
}
206207
if let Some(result) = iterate_trait_method_candidates(
@@ -283,11 +284,11 @@ fn iterate_inherent_methods<T>(
283284
db: &impl HirDatabase,
284285
name: Option<&Name>,
285286
mode: LookupMode,
286-
krate: Crate,
287+
krate: CrateId,
287288
mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>,
288289
) -> Option<T> {
289-
for krate in def_crates(db, krate, &ty.value)? {
290-
let impls = db.impls_in_crate(krate.crate_id);
290+
for krate in ty.value.def_crates(db, krate)? {
291+
let impls = db.impls_in_crate(krate);
291292

292293
for impl_block in impls.lookup_impl_blocks(&ty.value) {
293294
for &item in db.impl_data(impl_block).items.iter() {
@@ -327,7 +328,7 @@ pub(crate) fn implements_trait(
327328
ty: &Canonical<Ty>,
328329
db: &impl HirDatabase,
329330
resolver: &Resolver,
330-
krate: Crate,
331+
krate: CrateId,
331332
trait_: TraitId,
332333
) -> bool {
333334
if ty.value.inherent_trait() == Some(trait_) {
@@ -337,35 +338,11 @@ pub(crate) fn implements_trait(
337338
}
338339
let env = TraitEnvironment::lower(db, resolver);
339340
let goal = generic_implements_goal(db, env, trait_, ty.clone());
340-
let solution = db.trait_solve(krate, goal);
341+
let solution = db.trait_solve(krate.into(), goal);
341342

342343
solution.is_some()
343344
}
344345

345-
impl Ty {
346-
// This would be nicer if it just returned an iterator, but that runs into
347-
// lifetime problems, because we need to borrow temp `CrateImplBlocks`.
348-
pub fn iterate_impl_items<T>(
349-
self,
350-
db: &impl HirDatabase,
351-
krate: Crate,
352-
mut callback: impl FnMut(AssocItem) -> Option<T>,
353-
) -> Option<T> {
354-
for krate in def_crates(db, krate, &self)? {
355-
let impls = db.impls_in_crate(krate.crate_id);
356-
357-
for impl_block in impls.lookup_impl_blocks(&self) {
358-
for &item in db.impl_data(impl_block).items.iter() {
359-
if let Some(result) = callback(item.into()) {
360-
return Some(result);
361-
}
362-
}
363-
}
364-
}
365-
None
366-
}
367-
}
368-
369346
/// This creates Substs for a trait with the given Self type and type variables
370347
/// for all other parameters, to query Chalk with it.
371348
fn generic_implements_goal(

0 commit comments

Comments
 (0)