Skip to content

Commit bed6869

Browse files
committed
Cleanup
1 parent cace49e commit bed6869

File tree

6 files changed

+85
-77
lines changed

6 files changed

+85
-77
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 40 additions & 9 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 {
@@ -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/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: 39 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,15 @@ impl CrateImplBlocks {
9797
}
9898
}
9999

100-
fn def_crates(
101-
db: &impl HirDatabase,
102-
cur_crate: CrateId,
103-
ty: &Ty,
104-
) -> Option<ArrayVec<[CrateId; 2]>> {
105-
// Types like slice can have inherent impls in several crates, (core and alloc).
106-
// The corresponding impls are marked with lang items, so we can use them to find the required crates.
107-
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 {
108109
($($name:expr),+ $(,)?) => {{
109110
let mut v = ArrayVec::<[LangItemTarget; 2]>::new();
110111
$(
@@ -114,38 +115,38 @@ fn def_crates(
114115
}};
115116
}
116117

117-
let lang_item_targets = match ty {
118-
Ty::Apply(a_ty) => match a_ty.ctor {
119-
TypeCtor::Adt(def_id) => {
120-
return Some(std::iter::once(def_id.module(db).krate).collect())
121-
}
122-
TypeCtor::Bool => lang_item_crate!("bool"),
123-
TypeCtor::Char => lang_item_crate!("char"),
124-
TypeCtor::Float(Uncertain::Known(f)) => match f.bitness {
125-
// There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime)
126-
FloatBitness::X32 => lang_item_crate!("f32", "f32_runtime"),
127-
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,
128136
},
129-
TypeCtor::Int(Uncertain::Known(i)) => lang_item_crate!(i.ty_to_string()),
130-
TypeCtor::Str => lang_item_crate!("str_alloc", "str"),
131-
TypeCtor::Slice => lang_item_crate!("slice_alloc", "slice"),
132-
TypeCtor::RawPtr(Mutability::Shared) => lang_item_crate!("const_ptr"),
133-
TypeCtor::RawPtr(Mutability::Mut) => lang_item_crate!("mut_ptr"),
134137
_ => return None,
135-
},
136-
_ => return None,
137-
};
138-
let res = lang_item_targets
139-
.into_iter()
140-
.filter_map(|it| match it {
141-
LangItemTarget::ImplBlockId(it) => Some(it),
142-
_ => None,
143-
})
144-
.map(|it| it.module(db).krate)
145-
.collect();
146-
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+
}
147149
}
148-
149150
/// Look up the method with the given name, returning the actual autoderefed
150151
/// receiver type (but without autoref applied yet).
151152
pub(crate) fn lookup_method(
@@ -286,7 +287,7 @@ fn iterate_inherent_methods<T>(
286287
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+
for krate in ty.value.def_crates(db, krate)? {
290291
let impls = db.impls_in_crate(krate);
291292

292293
for impl_block in impls.lookup_impl_blocks(&ty.value) {
@@ -342,30 +343,6 @@ pub(crate) fn implements_trait(
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: CrateId,
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);
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(

crates/ra_hir_def/src/resolver.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ impl Resolver {
484484
}
485485
}
486486

487-
pub trait HasResolver {
487+
pub trait HasResolver: Copy {
488488
/// Builds a resolver for type references inside this def.
489489
fn resolver(self, db: &impl DefDatabase) -> Resolver;
490490
}
@@ -502,7 +502,7 @@ impl HasResolver for TraitId {
502502
}
503503
}
504504

505-
impl<T: Into<AdtId>> HasResolver for T {
505+
impl<T: Into<AdtId> + Copy> HasResolver for T {
506506
fn resolver(self, db: &impl DefDatabase) -> Resolver {
507507
let def = self.into();
508508
def.module(db)

crates/ra_ide_api/src/completion/complete_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
5050
hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db),
5151
_ => unreachable!(),
5252
};
53-
ctx.analyzer.iterate_path_candidates(ctx.db, ty.clone(), None, |_ty, item| {
53+
ctx.analyzer.iterate_path_candidates(ctx.db, &ty, None, |_ty, item| {
5454
match item {
5555
hir::AssocItem::Function(func) => {
5656
if !func.has_self_param(ctx.db) {

0 commit comments

Comments
 (0)