Skip to content

Commit 45510ae

Browse files
bors[bot]Veykril
andauthored
Merge #8396
8396: Uncouple Ty::builtin_deref and Ty::def_crates from Ty r=Veykril a=Veykril bors r+ CC #8313 Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents a8f1e41 + c3c8e82 commit 45510ae

File tree

4 files changed

+64
-66
lines changed

4 files changed

+64
-66
lines changed

crates/hir/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use hir_def::{
5353
use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind};
5454
use hir_ty::{
5555
autoderef, could_unify,
56-
method_resolution::{self, TyFingerprint},
56+
method_resolution::{self, def_crates, TyFingerprint},
5757
primitive::UintTy,
5858
subst_prefix,
5959
traits::FnTrait,
@@ -1568,7 +1568,7 @@ impl Impl {
15681568
}
15691569

15701570
pub fn all_for_type(db: &dyn HirDatabase, Type { krate, ty, .. }: Type) -> Vec<Impl> {
1571-
let def_crates = match ty.def_crates(db, krate) {
1571+
let def_crates = match def_crates(db, &ty, krate) {
15721572
Some(def_crates) => def_crates,
15731573
None => return Vec::new(),
15741574
};
@@ -1955,7 +1955,7 @@ impl Type {
19551955
krate: Crate,
19561956
mut callback: impl FnMut(AssocItem) -> Option<T>,
19571957
) -> Option<T> {
1958-
for krate in self.ty.def_crates(db, krate.id)? {
1958+
for krate in def_crates(db, &self.ty, krate.id)? {
19591959
let impls = db.inherent_impls_in_crate(krate);
19601960

19611961
for impl_def in impls.for_self_ty(&self.ty) {

crates/hir_ty/src/autoderef.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,21 @@ pub(crate) fn deref(
3535
krate: CrateId,
3636
ty: InEnvironment<&Canonical<Ty>>,
3737
) -> Option<Canonical<Ty>> {
38-
if let Some(derefed) = ty.goal.value.builtin_deref() {
38+
if let Some(derefed) = builtin_deref(&ty.goal.value) {
3939
Some(Canonical { value: derefed, binders: ty.goal.binders.clone() })
4040
} else {
4141
deref_by_trait(db, krate, ty)
4242
}
4343
}
4444

45+
fn builtin_deref(ty: &Ty) -> Option<Ty> {
46+
match ty.kind(&Interner) {
47+
TyKind::Ref(.., ty) => Some(ty.clone()),
48+
TyKind::Raw(.., ty) => Some(ty.clone()),
49+
_ => None,
50+
}
51+
}
52+
4553
fn deref_by_trait(
4654
db: &dyn HirDatabase,
4755
krate: CrateId,

crates/hir_ty/src/lib.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,6 @@ impl Ty {
199199
}
200200
}
201201

202-
fn builtin_deref(&self) -> Option<Ty> {
203-
match self.kind(&Interner) {
204-
TyKind::Ref(.., ty) => Some(ty.clone()),
205-
TyKind::Raw(.., ty) => Some(ty.clone()),
206-
_ => None,
207-
}
208-
}
209-
210202
/// Returns the type parameters of this type if it has some (i.e. is an ADT
211203
/// or function); so if `self` is `Option<u32>`, this returns the `u32`.
212204
pub fn substs(&self) -> Option<&Substitution> {

crates/hir_ty/src/method_resolution.rs

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,14 @@ impl InherentImpls {
239239
}
240240
}
241241

242-
impl Ty {
243-
pub fn def_crates(
244-
&self,
245-
db: &dyn HirDatabase,
246-
cur_crate: CrateId,
247-
) -> Option<ArrayVec<CrateId, 2>> {
248-
// Types like slice can have inherent impls in several crates, (core and alloc).
249-
// The corresponding impls are marked with lang items, so we can use them to find the required crates.
250-
macro_rules! lang_item_crate {
242+
pub fn def_crates(
243+
db: &dyn HirDatabase,
244+
ty: &Ty,
245+
cur_crate: CrateId,
246+
) -> Option<ArrayVec<CrateId, 2>> {
247+
// Types like slice can have inherent impls in several crates, (core and alloc).
248+
// The corresponding impls are marked with lang items, so we can use them to find the required crates.
249+
macro_rules! lang_item_crate {
251250
($($name:expr),+ $(,)?) => {{
252251
let mut v = ArrayVec::<LangItemTarget, 2>::new();
253252
$(
@@ -257,51 +256,50 @@ impl Ty {
257256
}};
258257
}
259258

260-
let mod_to_crate_ids = |module: ModuleId| Some(std::iter::once(module.krate()).collect());
259+
let mod_to_crate_ids = |module: ModuleId| Some(std::iter::once(module.krate()).collect());
261260

262-
let lang_item_targets = match self.kind(&Interner) {
263-
TyKind::Adt(AdtId(def_id), _) => {
264-
return mod_to_crate_ids(def_id.module(db.upcast()));
265-
}
266-
TyKind::Foreign(id) => {
267-
return mod_to_crate_ids(
268-
from_foreign_def_id(*id).lookup(db.upcast()).module(db.upcast()),
269-
);
270-
}
271-
TyKind::Scalar(Scalar::Bool) => lang_item_crate!("bool"),
272-
TyKind::Scalar(Scalar::Char) => lang_item_crate!("char"),
273-
TyKind::Scalar(Scalar::Float(f)) => match f {
274-
// There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime)
275-
FloatTy::F32 => lang_item_crate!("f32", "f32_runtime"),
276-
FloatTy::F64 => lang_item_crate!("f64", "f64_runtime"),
277-
},
278-
&TyKind::Scalar(Scalar::Int(t)) => {
279-
lang_item_crate!(primitive::int_ty_to_string(t))
280-
}
281-
&TyKind::Scalar(Scalar::Uint(t)) => {
282-
lang_item_crate!(primitive::uint_ty_to_string(t))
283-
}
284-
TyKind::Str => lang_item_crate!("str_alloc", "str"),
285-
TyKind::Slice(_) => lang_item_crate!("slice_alloc", "slice"),
286-
TyKind::Raw(Mutability::Not, _) => lang_item_crate!("const_ptr"),
287-
TyKind::Raw(Mutability::Mut, _) => lang_item_crate!("mut_ptr"),
288-
TyKind::Dyn(_) => {
289-
return self.dyn_trait().and_then(|trait_| {
290-
mod_to_crate_ids(GenericDefId::TraitId(trait_).module(db.upcast()))
291-
});
292-
}
293-
_ => return None,
294-
};
295-
let res = lang_item_targets
296-
.into_iter()
297-
.filter_map(|it| match it {
298-
LangItemTarget::ImplDefId(it) => Some(it),
299-
_ => None,
300-
})
301-
.map(|it| it.lookup(db.upcast()).container.krate())
302-
.collect();
303-
Some(res)
304-
}
261+
let lang_item_targets = match ty.kind(&Interner) {
262+
TyKind::Adt(AdtId(def_id), _) => {
263+
return mod_to_crate_ids(def_id.module(db.upcast()));
264+
}
265+
TyKind::Foreign(id) => {
266+
return mod_to_crate_ids(
267+
from_foreign_def_id(*id).lookup(db.upcast()).module(db.upcast()),
268+
);
269+
}
270+
TyKind::Scalar(Scalar::Bool) => lang_item_crate!("bool"),
271+
TyKind::Scalar(Scalar::Char) => lang_item_crate!("char"),
272+
TyKind::Scalar(Scalar::Float(f)) => match f {
273+
// There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime)
274+
FloatTy::F32 => lang_item_crate!("f32", "f32_runtime"),
275+
FloatTy::F64 => lang_item_crate!("f64", "f64_runtime"),
276+
},
277+
&TyKind::Scalar(Scalar::Int(t)) => {
278+
lang_item_crate!(primitive::int_ty_to_string(t))
279+
}
280+
&TyKind::Scalar(Scalar::Uint(t)) => {
281+
lang_item_crate!(primitive::uint_ty_to_string(t))
282+
}
283+
TyKind::Str => lang_item_crate!("str_alloc", "str"),
284+
TyKind::Slice(_) => lang_item_crate!("slice_alloc", "slice"),
285+
TyKind::Raw(Mutability::Not, _) => lang_item_crate!("const_ptr"),
286+
TyKind::Raw(Mutability::Mut, _) => lang_item_crate!("mut_ptr"),
287+
TyKind::Dyn(_) => {
288+
return ty.dyn_trait().and_then(|trait_| {
289+
mod_to_crate_ids(GenericDefId::TraitId(trait_).module(db.upcast()))
290+
});
291+
}
292+
_ => return None,
293+
};
294+
let res = lang_item_targets
295+
.into_iter()
296+
.filter_map(|it| match it {
297+
LangItemTarget::ImplDefId(it) => Some(it),
298+
_ => None,
299+
})
300+
.map(|it| it.lookup(db.upcast()).container.krate())
301+
.collect();
302+
Some(res)
305303
}
306304

307305
/// Look up the method with the given name, returning the actual autoderefed
@@ -628,7 +626,7 @@ fn iterate_inherent_methods(
628626
visible_from_module: Option<ModuleId>,
629627
callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
630628
) -> bool {
631-
let def_crates = match self_ty.value.def_crates(db, krate) {
629+
let def_crates = match def_crates(db, &self_ty.value, krate) {
632630
Some(k) => k,
633631
None => return false,
634632
};

0 commit comments

Comments
 (0)