Skip to content

Commit 84bd791

Browse files
bors[bot]matklad
andauthored
Merge #2426
2426: Decouple r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 57ad454 + 825049b commit 84bd791

File tree

9 files changed

+90
-62
lines changed

9 files changed

+90
-62
lines changed

crates/ra_hir/src/source_binder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use hir_def::{
1111
expr::{ExprId, PatId},
1212
path::known,
1313
resolver::{self, resolver_for_scope, HasResolver, Resolver, TypeNs, ValueNs},
14-
DefWithBodyId,
14+
AssocItemId, DefWithBodyId,
1515
};
1616
use hir_expand::{
1717
hygiene::Hygiene, name::AsName, AstId, HirFileId, MacroCallId, MacroFileKind, Source,
@@ -380,7 +380,7 @@ impl SourceAnalyzer {
380380
name,
381381
method_resolution::LookupMode::MethodCall,
382382
|ty, it| match it {
383-
AssocItem::Function(f) => callback(ty, f),
383+
AssocItemId::FunctionId(f) => callback(ty, f.into()),
384384
_ => None,
385385
},
386386
)
@@ -391,7 +391,7 @@ impl SourceAnalyzer {
391391
db: &impl HirDatabase,
392392
ty: &Type,
393393
name: Option<&Name>,
394-
callback: impl FnMut(&Ty, AssocItem) -> Option<T>,
394+
mut 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?
@@ -403,7 +403,7 @@ impl SourceAnalyzer {
403403
&self.resolver,
404404
name,
405405
method_resolution::LookupMode::Path,
406-
callback,
406+
|ty, it| callback(ty, it.into()),
407407
)
408408
}
409409

crates/ra_hir/src/ty/infer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use hir_def::{
2626
path::known,
2727
resolver::{HasResolver, Resolver, TypeNs},
2828
type_ref::{Mutability, TypeRef},
29-
AdtId, DefWithBodyId,
29+
AdtId, AssocItemId, DefWithBodyId,
3030
};
3131
use hir_expand::{diagnostics::DiagnosticSink, name};
3232
use ra_arena::map::ArenaMap;
@@ -255,8 +255,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
255255
self.result.variant_resolutions.insert(id, variant);
256256
}
257257

