Remove duplicated crate id field from hir::Type

This commit is contained in:
Lukas Wirth 2022-04-15 20:14:35 +02:00
parent 7ce3ca5aab
commit 4b4a34327e
2 changed files with 48 additions and 86 deletions

View File

@ -809,7 +809,7 @@ impl Field {
};
let substs = TyBuilder::placeholder_subst(db, generic_def_id);
let ty = db.field_types(var_id)[self.id].clone().substitute(Interner, &substs);
Type::new(db, self.parent.module(db).id.krate(), var_id, ty)
Type::new(db, var_id, ty)
}
pub fn parent_def(&self, _db: &dyn HirDatabase) -> VariantDef {
@ -850,7 +850,7 @@ impl Struct {
}
pub fn ty(self, db: &dyn HirDatabase) -> Type {
Type::from_def(db, self.id.lookup(db.upcast()).container.krate(), self.id)
Type::from_def(db, self.id)
}
pub fn repr(self, db: &dyn HirDatabase) -> Option<ReprKind> {
@ -887,7 +887,7 @@ impl Union {
}
pub fn ty(self, db: &dyn HirDatabase) -> Type {
Type::from_def(db, self.id.lookup(db.upcast()).container.krate(), self.id)
Type::from_def(db, self.id)
}
pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> {
@ -929,7 +929,7 @@ impl Enum {
}
pub fn ty(self, db: &dyn HirDatabase) -> Type {
Type::from_def(db, self.id.lookup(db.upcast()).container.krate(), self.id)
Type::from_def(db, self.id)
}
}
@ -1005,7 +1005,7 @@ impl Adt {
/// general set of completions, but will not look very nice when printed.
pub fn ty(self, db: &dyn HirDatabase) -> Type {
let id = AdtId::from(self);
Type::from_def(db, id.module(db.upcast()).krate(), id)
Type::from_def(db, id)
}
/// Turns this ADT into a type with the given type parameters. This isn't
@ -1022,8 +1022,7 @@ impl Adt {
}
})
.build();
let krate = id.module(db.upcast()).krate();
Type::new(db, krate, id, ty)
Type::new(db, id, ty)
}
pub fn module(self, db: &dyn HirDatabase) -> Module {
@ -1213,18 +1212,8 @@ impl DefWithBody {
acc.push(
TypeMismatch {
expr,
expected: Type::new(
db,
krate,
DefWithBodyId::from(self),
mismatch.expected.clone(),
),
actual: Type::new(
db,
krate,
DefWithBodyId::from(self),
mismatch.actual.clone(),
),
expected: Type::new(db, DefWithBodyId::from(self), mismatch.expected.clone()),
actual: Type::new(db, DefWithBodyId::from(self), mismatch.actual.clone()),
}
.into(),
);
@ -1369,11 +1358,10 @@ impl Function {
/// Get this function's return type
pub fn ret_type(self, db: &dyn HirDatabase) -> Type {
let resolver = self.id.resolver(db.upcast());
let krate = self.krate_id(db);
let ret_type = &db.function_data(self.id).ret_type;
let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
let ty = ctx.lower_ty(ret_type);
Type::new_with_resolver_inner(db, krate, &resolver, ty)
Type::new_with_resolver_inner(db, &resolver, ty)
}
pub fn self_param(self, db: &dyn HirDatabase) -> Option<SelfParam> {
@ -1385,7 +1373,6 @@ impl Function {
pub fn assoc_fn_params(self, db: &dyn HirDatabase) -> Vec<Param> {
let resolver = self.id.resolver(db.upcast());
let krate = self.krate_id(db);
let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
let environment = db.trait_environment(self.id.into());
db.function_data(self.id)
@ -1393,7 +1380,7 @@ impl Function {
.iter()
.enumerate()
.map(|(idx, (_, type_ref))| {
let ty = Type { krate, env: environment.clone(), ty: ctx.lower_ty(type_ref) };
let ty = Type { env: environment.clone(), ty: ctx.lower_ty(type_ref) };
Param { func: self, ty, idx }
})
.collect()
@ -1408,7 +1395,6 @@ impl Function {
pub fn params_without_self(self, db: &dyn HirDatabase) -> Vec<Param> {
let resolver = self.id.resolver(db.upcast());
let krate = self.krate_id(db);
let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
let environment = db.trait_environment(self.id.into());
let skip = if db.function_data(self.id).has_self_param() { 1 } else { 0 };
@ -1418,7 +1404,7 @@ impl Function {
.enumerate()
.skip(skip)
.map(|(idx, (_, type_ref))| {
let ty = Type { krate, env: environment.clone(), ty: ctx.lower_ty(type_ref) };
let ty = Type { env: environment.clone(), ty: ctx.lower_ty(type_ref) };
Param { func: self, ty, idx }
})
.collect()
@ -1470,10 +1456,6 @@ impl Function {
result
}
fn krate_id(self, db: &dyn HirDatabase) -> CrateId {
self.id.lookup(db.upcast()).module(db.upcast()).krate()
}
}
// Note: logically, this belongs to `hir_ty`, but we are not using it there yet.
@ -1575,11 +1557,10 @@ impl SelfParam {
pub fn ty(&self, db: &dyn HirDatabase) -> Type {
let resolver = self.func.resolver(db.upcast());
let krate = self.func.lookup(db.upcast()).container.module(db.upcast()).krate();
let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
let environment = db.trait_environment(self.func.into());
Type { krate, env: environment, ty: ctx.lower_ty(&db.function_data(self.func).params[0].1) }
Type { env: environment, ty: ctx.lower_ty(&db.function_data(self.func).params[0].1) }
}
}
@ -1610,10 +1591,9 @@ impl Const {
pub fn ty(self, db: &dyn HirDatabase) -> Type {
let data = db.const_data(self.id);
let resolver = self.id.resolver(db.upcast());
let krate = self.id.lookup(db.upcast()).container.krate(db);
let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
let ty = ctx.lower_ty(&data.type_ref);
Type::new_with_resolver_inner(db, krate.id, &resolver, ty)
Type::new_with_resolver_inner(db, &resolver, ty)
}
pub fn eval(self, db: &dyn HirDatabase) -> Result<ComputedExpr, ConstEvalError> {
@ -1652,10 +1632,9 @@ impl Static {
pub fn ty(self, db: &dyn HirDatabase) -> Type {
let data = db.static_data(self.id);
let resolver = self.id.resolver(db.upcast());
let krate = self.id.lookup(db.upcast()).container.module(db.upcast()).krate();
let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
let ty = ctx.lower_ty(&data.type_ref);
Type::new_with_resolver_inner(db, krate, &resolver, ty)
Type::new_with_resolver_inner(db, &resolver, ty)
}
}
@ -1727,7 +1706,7 @@ impl TypeAlias {
}
pub fn ty(self, db: &dyn HirDatabase) -> Type {
Type::from_def(db, self.id.lookup(db.upcast()).module(db.upcast()).krate(), self.id)
Type::from_def(db, self.id)
}
pub fn name(self, db: &dyn HirDatabase) -> Name {
@ -2185,8 +2164,7 @@ impl Local {
let def = self.parent;
let infer = db.infer(def);
let ty = infer[self.pat_id].clone();
let krate = def.module(db.upcast()).krate();
Type::new(db, krate, def, ty)
Type::new(db, def, ty)
}
pub fn associated_locals(self, db: &dyn HirDatabase) -> Box<[Local]> {
@ -2373,10 +2351,9 @@ impl TypeParam {
pub fn ty(self, db: &dyn HirDatabase) -> Type {
let resolver = self.id.parent().resolver(db.upcast());
let krate = self.id.parent().module(db.upcast()).krate();
let ty =
TyKind::Placeholder(hir_ty::to_placeholder_idx(db, self.id.into())).intern(Interner);
Type::new_with_resolver_inner(db, krate, &resolver, ty)
Type::new_with_resolver_inner(db, &resolver, ty)
}
/// FIXME: this only lists trait bounds from the item defining the type
@ -2398,14 +2375,11 @@ impl TypeParam {
let params = db.generic_defaults(self.id.parent());
let local_idx = hir_ty::param_idx(db, self.id.into())?;
let resolver = self.id.parent().resolver(db.upcast());
let krate = self.id.parent().module(db.upcast()).krate();
let ty = params.get(local_idx)?.clone();
let subst = TyBuilder::placeholder_subst(db, self.id.parent());
let ty = ty.substitute(Interner, &subst_prefix(&subst, local_idx));
match ty.data(Interner) {
GenericArgData::Ty(x) => {
Some(Type::new_with_resolver_inner(db, krate, &resolver, x.clone()))
}
GenericArgData::Ty(x) => Some(Type::new_with_resolver_inner(db, &resolver, x.clone())),
_ => None,
}
}
@ -2461,9 +2435,7 @@ impl ConstParam {
}
pub fn ty(self, db: &dyn HirDatabase) -> Type {
let def = self.id.parent();
let krate = def.module(db.upcast()).krate();
Type::new(db, krate, def, db.const_param_ty(self.id))
Type::new(db, self.id.parent(), db.const_param_ty(self.id))
}
}
@ -2522,8 +2494,8 @@ impl Impl {
inherent.all_impls().chain(trait_.all_impls()).map(Self::from).collect()
}
pub fn all_for_type(db: &dyn HirDatabase, Type { krate, ty, .. }: Type) -> Vec<Impl> {
let def_crates = match method_resolution::def_crates(db, &ty, krate) {
pub fn all_for_type(db: &dyn HirDatabase, Type { ty, env }: Type) -> Vec<Impl> {
let def_crates = match method_resolution::def_crates(db, &ty, env.krate) {
Some(def_crates) => def_crates,
None => return Vec::new(),
};
@ -2589,10 +2561,9 @@ impl Impl {
pub fn self_ty(self, db: &dyn HirDatabase) -> Type {
let impl_data = db.impl_data(self.id);
let resolver = self.id.resolver(db.upcast());
let krate = self.id.lookup(db.upcast()).container.krate();
let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
let ty = ctx.lower_ty(&impl_data.self_ty);
Type::new_with_resolver_inner(db, krate, &resolver, ty)
Type::new_with_resolver_inner(db, &resolver, ty)
}
pub fn items(self, db: &dyn HirDatabase) -> Vec<AssocItem> {
@ -2615,31 +2586,29 @@ impl Impl {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Type {
krate: CrateId, // FIXME this is probably redundant with the TraitEnvironment
env: Arc<TraitEnvironment>,
ty: Ty,
}
impl Type {
pub(crate) fn new_with_resolver(db: &dyn HirDatabase, resolver: &Resolver, ty: Ty) -> Type {
let krate = resolver.krate();
Type::new_with_resolver_inner(db, krate, resolver, ty)
Type::new_with_resolver_inner(db, resolver, ty)
}
pub(crate) fn new_with_resolver_inner(
db: &dyn HirDatabase,
krate: CrateId,
resolver: &Resolver,
ty: Ty,
) -> Type {
let environment = resolver
.generic_def()
.map_or_else(|| Arc::new(TraitEnvironment::empty(krate)), |d| db.trait_environment(d));
Type { krate, env: environment, ty }
let environment = resolver.generic_def().map_or_else(
|| Arc::new(TraitEnvironment::empty(resolver.krate())),
|d| db.trait_environment(d),
);
Type { env: environment, ty }
}
pub(crate) fn new_for_crate(krate: CrateId, ty: Ty) -> Type {
Type { krate, env: Arc::new(TraitEnvironment::empty(krate)), ty }
Type { env: Arc::new(TraitEnvironment::empty(krate)), ty }
}
pub fn reference(inner: &Type, m: Mutability) -> Type {
@ -2653,25 +2622,22 @@ impl Type {
)
}
fn new(db: &dyn HirDatabase, krate: CrateId, lexical_env: impl HasResolver, ty: Ty) -> Type {
fn new(db: &dyn HirDatabase, lexical_env: impl HasResolver, ty: Ty) -> Type {
let resolver = lexical_env.resolver(db.upcast());
let environment = resolver
.generic_def()
.map_or_else(|| Arc::new(TraitEnvironment::empty(krate)), |d| db.trait_environment(d));
Type { krate, env: environment, ty }
let environment = resolver.generic_def().map_or_else(
|| Arc::new(TraitEnvironment::empty(resolver.krate())),
|d| db.trait_environment(d),
);
Type { env: environment, ty }
}
fn from_def(
db: &dyn HirDatabase,
krate: CrateId,
def: impl HasResolver + Into<TyDefId>,
) -> Type {
fn from_def(db: &dyn HirDatabase, def: impl HasResolver + Into<TyDefId>) -> Type {
let ty = TyBuilder::def_ty(db, def.into()).fill_with_unknown().build();
Type::new(db, krate, def, ty)
Type::new(db, def, ty)
}
pub fn new_slice(ty: Type) -> Type {
Type { krate: ty.krate, env: ty.env, ty: TyBuilder::slice(ty.ty) }
Type { env: ty.env, ty: TyBuilder::slice(ty.ty) }
}
pub fn is_unit(&self) -> bool {
@ -2727,7 +2693,7 @@ impl Type {
/// This function is used in `.await` syntax completion.
pub fn impls_future(&self, db: &dyn HirDatabase) -> bool {
let std_future_trait = db
.lang_item(self.krate, SmolStr::new_inline("future_trait"))
.lang_item(self.env.krate, SmolStr::new_inline("future_trait"))
.and_then(|it| it.as_trait());
let std_future_trait = match std_future_trait {
Some(it) => it,
@ -2744,7 +2710,7 @@ impl Type {
/// This function can be used to check if a particular type is callable, since FnOnce is a
/// supertrait of Fn and FnMut, so all callable types implements at least FnOnce.
pub fn impls_fnonce(&self, db: &dyn HirDatabase) -> bool {
let fnonce_trait = match FnTrait::FnOnce.get_id(db, self.krate) {
let fnonce_trait = match FnTrait::FnOnce.get_id(db, self.env.krate) {
Some(it) => it,
None => return false,
};
@ -2780,7 +2746,7 @@ impl Type {
binders: CanonicalVarKinds::empty(Interner),
};
db.trait_solve(self.krate, goal).is_some()
db.trait_solve(self.env.krate, goal).is_some()
}
pub fn normalize_trait_assoc_type(
@ -2815,7 +2781,7 @@ impl Type {
[TyVariableKind::General].into_iter(),
);
match db.trait_solve(self.krate, goal)? {
match db.trait_solve(self.env.krate, goal)? {
Solution::Unique(s) => s
.value
.subst
@ -2827,7 +2793,7 @@ impl Type {
}
pub fn is_copy(&self, db: &dyn HirDatabase) -> bool {
let lang_item = db.lang_item(self.krate, SmolStr::new_inline("copy"));
let lang_item = db.lang_item(self.env.krate, SmolStr::new_inline("copy"));
let copy_trait = match lang_item {
Some(LangItemTarget::TraitId(it)) => it,
_ => return false,
@ -3170,7 +3136,7 @@ impl Type {
}
fn derived(&self, ty: Ty) -> Type {
Type { krate: self.krate, env: self.env.clone(), ty }
Type { env: self.env.clone(), ty }
}
pub fn walk(&self, db: &dyn HirDatabase, mut cb: impl FnMut(Type)) {
@ -3480,7 +3446,7 @@ impl HasCrate for TypeAlias {
impl HasCrate for Type {
fn krate(&self, _db: &dyn HirDatabase) -> Crate {
self.krate.into()
self.env.krate.into()
}
}

View File

@ -44,7 +44,6 @@ use crate::{
Field, Function, Local, Macro, ModuleDef, Static, Struct, ToolModule, Trait, Type, TypeAlias,
Variant,
};
use base_db::CrateId;
/// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of
/// original source files. It should not be used inside the HIR itself.
@ -414,7 +413,6 @@ impl SourceAnalyzer {
db: &dyn HirDatabase,
literal: &ast::RecordExpr,
) -> Option<Vec<(Field, Type)>> {
let krate = self.resolver.krate();
let body = self.body()?;
let infer = self.infer.as_ref()?;
@ -423,7 +421,7 @@ impl SourceAnalyzer {
let (variant, missing_fields, _exhaustive) =
record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?;
let res = self.missing_fields(db, krate, substs, variant, missing_fields);
let res = self.missing_fields(db, substs, variant, missing_fields);
Some(res)
}
@ -432,7 +430,6 @@ impl SourceAnalyzer {
db: &dyn HirDatabase,
pattern: &ast::RecordPat,
) -> Option<Vec<(Field, Type)>> {
let krate = self.resolver.krate();
let body = self.body()?;
let infer = self.infer.as_ref()?;
@ -441,14 +438,13 @@ impl SourceAnalyzer {
let (variant, missing_fields, _exhaustive) =
record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?;
let res = self.missing_fields(db, krate, substs, variant, missing_fields);
let res = self.missing_fields(db, substs, variant, missing_fields);
Some(res)
}
fn missing_fields(
&self,
db: &dyn HirDatabase,
krate: CrateId,
substs: &Substitution,
variant: VariantId,
missing_fields: Vec<LocalFieldId>,
@ -460,7 +456,7 @@ impl SourceAnalyzer {
.map(|local_id| {
let field = FieldId { parent: variant, local_id };
let ty = field_types[local_id].clone().substitute(Interner, substs);
(field.into(), Type::new_with_resolver_inner(db, krate, &self.resolver, ty))
(field.into(), Type::new_with_resolver_inner(db, &self.resolver, ty))
})
.collect()
}