Skip to content

Commit 6e36ead

Browse files
bors[bot]matklad
andauthored
Merge #2409
2409: Id-ify Ty::Adt r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 500e022 + a443b50 commit 6e36ead

File tree

11 files changed

+98
-39
lines changed

11 files changed

+98
-39
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use hir_def::{
1111
per_ns::PerNs,
1212
resolver::{HasResolver, TypeNs},
1313
type_ref::{Mutability, TypeRef},
14-
AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule,
15-
ImplId, LocalEnumVariantId, LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId,
16-
StaticId, StructId, TraitId, TypeAliasId, UnionId,
14+
AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, FunctionId, GenericDefId,
15+
HasModule, ImplId, LocalEnumVariantId, LocalImportId, LocalModuleId, LocalStructFieldId,
16+
Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, UnionId,
1717
};
1818
use hir_expand::{
1919
diagnostics::DiagnosticSink,
@@ -383,6 +383,28 @@ impl Union {
383383
pub fn ty(self, db: &impl HirDatabase) -> Ty {
384384
db.type_for_def(self.into(), Namespace::Types)
385385
}
386+
387+
pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
388+
db.union_data(self.id)
389+
.variant_data
390+
.fields()
391+
.iter()
392+
.map(|(id, _)| StructField { parent: self.into(), id })
393+
.collect()
394+
}
395+
396+
pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
397+
db.union_data(self.id)
398+
.variant_data
399+
.fields()
400+
.iter()
401+
.find(|(_id, data)| data.name == *name)
402+
.map(|(id, _)| StructField { parent: self.into(), id })
403+
}
404+
405+
fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
406+
db.union_data(self.id).variant_data.clone()
407+
}
386408
}
387409

388410
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -501,35 +523,40 @@ impl Adt {
501523
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
502524
pub enum VariantDef {
503525
Struct(Struct),
526+
Union(Union),
504527
EnumVariant(EnumVariant),
505528
}
506-
impl_froms!(VariantDef: Struct, EnumVariant);
529+
impl_froms!(VariantDef: Struct, Union, EnumVariant);
507530

508531
impl VariantDef {
509532
pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
510533
match self {
511534
VariantDef::Struct(it) => it.fields(db),
535+
VariantDef::Union(it) => it.fields(db),
512536
VariantDef::EnumVariant(it) => it.fields(db),
513537
}
514538
}
515539

516540
pub(crate) fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
517541
match self {
518542
VariantDef::Struct(it) => it.field(db, name),
543+
VariantDef::Union(it) => it.field(db, name),
519544
VariantDef::EnumVariant(it) => it.field(db, name),
520545
}
521546
}
522547

523548
pub fn module(self, db: &impl HirDatabase) -> Module {
524549
match self {
525550
VariantDef::Struct(it) => it.module(db),
551+
VariantDef::Union(it) => it.module(db),
526552
VariantDef::EnumVariant(it) => it.module(db),
527553
}
528554
}
529555

530556
pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
531557
match self {
532558
VariantDef::Struct(it) => it.variant_data(db),
559+
VariantDef::Union(it) => it.variant_data(db),
533560
VariantDef::EnumVariant(it) => it.variant_data(db),
534561
}
535562
}
@@ -1056,19 +1083,24 @@ impl Type {
10561083
}
10571084

10581085
pub fn fields(&self, db: &impl HirDatabase) -> Vec<(StructField, Type)> {
1059-
let mut res = Vec::new();
10601086
if let Ty::Apply(a_ty) = &self.ty.value {
10611087
match a_ty.ctor {
1062-
ty::TypeCtor::Adt(Adt::Struct(s)) => {
1063-
for field in s.fields(db) {
1064-
let ty = field.ty(db).subst(&a_ty.parameters);
1065-
res.push((field, self.derived(ty)));
1066-
}
1088+
ty::TypeCtor::Adt(AdtId::StructId(s)) => {
1089+
let var_def = s.into();
1090+
return db
1091+
.field_types(var_def)
1092+
.iter()
1093+
.map(|(local_id, ty)| {
1094+
let def = StructField { parent: var_def.into(), id: local_id };
1095+
let ty = ty.clone().subst(&a_ty.parameters);
1096+
(def, self.derived(ty))
1097+
})
1098+
.collect();
10671099
}
10681100
_ => {}
10691101
}
10701102
};
1071-
res
1103+
Vec::new()
10721104
}
10731105

10741106
pub fn tuple_fields(&self, _db: &impl HirDatabase) -> Vec<Type> {

crates/ra_hir/src/expr.rs

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

5-
use hir_def::{path::known, resolver::HasResolver};
5+
use hir_def::{path::known, resolver::HasResolver, AdtId};
66
use hir_expand::diagnostics::DiagnosticSink;
77
use ra_syntax::ast;
88
use ra_syntax::AstPtr;
@@ -127,7 +127,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
127127
_ => return,
128128
};
129129

130-
let std_result_ctor = TypeCtor::Adt(Adt::Enum(std_result_enum.into()));
130+
let std_result_ctor = TypeCtor::Adt(AdtId::EnumId(std_result_enum));
131131
let params = match &mismatch.expected {
132132
Ty::Apply(ApplicationTy { ctor, parameters }) if ctor == &std_result_ctor => parameters,
133133
_ => return,

crates/ra_hir/src/from_id.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,22 @@ impl From<Adt> for GenericDefId {
199199
}
200200
}
201201

202+
impl From<VariantId> for VariantDef {
203+
fn from(def: VariantId) -> Self {
204+
match def {
205+
VariantId::StructId(it) => VariantDef::Struct(it.into()),
206+
VariantId::EnumVariantId(it) => VariantDef::EnumVariant(it.into()),
207+
VariantId::UnionId(it) => VariantDef::Union(it.into()),
208+
}
209+
}
210+
}
211+
202212
impl From<VariantDef> for VariantId {
203213
fn from(def: VariantDef) -> Self {
204214
match def {
205215
VariantDef::Struct(it) => VariantId::StructId(it.id),
206216
VariantDef::EnumVariant(it) => VariantId::EnumVariantId(it.into()),
217+
VariantDef::Union(it) => VariantId::UnionId(it.id),
207218
}
208219
}
209220
}
@@ -214,6 +225,12 @@ impl From<StructField> for StructFieldId {
214225
}
215226
}
216227

