Skip to content

Commit 9d786ea

Browse files
bors[bot]matklad
andauthored
Merge #2198
2198: Unfork struct and union ids r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents defc7ad + 6294fd5 commit 9d786ea

File tree

10 files changed

+51
-53
lines changed

10 files changed

+51
-53
lines changed

crates/ra_hir/src/adt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212

1313
impl Struct {
1414
pub(crate) fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
15-
db.struct_data(self.id).variant_data.clone()
15+
db.struct_data(self.id.into()).variant_data.clone()
1616
}
1717
}
1818

crates/ra_hir/src/code_model.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,19 +288,19 @@ pub struct Struct {
288288

289289
impl Struct {
290290
pub fn module(self, db: &impl DefDatabase) -> Module {
291-
Module { id: self.id.module(db) }
291+
Module { id: self.id.0.module(db) }
292292
}
293293

294294
pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
295295
Some(self.module(db).krate())
296296
}
297297

298298
pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
299-
db.struct_data(self.id).name.clone()
299+
db.struct_data(self.id.into()).name.clone()
300300
}
301301

302302
pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
303-
db.struct_data(self.id)
303+
db.struct_data(self.id.into())
304304
.variant_data
305305
.fields()
306306
.into_iter()
@@ -310,7 +310,7 @@ impl Struct {
310310
}
311311

