aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/method_resolution.rs
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-04-07 12:09:31 +0100
committerLukas Wirth <[email protected]>2021-04-07 12:09:31 +0100
commitc3c8e8225ff0a7a741f24777b5ee7a9c3e91eeb7 (patch)
treecb2e469e3a80aaf8dae2f4dbaf3a8bbfb538894f /crates/hir_ty/src/method_resolution.rs
parente3a5c15d18492f5b9f0c606f988b914918d29ef5 (diff)
Free Ty::def_crates
Diffstat (limited to 'crates/hir_ty/src/method_resolution.rs')
-rw-r--r--crates/hir_ty/src/method_resolution.rs106
1 files changed, 52 insertions, 54 deletions
diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs
index f29319f20..c601f2d53 100644
--- a/crates/hir_ty/src/method_resolution.rs
+++ b/crates/hir_ty/src/method_resolution.rs
@@ -239,15 +239,14 @@ impl InherentImpls {
239 } 239 }
240} 240}
241 241
242impl Ty { 242pub fn def_crates(
243 pub fn def_crates( 243 db: &dyn HirDatabase,
244 &self, 244 ty: &Ty,
245 db: &dyn HirDatabase, 245 cur_crate: CrateId,
246 cur_crate: CrateId, 246) -> Option<ArrayVec<CrateId, 2>> {
247 ) -> Option<ArrayVec<CrateId, 2>> { 247 // Types like slice can have inherent impls in several crates, (core and alloc).
248 // 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 // The corresponding impls are marked with lang items, so we can use them to find the required crates. 249 macro_rules! lang_item_crate {
250 macro_rules! lang_item_crate {
251 ($($name:expr),+ $(,)?) => {{ 250 ($($name:expr),+ $(,)?) => {{
252 let mut v = ArrayVec::<LangItemTarget, 2>::new(); 251 let mut v = ArrayVec::<LangItemTarget, 2>::new();
253 $( 252 $(
@@ -257,51 +256,50 @@ impl Ty {
257 }}; 256 }};
258 } 257 }
259 258
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());
261 260
262 let lang_item_targets = match self.kind(&Interner) { 261 let lang_item_targets = match ty.kind(&Interner) {
263 TyKind::Adt(AdtId(def_id), _) => { 262 TyKind::Adt(AdtId(def_id), _) => {
264 return mod_to_crate_ids(def_id.module(db.upcast())); 263 return mod_to_crate_ids(def_id.module(db.upcast()));
265 } 264 }
266 TyKind::Foreign(id) => { 265 TyKind::Foreign(id) => {
267 return mod_to_crate_ids( 266 return mod_to_crate_ids(
268 from_foreign_def_id(*id).lookup(db.upcast()).module(db.upcast()), 267 from_foreign_def_id(*id).lookup(db.upcast()).module(db.upcast()),
269 ); 268 );
270 } 269 }
271 TyKind::Scalar(Scalar::Bool) => lang_item_crate!("bool"), 270 TyKind::Scalar(Scalar::Bool) => lang_item_crate!("bool"),
272 TyKind::Scalar(Scalar::Char) => lang_item_crate!("char"), 271 TyKind::Scalar(Scalar::Char) => lang_item_crate!("char"),
273 TyKind::Scalar(Scalar::Float(f)) => match f { 272 TyKind::Scalar(Scalar::Float(f)) => match f {
274 // There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime) 273 // There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime)
275 FloatTy::F32 => lang_item_crate!("f32", "f32_runtime"), 274 FloatTy::F32 => lang_item_crate!("f32", "f32_runtime"),
276 FloatTy::F64 => lang_item_crate!("f64", "f64_runtime"), 275 FloatTy::F64 => lang_item_crate!("f64", "f64_runtime"),
277 }, 276 },
278 &TyKind::Scalar(Scalar::Int(t)) => { 277 &TyKind::Scalar(Scalar::Int(t)) => {
279 lang_item_crate!(primitive::int_ty_to_string(t)) 278 lang_item_crate!(primitive::int_ty_to_string(t))
280 } 279 }
281 &TyKind::Scalar(Scalar::Uint(t)) => { 280 &TyKind::Scalar(Scalar::Uint(t)) => {
282 lang_item_crate!(primitive::uint_ty_to_string(t)) 281 lang_item_crate!(primitive::uint_ty_to_string(t))
283 } 282 }
284 TyKind::Str => lang_item_crate!("str_alloc", "str"), 283 TyKind::Str => lang_item_crate!("str_alloc", "str"),
285 TyKind::Slice(_) => lang_item_crate!("slice_alloc", "slice"), 284 TyKind::Slice(_) => lang_item_crate!("slice_alloc", "slice"),
286 TyKind::Raw(Mutability::Not, _) => lang_item_crate!("const_ptr"), 285 TyKind::Raw(Mutability::Not, _) => lang_item_crate!("const_ptr"),
287 TyKind::Raw(Mutability::Mut, _) => lang_item_crate!("mut_ptr"), 286 TyKind::Raw(Mutability::Mut, _) => lang_item_crate!("mut_ptr"),
288 TyKind::Dyn(_) => { 287 TyKind::Dyn(_) => {
289 return self.dyn_trait().and_then(|trait_| { 288 return ty.dyn_trait().and_then(|trait_| {
290 mod_to_crate_ids(GenericDefId::TraitId(trait_).module(db.upcast())) 289 mod_to_crate_ids(GenericDefId::TraitId(trait_).module(db.upcast()))
291 }); 290 });
292 } 291 }
293 _ => return None, 292 _ => return None,
294 }; 293 };
295 let res = lang_item_targets 294 let res = lang_item_targets
296 .into_iter() 295 .into_iter()
297 .filter_map(|it| match it { 296 .filter_map(|it| match it {
298 LangItemTarget::ImplDefId(it) => Some(it), 297 LangItemTarget::ImplDefId(it) => Some(it),
299 _ => None, 298 _ => None,
300 }) 299 })
301 .map(|it| it.lookup(db.upcast()).container.krate()) 300 .map(|it| it.lookup(db.upcast()).container.krate())
302 .collect(); 301 .collect();
303 Some(res) 302 Some(res)
304 }
305} 303}
306 304
307/// Look up the method with the given name, returning the actual autoderefed 305/// Look up the method with the given name, returning the actual autoderefed
@@ -628,7 +626,7 @@ fn iterate_inherent_methods(
628 visible_from_module: Option<ModuleId>, 626 visible_from_module: Option<ModuleId>,
629 callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool, 627 callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
630) -> bool { 628) -> 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) {
632 Some(k) => k, 630 Some(k) => k,
633 None => return false, 631 None => return false,
634 }; 632 };