228+
impl From<StructFieldId> for StructField {
229+
fn from(def: StructFieldId) -> Self {
230+
StructField { parent: def.parent.into(), id: def.local_id }
231+
}
232+
}
233+
217234
impl From<AttrDef> for AttrDefId {
218235
fn from(def: AttrDef) -> Self {
219236
match def {

crates/ra_hir/src/ty.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub enum TypeCtor {
5858
Float(Uncertain<FloatTy>),
5959

6060
/// Structures, enumerations and unions.
61-
Adt(Adt),
61+
Adt(AdtId),
6262

6363
/// The pointee of a string slice. Written as `str`.
6464
Str,
@@ -174,7 +174,7 @@ impl TypeCtor {
174174
| TypeCtor::Tuple { .. } => None,
175175
// Closure's krate is irrelevant for coherence I would think?
176176
TypeCtor::Closure { .. } => None,
177-
TypeCtor::Adt(adt) => adt.krate(db),
177+
TypeCtor::Adt(adt) => Some(adt.module(db).krate.into()),
178178
TypeCtor::FnDef(callable) => Some(callable.krate(db).into()),
179179
TypeCtor::AssociatedType(type_alias) => {
180180
Some(type_alias.lookup(db).module(db).krate.into())
@@ -598,7 +598,7 @@ impl Ty {
598598
pub fn as_adt(&self) -> Option<(Adt, &Substs)> {
599599
match self {
600600
Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(adt_def), parameters }) => {
601-
Some((*adt_def, parameters))
601+
Some(((*adt_def).into(), parameters))
602602
}
603603
_ => None,
604604
}
@@ -889,9 +889,9 @@ impl HirDisplay for ApplicationTy {
889889
}
890890
TypeCtor::Adt(def_id) => {
891891
let name = match def_id {
892-
Adt::Struct(s) => s.name(f.db),
893-
Adt::Union(u) => u.name(f.db),
894-
Adt::Enum(e) => e.name(f.db),
892+
AdtId::StructId(it) => f.db.struct_data(it).name.clone(),
893+
AdtId::UnionId(it) => f.db.union_data(it).name.clone(),
894+
AdtId::EnumId(it) => f.db.enum_data(it).name.clone(),
895895
}
896896
.unwrap_or_else(Name::missing);
897897
write!(f, "{}", name)?;

crates/ra_hir/src/ty/infer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,10 +598,10 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
598598
trait_.associated_type_by_name(self.db, &name::OUTPUT_TYPE)
599599
}
600600

601-
fn resolve_boxed_box(&self) -> Option<Adt> {
601+
fn resolve_boxed_box(&self) -> Option<AdtId> {
602602
let path = known::std_boxed_box();
603603
let struct_ = self.resolver.resolve_known_struct(self.db, &path)?;
604-
Some(Adt::Struct(struct_.into()))
604+
Some(struct_.into())
605605
}
606606
}
607607

crates/ra_hir/src/ty/infer/coerce.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
//!
55
//! See: https://doc.rust-lang.org/nomicon/coercions.html
66
7-
use hir_def::{lang_item::LangItemTarget, resolver::Resolver};
7+
use hir_def::{lang_item::LangItemTarget, resolver::Resolver, AdtId};
88
use rustc_hash::FxHashMap;
99
use test_utils::tested_by;
1010

1111
use crate::{
1212
db::HirDatabase,
1313
ty::{autoderef, Substs, Ty, TypeCtor, TypeWalk},
14-
Adt, Mutability,
14+
Mutability,
1515
};
1616

1717
use super::{InEnvironment, InferTy, InferenceContext, TypeVarValue};
@@ -242,11 +242,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
242242
// - T is not part of the type of any other fields
243243
// - Bar<T>: Unsize<Bar<U>>, if the last field of Foo has type Bar<T>
244244
(
245-
ty_app!(TypeCtor::Adt(Adt::Struct(struct1)), st1),
246-
ty_app!(TypeCtor::Adt(Adt::Struct(struct2)), st2),
245+
ty_app!(TypeCtor::Adt(AdtId::StructId(struct1)), st1),
246+
ty_app!(TypeCtor::Adt(AdtId::StructId(struct2)), st2),
247247
) if struct1 == struct2 => {
248-
let field_tys = self.db.field_types(struct1.id.into());
249-
let struct_data = self.db.struct_data(struct1.id);
248+
let field_tys = self.db.field_types((*struct1).into());
249+
let struct_data = self.db.struct_data(*struct1);
250250

251251
let mut fields = struct_data.variant_data.fields().iter();
252252
let (last_field_id, _data) = fields.next_back()?;

crates/ra_hir/src/ty/infer/expr.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use hir_def::{
88
generics::GenericParams,
99
path::{GenericArg, GenericArgs},
1010
resolver::resolver_for_expr,
11-
ContainerId, Lookup,
11+
AdtId, ContainerId, Lookup, StructFieldId,
1212
};
1313
use hir_expand::name;
1414

@@ -20,7 +20,7 @@ use crate::{
2020
Mutability, Namespace, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitRef, Ty,
2121
TypeCtor, TypeWalk, Uncertain,
2222
},
23-
Adt, Name,
23+
Name,
2424
};
2525

2626
use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch};
@@ -259,14 +259,17 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
259259
TypeCtor::Tuple { .. } => name
260260
.as_tuple_index()
261261
.and_then(|idx| a_ty.parameters.0.get(idx).cloned()),
262-
TypeCtor::Adt(Adt::Struct(s)) => s.field(self.db, name).map(|field| {
263-
self.write_field_resolution(tgt_expr, field);
264-
self.db.field_types(s.id.into())[field.id]
265-
.clone()
266-
.subst(&a_ty.parameters)
267-
}),
262+
TypeCtor::Adt(AdtId::StructId(s)) => {
263+
self.db.struct_data(s).variant_data.field(name).map(|local_id| {
264+
let field = StructFieldId { parent: s.into(), local_id }.into();
265+
self.write_field_resolution(tgt_expr, field);
266+
self.db.field_types(s.into())[field.id]
267+
.clone()
268+
.subst(&a_ty.parameters)
269+
})
270+
}
268271
// FIXME:
269-
TypeCtor::Adt(Adt::Union(_)) => None,
272+
TypeCtor::Adt(AdtId::UnionId(_)) => None,
270273
_ => None,
271274
},
272275
_ => None,

crates/ra_hir/src/ty/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ fn type_for_adt(db: &impl HirDatabase, adt: impl Into<Adt>) -> Ty {
762762
let adt = adt.into();
763763
let adt_id: AdtId = adt.into();
764764
let generics = db.generic_params(adt_id.into());
765-
Ty::apply(TypeCtor::Adt(adt), Substs::identity(&generics))
765+
Ty::apply(TypeCtor::Adt(adt_id), Substs::identity(&generics))
766766
}
767767

768768
fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty {

crates/ra_hir/src/ty/method_resolution.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use std::sync::Arc;
66

77
use arrayvec::ArrayVec;
8-
use hir_def::{lang_item::LangItemTarget, resolver::Resolver, AstItemDef};
8+
use hir_def::{lang_item::LangItemTarget, resolver::Resolver, AstItemDef, HasModule};
99
use rustc_hash::FxHashMap;
1010

1111
use crate::{
@@ -102,7 +102,9 @@ fn def_crates(db: &impl HirDatabase, cur_crate: Crate, ty: &Ty) -> Option<ArrayV
102102

103103
let lang_item_targets = match ty {
104104
Ty::Apply(a_ty) => match a_ty.ctor {
105-
TypeCtor::Adt(def_id) => return Some(std::iter::once(def_id.krate(db)?).collect()),
105+
TypeCtor::Adt(def_id) => {
106+
return Some(std::iter::once(def_id.module(db).krate.into()).collect())
107+
}
106108
TypeCtor::Bool => lang_item_crate!("bool"),
107109
TypeCtor::Char => lang_item_crate!("char"),
108110
TypeCtor::Float(Uncertain::Known(f)) => match f.bitness {

crates/ra_hir_def/src/adt.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ impl VariantData {
129129
}
130130
}
131131

132+
pub fn field(&self, name: &Name) -> Option<LocalStructFieldId> {
133+
self.fields().iter().find_map(|(id, data)| if &data.name == name { Some(id) } else { None })
134+
}
135+
132136
pub fn is_unit(&self) -> bool {
133137
match self {
134138
VariantData::Unit => true,

0 commit comments

Comments
 (0)