Skip to content

Commit 50e01d2

Browse files
committed
Use chalk_ir::AdtId
1 parent 16a76aa commit 50e01d2

File tree

11 files changed

+68
-73
lines changed

11 files changed

+68
-73
lines changed

crates/hir/src/code_model.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,7 +1700,7 @@ impl Type {
17001700

17011701
pub fn is_packed(&self, db: &dyn HirDatabase) -> bool {
17021702
let adt_id = match self.ty.value {
1703-
Ty::Adt(adt_id, ..) => adt_id,
1703+
Ty::Adt(hir_ty::AdtId(adt_id), ..) => adt_id,
17041704
_ => return false,
17051705
};
17061706

@@ -1728,8 +1728,8 @@ impl Type {
17281728

17291729
pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> {
17301730
let (variant_id, substs) = match self.ty.value {
1731-
Ty::Adt(AdtId::StructId(s), ref substs) => (s.into(), substs),
1732-
Ty::Adt(AdtId::UnionId(u), ref substs) => (u.into(), substs),
1731+
Ty::Adt(hir_ty::AdtId(AdtId::StructId(s)), ref substs) => (s.into(), substs),
1732+
Ty::Adt(hir_ty::AdtId(AdtId::UnionId(u)), ref substs) => (u.into(), substs),
17331733
_ => return Vec::new(),
17341734
};
17351735

crates/hir_ty/src/diagnostics/expr.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
33
use std::sync::Arc;
44

5-
use hir_def::{
6-
expr::Statement, path::path, resolver::HasResolver, AdtId, AssocItemId, DefWithBodyId,
7-
};
5+
use hir_def::{expr::Statement, path::path, resolver::HasResolver, AssocItemId, DefWithBodyId};
86
use hir_expand::{diagnostics::DiagnosticSink, name};
97
use rustc_hash::FxHashSet;
108
use syntax::{ast, AstPtr};
@@ -17,7 +15,7 @@ use crate::{
1715
MissingPatFields, RemoveThisSemicolon,
1816
},
1917
utils::variant_data,
20-
InferenceResult, Ty,
18+
AdtId, InferenceResult, Ty,
2119
};
2220

2321
pub(crate) use hir_def::{
@@ -382,10 +380,14 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
382380
};
383381

384382
let (params, required) = match mismatch.expected {
385-
Ty::Adt(AdtId::EnumId(enum_id), ref parameters) if enum_id == core_result_enum => {
383+
Ty::Adt(AdtId(hir_def::AdtId::EnumId(enum_id)), ref parameters)
384+
if enum_id == core_result_enum =>
385+
{
386386
(parameters, "Ok".to_string())
387387
}
388-
Ty::Adt(AdtId::EnumId(enum_id), ref parameters) if enum_id == core_option_enum => {
388+
Ty::Adt(AdtId(hir_def::AdtId::EnumId(enum_id)), ref parameters)
389+
if enum_id == core_option_enum =>
390+
{
389391
(parameters, "Some".to_string())
390392
}
391393
_ => return,

crates/hir_ty/src/diagnostics/match_check.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,12 @@ use hir_def::{
222222
adt::VariantData,
223223
body::Body,
224224
expr::{Expr, Literal, Pat, PatId},
225-
AdtId, EnumVariantId, StructId, VariantId,
225+
EnumVariantId, StructId, VariantId,
226226
};
227227
use la_arena::Idx;
228228
use smallvec::{smallvec, SmallVec};
229229

230-
use crate::{db::HirDatabase, InferenceResult, Ty};
230+
use crate::{db::HirDatabase, AdtId, InferenceResult, Ty};
231231

232232
#[derive(Debug, Clone, Copy)]
233233
/// Either a pattern from the source code being analyzed, represented as
@@ -627,7 +627,7 @@ pub(super) fn is_useful(
627627
// - `!` type
628628
// In those cases, no match arm is useful.
629629
match cx.infer[cx.match_expr].strip_references() {
630-
Ty::Adt(AdtId::EnumId(enum_id), ..) => {
630+
Ty::Adt(AdtId(hir_def::AdtId::EnumId(enum_id)), ..) => {
631631
if cx.db.enum_data(*enum_id).variants.is_empty() {
632632
return Ok(Usefulness::NotUseful);
633633
}

crates/hir_ty/src/display.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
33
use std::{borrow::Cow, fmt};
44

5-
use crate::{
6-
db::HirDatabase, primitive, utils::generics, AliasTy, CallableDefId, CallableSig,
7-
GenericPredicate, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs,
8-
TraitRef, Ty,
9-
};
105
use arrayvec::ArrayVec;
116
use chalk_ir::Mutability;
127
use hir_def::{
13-
db::DefDatabase, find_path, generics::TypeParamProvenance, item_scope::ItemInNs, AdtId,
8+
db::DefDatabase, find_path, generics::TypeParamProvenance, item_scope::ItemInNs,
149
AssocContainerId, HasModule, Lookup, ModuleId, TraitId,
1510
};
1611
use hir_expand::name::Name;
1712

13+
use crate::{
14+
db::HirDatabase, primitive, utils::generics, AdtId, AliasTy, CallableDefId, CallableSig,
15+
GenericPredicate, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs,
16+
TraitRef, Ty,
17+
};
18+
1819
pub struct HirFormatter<'a> {
1920
pub db: &'a dyn HirDatabase,
2021
fmt: &'a mut dyn fmt::Write,
@@ -400,13 +401,13 @@ impl HirDisplay for Ty {
400401
write!(f, " -> {}", ret_display)?;
401402
}
402403
}
403-
Ty::Adt(def_id, parameters) => {
404+
Ty::Adt(AdtId(def_id), parameters) => {
404405
match f.display_target {
405406
DisplayTarget::Diagnostics | DisplayTarget::Test => {
406407
let name = match *def_id {
407-
AdtId::StructId(it) => f.db.struct_data(it).name.clone(),
408-
AdtId::UnionId(it) => f.db.union_data(it).name.clone(),
409-
AdtId::EnumId(it) => f.db.enum_data(it).name.clone(),
408+
hir_def::AdtId::StructId(it) => f.db.struct_data(it).name.clone(),
409+
hir_def::AdtId::UnionId(it) => f.db.union_data(it).name.clone(),
410+
hir_def::AdtId::EnumId(it) => f.db.enum_data(it).name.clone(),
410411
};
411412
write!(f, "{}", name)?;
412413
}

crates/hir_ty/src/infer/expr.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use hir_def::{
88
expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
99
path::{GenericArg, GenericArgs},
1010
resolver::resolver_for_expr,
11-
AdtId, AssocContainerId, FieldId, Lookup,
11+
AssocContainerId, FieldId, Lookup,
1212
};
1313
use hir_expand::name::{name, Name};
1414
use syntax::ast::RangeOp;
@@ -21,8 +21,8 @@ use crate::{
2121
primitive::{self, UintTy},
2222
traits::{FnTrait, InEnvironment},
2323
utils::{generics, variant_data, Generics},
24-
Binders, CallableDefId, FnPointer, FnSig, Obligation, OpaqueTyId, Rawness, Scalar, Substs,
25-
TraitRef, Ty,
24+
AdtId, Binders, CallableDefId, FnPointer, FnSig, Obligation, OpaqueTyId, Rawness, Scalar,
25+
Substs, TraitRef, Ty,
2626
};
2727

2828
use super::{
@@ -429,14 +429,14 @@ impl<'a> InferenceContext<'a> {
429429
Ty::Tuple(_, substs) => {
430430
name.as_tuple_index().and_then(|idx| substs.0.get(idx).cloned())
431431
}
432-
Ty::Adt(AdtId::StructId(s), parameters) => {
432+
Ty::Adt(AdtId(hir_def::AdtId::StructId(s)), parameters) => {
433433
self.db.struct_data(s).variant_data.field(name).map(|local_id| {
434434
let field = FieldId { parent: s.into(), local_id };
435435
self.write_field_resolution(tgt_expr, field);
436436
self.db.field_types(s.into())[field.local_id].clone().subst(&parameters)
437437
})
438438
}
439-
Ty::Adt(AdtId::UnionId(u), parameters) => {
439+
Ty::Adt(AdtId(hir_def::AdtId::UnionId(u)), parameters) => {
440440
self.db.union_data(u).variant_data.field(name).map(|local_id| {
441441
let field = FieldId { parent: u.into(), local_id };
442442
self.write_field_resolution(tgt_expr, field);
@@ -498,7 +498,7 @@ impl<'a> InferenceContext<'a> {
498498
_ => (),
499499
}
500500
sb = sb.fill(repeat_with(|| self.table.new_type_var()));
501-
Ty::Adt(box_, sb.build())
501+
Ty::adt_ty(box_, sb.build())
502502
} else {
503503
Ty::Unknown
504504
}
@@ -586,31 +586,31 @@ impl<'a> InferenceContext<'a> {
586586
let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect));
587587
match (range_type, lhs_ty, rhs_ty) {
588588
(RangeOp::Exclusive, None, None) => match self.resolve_range_full() {
589-
Some(adt) => Ty::Adt(adt, Substs::empty()),
589+
Some(adt) => Ty::adt_ty(adt, Substs::empty()),
590590
None => Ty::Unknown,
591591
},
592592
(RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() {
593-
Some(adt) => Ty::Adt(adt, Substs::single(ty)),
593+
Some(adt) => Ty::adt_ty(adt, Substs::single(ty)),
594594
None => Ty::Unknown,
595595
},
596596
(RangeOp::Inclusive, None, Some(ty)) => {
597597
match self.resolve_range_to_inclusive() {
598-
Some(adt) => Ty::Adt(adt, Substs::single(ty)),
598+
Some(adt) => Ty::adt_ty(adt, Substs::single(ty)),
599599
None => Ty::Unknown,
600600
}
601601
}
602602
(RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() {
603-
Some(adt) => Ty::Adt(adt, Substs::single(ty)),
603+
Some(adt) => Ty::adt_ty(adt, Substs::single(ty)),
604604
None => Ty::Unknown,
605605
},
606606
(RangeOp::Inclusive, Some(_), Some(ty)) => {
607607
match self.resolve_range_inclusive() {
608-
Some(adt) => Ty::Adt(adt, Substs::single(ty)),
608+
Some(adt) => Ty::adt_ty(adt, Substs::single(ty)),
609609
None => Ty::Unknown,
610610
}
611611
}
612612
(RangeOp::Exclusive, Some(ty), None) => match self.resolve_range_from() {
613-
Some(adt) => Ty::Adt(adt, Substs::single(ty)),
613+
Some(adt) => Ty::adt_ty(adt, Substs::single(ty)),
614614
None => Ty::Unknown,
615615
},
616616
(RangeOp::Inclusive, _, None) => Ty::Unknown,

crates/hir_ty/src/infer/pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl<'a> InferenceContext<'a> {
237237
};
238238

239239
let inner_ty = self.infer_pat(*inner, inner_expected, default_bm);
240-
Ty::Adt(box_adt, Substs::single(inner_ty))
240+
Ty::adt_ty(box_adt, Substs::single(inner_ty))
241241
}
242242
None => Ty::Unknown,
243243
},

crates/hir_ty/src/lib.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ use std::{iter, mem, ops::Deref, sync::Arc};
2727

2828
use base_db::salsa;
2929
use hir_def::{
30-
builtin_type::BuiltinType, expr::ExprId, type_ref::Rawness, AdtId, AssocContainerId,
31-
DefWithBodyId, FunctionId, GenericDefId, HasModule, LifetimeParamId, Lookup, TraitId,
32-
TypeAliasId, TypeParamId,
30+
builtin_type::BuiltinType, expr::ExprId, type_ref::Rawness, AssocContainerId, DefWithBodyId,
31+
FunctionId, GenericDefId, HasModule, LifetimeParamId, Lookup, TraitId, TypeAliasId,
32+
TypeParamId,
3333
};
3434
use itertools::Itertools;
3535

@@ -47,7 +47,9 @@ pub use lower::{
4747
};
4848
pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment};
4949

50-
pub use chalk_ir::{BoundVar, DebruijnIndex, Mutability, Scalar, TyVariableKind};
50+
pub use chalk_ir::{AdtId, BoundVar, DebruijnIndex, Mutability, Scalar, TyVariableKind};
51+
52+
pub(crate) use crate::traits::chalk::Interner;
5153

5254
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
5355
pub enum Lifetime {
@@ -131,7 +133,7 @@ pub enum AliasTy {
131133
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
132134
pub enum Ty {
133135
/// Structures, enumerations and unions.
134-
Adt(AdtId, Substs),
136+
Adt(AdtId<Interner>, Substs),
135137

136138
/// Represents an associated item like `Iterator::Item`. This is used
137139
/// when we have tried to normalize a projection like `T::Item` but
@@ -602,6 +604,10 @@ impl Ty {
602604
Ty::Tuple(0, Substs::empty())
603605
}
604606

607+
pub fn adt_ty(adt: hir_def::AdtId, substs: Substs) -> Ty {
608+
Ty::Adt(AdtId(adt), substs)
609+
}
610+
605611
pub fn fn_ptr(sig: CallableSig) -> Self {
606612
Ty::Function(FnPointer {
607613
num_args: sig.params().len(),
@@ -650,9 +656,9 @@ impl Ty {
650656
t
651657
}
652658

653-
pub fn as_adt(&self) -> Option<(AdtId, &Substs)> {
659+
pub fn as_adt(&self) -> Option<(hir_def::AdtId, &Substs)> {
654660
match self {
655-
Ty::Adt(adt_def, parameters) => Some((*adt_def, parameters)),
661+
Ty::Adt(AdtId(adt), parameters) => Some((*adt, parameters)),
656662
_ => None,
657663
}
658664
}
@@ -666,7 +672,7 @@ impl Ty {
666672

667673
pub fn as_generic_def(&self) -> Option<GenericDefId> {
668674
match *self {
669-
Ty::Adt(adt, ..) => Some(adt.into()),
675+
Ty::Adt(AdtId(adt), ..) => Some(adt.into()),
670676
Ty::FnDef(callable, ..) => Some(callable.into()),
671677
Ty::AssociatedType(type_alias, ..) => Some(type_alias.into()),
672678
Ty::ForeignType(type_alias, ..) => Some(type_alias.into()),

crates/hir_ty/src/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -
11001100
fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> {
11011101
let generics = generics(db.upcast(), adt.into());
11021102
let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST);
1103-
Binders::new(substs.len(), Ty::Adt(adt, substs))
1103+
Binders::new(substs.len(), Ty::adt_ty(adt, substs))
11041104
}
11051105

11061106
fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> {

crates/hir_ty/src/method_resolution.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use arrayvec::ArrayVec;
88
use base_db::CrateId;
99
use chalk_ir::Mutability;
1010
use hir_def::{
11-
lang_item::LangItemTarget, AdtId, AssocContainerId, AssocItemId, FunctionId, GenericDefId,
12-
HasModule, ImplId, Lookup, ModuleId, TraitId, TypeAliasId,
11+
lang_item::LangItemTarget, AssocContainerId, AssocItemId, FunctionId, GenericDefId, HasModule,
12+
ImplId, Lookup, ModuleId, TraitId, TypeAliasId,
1313
};
1414
use hir_expand::name::Name;
1515
use rustc_hash::{FxHashMap, FxHashSet};
@@ -19,8 +19,8 @@ use crate::{
1919
db::HirDatabase,
2020
primitive::{self, FloatTy, IntTy, UintTy},
2121
utils::all_super_traits,
22-
Canonical, DebruijnIndex, FnPointer, FnSig, InEnvironment, Scalar, Substs, TraitEnvironment,
23-
TraitRef, Ty, TypeWalk,
22+
AdtId, Canonical, DebruijnIndex, FnPointer, FnSig, InEnvironment, Scalar, Substs,
23+
TraitEnvironment, TraitRef, Ty, TypeWalk,
2424
};
2525

2626
/// This is used as a key for indexing impls.
@@ -32,7 +32,7 @@ pub enum TyFingerprint {
3232
Never,
3333
RawPtr(Mutability),
3434
Scalar(Scalar),
35-
Adt(AdtId),
35+
Adt(hir_def::AdtId),
3636
Dyn(TraitId),
3737
Tuple(usize),
3838
ForeignType(TypeAliasId),
@@ -50,7 +50,7 @@ impl TyFingerprint {
5050
&Ty::Slice(..) => TyFingerprint::Slice,
5151
&Ty::Array(..) => TyFingerprint::Array,
5252
&Ty::Scalar(scalar) => TyFingerprint::Scalar(scalar),
53-
&Ty::Adt(adt, _) => TyFingerprint::Adt(adt),
53+
&Ty::Adt(AdtId(adt), _) => TyFingerprint::Adt(adt),
5454
&Ty::Tuple(cardinality, _) => TyFingerprint::Tuple(cardinality),
5555
&Ty::Raw(mutability, ..) => TyFingerprint::RawPtr(mutability),
5656
&Ty::ForeignType(alias_id, ..) => TyFingerprint::ForeignType(alias_id),
@@ -231,7 +231,7 @@ impl Ty {
231231
let mod_to_crate_ids = |module: ModuleId| Some(std::iter::once(module.krate()).collect());
232232

233233
let lang_item_targets = match self {
234-
Ty::Adt(def_id, _) => {
234+
Ty::Adt(AdtId(def_id), _) => {
235235
return mod_to_crate_ids(def_id.module(db.upcast()));
236236
}
237237
Ty::ForeignType(type_alias_id) => {

crates/hir_ty/src/traits/chalk.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,8 @@ impl<'a> chalk_solve::RustIrDatabase<Interner> for ChalkContext<'a> {
315315
let id = from_chalk(self.db, trait_id);
316316
self.db.trait_data(id).name.to_string()
317317
}
318-
fn adt_name(&self, adt_id: chalk_ir::AdtId<Interner>) -> String {
319-
let id = from_chalk(self.db, adt_id);
320-
match id {
318+
fn adt_name(&self, chalk_ir::AdtId(adt_id): AdtId) -> String {
319+
match adt_id {
321320
hir_def::AdtId::StructId(id) => self.db.struct_data(id).name.to_string(),
322321
hir_def::AdtId::EnumId(id) => self.db.enum_data(id).name.to_string(),
323322
hir_def::AdtId::UnionId(id) => self.db.union_data(id).name.to_string(),
@@ -488,8 +487,8 @@ pub(crate) fn struct_datum_query(
488487
struct_id: AdtId,
489488
) -> Arc<StructDatum> {
490489
debug!("struct_datum {:?}", struct_id);
491-
let adt_id = from_chalk(db, struct_id);
492-
let type_ctor = Ty::Adt(adt_id, Substs::empty());
490+
let type_ctor = Ty::Adt(struct_id, Substs::empty());
491+
let chalk_ir::AdtId(adt_id) = struct_id;
493492
debug!("struct {:?} = {:?}", struct_id, type_ctor);
494493
let num_params = generics(db.upcast(), adt_id.into()).len();
495494
let upstream = adt_id.module(db.upcast()).krate() != krate;
@@ -684,10 +683,9 @@ pub(crate) fn fn_def_variance_query(
684683
pub(crate) fn adt_variance_query(
685684
db: &dyn HirDatabase,
686685
_krate: CrateId,
687-
adt_id: AdtId,
686+
chalk_ir::AdtId(adt_id): AdtId,
688687
) -> Variances {
689-
let adt: crate::AdtId = from_chalk(db, adt_id);
690-
let generic_params = generics(db.upcast(), adt.into());
688+
let generic_params = generics(db.upcast(), adt_id.into());
691689
Variances::from_iter(
692690
&Interner,
693691
std::iter::repeat(chalk_ir::Variance::Invariant).take(generic_params.len()),

0 commit comments

Comments
 (0)