5405: Align CallableDefId naming with other ids r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-07-16 11:17:53 +00:00 committed by GitHub
commit 468982e45a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 69 additions and 67 deletions

View File

@ -40,7 +40,7 @@ use stdx::impl_from;
use crate::{
db::{DefDatabase, HirDatabase},
has_source::HasSource,
CallableDef, HirDisplay, InFile, Name,
CallableDefId, HirDisplay, InFile, Name,
};
/// hir::Crate describes a single crate. It's the main interface with which
@ -1226,7 +1226,7 @@ impl Type {
}
// FIXME: this method is broken, as it doesn't take closures into account.
pub fn as_callable(&self) -> Option<CallableDef> {
pub fn as_callable(&self) -> Option<CallableDefId> {
Some(self.ty.value.as_callable()?.0)
}

View File

@ -55,4 +55,4 @@ pub use hir_expand::{
hygiene::Hygiene, name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId,
MacroFile, Origin,
};
pub use hir_ty::{display::HirDisplay, CallableDef};
pub use hir_ty::{display::HirDisplay, CallableDefId};

View File

@ -13,7 +13,7 @@ use ra_prof::profile;
use crate::{
method_resolution::{InherentImpls, TraitImpls},
traits::chalk,
Binders, CallableDef, GenericPredicate, InferenceResult, OpaqueTyId, PolyFnSig,
Binders, CallableDefId, GenericPredicate, InferenceResult, OpaqueTyId, PolyFnSig,
ReturnTypeImplTraits, TraitRef, Ty, TyDefId, ValueTyDefId,
};
use hir_expand::name::Name;
@ -45,7 +45,7 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
fn field_types(&self, var: VariantId) -> Arc<ArenaMap<LocalFieldId, Binders<Ty>>>;
#[salsa::invoke(crate::callable_item_sig)]
fn callable_item_signature(&self, def: CallableDef) -> PolyFnSig;
fn callable_item_signature(&self, def: CallableDefId) -> PolyFnSig;
#[salsa::invoke(crate::lower::return_type_impl_traits)]
fn return_type_impl_traits(
@ -77,7 +77,7 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
// Interned IDs for Chalk integration
#[salsa::interned]
fn intern_callable_def(&self, callable_def: CallableDef) -> crate::CallableDefId;
fn intern_callable_def(&self, callable_def: CallableDefId) -> InternedCallableDefId;
#[salsa::interned]
fn intern_type_param_id(&self, param_id: TypeParamId) -> GlobalTypeParamId;
#[salsa::interned]
@ -151,3 +151,9 @@ impl_intern_key!(InternedOpaqueTyId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ClosureId(salsa::InternId);
impl_intern_key!(ClosureId);
/// This exists just for Chalk, because Chalk just has a single `FnDefId` where
/// we have different IDs for struct and enum variant constructors.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct InternedCallableDefId(salsa::InternId);
impl_intern_key!(InternedCallableDefId);

View File

@ -11,7 +11,7 @@ use hir_def::{
use hir_expand::diagnostics::DiagnosticSink;
use crate::{
db::HirDatabase, diagnostics::MissingUnsafe, lower::CallableDef, ApplicationTy,
db::HirDatabase, diagnostics::MissingUnsafe, lower::CallableDefId, ApplicationTy,
InferenceResult, Ty, TypeCtor,
};
@ -88,7 +88,7 @@ fn walk_unsafe(
Expr::Call { callee, .. } => {
let ty = &infer[*callee];
if let &Ty::Apply(ApplicationTy {
ctor: TypeCtor::FnDef(CallableDef::FunctionId(func)),
ctor: TypeCtor::FnDef(CallableDefId::FunctionId(func)),
..
}) = ty
{

View File

@ -3,7 +3,7 @@
use std::fmt;
use crate::{
db::HirDatabase, utils::generics, ApplicationTy, CallableDef, FnSig, GenericPredicate,
db::HirDatabase, utils::generics, ApplicationTy, CallableDefId, FnSig, GenericPredicate,
Obligation, OpaqueTyId, ProjectionTy, Substs, TraitRef, Ty, TypeCtor,
};
use hir_def::{
@ -263,9 +263,11 @@ impl HirDisplay for ApplicationTy {
TypeCtor::FnDef(def) => {
let sig = f.db.callable_item_signature(def).subst(&self.parameters);
match def {
CallableDef::FunctionId(ff) => write!(f, "fn {}", f.db.function_data(ff).name)?,
CallableDef::StructId(s) => write!(f, "{}", f.db.struct_data(s).name)?,
CallableDef::EnumVariantId(e) => {
CallableDefId::FunctionId(ff) => {
write!(f, "fn {}", f.db.function_data(ff).name)?
}
CallableDefId::StructId(s) => write!(f, "{}", f.db.struct_data(s).name)?,
CallableDefId::EnumVariantId(e) => {
write!(f, "{}", f.db.enum_data(e.parent).variants[e.local_id].name)?
}
};

View File

@ -17,7 +17,7 @@ use crate::{
autoderef, method_resolution, op,
traits::{FnTrait, InEnvironment},
utils::{generics, variant_data, Generics},
ApplicationTy, Binders, CallableDef, InferTy, IntTy, Mutability, Obligation, Rawness, Substs,
ApplicationTy, Binders, CallableDefId, InferTy, IntTy, Mutability, Obligation, Rawness, Substs,
TraitRef, Ty, TypeCtor,
};
@ -854,7 +854,7 @@ impl<'a> InferenceContext<'a> {
}
// add obligation for trait implementation, if this is a trait method
match def {
CallableDef::FunctionId(f) => {
CallableDefId::FunctionId(f) => {
if let AssocContainerId::TraitId(trait_) =
f.lookup(self.db.upcast()).container
{
@ -865,7 +865,7 @@ impl<'a> InferenceContext<'a> {
self.obligations.push(Obligation::Trait(TraitRef { trait_, substs }));
}
}
CallableDef::StructId(_) | CallableDef::EnumVariantId(_) => {}
CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => {}
}
}
}

View File

@ -33,7 +33,7 @@ use hir_def::{
TypeParamId,
};
use itertools::Itertools;
use ra_db::{impl_intern_key, salsa, CrateId};
use ra_db::{salsa, CrateId};
use crate::{
db::HirDatabase,
@ -44,7 +44,7 @@ use crate::{
pub use autoderef::autoderef;
pub use infer::{InferTy, InferenceResult};
pub use lower::CallableDef;
pub use lower::CallableDefId;
pub use lower::{
associated_type_shorthand_candidates, callable_item_sig, ImplTraitLoweringMode, TyDefId,
TyLoweringContext, ValueTyDefId,
@ -102,7 +102,7 @@ pub enum TypeCtor {
/// fn foo() -> i32 { 1 }
/// let bar = foo; // bar: fn() -> i32 {foo}
/// ```
FnDef(CallableDef),
FnDef(CallableDefId),
/// A pointer to a function. Written as `fn() -> i32`.
///
@ -140,12 +140,6 @@ pub enum TypeCtor {
Closure { def: DefWithBodyId, expr: ExprId },
}
/// This exists just for Chalk, because Chalk just has a single `FnDefId` where
/// we have different IDs for struct and enum variant constructors.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct CallableDefId(salsa::InternId);
impl_intern_key!(CallableDefId);
impl TypeCtor {
pub fn num_ty_params(self, db: &dyn HirDatabase) -> usize {
match self {
@ -773,7 +767,7 @@ impl Ty {
}
}
pub fn as_callable(&self) -> Option<(CallableDef, &Substs)> {
pub fn as_callable(&self) -> Option<(CallableDefId, &Substs)> {
match self {
Ty::Apply(ApplicationTy { ctor: TypeCtor::FnDef(callable_def), parameters }) => {
Some((*callable_def, parameters))

View File

@ -768,11 +768,11 @@ fn count_impl_traits(type_ref: &TypeRef) -> usize {
}
/// Build the signature of a callable item (function, struct or enum variant).
pub fn callable_item_sig(db: &dyn HirDatabase, def: CallableDef) -> PolyFnSig {
pub fn callable_item_sig(db: &dyn HirDatabase, def: CallableDefId) -> PolyFnSig {
match def {
CallableDef::FunctionId(f) => fn_sig_for_fn(db, f),
CallableDef::StructId(s) => fn_sig_for_struct_constructor(db, s),
CallableDef::EnumVariantId(e) => fn_sig_for_enum_variant_constructor(db, e),
CallableDefId::FunctionId(f) => fn_sig_for_fn(db, f),
CallableDefId::StructId(s) => fn_sig_for_struct_constructor(db, s),
CallableDefId::EnumVariantId(e) => fn_sig_for_enum_variant_constructor(db, e),
}
}
@ -1107,31 +1107,31 @@ fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> {
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum CallableDef {
pub enum CallableDefId {
FunctionId(FunctionId),
StructId(StructId),
EnumVariantId(EnumVariantId),
}
impl_from!(FunctionId, StructId, EnumVariantId for CallableDef);
impl_from!(FunctionId, StructId, EnumVariantId for CallableDefId);
impl CallableDef {
impl CallableDefId {
pub fn krate(self, db: &dyn HirDatabase) -> CrateId {
let db = db.upcast();
match self {
CallableDef::FunctionId(f) => f.lookup(db).module(db),
CallableDef::StructId(s) => s.lookup(db).container.module(db),
CallableDef::EnumVariantId(e) => e.parent.lookup(db).container.module(db),
CallableDefId::FunctionId(f) => f.lookup(db).module(db),
CallableDefId::StructId(s) => s.lookup(db).container.module(db),
CallableDefId::EnumVariantId(e) => e.parent.lookup(db).container.module(db),
}
.krate
}
}
impl From<CallableDef> for GenericDefId {
fn from(def: CallableDef) -> GenericDefId {
impl From<CallableDefId> for GenericDefId {
fn from(def: CallableDefId) -> GenericDefId {
match def {
CallableDef::FunctionId(f) => f.into(),
CallableDef::StructId(s) => s.into(),
CallableDef::EnumVariantId(e) => e.into(),
CallableDefId::FunctionId(f) => f.into(),
CallableDefId::StructId(s) => s.into(),
CallableDefId::EnumVariantId(e) => e.into(),
}
}
}

View File

@ -18,7 +18,7 @@ use crate::{
display::HirDisplay,
method_resolution::{TyFingerprint, ALL_FLOAT_FPS, ALL_INT_FPS},
utils::generics,
CallableDef, DebruijnIndex, FnSig, GenericPredicate, Substs, Ty, TypeCtor,
CallableDefId, DebruijnIndex, FnSig, GenericPredicate, Substs, Ty, TypeCtor,
};
use mapping::{
convert_where_clauses, generic_predicate_to_inline_bound, make_binders, TypeAliasAsValue,
@ -525,7 +525,7 @@ pub(crate) fn fn_def_datum_query(
_krate: CrateId,
fn_def_id: FnDefId,
) -> Arc<FnDefDatum> {
let callable_def: CallableDef = from_chalk(db, fn_def_id);
let callable_def: CallableDefId = from_chalk(db, fn_def_id);
let generic_params = generics(db.upcast(), callable_def.into());
let sig = db.callable_item_signature(callable_def);
let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST);
@ -552,14 +552,14 @@ pub(crate) fn fn_def_datum_query(
Arc::new(datum)
}
impl From<FnDefId> for crate::CallableDefId {
impl From<FnDefId> for crate::db::InternedCallableDefId {
fn from(fn_def_id: FnDefId) -> Self {
InternKey::from_intern_id(fn_def_id.0)
}
}
impl From<crate::CallableDefId> for FnDefId {
fn from(callable_def_id: crate::CallableDefId) -> Self {
impl From<crate::db::InternedCallableDefId> for FnDefId {
fn from(callable_def_id: crate::db::InternedCallableDefId) -> Self {
chalk_ir::FnDefId(callable_def_id.as_intern_id())
}
}

View File

@ -16,7 +16,7 @@ use crate::{
db::HirDatabase,
primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness},
traits::{Canonical, Obligation},
ApplicationTy, CallableDef, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId,
ApplicationTy, CallableDefId, GenericPredicate, InEnvironment, OpaqueTy, OpaqueTyId,
ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TyKind, TypeCtor,
};
@ -454,14 +454,14 @@ impl ToChalk for hir_def::ImplId {
}
}
impl ToChalk for CallableDef {
impl ToChalk for CallableDefId {
type Chalk = FnDefId;
fn to_chalk(self, db: &dyn HirDatabase) -> FnDefId {
db.intern_callable_def(self).into()
}
fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDef {
fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDefId {
db.lookup_intern_callable_def(fn_def_id.into())
}
}

View File

@ -5,7 +5,7 @@ use chalk_ir::{AliasTy, GenericArg, Goal, Goals, Lifetime, ProgramClauseImplicat
use itertools::Itertools;
use super::{from_chalk, Interner};
use crate::{db::HirDatabase, CallableDef, TypeCtor};
use crate::{db::HirDatabase, CallableDefId, TypeCtor};
use hir_def::{AdtId, AssocContainerId, DefWithBodyId, Lookup, TypeAliasId};
pub use unsafe_tls::{set_current_program, with_current_program};
@ -38,16 +38,16 @@ impl DebugContext<'_> {
}
TypeCtor::FnDef(def) => {
let name = match def {
CallableDef::FunctionId(ff) => self.0.function_data(ff).name.clone(),
CallableDef::StructId(s) => self.0.struct_data(s).name.clone(),
CallableDef::EnumVariantId(e) => {
CallableDefId::FunctionId(ff) => self.0.function_data(ff).name.clone(),
CallableDefId::StructId(s) => self.0.struct_data(s).name.clone(),
CallableDefId::EnumVariantId(e) => {
let enum_data = self.0.enum_data(e.parent);
enum_data.variants[e.local_id].name.clone()
}
};
match def {
CallableDef::FunctionId(_) => write!(f, "{{fn {}}}", name)?,
CallableDef::StructId(_) | CallableDef::EnumVariantId(_) => {
CallableDefId::FunctionId(_) => write!(f, "{{fn {}}}", name)?,
CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => {
write!(f, "{{ctor {}}}", name)?
}
}
@ -255,18 +255,18 @@ impl DebugContext<'_> {
fn_def_id: chalk_ir::FnDefId<Interner>,
fmt: &mut fmt::Formatter<'_>,
) -> Result<(), fmt::Error> {
let def: CallableDef = from_chalk(self.0, fn_def_id);
let def: CallableDefId = from_chalk(self.0, fn_def_id);
let name = match def {
CallableDef::FunctionId(ff) => self.0.function_data(ff).name.clone(),
CallableDef::StructId(s) => self.0.struct_data(s).name.clone(),
CallableDef::EnumVariantId(e) => {
CallableDefId::FunctionId(ff) => self.0.function_data(ff).name.clone(),
CallableDefId::StructId(s) => self.0.struct_data(s).name.clone(),
CallableDefId::EnumVariantId(e) => {
let enum_data = self.0.enum_data(e.parent);
enum_data.variants[e.local_id].name.clone()
}
};
match def {
CallableDef::FunctionId(_) => write!(fmt, "{{fn {}}}", name),
CallableDef::StructId(_) | CallableDef::EnumVariantId(_) => {
CallableDefId::FunctionId(_) => write!(fmt, "{{fn {}}}", name),
CallableDefId::StructId(_) | CallableDefId::EnumVariantId(_) => {
write!(fmt, "{{ctor {}}}", name)
}
}

View File

@ -97,7 +97,7 @@ pub(crate) fn outgoing_calls(db: &RootDatabase, position: FilePosition) -> Optio
//FIXME: Type::as_callable is broken
let callable_def = sema.type_of_expr(&expr.expr()?)?.as_callable()?;
match callable_def {
hir::CallableDef::FunctionId(it) => {
hir::CallableDefId::FunctionId(it) => {
let fn_def: hir::Function = it.into();
let nav = fn_def.to_nav(db);
Some(nav)

View File

@ -53,14 +53,14 @@ fn call_info_for_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Op
//FIXME: Type::as_callable is broken
let callable_def = sema.type_of_expr(&call.expr()?)?.as_callable()?;
match callable_def {
hir::CallableDef::FunctionId(it) => {
hir::CallableDefId::FunctionId(it) => {
let fn_def = it.into();
FunctionSignature::from_hir(sema.db, fn_def)
}
hir::CallableDef::StructId(it) => {
hir::CallableDefId::StructId(it) => {
FunctionSignature::from_struct(sema.db, it.into())?
}
hir::CallableDef::EnumVariantId(it) => {
hir::CallableDefId::EnumVariantId(it) => {
FunctionSignature::from_enum_variant(sema.db, it.into())?
}
}

View File

@ -324,13 +324,13 @@ fn get_fn_signature(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<
// FIXME: Type::as_callable is broken for closures
let callable_def = sema.type_of_expr(&expr.expr()?)?.as_callable()?;
match callable_def {
hir::CallableDef::FunctionId(it) => {
hir::CallableDefId::FunctionId(it) => {
Some(FunctionSignature::from_hir(sema.db, it.into()))
}
hir::CallableDef::StructId(it) => {
hir::CallableDefId::StructId(it) => {
FunctionSignature::from_struct(sema.db, it.into())
}
hir::CallableDef::EnumVariantId(it) => {
hir::CallableDefId::EnumVariantId(it) => {
FunctionSignature::from_enum_variant(sema.db, it.into())
}
}