From 4daf931111029cee9bb43691f88c32f0aa47a802 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 21 Nov 2019 16:23:02 +0300 Subject: [PATCH] Remove old hir::generics module --- crates/ra_hir/src/code_model.rs | 46 +++++++++++++++++------- crates/ra_hir/src/db.rs | 5 ++- crates/ra_hir/src/from_id.rs | 17 +++++++-- crates/ra_hir/src/generics.rs | 54 ---------------------------- crates/ra_hir/src/lib.rs | 3 +- crates/ra_hir/src/ty.rs | 22 ++++++------ crates/ra_hir/src/ty/autoderef.rs | 4 +-- crates/ra_hir/src/ty/infer/expr.rs | 8 +++-- crates/ra_hir/src/ty/infer/path.rs | 3 +- crates/ra_hir/src/ty/lower.rs | 33 ++++++++--------- crates/ra_hir/src/ty/traits/chalk.rs | 13 ++++--- 11 files changed, 94 insertions(+), 114 deletions(-) delete mode 100644 crates/ra_hir/src/generics.rs diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 92860fb598..5690040a75 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -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 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 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, diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index a9982a70f6..ed0d680011 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -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::{ diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs index a3e9d8525f..e8ed04056a 100644 --- a/crates/ra_hir/src/from_id.rs +++ b/crates/ra_hir/src/from_id.rs @@ -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 for Crate { @@ -206,3 +207,15 @@ impl From for GenericDefId { } } } + +impl From 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() + } + } + } +} diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs deleted file mode 100644 index f1bf2ee9d1..0000000000 --- a/crates/ra_hir/src/generics.rs +++ /dev/null @@ -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 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; -} - -impl HasGenericParams for T -where - T: Into + Copy, -{ - fn generic_params(self, db: &impl DefDatabase) -> Arc { - db.generic_params(self.into().into()) - } -} diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 76c96bdcfb..4be49856ca 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -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::{ diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 36ece723f2..95b8df1810 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -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 { + pub fn as_generic_def(self) -> Option { 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) -> SubstsBuilder { + let def = def.into(); + let params = db.generic_params(def.into()); let param_count = params.count_params_including_parent(); Substs::builder(param_count) } diff --git a/crates/ra_hir/src/ty/autoderef.rs b/crates/ra_hir/src/ty/autoderef.rs index 5d8518041f..33ac782fe9 100644 --- a/crates/ra_hir/src/ty/autoderef.rs +++ b/crates/ra_hir/src/ty/autoderef.rs @@ -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 diff --git a/crates/ra_hir/src/ty/infer/expr.rs b/crates/ra_hir/src/ty/infer/expr.rs index ac570075f0..20a7e93525 100644 --- a/crates/ra_hir/src/ty/infer/expr.rs +++ b/crates/ra_hir/src/ty/infer/expr.rs @@ -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 })); } diff --git a/crates/ra_hir/src/ty/infer/path.rs b/crates/ra_hir/src/ty/infer/path.rs index 70136e514e..ee54d82173 100644 --- a/crates/ra_hir/src/ty/infer/path.rs +++ b/crates/ra_hir/src/ty/infer/path.rs @@ -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; } diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index c6ad0811b9..75c5525692 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -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::>(); - 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 + 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) -> 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); diff --git a/crates/ra_hir/src/ty/traits/chalk.rs b/crates/ra_hir/src/ty/traits/chalk.rs index 9bf93981a0..88785f3050 100644 --- a/crates/ra_hir/src/ty/traits/chalk.rs +++ b/crates/ra_hir/src/ty/traits/chalk.rs @@ -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>> { - 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) };