258-
fn write_assoc_resolution(&mut self, id: ExprOrPatId, item: AssocItem) {
259-
self.result.assoc_resolutions.insert(id, item);
258+
fn write_assoc_resolution(&mut self, id: ExprOrPatId, item: AssocItemId) {
259+
self.result.assoc_resolutions.insert(id, item.into());
260260
}
261261

262262
fn write_pat_ty(&mut self, pat: PatId, ty: Ty) {

crates/ra_hir/src/ty/infer/coerce.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use hir_def::{
88
lang_item::LangItemTarget,
99
resolver::{HasResolver, Resolver},
10+
type_ref::Mutability,
1011
AdtId,
1112
};
1213
use rustc_hash::FxHashMap;
@@ -15,7 +16,6 @@ use test_utils::tested_by;
1516
use crate::{
1617
db::HirDatabase,
1718
ty::{autoderef, Substs, TraitRef, Ty, TypeCtor, TypeWalk},
18-
Mutability,
1919
};
2020

2121
use super::{InEnvironment, InferTy, InferenceContext, TypeVarValue};

crates/ra_hir/src/ty/infer/expr.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@ use std::sync::Arc;
55

66
use hir_def::{
77
builtin_type::Signedness,
8+
expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
89
generics::GenericParams,
910
path::{GenericArg, GenericArgs},
1011
resolver::resolver_for_expr,
1112
AdtId, ContainerId, Lookup, StructFieldId,
1213
};
13-
use hir_expand::name;
14+
use hir_expand::name::{self, Name};
1415

1516
use crate::{
1617
db::HirDatabase,
17-
expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
1818
ty::{
1919
autoderef, method_resolution, op, traits::InEnvironment, CallableDef, InferTy, IntTy,
2020
Mutability, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor,
2121
TypeWalk, Uncertain,
2222
},
23-
Name,
2423
};
2524

2625
use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch};

crates/ra_hir/src/ty/infer/pat.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
use std::iter::repeat;
44
use std::sync::Arc;
55

6+
use hir_def::{
7+
expr::{BindingAnnotation, Pat, PatId, RecordFieldPat},
8+
path::Path,
9+
type_ref::Mutability,
10+
};
11+
use hir_expand::name::Name;
612
use test_utils::tested_by;
713

814
use super::{BindingMode, InferenceContext};
915
use crate::{
1016
db::HirDatabase,
11-
expr::{BindingAnnotation, Pat, PatId, RecordFieldPat},
12-
ty::{Mutability, Substs, Ty, TypeCtor, TypeWalk},
13-
Name, Path,
17+
ty::{Substs, Ty, TypeCtor, TypeWalk},
1418
};
1519

1620
impl<'a, D: HirDatabase> InferenceContext<'a, D> {

crates/ra_hir/src/ty/infer/path.rs

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
//! Path expression resolution.
22
33
use hir_def::{
4-
path::PathSegment,
5-
resolver::{ResolveValueResult, Resolver, TypeNs, ValueNs},
4+
path::{Path, PathSegment},
5+
resolver::{HasResolver, ResolveValueResult, Resolver, TypeNs, ValueNs},
6+
AssocItemId, ContainerId, Lookup,
67
};
8+
use hir_expand::name::Name;
79

810
use crate::{
911
db::HirDatabase,
1012
ty::{method_resolution, Substs, Ty, TypeWalk, ValueTyDefId},
11-
AssocItem, Container, Function, Name, Path,
1213
};
1314

1415
use super::{ExprOrPatId, InferenceContext, TraitRef};
@@ -142,31 +143,35 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
142143
id: ExprOrPatId,
143144
) -> Option<(ValueNs, Option<Substs>)> {
144145
let trait_ = trait_ref.trait_;
145-
let item =
146-
self.db.trait_data(trait_).items.iter().map(|(_name, id)| (*id).into()).find_map(
147-
|item| match item {
148-
AssocItem::Function(func) => {
149-
if segment.name == func.name(self.db) {
150-
Some(AssocItem::Function(func))
151-
} else {
152-
None
153-
}
146+
let item = self
147+
.db
148+
.trait_data(trait_)
149+
.items
150+
.iter()
151+
.map(|(_name, id)| (*id).into())
152+
.find_map(|item| match item {
153+
AssocItemId::FunctionId(func) => {
154+
if segment.name == self.db.function_data(func).name {
155+
Some(AssocItemId::FunctionId(func))
156+
} else {
157+
None
154158
}
159+
}
155160

156-
AssocItem::Const(konst) => {
157-
if konst.name(self.db).map_or(false, |n| n == segment.name) {
158-
Some(AssocItem::Const(konst))
159-
} else {
160-
None
161-
}
161+
AssocItemId::ConstId(konst) => {
162+
if self.db.const_data(konst).name.as_ref().map_or(false, |n| n == &segment.name)
163+
{
164+
Some(AssocItemId::ConstId(konst))
165+
} else {
166+
None
162167
}
163-
AssocItem::TypeAlias(_) => None,
164-
},
165-
)?;
168+
}
169+
AssocItemId::TypeAliasId(_) => None,
170+
})?;
166171
let def = match item {
167-
AssocItem::Function(f) => ValueNs::FunctionId(f.id),
168-
AssocItem::Const(c) => ValueNs::ConstId(c.id),
169-
AssocItem::TypeAlias(_) => unreachable!(),
172+
AssocItemId::FunctionId(f) => ValueNs::FunctionId(f),
173+
AssocItemId::ConstId(c) => ValueNs::ConstId(c),
174+
AssocItemId::TypeAliasId(_) => unreachable!(),
170175
};
171176
let substs = Substs::build_for_def(self.db, item)
172177
.use_parent_substs(&trait_ref.substs)
@@ -196,16 +201,18 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
196201
Some(name),
197202
method_resolution::LookupMode::Path,
198203
move |_ty, item| {
199-
let def = match item {
200-
AssocItem::Function(f) => ValueNs::FunctionId(f.id),
201-
AssocItem::Const(c) => ValueNs::ConstId(c.id),
202-
AssocItem::TypeAlias(_) => unreachable!(),
204+
let (def, container) = match item {
205+
AssocItemId::FunctionId(f) => {
206+
(ValueNs::FunctionId(f), f.lookup(self.db).container)
207+
}
208+
AssocItemId::ConstId(c) => (ValueNs::ConstId(c), c.lookup(self.db).container),
209+
AssocItemId::TypeAliasId(_) => unreachable!(),
203210
};
204-
let substs = match item.container(self.db) {
205-
Container::ImplBlock(_) => self.find_self_types(&def, ty.clone()),
206-
Container::Trait(t) => {
211+
let substs = match container {
212+
ContainerId::ImplId(_) => self.find_self_types(&def, ty.clone()),
213+
ContainerId::TraitId(trait_) => {
207214
// we're picking this method
208-
let trait_substs = Substs::build_for_def(self.db, t.id)
215+
let trait_substs = Substs::build_for_def(self.db, trait_)
209216
.push(ty.clone())
210217
.fill(std::iter::repeat_with(|| self.new_type_var()))
211218
.build();
@@ -214,29 +221,35 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
214221
.fill_with_params()
215222
.build();
216223
self.obligations.push(super::Obligation::Trait(TraitRef {
217-
trait_: t.id,
224+
trait_,
218225
substs: trait_substs,
219226
}));
220227
Some(substs)
221228
}
229+
ContainerId::ModuleId(_) => None,
222230
};
223231

224-
self.write_assoc_resolution(id, item);
232+
self.write_assoc_resolution(id, item.into());
225233
Some((def, substs))
226234
},
227235
)
228236
}
229237

230238
fn find_self_types(&self, def: &ValueNs, actual_def_ty: Ty) -> Option<Substs> {
231-
if let ValueNs::FunctionId(func) = def {
232-
let func = Function::from(*func);
239+
if let ValueNs::FunctionId(func) = *def {
233240
// We only do the infer if parent has generic params
234-
let gen = self.db.generic_params(func.id.into());
241+
let gen = self.db.generic_params(func.into());
235242
if gen.count_parent_params() == 0 {
236243
return None;
237244
}
238245

239-
let impl_block = func.impl_block(self.db)?.target_ty(self.db);
246+
let impl_id = match func.lookup(self.db).container {
247+
ContainerId::ImplId(it) => it,
248+
_ => return None,
249+
};
250+
let resolver = impl_id.resolver(self.db);
251+
let impl_data = self.db.impl_data(impl_id);
252+
let impl_block = Ty::from_hir(self.db, &resolver, &impl_data.target_type);
240253
let impl_block_substs = impl_block.substs()?;
241254
let actual_substs = actual_def_ty.substs()?;
242255

crates/ra_hir/src/ty/infer/unify.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
//! Unification and canonicalization logic.
22
33
use super::{InferenceContext, Obligation};
4-
use crate::db::HirDatabase;
5-
use crate::ty::{
6-
Canonical, InEnvironment, InferTy, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty,
7-
TypeWalk,
4+
use crate::{
5+
db::HirDatabase,
6+
ty::{
7+
Canonical, InEnvironment, InferTy, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty,
8+
TypeWalk,
9+
},
10+
util::make_mut_slice,
811
};
9-
use crate::util::make_mut_slice;
1012

1113
impl<'a, D: HirDatabase> InferenceContext<'a, D> {
1214
pub(super) fn canonicalizer<'b>(&'b mut self) -> Canonicalizer<'a, 'b, D>

crates/ra_hir/src/ty/method_resolution.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
db::HirDatabase,
1919
ty::primitive::{FloatBitness, Uncertain},
2020
ty::{utils::all_super_traits, Ty, TypeCtor},
21-
AssocItem, Function,
21+
Function,
2222
};
2323

2424
use super::{autoderef, Canonical, InEnvironment, TraitEnvironment, TraitRef};
@@ -157,7 +157,7 @@ pub(crate) fn lookup_method(
157157
) -> Option<(Ty, Function)> {
158158
iterate_method_candidates(ty, db, resolver, Some(name), LookupMode::MethodCall, |ty, f| match f
159159
{
160-
AssocItem::Function(f) => Some((ty.clone(), f)),
160+
AssocItemId::FunctionId(f) => Some((ty.clone(), f.into())),
161161
_ => None,
162162
})
163163
}
@@ -183,7 +183,7 @@ pub(crate) fn iterate_method_candidates<T>(
183183
resolver: &Resolver,
184184
name: Option<&Name>,
185185
mode: LookupMode,
186-
mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>,
186+
mut callback: impl FnMut(&Ty, AssocItemId) -> Option<T>,
187187
) -> Option<T> {
188188
let krate = resolver.krate()?;
189189
match mode {
@@ -239,7 +239,7 @@ fn iterate_trait_method_candidates<T>(
239239
resolver: &Resolver,
240240
name: Option<&Name>,
241241
mode: LookupMode,
242-
mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>,
242+
mut callback: impl FnMut(&Ty, AssocItemId) -> Option<T>,
243243
) -> Option<T> {
244244
let krate = resolver.krate()?;
245245
// FIXME: maybe put the trait_env behind a query (need to figure out good input parameters for that)
@@ -285,7 +285,7 @@ fn iterate_inherent_methods<T>(
285285
name: Option<&Name>,
286286
mode: LookupMode,
287287
krate: CrateId,
288-
mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>,
288+
mut callback: impl FnMut(&Ty, AssocItemId) -> Option<T>,
289289
) -> Option<T> {
290290
for krate in ty.value.def_crates(db, krate)? {
291291
let impls = db.impls_in_crate(krate);

crates/ra_hir_def/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,16 @@ impl_froms!(
398398
ConstId
399399
);
400400

401+
impl From<AssocItemId> for GenericDefId {
402+
fn from(item: AssocItemId) -> Self {
403+
match item {
404+
AssocItemId::FunctionId(f) => f.into(),
405+
AssocItemId::ConstId(c) => c.into(),
406+
AssocItemId::TypeAliasId(t) => t.into(),
407+
}
408+
}
409+
}
410+
401411
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
402412
pub enum AttrDefId {
403413
ModuleId(ModuleId),

0 commit comments

Comments
 (0)