312312
pub fn field(self, db: &impl HirDatabase, name: &Name) -> Option<StructField> {
313-
db.struct_data(self.id)
313+
db.struct_data(self.id.into())
314314
.variant_data
315315
.fields()
316316
.into_iter()
@@ -346,11 +346,11 @@ pub struct Union {
346346

347347
impl Union {
348348
pub fn name(self, db: &impl DefDatabase) -> Option<Name> {
349-
db.union_data(self.id).name.clone()
349+
db.struct_data(self.id.into()).name.clone()
350350
}
351351

352352
pub fn module(self, db: &impl HirDatabase) -> Module {
353-
Module { id: self.id.module(db) }
353+
Module { id: self.id.0.module(db) }
354354
}
355355

356356
pub fn ty(self, db: &impl HirDatabase) -> Ty {

crates/ra_hir/src/code_model/src.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ impl HasSource for StructField {
7878
impl HasSource for Struct {
7979
type Ast = ast::StructDef;
8080
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::StructDef> {
81-
self.id.source(db)
81+
self.id.0.source(db)
8282
}
8383
}
8484
impl HasSource for Union {
8585
type Ast = ast::StructDef;
8686
fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::StructDef> {
87-
self.id.source(db)
87+
self.id.0.source(db)
8888
}
8989
}
9090
impl HasSource for Enum {

crates/ra_hir/src/expr/scope.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ impl ExprScopes {
6767
&self.scopes[scope].entries
6868
}
6969

70-
pub(crate) fn scope_chain<'a>(
71-
&'a self,
72-
scope: Option<ScopeId>,
73-
) -> impl Iterator<Item = ScopeId> + 'a {
70+
pub(crate) fn scope_chain(&self, scope: Option<ScopeId>) -> impl Iterator<Item = ScopeId> + '_ {
7471
std::iter::successors(scope, move |&scope| self.scopes[scope].parent)
7572
}
7673

crates/ra_hir/src/from_source.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! FIXME: write short doc here
22
3+
use hir_def::{StructId, StructOrUnionId, UnionId};
34
use hir_expand::name::AsName;
45
use ra_syntax::ast::{self, AstNode, NameOwner};
56

@@ -15,18 +16,19 @@ pub trait FromSource: Sized {
1516
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self>;
1617
}
1718

19+
// FIXIME: these two impls are wrong, `ast::StructDef` might produce either a struct or a union
1820
impl FromSource for Struct {
1921
type Ast = ast::StructDef;
2022
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
21-
let id = from_source(db, src)?;
22-
Some(Struct { id })
23+
let id: StructOrUnionId = from_source(db, src)?;
24+
Some(Struct { id: StructId(id) })
2325
}
2426
}
2527
impl FromSource for Union {
2628
type Ast = ast::StructDef;
2729
fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
28-
let id = from_source(db, src)?;
29-
Some(Union { id })
30+
let id: StructOrUnionId = from_source(db, src)?;
31+
Some(Union { id: UnionId(id) })
3032
}
3133
}
3234
impl FromSource for Enum {

crates/ra_hir/src/ty/lower.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ fn type_for_builtin(def: BuiltinType) -> Ty {
665665
}
666666

667667
fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig {
668-
let struct_data = db.struct_data(def.id);
668+
let struct_data = db.struct_data(def.id.into());
669669
let fields = match struct_data.variant_data.fields() {
670670
Some(fields) => fields,
671671
None => panic!("fn_sig_for_struct_constructor called on unit struct"),
@@ -681,7 +681,7 @@ fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig {
681681

682682
/// Build the type of a tuple struct constructor.
683683
fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty {
684-
let struct_data = db.struct_data(def.id);
684+
let struct_data = db.struct_data(def.id.into());
685685
if struct_data.variant_data.fields().is_none() {
686686
return type_for_adt(db, def); // Unit struct
687687
}

crates/ra_hir_def/src/adt.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
88

99
use crate::{
1010
db::DefDatabase2, type_ref::TypeRef, AstItemDef, EnumId, LocalEnumVariantId,
11-
LocalStructFieldId, StructId, UnionId,
11+
LocalStructFieldId, StructOrUnionId,
1212
};
1313

1414
/// Note that we use `StructData` for unions as well!
@@ -49,15 +49,11 @@ pub struct StructFieldData {
4949
}
5050

5151
impl StructData {
52-
pub(crate) fn struct_data_query(db: &impl DefDatabase2, struct_: StructId) -> Arc<StructData> {
53-
let src = struct_.source(db);
54-
let name = src.ast.name().map(|n| n.as_name());
55-
let variant_data = VariantData::new(src.ast.kind());
56-
let variant_data = Arc::new(variant_data);
57-
Arc::new(StructData { name, variant_data })
58-
}
59-
pub(crate) fn union_data_query(db: &impl DefDatabase2, struct_: UnionId) -> Arc<StructData> {
60-
let src = struct_.source(db);
52+
pub(crate) fn struct_data_query(
53+
db: &impl DefDatabase2,
54+
id: StructOrUnionId,
55+
) -> Arc<StructData> {
56+
let src = id.source(db);
6157
let name = src.ast.name().map(|n| n.as_name());
6258
let variant_data = VariantData::new(src.ast.kind());
6359
let variant_data = Arc::new(variant_data);

crates/ra_hir_def/src/db.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,16 @@ use crate::{
1111
raw::{ImportSourceMap, RawItems},
1212
CrateDefMap,
1313
},
14-
EnumId, StructId, UnionId,
14+
EnumId, StructOrUnionId,
1515
};
1616

1717
#[salsa::query_group(InternDatabaseStorage)]
1818
pub trait InternDatabase: SourceDatabase {
1919
#[salsa::interned]
2020
fn intern_function(&self, loc: crate::ItemLoc<ast::FnDef>) -> crate::FunctionId;
2121
#[salsa::interned]
22-
fn intern_struct(&self, loc: crate::ItemLoc<ast::StructDef>) -> crate::StructId;
23-
#[salsa::interned]
24-
fn intern_union(&self, loc: crate::ItemLoc<ast::StructDef>) -> crate::UnionId;
22+
fn intern_struct_or_union(&self, loc: crate::ItemLoc<ast::StructDef>)
23+
-> crate::StructOrUnionId;
2524
#[salsa::interned]
2625
fn intern_enum(&self, loc: crate::ItemLoc<ast::EnumDef>) -> crate::EnumId;
2726
#[salsa::interned]
@@ -49,10 +48,7 @@ pub trait DefDatabase2: InternDatabase + AstDatabase {
4948
fn crate_def_map(&self, krate: CrateId) -> Arc<CrateDefMap>;
5049

5150
#[salsa::invoke(StructData::struct_data_query)]
52-
fn struct_data(&self, s: StructId) -> Arc<StructData>;
53-
54-
#[salsa::invoke(StructData::union_data_query)]
55-
fn union_data(&self, s: UnionId) -> Arc<StructData>;
51+
fn struct_data(&self, id: StructOrUnionId) -> Arc<StructData>;
5652

5753
#[salsa::invoke(EnumData::enum_data_query)]
5854
fn enum_data(&self, e: EnumId) -> Arc<EnumData>;

crates/ra_hir_def/src/lib.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -205,26 +205,30 @@ impl AstItemDef<ast::FnDef> for FunctionId {
205205
}
206206

207207
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
208-
pub struct StructId(salsa::InternId);
209-
impl_intern_key!(StructId);
210-
impl AstItemDef<ast::StructDef> for StructId {
208+
pub struct StructOrUnionId(salsa::InternId);
209+
impl_intern_key!(StructOrUnionId);
210+
impl AstItemDef<ast::StructDef> for StructOrUnionId {
211211
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StructDef>) -> Self {
212-
db.intern_struct(loc)
212+
db.intern_struct_or_union(loc)
213213
}
214214
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StructDef> {
215-
db.lookup_intern_struct(self)
215+
db.lookup_intern_struct_or_union(self)
216216
}
217217
}
218218

219219
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
220-
pub struct UnionId(salsa::InternId);
221-
impl_intern_key!(UnionId);
222-
impl AstItemDef<ast::StructDef> for UnionId {
223-
fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StructDef>) -> Self {
224-
db.intern_union(loc)
220+
pub struct StructId(pub StructOrUnionId);
221+
impl From<StructId> for StructOrUnionId {
222+
fn from(id: StructId) -> StructOrUnionId {
223+
id.0
225224
}
226-
fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StructDef> {
227-
db.lookup_intern_union(self)
225+
}
226+
227+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
228+
pub struct UnionId(pub StructOrUnionId);
229+
impl From<UnionId> for StructOrUnionId {
230+
fn from(id: UnionId) -> StructOrUnionId {
231+
id.0
228232
}
229233
}
230234

crates/ra_hir_def/src/nameres/collector.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use crate::{
1919
},
2020
path::{Path, PathKind},
2121
AdtId, AstId, AstItemDef, ConstId, CrateModuleId, EnumId, EnumVariantId, FunctionId,
22-
LocationCtx, ModuleDefId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, UnionId,
22+
LocationCtx, ModuleDefId, ModuleId, StaticId, StructId, StructOrUnionId, TraitId, TypeAliasId,
23+
UnionId,
2324
};
2425

2526
pub(super) fn collect_defs(db: &impl DefDatabase2, mut def_map: CrateDefMap) -> CrateDefMap {
@@ -664,12 +665,14 @@ where
664665
PerNs::values(FunctionId::from_ast_id(ctx, ast_id).into())
665666
}
666667
raw::DefKind::Struct(ast_id) => {
667-
let s = StructId::from_ast_id(ctx, ast_id).into();
668+
let id = StructOrUnionId::from_ast_id(ctx, ast_id).into();
669+
let s = StructId(id).into();
668670
PerNs::both(s, s)
669671
}
670672
raw::DefKind::Union(ast_id) => {
671-
let s = UnionId::from_ast_id(ctx, ast_id).into();
672-
PerNs::both(s, s)
673+
let id = StructOrUnionId::from_ast_id(ctx, ast_id).into();
674+
let u = UnionId(id).into();
675+
PerNs::both(u, u)
673676
}
674677
raw::DefKind::Enum(ast_id) => PerNs::types(EnumId::from_ast_id(ctx, ast_id).into()),
675678
raw::DefKind::Const(ast_id) => PerNs::values(ConstId::from_ast_id(ctx, ast_id).into()),

0 commit comments

Comments
 (0)