mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Use GenericDefIdMore
This commit is contained in:
parent
c2a16632d0
commit
5f39c5794e
@ -11,9 +11,9 @@ use hir_def::{
|
||||
per_ns::PerNs,
|
||||
resolver::{HasResolver, TypeNs},
|
||||
type_ref::TypeRef,
|
||||
AstItemDef, ConstId, ContainerId, EnumId, FunctionId, HasModule, ImplId, LocalEnumVariantId,
|
||||
LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId,
|
||||
TraitId, TypeAliasId, UnionId,
|
||||
AstItemDef, ConstId, ContainerId, EnumId, FunctionId, GenericDefId, HasModule, ImplId,
|
||||
LocalEnumVariantId, LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId,
|
||||
StaticId, StructId, TraitId, TypeAliasId, UnionId,
|
||||
};
|
||||
use hir_expand::{
|
||||
diagnostics::DiagnosticSink,
|
||||
@ -897,16 +897,6 @@ impl_froms!(
|
||||
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,
|
||||
@ -960,7 +950,7 @@ impl Local {
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct GenericParam {
|
||||
pub(crate) parent: GenericDef,
|
||||
pub(crate) parent: GenericDefId,
|
||||
pub(crate) idx: u32,
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,7 @@ use crate::{
|
||||
CallableDef, FnSig, GenericPredicate, InferenceResult, Namespace, Substs, Ty, TypableDef,
|
||||
TypeCtor,
|
||||
},
|
||||
Crate, DefWithBody, GenericDef, ImplBlock, Trait,
|
||||
Crate, DefWithBody, ImplBlock, Trait,
|
||||
};
|
||||
|
||||
pub use hir_def::{
|
||||
@ -49,7 +49,7 @@ pub trait HirDatabase: DefDatabase {
|
||||
#[salsa::invoke(crate::ty::generic_predicates_for_param_query)]
|
||||
fn generic_predicates_for_param(
|
||||
&self,
|
||||
def: GenericDef,
|
||||
def: GenericDefId,
|
||||
param_idx: u32,
|
||||
) -> Arc<[GenericPredicate]>;
|
||||
|
||||
@ -57,7 +57,7 @@ pub trait HirDatabase: DefDatabase {
|
||||
fn generic_predicates(&self, def: GenericDefId) -> Arc<[GenericPredicate]>;
|
||||
|
||||
#[salsa::invoke(crate::ty::generic_defaults_query)]
|
||||
fn generic_defaults(&self, def: GenericDef) -> Substs;
|
||||
fn generic_defaults(&self, def: GenericDefId) -> Substs;
|
||||
|
||||
#[salsa::invoke(crate::ty::method_resolution::CrateImplBlocks::impls_in_crate_query)]
|
||||
fn impls_in_crate(&self, krate: Crate) -> Arc<CrateImplBlocks>;
|
||||
|
@ -137,20 +137,6 @@ impl From<GenericDef> for GenericDefId {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<GenericDefId> for GenericDef {
|
||||
fn from(def: GenericDefId) -> Self {
|
||||
match def {
|
||||
GenericDefId::FunctionId(it) => GenericDef::Function(it.into()),
|
||||
GenericDefId::AdtId(it) => GenericDef::Adt(it.into()),
|
||||
GenericDefId::TraitId(it) => GenericDef::Trait(it.into()),
|
||||
GenericDefId::TypeAliasId(it) => GenericDef::TypeAlias(it.into()),
|
||||
GenericDefId::ImplId(it) => GenericDef::ImplBlock(it.into()),
|
||||
GenericDefId::EnumVariantId(it) => GenericDef::EnumVariant(it.into()),
|
||||
GenericDefId::ConstId(it) => GenericDef::Const(it.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AdtId> for TypableDef {
|
||||
fn from(id: AdtId) -> Self {
|
||||
Adt::from(id).into()
|
||||
@ -244,3 +230,13 @@ impl From<AttrDef> for AttrDefId {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AssocItem> for GenericDefId {
|
||||
fn from(item: AssocItem) -> Self {
|
||||
match item {
|
||||
AssocItem::Function(f) => f.id.into(),
|
||||
AssocItem::Const(c) => c.id.into(),
|
||||
AssocItem::TypeAlias(t) => t.id.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -251,7 +251,7 @@ impl SourceAnalyzer {
|
||||
let types = self.resolver.resolve_path_in_type_ns_fully(db, &path).map(|ty| match ty {
|
||||
TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
|
||||
TypeNs::GenericParam(idx) => PathResolution::GenericParam(GenericParam {
|
||||
parent: self.resolver.generic_def().unwrap().into(),
|
||||
parent: self.resolver.generic_def().unwrap(),
|
||||
idx,
|
||||
}),
|
||||
TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => {
|
||||
@ -326,7 +326,7 @@ impl SourceAnalyzer {
|
||||
resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()),
|
||||
resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()),
|
||||
resolver::ScopeDef::GenericParam(idx) => {
|
||||
let parent = self.resolver.generic_def().unwrap().into();
|
||||
let parent = self.resolver.generic_def().unwrap();
|
||||
ScopeDef::GenericParam(GenericParam { parent, idx })
|
||||
}
|
||||
resolver::ScopeDef::Local(pat_id) => {
|
||||
|
@ -21,8 +21,8 @@ use hir_def::{generics::GenericParams, AdtId, GenericDefId};
|
||||
use ra_db::{impl_intern_key, salsa};
|
||||
|
||||
use crate::{
|
||||
db::HirDatabase, expr::ExprId, util::make_mut_slice, Adt, Crate, DefWithBody, FloatTy,
|
||||
GenericDef, IntTy, Mutability, Name, Trait, TypeAlias, Uncertain,
|
||||
db::HirDatabase, expr::ExprId, util::make_mut_slice, Adt, Crate, DefWithBody, FloatTy, IntTy,
|
||||
Mutability, Name, Trait, TypeAlias, Uncertain,
|
||||
};
|
||||
use display::{HirDisplay, HirFormatter};
|
||||
|
||||
@ -356,9 +356,9 @@ impl Substs {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn build_for_def(db: &impl HirDatabase, def: impl Into<GenericDef>) -> SubstsBuilder {
|
||||
pub fn build_for_def(db: &impl HirDatabase, def: impl Into<GenericDefId>) -> SubstsBuilder {
|
||||
let def = def.into();
|
||||
let params = db.generic_params(def.into());
|
||||
let params = db.generic_params(def);
|
||||
let param_count = params.count_params_including_parent();
|
||||
Substs::builder(param_count)
|
||||
}
|
||||
|
@ -203,7 +203,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||
Container::ImplBlock(_) => self.find_self_types(&def, ty.clone()),
|
||||
Container::Trait(t) => {
|
||||
// we're picking this method
|
||||
let trait_substs = Substs::build_for_def(self.db, t)
|
||||
let trait_substs = Substs::build_for_def(self.db, t.id)
|
||||
.push(ty.clone())
|
||||
.fill(std::iter::repeat_with(|| self.new_type_var()))
|
||||
.build();
|
||||
|
@ -29,8 +29,8 @@ use crate::{
|
||||
Adt,
|
||||
},
|
||||
util::make_mut_slice,
|
||||
Const, Enum, EnumVariant, Function, GenericDef, ImplBlock, ModuleDef, Path, Static, Struct,
|
||||
Trait, TypeAlias, Union,
|
||||
Const, Enum, EnumVariant, Function, ImplBlock, ModuleDef, Path, Static, Struct, Trait,
|
||||
TypeAlias, Union,
|
||||
};
|
||||
|
||||
// FIXME: this is only really used in `type_for_def`, which contains a bunch of
|
||||
@ -261,8 +261,10 @@ impl Ty {
|
||||
let traits = traits_from_env.flat_map(|t| t.all_super_traits(db));
|
||||
for t in traits {
|
||||
if let Some(associated_ty) = t.associated_type_by_name(db, &segment.name) {
|
||||
let substs =
|
||||
Substs::build_for_def(db, t).push(self_ty.clone()).fill_with_unknown().build();
|
||||
let substs = Substs::build_for_def(db, t.id)
|
||||
.push(self_ty.clone())
|
||||
.fill_with_unknown()
|
||||
.build();
|
||||
// FIXME handle type parameters on the segment
|
||||
return Ty::Projection(ProjectionTy { associated_ty, parameters: substs });
|
||||
}
|
||||
@ -287,11 +289,11 @@ impl Ty {
|
||||
segment: &PathSegment,
|
||||
resolved: TypableDef,
|
||||
) -> Substs {
|
||||
let def_generic: Option<GenericDef> = match resolved {
|
||||
TypableDef::Function(func) => Some(func.into()),
|
||||
let def_generic: Option<GenericDefId> = match resolved {
|
||||
TypableDef::Function(func) => Some(func.id.into()),
|
||||
TypableDef::Adt(adt) => Some(adt.into()),
|
||||
TypableDef::EnumVariant(var) => Some(var.parent_enum(db).into()),
|
||||
TypableDef::TypeAlias(t) => Some(t.into()),
|
||||
TypableDef::EnumVariant(var) => Some(var.parent_enum(db).id.into()),
|
||||
TypableDef::TypeAlias(t) => Some(t.id.into()),
|
||||
TypableDef::Const(_) | TypableDef::Static(_) | TypableDef::BuiltinType(_) => None,
|
||||
};
|
||||
substs_from_path_segment(db, resolver, segment, def_generic, false)
|
||||
@ -338,7 +340,7 @@ pub(super) fn substs_from_path_segment(
|
||||
db: &impl HirDatabase,
|
||||
resolver: &Resolver,
|
||||
segment: &PathSegment,
|
||||
def_generic: Option<GenericDef>,
|
||||
def_generic: Option<GenericDefId>,
|
||||
add_self_param: bool,
|
||||
) -> Substs {
|
||||
let mut substs = Vec::new();
|
||||
@ -376,7 +378,7 @@ pub(super) fn substs_from_path_segment(
|
||||
|
||||
// handle defaults
|
||||
if let Some(def_generic) = def_generic {
|
||||
let default_substs = db.generic_defaults(def_generic);
|
||||
let default_substs = db.generic_defaults(def_generic.into());
|
||||
assert_eq!(substs.len(), default_substs.len());
|
||||
|
||||
for (i, default_ty) in default_substs.iter().enumerate() {
|
||||
@ -439,7 +441,7 @@ impl TraitRef {
|
||||
) -> Substs {
|
||||
let has_self_param =
|
||||
segment.args_and_bindings.as_ref().map(|a| a.has_self_type).unwrap_or(false);
|
||||
substs_from_path_segment(db, resolver, segment, Some(resolved.into()), !has_self_param)
|
||||
substs_from_path_segment(db, resolver, segment, Some(resolved.id.into()), !has_self_param)
|
||||
}
|
||||
|
||||
pub(crate) fn for_trait(db: &impl HirDatabase, trait_: Trait) -> TraitRef {
|
||||
@ -579,10 +581,10 @@ pub(crate) fn field_types_query(
|
||||
/// these are fine: `T: Foo<U::Item>, U: Foo<()>`.
|
||||
pub(crate) fn generic_predicates_for_param_query(
|
||||
db: &impl HirDatabase,
|
||||
def: GenericDef,
|
||||
def: GenericDefId,
|
||||
param_idx: u32,
|
||||
) -> Arc<[GenericPredicate]> {
|
||||
let resolver = GenericDefId::from(def).resolver(db);
|
||||
let resolver = def.resolver(db);
|
||||
resolver
|
||||
.where_predicates_in_scope()
|
||||
// we have to filter out all other predicates *first*, before attempting to lower them
|
||||
@ -615,8 +617,8 @@ 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);
|
||||
pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDefId) -> Substs {
|
||||
let resolver = def.resolver(db);
|
||||
let generic_params = db.generic_params(def.into());
|
||||
|
||||
let defaults = generic_params
|
||||
|
@ -365,7 +365,7 @@ fn generic_implements_goal(
|
||||
self_ty: Canonical<Ty>,
|
||||
) -> Canonical<InEnvironment<super::Obligation>> {
|
||||
let num_vars = self_ty.num_vars;
|
||||
let substs = super::Substs::build_for_def(db, trait_)
|
||||
let substs = super::Substs::build_for_def(db, trait_.id)
|
||||
.push(self_ty.value)
|
||||
.fill_with_bound_vars(num_vars as u32)
|
||||
.build();
|
||||
|
@ -736,7 +736,7 @@ fn closure_fn_trait_impl_datum(
|
||||
|
||||
let trait_ref = TraitRef {
|
||||
trait_,
|
||||
substs: Substs::build_for_def(db, trait_).push(self_ty).push(arg_ty).build(),
|
||||
substs: Substs::build_for_def(db, trait_.id).push(self_ty).push(arg_ty).build(),
|
||||
};
|
||||
|
||||
let output_ty_id = AssocTyValue::ClosureFnTraitImplOutput(data.clone()).to_chalk(db);
|
||||
|
@ -292,7 +292,7 @@ fn is_deprecated(node: impl HasAttrs, db: &impl HirDatabase) -> bool {
|
||||
}
|
||||
|
||||
fn has_non_default_type_params(def: hir::GenericDef, db: &db::RootDatabase) -> bool {
|
||||
let subst = db.generic_defaults(def);
|
||||
let subst = db.generic_defaults(def.into());
|
||||
subst.iter().any(|ty| ty == &Ty::Unknown)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user