Remove old hir::generics module

This commit is contained in:
Aleksey Kladov 2019-11-21 16:23:02 +03:00
parent 4f8f3393bc
commit 4daf931111
11 changed files with 94 additions and 114 deletions

View file

@ -27,7 +27,6 @@ use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
use crate::{
db::{AstDatabase, DefDatabase, HirDatabase},
expr::{BindingAnnotation, Body, BodySourceMap, ExprValidator, Pat, PatId},
generics::{GenericDef, HasGenericParams},
ids::{
AstItemDef, ConstId, EnumId, FunctionId, MacroDefId, StaticId, StructId, TraitId,
TypeAliasId,
@ -835,7 +834,7 @@ impl Trait {
// lifetime problems, but since there usually shouldn't be more than a
// few direct traits this should be fine (we could even use some kind of
// SmallVec if performance is a concern)
self.generic_params(db)
db.generic_params(self.id.into())
.where_predicates
.iter()
.filter_map(|pred| match &pred.type_ref {
@ -975,16 +974,6 @@ pub enum AssocItem {
// casting them, and somehow making the constructors private, which would be annoying.
impl_froms!(AssocItem: Function, Const, TypeAlias);
impl From<AssocItem> for crate::generics::GenericDef {
fn from(item: AssocItem) -> Self {
match item {
AssocItem::Function(f) => f.into(),
AssocItem::Const(c) => c.into(),
AssocItem::TypeAlias(t) => t.into(),
}
}
}
impl AssocItem {
pub fn module(self, db: &impl DefDatabase) -> Module {
match self {
@ -1004,6 +993,39 @@ impl AssocItem {
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum GenericDef {
Function(Function),
Adt(Adt),
Trait(Trait),
TypeAlias(TypeAlias),
ImplBlock(ImplBlock),
// enum variants cannot have generics themselves, but their parent enums
// can, and this makes some code easier to write
EnumVariant(EnumVariant),
// consts can have type parameters from their parents (i.e. associated consts of traits)
Const(Const),
}
impl_froms!(
GenericDef: Function,
Adt(Struct, Enum, Union),
Trait,
TypeAlias,
ImplBlock,
EnumVariant,
Const
);
impl From<AssocItem> for GenericDef {
fn from(item: AssocItem) -> Self {
match item {
AssocItem::Function(f) => f.into(),
AssocItem::Const(c) => c.into(),
AssocItem::TypeAlias(t) => t.into(),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Local {
pub(crate) parent: DefWithBody,

View file

@ -8,7 +8,6 @@ use ra_syntax::SmolStr;
use crate::{
debug::HirDebugDatabase,
generics::GenericDef,
ids,
lang_item::{LangItemTarget, LangItems},
ty::{
@ -18,8 +17,8 @@ use crate::{
TypeCtor,
},
type_alias::TypeAliasData,
Const, ConstData, Crate, DefWithBody, FnData, Function, ImplBlock, Module, Static, StructField,
Trait, TypeAlias,
Const, ConstData, Crate, DefWithBody, FnData, Function, GenericDef, ImplBlock, Module, Static,
StructField, Trait, TypeAlias,
};
pub use hir_def::db::{

View file

@ -9,8 +9,9 @@ use hir_def::{
};
use crate::{
ty::TypableDef, Adt, AssocItem, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef,
ModuleDef, Static, TypeAlias,
ty::{CallableDef, TypableDef},
Adt, AssocItem, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef, ModuleDef,
Static, TypeAlias,
};
impl From<ra_db::CrateId> for Crate {
@ -206,3 +207,15 @@ impl From<Adt> for GenericDefId {
}
}
}
impl From<CallableDef> for GenericDefId {
fn from(def: CallableDef) -> Self {
match def {
CallableDef::Function(it) => it.id.into(),
CallableDef::Struct(it) => it.id.into(),
CallableDef::EnumVariant(it) => {
EnumVariantId { parent: it.parent.id, local_id: it.id }.into()
}
}
}
}

View file

@ -1,54 +0,0 @@
//! Temp module to wrap hir_def::generics
use std::sync::Arc;
use crate::{
db::DefDatabase, Adt, Const, Container, Enum, EnumVariant, Function, ImplBlock, Struct, Trait,
TypeAlias, Union,
};
pub use hir_def::generics::{GenericParam, GenericParams, WherePredicate};
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
pub enum GenericDef {
Function(Function),
Adt(Adt),
Trait(Trait),
TypeAlias(TypeAlias),
ImplBlock(ImplBlock),
// enum variants cannot have generics themselves, but their parent enums
// can, and this makes some code easier to write
EnumVariant(EnumVariant),
// consts can have type parameters from their parents (i.e. associated consts of traits)
Const(Const),
}
impl_froms!(
GenericDef: Function,
Adt(Struct, Enum, Union),
Trait,
TypeAlias,
ImplBlock,
EnumVariant,
Const
);
impl From<Container> for GenericDef {
fn from(c: Container) -> Self {
match c {
Container::Trait(trait_) => trait_.into(),
Container::ImplBlock(impl_block) => impl_block.into(),
}
}
}
pub trait HasGenericParams: Copy {
fn generic_params(self, db: &impl DefDatabase) -> Arc<GenericParams>;
}
impl<T> HasGenericParams for T
where
T: Into<GenericDef> + Copy,
{
fn generic_params(self, db: &impl DefDatabase) -> Arc<GenericParams> {
db.generic_params(self.into().into())
}
}

View file

@ -37,7 +37,6 @@ mod ty;
mod impl_block;
mod expr;
mod lang_item;
pub mod generics;
pub mod diagnostics;
mod util;
@ -60,10 +59,10 @@ pub use crate::{
EnumVariant, FieldSource, FnData, Function, GenericParam, HasBody, ImplBlock, Local,
MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField, Trait,
TypeAlias, Union, VariantDef,
GenericDef
},
expr::ExprScopes,
from_source::FromSource,
generics::GenericDef,
ids::{HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroFile},
source_binder::{PathResolution, ScopeEntryWithSyntax, SourceAnalyzer},
ty::{

View file

@ -17,12 +17,11 @@ use std::ops::Deref;
use std::sync::Arc;
use std::{fmt, iter, mem};
use hir_def::{generics::GenericParams, AdtId};
use crate::{
db::HirDatabase,
expr::ExprId,
generics::{GenericParams, HasGenericParams},
util::make_mut_slice,
Adt, Crate, DefWithBody, FloatTy, IntTy, Mutability, Name, Trait, TypeAlias, Uncertain,
db::HirDatabase, expr::ExprId, util::make_mut_slice, Adt, Crate, DefWithBody, FloatTy,
GenericDef, IntTy, Mutability, Name, Trait, TypeAlias, Uncertain,
};
use display::{HirDisplay, HirFormatter};
@ -131,15 +130,15 @@ impl TypeCtor {
| TypeCtor::Closure { .. } // 1 param representing the signature of the closure
=> 1,
TypeCtor::Adt(adt) => {
let generic_params = adt.generic_params(db);
let generic_params = db.generic_params(AdtId::from(adt).into());
generic_params.count_params_including_parent()
}
TypeCtor::FnDef(callable) => {
let generic_params = callable.generic_params(db);
let generic_params = db.generic_params(callable.into());
generic_params.count_params_including_parent()
}
TypeCtor::AssociatedType(type_alias) => {
let generic_params = type_alias.generic_params(db);
let generic_params = db.generic_params(type_alias.id.into());
generic_params.count_params_including_parent()
}
TypeCtor::FnPtr { num_args } => num_args as usize + 1,
@ -168,7 +167,7 @@ impl TypeCtor {
}
}
pub fn as_generic_def(self) -> Option<crate::generics::GenericDef> {
pub fn as_generic_def(self) -> Option<crate::GenericDef> {
match self {
TypeCtor::Bool
| TypeCtor::Char
@ -348,8 +347,9 @@ impl Substs {
)
}
pub fn build_for_def(db: &impl HirDatabase, def: impl HasGenericParams) -> SubstsBuilder {
let params = def.generic_params(db);
pub fn build_for_def(db: &impl HirDatabase, def: impl Into<GenericDef>) -> SubstsBuilder {
let def = def.into();
let params = db.generic_params(def.into());
let param_count = params.count_params_including_parent();
Substs::builder(param_count)
}

View file

@ -10,7 +10,7 @@ use hir_expand::name;
use log::{info, warn};
use super::{traits::Solution, Canonical, Substs, Ty, TypeWalk};
use crate::{db::HirDatabase, generics::HasGenericParams};
use crate::{db::HirDatabase};
const AUTODEREF_RECURSION_LIMIT: usize = 10;
@ -46,7 +46,7 @@ fn deref_by_trait(
};
let target = deref_trait.associated_type_by_name(db, &name::TARGET_TYPE)?;
let generic_params = target.generic_params(db);
let generic_params = db.generic_params(target.id.into());
if generic_params.count_params_including_parent() != 1 {
// the Target type + Deref trait should only have one generic parameter,
// namely Deref's Self type

View file

@ -5,6 +5,7 @@ use std::sync::Arc;
use hir_def::{
builtin_type::Signedness,
generics::GenericParams,
path::{GenericArg, GenericArgs},
resolver::resolver_for_expr,
};
@ -13,7 +14,6 @@ use hir_expand::name;
use crate::{
db::HirDatabase,
expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
generics::{GenericParams, HasGenericParams},
ty::{
autoderef, method_resolution, op, CallableDef, InferTy, IntTy, Mutability, Namespace,
Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk,
@ -534,7 +534,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
(
ty,
self.db.type_for_def(func.into(), Namespace::Values),
Some(func.generic_params(self.db)),
Some(self.db.generic_params(func.id.into())),
)
}
None => (receiver_ty, Ty::Unknown, None),
@ -645,7 +645,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
if let Some(trait_) = f.parent_trait(self.db) {
// construct a TraitDef
let substs = a_ty.parameters.prefix(
trait_.generic_params(self.db).count_params_including_parent(),
self.db
.generic_params(trait_.id.into())
.count_params_including_parent(),
);
self.obligations.push(Obligation::Trait(TraitRef { trait_, substs }));
}

View file

@ -7,7 +7,6 @@ use hir_def::{
use crate::{
db::HirDatabase,
generics::HasGenericParams,
ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk},
AssocItem, Container, Function, Name, Path,
};
@ -230,7 +229,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
if let ValueNs::FunctionId(func) = def {
let func = Function::from(*func);
// We only do the infer if parent has generic params
let gen = func.generic_params(self.db);
let gen = self.db.generic_params(func.id.into());
if gen.count_parent_params() == 0 {
return None;
}

View file

@ -10,10 +10,11 @@ use std::sync::Arc;
use hir_def::{
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType},
generics::WherePredicate,
path::{GenericArg, PathSegment},
resolver::{HasResolver, Resolver, TypeNs},
type_ref::{TypeBound, TypeRef},
GenericDefId,
AdtId, GenericDefId,
};
use super::{
@ -22,15 +23,13 @@ use super::{
};
use crate::{
db::HirDatabase,
generics::HasGenericParams,
generics::{GenericDef, WherePredicate},
ty::{
primitive::{FloatTy, IntTy, Uncertain},
Adt,
},
util::make_mut_slice,
Const, Enum, EnumVariant, Function, ImplBlock, ModuleDef, Path, Static, Struct, StructField,
Trait, TypeAlias, Union, VariantDef,
Const, Enum, EnumVariant, Function, GenericDef, ImplBlock, ModuleDef, Path, Static, Struct,
StructField, Trait, TypeAlias, Union, VariantDef,
};
// FIXME: this is only really used in `type_for_def`, which contains a bunch of
@ -342,7 +341,7 @@ pub(super) fn substs_from_path_segment(
add_self_param: bool,
) -> Substs {
let mut substs = Vec::new();
let def_generics = def_generic.map(|def| def.generic_params(db));
let def_generics = def_generic.map(|def| db.generic_params(def.into()));
let (parent_param_count, param_count) =
def_generics.map_or((0, 0), |g| (g.count_parent_params(), g.params.len()));
@ -443,7 +442,7 @@ impl TraitRef {
}
pub(crate) fn for_trait(db: &impl HirDatabase, trait_: Trait) -> TraitRef {
let substs = Substs::identity(&trait_.generic_params(db));
let substs = Substs::identity(&db.generic_params(trait_.id.into()));
TraitRef { trait_, substs }
}
@ -611,7 +610,7 @@ pub(crate) fn generic_predicates_query(
/// Resolve the default type params from generics
pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> Substs {
let resolver = GenericDefId::from(def).resolver(db);
let generic_params = def.generic_params(db);
let generic_params = db.generic_params(def.into());
let defaults = generic_params
.params_including_parent()
@ -633,7 +632,7 @@ fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig {
/// Build the declared type of a function. This should not need to look at the
/// function body.
fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty {
let generics = def.generic_params(db);
let generics = db.generic_params(def.id.into());
let substs = Substs::identity(&generics);
Ty::apply(TypeCtor::FnDef(def.into()), substs)
}
@ -716,7 +715,7 @@ fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty {
if struct_data.variant_data.fields().is_none() {
return type_for_adt(db, def); // Unit struct
}
let generics = def.generic_params(db);
let generics = db.generic_params(def.id.into());
let substs = Substs::identity(&generics);
Ty::apply(TypeCtor::FnDef(def.into()), substs)
}
@ -732,7 +731,7 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant)
.iter()
.map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref))
.collect::<Vec<_>>();
let generics = def.parent_enum(db).generic_params(db);
let generics = db.generic_params(def.parent_enum(db).id.into());
let substs = Substs::identity(&generics);
let ret = type_for_adt(db, def.parent_enum(db)).subst(&substs);
FnSig::from_params_and_return(params, ret)
@ -744,18 +743,20 @@ fn type_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant) ->
if var_data.fields().is_none() {
return type_for_adt(db, def.parent_enum(db)); // Unit variant
}
let generics = def.parent_enum(db).generic_params(db);
let generics = db.generic_params(def.parent_enum(db).id.into());
let substs = Substs::identity(&generics);
Ty::apply(TypeCtor::FnDef(def.into()), substs)
}
fn type_for_adt(db: &impl HirDatabase, adt: impl Into<Adt> + HasGenericParams) -> Ty {
let generics = adt.generic_params(db);
Ty::apply(TypeCtor::Adt(adt.into()), Substs::identity(&generics))
fn type_for_adt(db: &impl HirDatabase, adt: impl Into<Adt>) -> Ty {
let adt = adt.into();
let adt_id: AdtId = adt.into();
let generics = db.generic_params(adt_id.into());
Ty::apply(TypeCtor::Adt(adt), Substs::identity(&generics))
}
fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty {
let generics = t.generic_params(db);
let generics = db.generic_params(t.id.into());
let resolver = t.id.resolver(db);
let type_ref = t.type_ref(db);
let substs = Substs::identity(&generics);

View file

@ -16,10 +16,9 @@ use ra_db::salsa::{InternId, InternKey};
use super::{AssocTyValue, Canonical, ChalkContext, Impl, Obligation};
use crate::{
db::HirDatabase,
generics::{GenericDef, HasGenericParams},
ty::display::HirDisplay,
ty::{ApplicationTy, GenericPredicate, ProjectionTy, Substs, TraitRef, Ty, TypeCtor, TypeWalk},
Crate, HasBody, ImplBlock, Trait, TypeAlias,
Crate, GenericDef, HasBody, ImplBlock, Trait, TypeAlias,
};
/// This represents a trait whose name we could not resolve.
@ -509,7 +508,7 @@ pub(crate) fn associated_ty_data_query(
Some(crate::Container::Trait(t)) => t,
_ => panic!("associated type not in trait"),
};
let generic_params = type_alias.generic_params(db);
let generic_params = db.generic_params(type_alias.id.into());
let bound_data = chalk_rust_ir::AssociatedTyDatumBound {
// FIXME add bounds and where clauses
bounds: vec![],
@ -550,7 +549,7 @@ pub(crate) fn trait_datum_query(
}
let trait_: Trait = from_chalk(db, trait_id);
debug!("trait {:?} = {:?}", trait_id, trait_.name(db));
let generic_params = trait_.generic_params(db);
let generic_params = db.generic_params(trait_.id.into());
let bound_vars = Substs::bound_vars(&generic_params);
let flags = chalk_rust_ir::TraitFlags {
auto: trait_.is_auto(db),
@ -594,7 +593,7 @@ pub(crate) fn struct_datum_query(
let where_clauses = type_ctor
.as_generic_def()
.map(|generic_def| {
let generic_params = generic_def.generic_params(db);
let generic_params = db.generic_params(generic_def.into());
let bound_vars = Substs::bound_vars(&generic_params);
convert_where_clauses(db, generic_def, &bound_vars)
})
@ -634,7 +633,7 @@ fn impl_block_datum(
impl_id: ImplId,
impl_block: ImplBlock,
) -> Option<Arc<ImplDatum<ChalkIr>>> {
let generic_params = impl_block.generic_params(db);
let generic_params = db.generic_params(impl_block.id.into());
let bound_vars = Substs::bound_vars(&generic_params);
let trait_ref = impl_block.target_trait_ref(db)?.subst(&bound_vars);
let trait_ = trait_ref.trait_;
@ -786,7 +785,7 @@ fn type_alias_associated_ty_value(
let assoc_ty = trait_
.associated_type_by_name(db, &type_alias.name(db))
.expect("assoc ty value should not exist"); // validated when building the impl data as well
let generic_params = impl_block.generic_params(db);
let generic_params = db.generic_params(impl_block.id.into());
let bound_vars = Substs::bound_vars(&generic_params);
let ty = db.type_for_def(type_alias.into(), crate::ty::Namespace::Types).subst(&bound_vars);
let value_bound = chalk_rust_ir::AssociatedTyValueBound { ty: ty.to_chalk(db) };