From 50e01d2bc7429d718e0783d75458a6a047ee2b70 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 1 Mar 2021 21:57:39 +0100 Subject: Use chalk_ir::AdtId --- crates/hir_ty/src/diagnostics/expr.rs | 14 ++++++++------ crates/hir_ty/src/diagnostics/match_check.rs | 6 +++--- crates/hir_ty/src/display.rs | 21 +++++++++++---------- crates/hir_ty/src/infer/expr.rs | 24 ++++++++++++------------ crates/hir_ty/src/infer/pat.rs | 2 +- crates/hir_ty/src/lib.rs | 22 ++++++++++++++-------- crates/hir_ty/src/lower.rs | 2 +- crates/hir_ty/src/method_resolution.rs | 14 +++++++------- crates/hir_ty/src/traits/chalk.rs | 14 ++++++-------- crates/hir_ty/src/traits/chalk/mapping.rs | 16 ++-------------- 10 files changed, 65 insertions(+), 70 deletions(-) (limited to 'crates/hir_ty') diff --git a/crates/hir_ty/src/diagnostics/expr.rs b/crates/hir_ty/src/diagnostics/expr.rs index 66a88e2b6..2751cd304 100644 --- a/crates/hir_ty/src/diagnostics/expr.rs +++ b/crates/hir_ty/src/diagnostics/expr.rs @@ -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 @@ use crate::{ MissingPatFields, RemoveThisSemicolon, }, utils::variant_data, - InferenceResult, Ty, + AdtId, InferenceResult, Ty, }; pub(crate) use hir_def::{ @@ -382,10 +380,14 @@ impl<'a, 'b> ExprValidator<'a, 'b> { }; 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, diff --git a/crates/hir_ty/src/diagnostics/match_check.rs b/crates/hir_ty/src/diagnostics/match_check.rs index 86fee0050..04d39c571 100644 --- a/crates/hir_ty/src/diagnostics/match_check.rs +++ b/crates/hir_ty/src/diagnostics/match_check.rs @@ -222,12 +222,12 @@ use hir_def::{ 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); } diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index d4a8b48e6..a0882a2a1 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs @@ -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 @@ impl HirDisplay for Ty { 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)?; } diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index cf1f1038a..ec2c13154 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -8,7 +8,7 @@ use hir_def::{ 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 @@ use crate::{ 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 @@ impl<'a> InferenceContext<'a> { 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(¶meters) }) } - 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 @@ impl<'a> InferenceContext<'a> { _ => (), } 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 @@ impl<'a> InferenceContext<'a> { 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, diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index eb099311c..987793e2e 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -237,7 +237,7 @@ impl<'a> InferenceContext<'a> { }; 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, }, diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index c2a20c480..e77f24e4e 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -27,9 +27,9 @@ use std::{iter, mem, ops::Deref, sync::Arc}; 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 @@ pub use lower::{ }; 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, 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 @@ impl Ty { 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 @@ impl 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 @@ impl Ty { pub fn as_generic_def(&self) -> Option { 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()), diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 1b5843d48..5fe5b8ad1 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -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 { 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 { diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index f301a8477..dfcf346fb 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -8,8 +8,8 @@ use arrayvec::ArrayVec; 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 @@ use crate::{ 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 @@ impl 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 @@ impl Ty { 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) => { diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index e513fa8f4..4378a9723 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs @@ -315,9 +315,8 @@ impl<'a> chalk_solve::RustIrDatabase for ChalkContext<'a> { 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) -> 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 { 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()), diff --git a/crates/hir_ty/src/traits/chalk/mapping.rs b/crates/hir_ty/src/traits/chalk/mapping.rs index db1760e6c..3a08b67e9 100644 --- a/crates/hir_ty/src/traits/chalk/mapping.rs +++ b/crates/hir_ty/src/traits/chalk/mapping.rs @@ -86,7 +86,7 @@ impl ToChalk for Ty { 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 @@ impl ToChalk for Ty { 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::(db, type_id).0, from_chalk(db, subst), @@ -325,18 +325,6 @@ impl ToChalk for 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; -- cgit v1.2.3