@@ -27,7 +27,6 @@ use crate::{
27
27
#[ derive( Debug , Clone , PartialEq , Eq ) ]
28
28
pub struct StructData {
29
29
pub name : Name ,
30
- pub variant_data : Arc < VariantData > ,
31
30
pub repr : Option < ReprOptions > ,
32
31
pub visibility : RawVisibility ,
33
32
pub flags : StructFlags ,
@@ -69,7 +68,6 @@ pub struct EnumVariants {
69
68
#[ derive( Debug , Clone , PartialEq , Eq ) ]
70
69
pub struct EnumVariantData {
71
70
pub name : Name ,
72
- pub variant_data : Arc < VariantData > ,
73
71
}
74
72
75
73
#[ derive( Debug , Clone , PartialEq , Eq ) ]
@@ -79,6 +77,94 @@ pub enum VariantData {
79
77
Unit ,
80
78
}
81
79
80
+ impl VariantData {
81
+ #[ inline]
82
+ pub ( crate ) fn variant_data_query ( db : & dyn DefDatabase , id : VariantId ) -> Arc < VariantData > {
83
+ db. variant_data_with_diagnostics ( id) . 0
84
+ }
85
+
86
+ pub ( crate ) fn variant_data_with_diagnostics_query (
87
+ db : & dyn DefDatabase ,
88
+ id : VariantId ,
89
+ ) -> ( Arc < VariantData > , DefDiagnostics ) {
90
+ let ( shape, types_map, ( fields, diagnostics) ) = match id {
91
+ VariantId :: EnumVariantId ( id) => {
92
+ let loc = id. lookup ( db) ;
93
+ let item_tree = loc. id . item_tree ( db) ;
94
+ let parent = loc. parent . lookup ( db) ;
95
+ let krate = parent. container . krate ;
96
+ let variant = & item_tree[ loc. id . value ] ;
97
+ (
98
+ variant. shape ,
99
+ variant. types_map . clone ( ) ,
100
+ lower_fields (
101
+ db,
102
+ krate,
103
+ parent. container . local_id ,
104
+ loc. id . tree_id ( ) ,
105
+ & item_tree,
106
+ krate. cfg_options ( db) ,
107
+ FieldParent :: EnumVariant ( loc. id . value ) ,
108
+ & variant. fields ,
109
+ Some ( item_tree[ parent. id . value ] . visibility ) ,
110
+ ) ,
111
+ )
112
+ }
113
+ VariantId :: StructId ( id) => {
114
+ let loc = id. lookup ( db) ;
115
+ let item_tree = loc. id . item_tree ( db) ;
116
+ let krate = loc. container . krate ;
117
+ let strukt = & item_tree[ loc. id . value ] ;
118
+ (
119
+ strukt. shape ,
120
+ strukt. types_map . clone ( ) ,
121
+ lower_fields (
122
+ db,
123
+ krate,
124
+ loc. container . local_id ,
125
+ loc. id . tree_id ( ) ,
126
+ & item_tree,
127
+ krate. cfg_options ( db) ,
128
+ FieldParent :: Struct ( loc. id . value ) ,
129
+ & strukt. fields ,
130
+ None ,
131
+ ) ,
132
+ )
133
+ }
134
+ VariantId :: UnionId ( id) => {
135
+ let loc = id. lookup ( db) ;
136
+ let item_tree = loc. id . item_tree ( db) ;
137
+ let krate = loc. container . krate ;
138
+ let union = & item_tree[ loc. id . value ] ;
139
+ (
140
+ FieldsShape :: Record ,
141
+ union. types_map . clone ( ) ,
142
+ lower_fields (
143
+ db,
144
+ krate,
145
+ loc. container . local_id ,
146
+ loc. id . tree_id ( ) ,
147
+ & item_tree,
148
+ krate. cfg_options ( db) ,
149
+ FieldParent :: Union ( loc. id . value ) ,
150
+ & union. fields ,
151
+ None ,
152
+ ) ,
153
+ )
154
+ }
155
+ } ;
156
+
157
+ (
158
+ Arc :: new ( match shape {
159
+ FieldsShape :: Record => VariantData :: Record { fields, types_map } ,
160
+ FieldsShape :: Tuple => VariantData :: Tuple { fields, types_map } ,
161
+ FieldsShape :: Unit => VariantData :: Unit ,
162
+ } ) ,
163
+ DefDiagnostics :: new ( diagnostics) ,
164
+ )
165
+ }
166
+ }
167
+
82
168
/// A single field of an enum variant or struct
83
169
#[ derive( Debug , Clone , PartialEq , Eq ) ]
84
170
pub struct FieldData {
@@ -99,13 +185,6 @@ fn repr_from_value(
99
185
impl StructData {
100
186
#[ inline]
101
187
pub ( crate ) fn struct_data_query ( db : & dyn DefDatabase , id : StructId ) -> Arc < StructData > {
102
- db. struct_data_with_diagnostics ( id) . 0
103
- }
104
-
105
- pub ( crate ) fn struct_data_with_diagnostics_query (
106
- db : & dyn DefDatabase ,
107
- id : StructId ,
108
- ) -> ( Arc < StructData > , DefDiagnostics ) {
109
188
let loc = id. lookup ( db) ;
110
189
let krate = loc. container . krate ;
111
190
let item_tree = loc. id . item_tree ( db) ;
@@ -130,44 +209,16 @@ impl StructData {
130
209
}
131
210
132
211
let strukt = & item_tree[ loc. id . value ] ;
133
- let ( fields, diagnostics) = lower_fields (
134
- db,
135
- krate,
136
- loc. container . local_id ,
137
- loc. id . tree_id ( ) ,
138
- & item_tree,
139
- krate. cfg_options ( db) ,
140
- FieldParent :: Struct ( loc. id . value ) ,
141
- & strukt. fields ,
142
- None ,
143
- ) ;
144
- let types_map = strukt. types_map . clone ( ) ;
145
-
146
- (
147
- Arc :: new ( StructData {
148
- name : strukt. name . clone ( ) ,
149
- variant_data : Arc :: new ( match strukt. shape {
150
- FieldsShape :: Record => VariantData :: Record { fields, types_map } ,
151
- FieldsShape :: Tuple => VariantData :: Tuple { fields, types_map } ,
152
- FieldsShape :: Unit => VariantData :: Unit ,
153
- } ) ,
154
- repr,
155
- visibility : item_tree[ strukt. visibility ] . clone ( ) ,
156
- flags,
157
- } ) ,
158
- DefDiagnostics :: new ( diagnostics) ,
159
- )
212
+ Arc :: new ( StructData {
213
+ name : strukt. name . clone ( ) ,
214
+ repr,
215
+ visibility : item_tree[ strukt. visibility ] . clone ( ) ,
216
+ flags,
217
+ } )
160
218
}
161
219
162
220
#[ inline]
163
221
pub ( crate ) fn union_data_query ( db : & dyn DefDatabase , id : UnionId ) -> Arc < StructData > {
164
- db. union_data_with_diagnostics ( id) . 0
165
- }
166
-
167
- pub ( crate ) fn union_data_with_diagnostics_query (
168
- db : & dyn DefDatabase ,
169
- id : UnionId ,
170
- ) -> ( Arc < StructData > , DefDiagnostics ) {
171
222
let loc = id. lookup ( db) ;
172
223
let krate = loc. container . krate ;
173
224
let item_tree = loc. id . item_tree ( db) ;
@@ -182,28 +233,13 @@ impl StructData {
182
233
}
183
234
184
235
let union = & item_tree[ loc. id . value ] ;
185
- let ( fields, diagnostics) = lower_fields (
186
- db,
187
- krate,
188
- loc. container . local_id ,
189
- loc. id . tree_id ( ) ,
190
- & item_tree,
191
- krate. cfg_options ( db) ,
192
- FieldParent :: Union ( loc. id . value ) ,
193
- & union. fields ,
194
- None ,
195
- ) ;
196
- let types_map = union. types_map . clone ( ) ;
197
- (
198
- Arc :: new ( StructData {
199
- name : union. name . clone ( ) ,
200
- variant_data : Arc :: new ( VariantData :: Record { fields, types_map } ) ,
201
- repr,
202
- visibility : item_tree[ union. visibility ] . clone ( ) ,
203
- flags,
204
- } ) ,
205
- DefDiagnostics :: new ( diagnostics) ,
206
- )
236
+
237
+ Arc :: new ( StructData {
238
+ name : union. name . clone ( ) ,
239
+ repr,
240
+ visibility : item_tree[ union. visibility ] . clone ( ) ,
241
+ flags,
242
+ } )
207
243
}
208
244
}
209
245
@@ -227,16 +263,16 @@ impl EnumVariants {
227
263
228
264
// [Adopted from rustc](https://github.com/rust-lang/rust/blob/bd53aa3bf7a24a70d763182303bd75e5fc51a9af/compiler/rustc_middle/src/ty/adt.rs#L446-L448)
229
265
pub fn is_payload_free ( & self , db : & dyn DefDatabase ) -> bool {
230
- self . variants . iter ( ) . all ( |( v, _) | {
266
+ self . variants . iter ( ) . all ( |& ( v, _) | {
231
267
// The condition check order is slightly modified from rustc
232
268
// to improve performance by early returning with relatively fast checks
233
- let variant = & db. enum_variant_data ( * v ) . variant_data ;
269
+ let variant = & db. variant_data ( v . into ( ) ) ;
234
270
if !variant. fields ( ) . is_empty ( ) {
235
271
return false ;
236
272
}
237
273
// The outer if condition is whether this variant has const ctor or not
238
274
if !matches ! ( variant. kind( ) , StructKind :: Unit ) {
239
- let body = db. body ( ( * v ) . into ( ) ) ;
275
+ let body = db. body ( v . into ( ) ) ;
240
276
// A variant with explicit discriminant
241
277
if body. exprs [ body. body_expr ] != Expr :: Missing {
242
278
return false ;
@@ -282,43 +318,11 @@ impl EnumVariantData {
282
318
db : & dyn DefDatabase ,
283
319
e : EnumVariantId ,
284
320
) -> Arc < EnumVariantData > {
285
- db. enum_variant_data_with_diagnostics ( e) . 0
286
- }
287
-
288
- pub ( crate ) fn enum_variant_data_with_diagnostics_query (
289
- db : & dyn DefDatabase ,
290
- e : EnumVariantId ,
291
- ) -> ( Arc < EnumVariantData > , DefDiagnostics ) {
292
321
let loc = e. lookup ( db) ;
293
- let container = loc. parent . lookup ( db) . container ;
294
- let krate = container. krate ;
295
322
let item_tree = loc. id . item_tree ( db) ;
296
323
let variant = & item_tree[ loc. id . value ] ;
297
324
298
- let ( fields, diagnostics) = lower_fields (
299
- db,
300
- krate,
301
- container. local_id ,
302
- loc. id . tree_id ( ) ,
303
- & item_tree,
304
- krate. cfg_options ( db) ,
305
- FieldParent :: Variant ( loc. id . value ) ,
306
- & variant. fields ,
307
- Some ( item_tree[ loc. parent . lookup ( db) . id . value ] . visibility ) ,
308
- ) ;
309
- let types_map = variant. types_map . clone ( ) ;
310
-
311
- (
312
- Arc :: new ( EnumVariantData {
313
- name : variant. name . clone ( ) ,
314
- variant_data : Arc :: new ( match variant. shape {
315
- FieldsShape :: Record => VariantData :: Record { fields, types_map } ,
316
- FieldsShape :: Tuple => VariantData :: Tuple { fields, types_map } ,
317
- FieldsShape :: Unit => VariantData :: Unit ,
318
- } ) ,
319
- } ) ,
320
- DefDiagnostics :: new ( diagnostics) ,
321
- )
325
+ Arc :: new ( EnumVariantData { name : variant. name . clone ( ) } )
322
326
}
323
327
}
324
328
@@ -352,15 +356,6 @@ impl VariantData {
352
356
VariantData :: Unit => StructKind :: Unit ,
353
357
}
354
358
}
355
-
356
- #[ allow( clippy:: self_named_constructors) ]
357
- pub ( crate ) fn variant_data ( db : & dyn DefDatabase , id : VariantId ) -> Arc < VariantData > {
358
- match id {
359
- VariantId :: StructId ( it) => db. struct_data ( it) . variant_data . clone ( ) ,
360
- VariantId :: EnumVariantId ( it) => db. enum_variant_data ( it) . variant_data . clone ( ) ,
361
- VariantId :: UnionId ( it) => db. union_data ( it) . variant_data . clone ( ) ,
362
- }
363
- }
364
359
}
365
360
366
361
#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
0 commit comments