From 8692977d2798831d0aea434277ee54d589aa9be3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 12:58:01 +0300 Subject: Move lang_items to hir_def --- crates/ra_hir/src/ty/autoderef.rs | 7 +++-- crates/ra_hir/src/ty/infer/coerce.rs | 7 +++-- crates/ra_hir/src/ty/method_resolution.rs | 47 ++++++++++++++++++------------- crates/ra_hir/src/ty/traits/chalk.rs | 5 ++-- 4 files changed, 39 insertions(+), 27 deletions(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/autoderef.rs b/crates/ra_hir/src/ty/autoderef.rs index b60e4bb31..41c99d227 100644 --- a/crates/ra_hir/src/ty/autoderef.rs +++ b/crates/ra_hir/src/ty/autoderef.rs @@ -5,12 +5,13 @@ use std::iter::successors; -use hir_def::resolver::Resolver; +use hir_def::{lang_item::LangItemTarget, resolver::Resolver}; use hir_expand::name; use log::{info, warn}; +use crate::{db::HirDatabase, Trait}; + use super::{traits::Solution, Canonical, Substs, Ty, TypeWalk}; -use crate::db::HirDatabase; const AUTODEREF_RECURSION_LIMIT: usize = 10; @@ -41,7 +42,7 @@ fn deref_by_trait( ) -> Option> { let krate = resolver.krate()?; let deref_trait = match db.lang_item(krate.into(), "deref".into())? { - crate::lang_item::LangItemTarget::Trait(t) => t, + LangItemTarget::TraitId(t) => Trait::from(t), _ => return None, }; let target = deref_trait.associated_type_by_name(db, &name::TARGET_TYPE)?; diff --git a/crates/ra_hir/src/ty/infer/coerce.rs b/crates/ra_hir/src/ty/infer/coerce.rs index 0772b9df5..4ea038d99 100644 --- a/crates/ra_hir/src/ty/infer/coerce.rs +++ b/crates/ra_hir/src/ty/infer/coerce.rs @@ -4,13 +4,12 @@ //! //! See: https://doc.rust-lang.org/nomicon/coercions.html -use hir_def::resolver::Resolver; +use hir_def::{lang_item::LangItemTarget, resolver::Resolver}; use rustc_hash::FxHashMap; use test_utils::tested_by; use crate::{ db::HirDatabase, - lang_item::LangItemTarget, ty::{autoderef, Substs, Ty, TypeCtor, TypeWalk}, Adt, Mutability, }; @@ -50,7 +49,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { ) -> FxHashMap<(TypeCtor, TypeCtor), usize> { let krate = resolver.krate().unwrap(); let impls = match db.lang_item(krate.into(), "coerce_unsized".into()) { - Some(LangItemTarget::Trait(trait_)) => db.impls_for_trait(krate.into(), trait_), + Some(LangItemTarget::TraitId(trait_)) => { + db.impls_for_trait(krate.into(), trait_.into()) + } _ => return FxHashMap::default(), }; diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index f61c27218..caa5f5f74 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -5,7 +5,7 @@ use std::sync::Arc; use arrayvec::ArrayVec; -use hir_def::resolver::Resolver; +use hir_def::{lang_item::LangItemTarget, resolver::Resolver, AstItemDef}; use rustc_hash::FxHashMap; use crate::{ @@ -91,34 +91,43 @@ fn def_crates(db: &impl HirDatabase, cur_crate: Crate, ty: &Ty) -> Option {{ - let mut v = ArrayVec::<[Crate; 2]>::new(); + ($($name:expr),+ $(,)?) => {{ + let mut v = ArrayVec::<[LangItemTarget; 2]>::new(); $( - v.extend($db.lang_item($cur_crate, $name.into()).and_then(|item| item.krate($db))); + v.extend(db.lang_item(cur_crate.crate_id, $name.into())); )+ - Some(v) + v }}; } - match ty { + let lang_item_targets = match ty { Ty::Apply(a_ty) => match a_ty.ctor { - TypeCtor::Adt(def_id) => Some(std::iter::once(def_id.krate(db)?).collect()), - TypeCtor::Bool => lang_item_crate!(db, cur_crate, "bool"), - TypeCtor::Char => lang_item_crate!(db, cur_crate, "char"), + TypeCtor::Adt(def_id) => return Some(std::iter::once(def_id.krate(db)?).collect()), + TypeCtor::Bool => lang_item_crate!("bool"), + TypeCtor::Char => lang_item_crate!("char"), TypeCtor::Float(Uncertain::Known(f)) => match f.bitness { // There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime) - FloatBitness::X32 => lang_item_crate!(db, cur_crate, "f32", "f32_runtime"), - FloatBitness::X64 => lang_item_crate!(db, cur_crate, "f64", "f64_runtime"), + FloatBitness::X32 => lang_item_crate!("f32", "f32_runtime"), + FloatBitness::X64 => lang_item_crate!("f64", "f64_runtime"), }, - TypeCtor::Int(Uncertain::Known(i)) => lang_item_crate!(db, cur_crate, i.ty_to_string()), - TypeCtor::Str => lang_item_crate!(db, cur_crate, "str_alloc", "str"), - TypeCtor::Slice => lang_item_crate!(db, cur_crate, "slice_alloc", "slice"), - TypeCtor::RawPtr(Mutability::Shared) => lang_item_crate!(db, cur_crate, "const_ptr"), - TypeCtor::RawPtr(Mutability::Mut) => lang_item_crate!(db, cur_crate, "mut_ptr"), - _ => None, + TypeCtor::Int(Uncertain::Known(i)) => lang_item_crate!(i.ty_to_string()), + TypeCtor::Str => lang_item_crate!("str_alloc", "str"), + TypeCtor::Slice => lang_item_crate!("slice_alloc", "slice"), + TypeCtor::RawPtr(Mutability::Shared) => lang_item_crate!("const_ptr"), + TypeCtor::RawPtr(Mutability::Mut) => lang_item_crate!("mut_ptr"), + _ => return None, }, - _ => None, - } + _ => return None, + }; + let res = lang_item_targets + .into_iter() + .filter_map(|it| match it { + LangItemTarget::ImplBlockId(it) => Some(it), + _ => None, + }) + .map(|it| it.module(db).krate.into()) + .collect(); + Some(res) } /// Look up the method with the given name, returning the actual autoderefed diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs index 88785f305..53818a5e5 100644 --- a/crates/ra_hir/src/ty/traits/chalk.rs +++ b/crates/ra_hir/src/ty/traits/chalk.rs @@ -9,6 +9,7 @@ use chalk_ir::{ }; use chalk_rust_ir::{AssociatedTyDatum, AssociatedTyValue, ImplDatum, StructDatum, TraitDatum}; +use hir_def::lang_item::LangItemTarget; use hir_expand::name; use ra_db::salsa::{InternId, InternKey}; @@ -832,9 +833,9 @@ fn closure_fn_trait_output_assoc_ty_value( } fn get_fn_trait(db: &impl HirDatabase, krate: Crate, fn_trait: super::FnTrait) -> Option { - let target = db.lang_item(krate, fn_trait.lang_item_name().into())?; + let target = db.lang_item(krate.crate_id, fn_trait.lang_item_name().into())?; match target { - crate::lang_item::LangItemTarget::Trait(t) => Some(t), + LangItemTarget::TraitId(t) => Some(t.into()), _ => None, } } -- cgit v1.2.3