@@ -27,7 +27,6 @@ use crate::{
2727#[ derive( Debug , Clone , PartialEq , Eq ) ]
2828pub struct StructData {
2929 pub name : Name ,
30- pub variant_data : Arc < VariantData > ,
3130 pub repr : Option < ReprOptions > ,
3231 pub visibility : RawVisibility ,
3332 pub flags : StructFlags ,
@@ -69,7 +68,6 @@ pub struct EnumVariants {
6968#[ derive( Debug , Clone , PartialEq , Eq ) ]
7069pub struct EnumVariantData {
7170 pub name : Name ,
72- pub variant_data : Arc < VariantData > ,
7371}
7472
7573#[ derive( Debug , Clone , PartialEq , Eq ) ]
@@ -79,6 +77,94 @@ pub enum VariantData {
7977 Unit ,
8078}
8179
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+
82168/// A single field of an enum variant or struct
83169#[ derive( Debug , Clone , PartialEq , Eq ) ]
84170pub struct FieldData {
@@ -99,13 +185,6 @@ fn repr_from_value(
99185impl StructData {
100186 #[ inline]
101187 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 ) {
109188 let loc = id. lookup ( db) ;
110189 let krate = loc. container . krate ;
111190 let item_tree = loc. id . item_tree ( db) ;
@@ -130,44 +209,16 @@ impl StructData {
130209 }
131210
132211 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+ } )
160218 }
161219
162220 #[ inline]
163221 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 ) {
171222 let loc = id. lookup ( db) ;
172223 let krate = loc. container . krate ;
173224 let item_tree = loc. id . item_tree ( db) ;
@@ -182,28 +233,13 @@ impl StructData {
182233 }
183234
184235 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+ } )
207243 }
208244}
209245
@@ -227,16 +263,16 @@ impl EnumVariants {
227263
228264 // [Adopted from rustc](https://github.com/rust-lang/rust/blob/bd53aa3bf7a24a70d763182303bd75e5fc51a9af/compiler/rustc_middle/src/ty/adt.rs#L446-L448)
229265 pub fn is_payload_free ( & self , db : & dyn DefDatabase ) -> bool {
230- self . variants . iter ( ) . all ( |( v, _) | {
266+ self . variants . iter ( ) . all ( |& ( v, _) | {
231267 // The condition check order is slightly modified from rustc
232268 // 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 ( ) ) ;
234270 if !variant. fields ( ) . is_empty ( ) {
235271 return false ;
236272 }
237273 // The outer if condition is whether this variant has const ctor or not
238274 if !matches ! ( variant. kind( ) , StructKind :: Unit ) {
239- let body = db. body ( ( * v ) . into ( ) ) ;
275+ let body = db. body ( v . into ( ) ) ;
240276 // A variant with explicit discriminant
241277 if body. exprs [ body. body_expr ] != Expr :: Missing {
242278 return false ;
@@ -282,43 +318,11 @@ impl EnumVariantData {
282318 db : & dyn DefDatabase ,
283319 e : EnumVariantId ,
284320 ) -> 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 ) {
292321 let loc = e. lookup ( db) ;
293- let container = loc. parent . lookup ( db) . container ;
294- let krate = container. krate ;
295322 let item_tree = loc. id . item_tree ( db) ;
296323 let variant = & item_tree[ loc. id . value ] ;
297324
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 ( ) } )
322326 }
323327}
324328
@@ -352,15 +356,6 @@ impl VariantData {
352356 VariantData :: Unit => StructKind :: Unit ,
353357 }
354358 }
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- }
364359}
365360
366361#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
0 commit comments