7870: Use chalk_ir::AdtId r=Veykril a=Veykril

It's a bit unfortunate that we got two AdtId's now(technically 3 with the alias in the chalk module but that one won't allow pattern matching), one from hir_def and one from chalk_ir(hir_ty). But the hir_ty/chalk one doesn't leave hir so it shouldn't be that bad I suppose. Though if I see this right this will happen for almost all IDs.

I imagine most of the intermediate changes to using chalk ids will turn out not too nice until the refactor is over.

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2021-03-05 16:04:49 +00:00 committed by GitHub
commit 6c27c55041
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 68 additions and 73 deletions

View File

@ -1700,7 +1700,7 @@ pub fn is_fn(&self) -> bool {
pub fn is_packed(&self, db: &dyn HirDatabase) -> bool {
let adt_id = match self.ty.value {
Ty::Adt(adt_id, ..) => adt_id,
Ty::Adt(hir_ty::AdtId(adt_id), ..) => adt_id,
_ => return false,
};
@ -1728,8 +1728,8 @@ fn go(ty: &Ty) -> bool {
pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> {
let (variant_id, substs) = match self.ty.value {
Ty::Adt(AdtId::StructId(s), ref substs) => (s.into(), substs),
Ty::Adt(AdtId::UnionId(u), ref substs) => (u.into(), substs),
Ty::Adt(hir_ty::AdtId(AdtId::StructId(s)), ref substs) => (s.into(), substs),
Ty::Adt(hir_ty::AdtId(AdtId::UnionId(u)), ref substs) => (u.into(), substs),
_ => return Vec::new(),
};

View File

@ -2,9 +2,7 @@
use std::sync::Arc;
use hir_def::{
expr::Statement, path::path, resolver::HasResolver, AdtId, AssocItemId, DefWithBodyId,
};
use hir_def::{expr::Statement, path::path, resolver::HasResolver, AssocItemId, DefWithBodyId};
use hir_expand::{diagnostics::DiagnosticSink, name};
use rustc_hash::FxHashSet;
use syntax::{ast, AstPtr};
@ -17,7 +15,7 @@
MissingPatFields, RemoveThisSemicolon,
},
utils::variant_data,
InferenceResult, Ty,
AdtId, InferenceResult, Ty,
};
pub(crate) use hir_def::{
@ -382,10 +380,14 @@ fn validate_results_in_tail_expr(&mut self, body_id: ExprId, id: ExprId, db: &dy
};
let (params, required) = match mismatch.expected {
Ty::Adt(AdtId::EnumId(enum_id), ref parameters) if enum_id == core_result_enum => {
Ty::Adt(AdtId(hir_def::AdtId::EnumId(enum_id)), ref parameters)
if enum_id == core_result_enum =>
{
(parameters, "Ok".to_string())
}
Ty::Adt(AdtId::EnumId(enum_id), ref parameters) if enum_id == core_option_enum => {
Ty::Adt(AdtId(hir_def::AdtId::EnumId(enum_id)), ref parameters)
if enum_id == core_option_enum =>
{
(parameters, "Some".to_string())
}
_ => return,

View File

@ -222,12 +222,12 @@
adt::VariantData,
body::Body,
expr::{Expr, Literal, Pat, PatId},
AdtId, EnumVariantId, StructId, VariantId,
EnumVariantId, StructId, VariantId,
};
use la_arena::Idx;
use smallvec::{smallvec, SmallVec};
use crate::{db::HirDatabase, InferenceResult, Ty};
use crate::{db::HirDatabase, AdtId, InferenceResult, Ty};
#[derive(Debug, Clone, Copy)]
/// Either a pattern from the source code being analyzed, represented as
@ -627,7 +627,7 @@ pub(super) fn is_useful(
// - `!` type
// In those cases, no match arm is useful.
match cx.infer[cx.match_expr].strip_references() {
Ty::Adt(AdtId::EnumId(enum_id), ..) => {
Ty::Adt(AdtId(hir_def::AdtId::EnumId(enum_id)), ..) => {
if cx.db.enum_data(*enum_id).variants.is_empty() {
return Ok(Usefulness::NotUseful);
}

View File

@ -2,19 +2,20 @@
use std::{borrow::Cow, fmt};
use crate::{
db::HirDatabase, primitive, utils::generics, AliasTy, CallableDefId, CallableSig,
GenericPredicate, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs,
TraitRef, Ty,
};
use arrayvec::ArrayVec;
use chalk_ir::Mutability;
use hir_def::{
db::DefDatabase, find_path, generics::TypeParamProvenance, item_scope::ItemInNs, AdtId,
db::DefDatabase, find_path, generics::TypeParamProvenance, item_scope::ItemInNs,
AssocContainerId, HasModule, Lookup, ModuleId, TraitId,
};
use hir_expand::name::Name;
use crate::{
db::HirDatabase, primitive, utils::generics, AdtId, AliasTy, CallableDefId, CallableSig,
GenericPredicate, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs,
TraitRef, Ty,
};
pub struct HirFormatter<'a> {
pub db: &'a dyn HirDatabase,
fmt: &'a mut dyn fmt::Write,
@ -400,13 +401,13 @@ fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> {
write!(f, " -> {}", ret_display)?;
}
}
Ty::Adt(def_id, parameters) => {
Ty::Adt(AdtId(def_id), parameters) => {
match f.display_target {
DisplayTarget::Diagnostics | DisplayTarget::Test => {
let name = match *def_id {
AdtId::StructId(it) => f.db.struct_data(it).name.clone(),
AdtId::UnionId(it) => f.db.union_data(it).name.clone(),
AdtId::EnumId(it) => f.db.enum_data(it).name.clone(),
hir_def::AdtId::StructId(it) => f.db.struct_data(it).name.clone(),
hir_def::AdtId::UnionId(it) => f.db.union_data(it).name.clone(),
hir_def::AdtId::EnumId(it) => f.db.enum_data(it).name.clone(),
};
write!(f, "{}", name)?;
}

View File

@ -8,7 +8,7 @@
expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
path::{GenericArg, GenericArgs},
resolver::resolver_for_expr,
AdtId, AssocContainerId, FieldId, Lookup,
AssocContainerId, FieldId, Lookup,
};
use hir_expand::name::{name, Name};
use syntax::ast::RangeOp;
@ -21,8 +21,8 @@
primitive::{self, UintTy},
traits::{FnTrait, InEnvironment},
utils::{generics, variant_data, Generics},
Binders, CallableDefId, FnPointer, FnSig, Obligation, OpaqueTyId, Rawness, Scalar, Substs,
TraitRef, Ty,
AdtId, Binders, CallableDefId, FnPointer, FnSig, Obligation, OpaqueTyId, Rawness, Scalar,
Substs, TraitRef, Ty,
};
use super::{
@ -429,14 +429,14 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
Ty::Tuple(_, substs) => {
name.as_tuple_index().and_then(|idx| substs.0.get(idx).cloned())
}
Ty::Adt(AdtId::StructId(s), parameters) => {
Ty::Adt(AdtId(hir_def::AdtId::StructId(s)), parameters) => {
self.db.struct_data(s).variant_data.field(name).map(|local_id| {
let field = FieldId { parent: s.into(), local_id };
self.write_field_resolution(tgt_expr, field);
self.db.field_types(s.into())[field.local_id].clone().subst(&parameters)
})
}
Ty::Adt(AdtId::UnionId(u), parameters) => {
Ty::Adt(AdtId(hir_def::AdtId::UnionId(u)), parameters) => {
self.db.union_data(u).variant_data.field(name).map(|local_id| {
let field = FieldId { parent: u.into(), local_id };
self.write_field_resolution(tgt_expr, field);
@ -498,7 +498,7 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
_ => (),
}
sb = sb.fill(repeat_with(|| self.table.new_type_var()));
Ty::Adt(box_, sb.build())
Ty::adt_ty(box_, sb.build())
} else {
Ty::Unknown
}
@ -586,31 +586,31 @@ fn infer_expr_inner(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect));
match (range_type, lhs_ty, rhs_ty) {
(RangeOp::Exclusive, None, None) => match self.resolve_range_full() {
Some(adt) => Ty::Adt(adt, Substs::empty()),
Some(adt) => Ty::adt_ty(adt, Substs::empty()),
None => Ty::Unknown,
},
(RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() {
Some(adt) => Ty::Adt(adt, Substs::single(ty)),
Some(adt) => Ty::adt_ty(adt, Substs::single(ty)),
None => Ty::Unknown,
},
(RangeOp::Inclusive, None, Some(ty)) => {
match self.resolve_range_to_inclusive() {
Some(adt) => Ty::Adt(adt, Substs::single(ty)),
Some(adt) => Ty::adt_ty(adt, Substs::single(ty)),
None => Ty::Unknown,
}
}
(RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() {
Some(adt) => Ty::Adt(adt, Substs::single(ty)),
Some(adt) => Ty::adt_ty(adt, Substs::single(ty)),
None => Ty::Unknown,
},
(RangeOp::Inclusive, Some(_), Some(ty)) => {
match self.resolve_range_inclusive() {
Some(adt) => Ty::Adt(adt, Substs::single(ty)),
Some(adt) => Ty::adt_ty(adt, Substs::single(ty)),
None => Ty::Unknown,
}
}
(RangeOp::Exclusive, Some(ty), None) => match self.resolve_range_from() {
Some(adt) => Ty::Adt(adt, Substs::single(ty)),
Some(adt) => Ty::adt_ty(adt, Substs::single(ty)),
None => Ty::Unknown,
},
(RangeOp::Inclusive, _, None) => Ty::Unknown,

View File

@ -237,7 +237,7 @@ pub(super) fn infer_pat(
};
let inner_ty = self.infer_pat(*inner, inner_expected, default_bm);
Ty::Adt(box_adt, Substs::single(inner_ty))
Ty::adt_ty(box_adt, Substs::single(inner_ty))
}
None => Ty::Unknown,
},

View File

@ -27,9 +27,9 @@ macro_rules! eprintln {
use base_db::salsa;
use hir_def::{
builtin_type::BuiltinType, expr::ExprId, type_ref::Rawness, AdtId, AssocContainerId,
DefWithBodyId, FunctionId, GenericDefId, HasModule, LifetimeParamId, Lookup, TraitId,
TypeAliasId, TypeParamId,
builtin_type::BuiltinType, expr::ExprId, type_ref::Rawness, AssocContainerId, DefWithBodyId,
FunctionId, GenericDefId, HasModule, LifetimeParamId, Lookup, TraitId, TypeAliasId,
TypeParamId,
};
use itertools::Itertools;
@ -47,7 +47,9 @@ macro_rules! eprintln {
};
pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment};
pub use chalk_ir::{BoundVar, DebruijnIndex, Mutability, Scalar, TyVariableKind};
pub use chalk_ir::{AdtId, BoundVar, DebruijnIndex, Mutability, Scalar, TyVariableKind};
pub(crate) use crate::traits::chalk::Interner;
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub enum Lifetime {
@ -131,7 +133,7 @@ pub enum AliasTy {
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub enum Ty {
/// Structures, enumerations and unions.
Adt(AdtId, Substs),
Adt(AdtId<Interner>, Substs),
/// Represents an associated item like `Iterator::Item`. This is used
/// when we have tried to normalize a projection like `T::Item` but
@ -602,6 +604,10 @@ pub fn unit() -> Self {
Ty::Tuple(0, Substs::empty())
}
pub fn adt_ty(adt: hir_def::AdtId, substs: Substs) -> Ty {
Ty::Adt(AdtId(adt), substs)
}
pub fn fn_ptr(sig: CallableSig) -> Self {
Ty::Function(FnPointer {
num_args: sig.params().len(),
@ -650,9 +656,9 @@ pub fn strip_references(&self) -> &Ty {
t
}
pub fn as_adt(&self) -> Option<(AdtId, &Substs)> {
pub fn as_adt(&self) -> Option<(hir_def::AdtId, &Substs)> {
match self {
Ty::Adt(adt_def, parameters) => Some((*adt_def, parameters)),
Ty::Adt(AdtId(adt), parameters) => Some((*adt, parameters)),
_ => None,
}
}
@ -666,7 +672,7 @@ pub fn as_tuple(&self) -> Option<&Substs> {
pub fn as_generic_def(&self) -> Option<GenericDefId> {
match *self {
Ty::Adt(adt, ..) => Some(adt.into()),
Ty::Adt(AdtId(adt), ..) => Some(adt.into()),
Ty::FnDef(callable, ..) => Some(callable.into()),
Ty::AssociatedType(type_alias, ..) => Some(type_alias.into()),
Ty::ForeignType(type_alias, ..) => Some(type_alias.into()),

View File

@ -1100,7 +1100,7 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -
fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> {
let generics = generics(db.upcast(), adt.into());
let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
Binders::new(substs.len(), Ty::Adt(adt, substs))
Binders::new(substs.len(), Ty::adt_ty(adt, substs))
}
fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> {

View File

@ -8,8 +8,8 @@
use base_db::CrateId;
use chalk_ir::Mutability;
use hir_def::{
lang_item::LangItemTarget, AdtId, AssocContainerId, AssocItemId, FunctionId, GenericDefId,
HasModule, ImplId, Lookup, ModuleId, TraitId, TypeAliasId,
lang_item::LangItemTarget, AssocContainerId, AssocItemId, FunctionId, GenericDefId, HasModule,
ImplId, Lookup, ModuleId, TraitId, TypeAliasId,
};
use hir_expand::name::Name;
use rustc_hash::{FxHashMap, FxHashSet};
@ -19,8 +19,8 @@
db::HirDatabase,
primitive::{self, FloatTy, IntTy, UintTy},
utils::all_super_traits,
Canonical, DebruijnIndex, FnPointer, FnSig, InEnvironment, Scalar, Substs, TraitEnvironment,
TraitRef, Ty, TypeWalk,
AdtId, Canonical, DebruijnIndex, FnPointer, FnSig, InEnvironment, Scalar, Substs,
TraitEnvironment, TraitRef, Ty, TypeWalk,
};
/// This is used as a key for indexing impls.
@ -32,7 +32,7 @@ pub enum TyFingerprint {
Never,
RawPtr(Mutability),
Scalar(Scalar),
Adt(AdtId),
Adt(hir_def::AdtId),
Dyn(TraitId),
Tuple(usize),
ForeignType(TypeAliasId),
@ -50,7 +50,7 @@ pub(crate) fn for_impl(ty: &Ty) -> Option<TyFingerprint> {
&Ty::Slice(..) => TyFingerprint::Slice,
&Ty::Array(..) => TyFingerprint::Array,
&Ty::Scalar(scalar) => TyFingerprint::Scalar(scalar),
&Ty::Adt(adt, _) => TyFingerprint::Adt(adt),
&Ty::Adt(AdtId(adt), _) => TyFingerprint::Adt(adt),
&Ty::Tuple(cardinality, _) => TyFingerprint::Tuple(cardinality),
&Ty::Raw(mutability, ..) => TyFingerprint::RawPtr(mutability),
&Ty::ForeignType(alias_id, ..) => TyFingerprint::ForeignType(alias_id),
@ -231,7 +231,7 @@ macro_rules! lang_item_crate {
let mod_to_crate_ids = |module: ModuleId| Some(std::iter::once(module.krate()).collect());
let lang_item_targets = match self {
Ty::Adt(def_id, _) => {
Ty::Adt(AdtId(def_id), _) => {
return mod_to_crate_ids(def_id.module(db.upcast()));
}
Ty::ForeignType(type_alias_id) => {

View File

@ -315,9 +315,8 @@ fn trait_name(&self, trait_id: chalk_ir::TraitId<Interner>) -> String {
let id = from_chalk(self.db, trait_id);
self.db.trait_data(id).name.to_string()
}
fn adt_name(&self, adt_id: chalk_ir::AdtId<Interner>) -> String {
let id = from_chalk(self.db, adt_id);
match id {
fn adt_name(&self, chalk_ir::AdtId(adt_id): AdtId) -> String {
match adt_id {
hir_def::AdtId::StructId(id) => self.db.struct_data(id).name.to_string(),
hir_def::AdtId::EnumId(id) => self.db.enum_data(id).name.to_string(),
hir_def::AdtId::UnionId(id) => self.db.union_data(id).name.to_string(),
@ -488,8 +487,8 @@ pub(crate) fn struct_datum_query(
struct_id: AdtId,
) -> Arc<StructDatum> {
debug!("struct_datum {:?}", struct_id);
let adt_id = from_chalk(db, struct_id);
let type_ctor = Ty::Adt(adt_id, Substs::empty());
let type_ctor = Ty::Adt(struct_id, Substs::empty());
let chalk_ir::AdtId(adt_id) = struct_id;
debug!("struct {:?} = {:?}", struct_id, type_ctor);
let num_params = generics(db.upcast(), adt_id.into()).len();
let upstream = adt_id.module(db.upcast()).krate() != krate;
@ -684,10 +683,9 @@ pub(crate) fn fn_def_variance_query(
pub(crate) fn adt_variance_query(
db: &dyn HirDatabase,
_krate: CrateId,
adt_id: AdtId,
chalk_ir::AdtId(adt_id): AdtId,
) -> Variances {
let adt: crate::AdtId = from_chalk(db, adt_id);
let generic_params = generics(db.upcast(), adt.into());
let generic_params = generics(db.upcast(), adt_id.into());
Variances::from_iter(
&Interner,
std::iter::repeat(chalk_ir::Variance::Invariant).take(generic_params.len()),

View File

@ -86,7 +86,7 @@ fn to_chalk(self, db: &dyn HirDatabase) -> chalk_ir::Ty<Interner> {
Ty::Adt(adt_id, substs) => {
let substitution = substs.to_chalk(db);
chalk_ir::TyKind::Adt(chalk_ir::AdtId(adt_id), substitution).intern(&Interner)
chalk_ir::TyKind::Adt(adt_id, substitution).intern(&Interner)
}
Ty::Alias(AliasTy::Projection(proj_ty)) => {
let associated_ty_id = TypeAliasAsAssocType(proj_ty.associated_ty).to_chalk(db);
@ -183,7 +183,7 @@ fn from_chalk(db: &dyn HirDatabase, chalk: chalk_ir::Ty<Interner>) -> Self {
Ty::Dyn(predicates)
}
chalk_ir::TyKind::Adt(struct_id, subst) => Ty::Adt(struct_id.0, from_chalk(db, subst)),
chalk_ir::TyKind::Adt(adt_id, subst) => Ty::Adt(adt_id, from_chalk(db, subst)),
chalk_ir::TyKind::AssociatedType(type_id, subst) => Ty::AssociatedType(
from_chalk::<TypeAliasAsAssocType, _>(db, type_id).0,
from_chalk(db, subst),
@ -325,18 +325,6 @@ fn from_chalk(_db: &dyn HirDatabase, impl_id: ImplId) -> hir_def::ImplId {
}
}
impl ToChalk for hir_def::AdtId {
type Chalk = AdtId;
fn to_chalk(self, _db: &dyn HirDatabase) -> Self::Chalk {
chalk_ir::AdtId(self.into())
}
fn from_chalk(_db: &dyn HirDatabase, id: AdtId) -> Self {
id.0
}
}
impl ToChalk for CallableDefId {
type Chalk = FnDefId;