6
6
//! either the `TyCtxt` (for information about types) or
7
7
//! `crate::chalk::lowering` (to lower rustc types into Chalk types).
8
8
9
- use rustc_middle:: traits:: { ChalkRustDefId as RustDefId , ChalkRustInterner as RustInterner } ;
9
+ use rustc_middle:: traits:: ChalkRustInterner as RustInterner ;
10
10
use rustc_middle:: ty:: subst:: { InternalSubsts , Subst , SubstsRef } ;
11
11
use rustc_middle:: ty:: { self , AssocItemContainer , AssocKind , TyCtxt } ;
12
12
@@ -39,10 +39,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
39
39
& self ,
40
40
assoc_type_id : chalk_ir:: AssocTypeId < RustInterner < ' tcx > > ,
41
41
) -> Arc < chalk_solve:: rust_ir:: AssociatedTyDatum < RustInterner < ' tcx > > > {
42
- let def_id = match assoc_type_id. 0 {
43
- RustDefId :: AssocTy ( def_id) => def_id,
44
- _ => bug ! ( "Did not use `AssocTy` variant when expecting associated type." ) ,
45
- } ;
42
+ let def_id = assoc_type_id. 0 ;
46
43
let assoc_item = self . tcx . associated_item ( def_id) ;
47
44
let trait_def_id = match assoc_item. container {
48
45
AssocItemContainer :: TraitContainer ( def_id) => def_id,
@@ -64,7 +61,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
64
61
. filter_map ( |wc| LowerInto :: < Option < chalk_ir:: QuantifiedWhereClause < RustInterner < ' tcx > > > > :: lower_into ( wc, & self . interner ) ) . collect ( ) ;
65
62
66
63
Arc :: new ( chalk_solve:: rust_ir:: AssociatedTyDatum {
67
- trait_id : chalk_ir:: TraitId ( RustDefId :: Trait ( trait_def_id) ) ,
64
+ trait_id : chalk_ir:: TraitId ( trait_def_id) ,
68
65
id : assoc_type_id,
69
66
name : ( ) ,
70
67
binders : chalk_ir:: Binders :: new (
@@ -78,10 +75,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
78
75
& self ,
79
76
trait_id : chalk_ir:: TraitId < RustInterner < ' tcx > > ,
80
77
) -> Arc < chalk_solve:: rust_ir:: TraitDatum < RustInterner < ' tcx > > > {
81
- let def_id = match trait_id. 0 {
82
- RustDefId :: Trait ( def_id) => def_id,
83
- _ => bug ! ( "Did not use `Trait` variant when expecting trait." ) ,
84
- } ;
78
+ let def_id = trait_id. 0 ;
85
79
let trait_def = self . tcx . trait_def ( def_id) ;
86
80
87
81
let bound_vars = bound_vars_for_item ( self . tcx , def_id) ;
@@ -125,62 +119,54 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
125
119
& self ,
126
120
struct_id : chalk_ir:: AdtId < RustInterner < ' tcx > > ,
127
121
) -> Arc < chalk_solve:: rust_ir:: AdtDatum < RustInterner < ' tcx > > > {
128
- match struct_id. 0 {
129
- RustDefId :: Adt ( adt_def_id) => {
130
- let adt_def = self . tcx . adt_def ( adt_def_id) ;
122
+ let adt_def_id = struct_id. 0 ;
123
+ let adt_def = self . tcx . adt_def ( adt_def_id) ;
131
124
132
- let bound_vars = bound_vars_for_item ( self . tcx , adt_def_id) ;
133
- let binders = binders_for ( & self . interner , bound_vars) ;
125
+ let bound_vars = bound_vars_for_item ( self . tcx , adt_def_id) ;
126
+ let binders = binders_for ( & self . interner , bound_vars) ;
134
127
135
- let predicates = self . tcx . predicates_of ( adt_def_id) . predicates ;
136
- let where_clauses: Vec < _ > = predicates
128
+ let predicates = self . tcx . predicates_of ( adt_def_id) . predicates ;
129
+ let where_clauses: Vec < _ > = predicates
130
+ . into_iter ( )
131
+ . map ( |( wc, _) | wc. subst ( self . tcx , bound_vars) )
132
+ . filter_map ( |wc| LowerInto :: < Option < chalk_ir:: QuantifiedWhereClause < RustInterner < ' tcx > > > > :: lower_into ( wc, & self . interner ) )
133
+ . collect ( ) ;
134
+ let fields = match adt_def. adt_kind ( ) {
135
+ ty:: AdtKind :: Struct | ty:: AdtKind :: Union => {
136
+ let variant = adt_def. non_enum_variant ( ) ;
137
+ variant
138
+ . fields
137
139
. iter ( )
138
- . map ( |( wc, _) | wc. subst ( self . tcx , bound_vars) )
139
- . filter_map ( |wc| LowerInto :: < Option < chalk_ir:: QuantifiedWhereClause < RustInterner < ' tcx > > > > :: lower_into ( wc, & self . interner ) )
140
- . collect ( ) ;
141
- let fields = match adt_def. adt_kind ( ) {
142
- ty:: AdtKind :: Struct | ty:: AdtKind :: Union => {
143
- let variant = adt_def. non_enum_variant ( ) ;
144
- variant
145
- . fields
146
- . iter ( )
147
- . map ( |field| {
148
- self . tcx
149
- . type_of ( field. did )
150
- . subst ( self . tcx , bound_vars)
151
- . lower_into ( & self . interner )
152
- } )
153
- . collect ( )
154
- }
155
- // FIXME(chalk): handle enums; force_impl_for requires this
156
- ty:: AdtKind :: Enum => vec ! [ ] ,
157
- } ;
158
- let struct_datum = Arc :: new ( chalk_solve:: rust_ir:: AdtDatum {
159
- id : struct_id,
160
- binders : chalk_ir:: Binders :: new (
161
- binders,
162
- chalk_solve:: rust_ir:: AdtDatumBound { fields, where_clauses } ,
163
- ) ,
164
- flags : chalk_solve:: rust_ir:: AdtFlags {
165
- upstream : !adt_def_id. is_local ( ) ,
166
- fundamental : adt_def. is_fundamental ( ) ,
167
- } ,
168
- } ) ;
169
- struct_datum
140
+ . map ( |field| {
141
+ self . tcx
142
+ . type_of ( field. did )
143
+ . subst ( self . tcx , bound_vars)
144
+ . lower_into ( & self . interner )
145
+ } )
146
+ . collect ( )
170
147
}
171
-
172
- v => bug ! ( "Used not struct variant ({:?}) when expecting struct variant." , v) ,
173
- }
148
+ // FIXME(chalk): handle enums; force_impl_for requires this
149
+ ty:: AdtKind :: Enum => vec ! [ ] ,
150
+ } ;
151
+ let struct_datum = Arc :: new ( chalk_solve:: rust_ir:: AdtDatum {
152
+ id : struct_id,
153
+ binders : chalk_ir:: Binders :: new (
154
+ binders,
155
+ chalk_solve:: rust_ir:: AdtDatumBound { fields, where_clauses } ,
156
+ ) ,
157
+ flags : chalk_solve:: rust_ir:: AdtFlags {
158
+ upstream : !adt_def_id. is_local ( ) ,
159
+ fundamental : adt_def. is_fundamental ( ) ,
160
+ } ,
161
+ } ) ;
162
+ return struct_datum;
174
163
}
175
164
176
165
fn fn_def_datum (
177
166
& self ,
178
167
fn_def_id : chalk_ir:: FnDefId < RustInterner < ' tcx > > ,
179
168
) -> Arc < chalk_solve:: rust_ir:: FnDefDatum < RustInterner < ' tcx > > > {
180
- let def_id = match fn_def_id. 0 {
181
- RustDefId :: FnDef ( def_id) => def_id,
182
- _ => bug ! ( "Did not use `FnDef` variant when expecting FnDef." ) ,
183
- } ;
169
+ let def_id = fn_def_id. 0 ;
184
170
let bound_vars = bound_vars_for_item ( self . tcx , def_id) ;
185
171
let binders = binders_for ( & self . interner , bound_vars) ;
186
172
@@ -214,10 +200,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
214
200
& self ,
215
201
impl_id : chalk_ir:: ImplId < RustInterner < ' tcx > > ,
216
202
) -> Arc < chalk_solve:: rust_ir:: ImplDatum < RustInterner < ' tcx > > > {
217
- let def_id = match impl_id. 0 {
218
- RustDefId :: Impl ( def_id) => def_id,
219
- _ => bug ! ( "Did not use `Impl` variant when expecting impl." ) ,
220
- } ;
203
+ let def_id = impl_id. 0 ;
221
204
let bound_vars = bound_vars_for_item ( self . tcx , def_id) ;
222
205
let binders = binders_for ( & self . interner , bound_vars) ;
223
206
@@ -248,10 +231,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
248
231
trait_id : chalk_ir:: TraitId < RustInterner < ' tcx > > ,
249
232
parameters : & [ chalk_ir:: GenericArg < RustInterner < ' tcx > > ] ,
250
233
) -> Vec < chalk_ir:: ImplId < RustInterner < ' tcx > > > {
251
- let def_id: DefId = match trait_id. 0 {
252
- RustDefId :: Trait ( def_id) => def_id,
253
- _ => bug ! ( "Did not use `Trait` variant when expecting trait." ) ,
254
- } ;
234
+ let def_id = trait_id. 0 ;
255
235
256
236
// FIXME(chalk): use TraitDef::for_each_relevant_impl, but that will
257
237
// require us to be able to interconvert `Ty<'tcx>`, and we're
@@ -270,9 +250,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
270
250
parameters[ 0 ] . assert_ty_ref ( & self . interner ) . could_match ( & self . interner , & lowered_ty)
271
251
} ) ;
272
252
273
- let impls = matched_impls
274
- . map ( |matched_impl| chalk_ir:: ImplId ( RustDefId :: Impl ( matched_impl) ) )
275
- . collect ( ) ;
253
+ let impls = matched_impls. map ( |matched_impl| chalk_ir:: ImplId ( matched_impl) ) . collect ( ) ;
276
254
impls
277
255
}
278
256
@@ -281,19 +259,8 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
281
259
auto_trait_id : chalk_ir:: TraitId < RustInterner < ' tcx > > ,
282
260
struct_id : chalk_ir:: AdtId < RustInterner < ' tcx > > ,
283
261
) -> bool {
284
- let trait_def_id: DefId = match auto_trait_id. 0 {
285
- RustDefId :: Trait ( def_id) => def_id,
286
- _ => bug ! ( "Did not use `Trait` variant when expecting trait." ) ,
287
- } ;
288
- // FIXME(chalk): this match can be removed when builtin types supported
289
- match struct_id. 0 {
290
- RustDefId :: Adt ( _) => { }
291
- _ => bug ! ( "Did not use `Adt` variant when expecting adt." ) ,
292
- }
293
- let adt_def_id: DefId = match struct_id. 0 {
294
- RustDefId :: Adt ( def_id) => def_id,
295
- _ => bug ! ( "Did not use `Adt` variant when expecting adt." ) ,
296
- } ;
262
+ let trait_def_id = auto_trait_id. 0 ;
263
+ let adt_def_id = struct_id. 0 ;
297
264
let all_impls = self . tcx . all_impls ( trait_def_id) ;
298
265
for impl_def_id in all_impls {
299
266
let trait_ref = self . tcx . impl_trait_ref ( impl_def_id) . unwrap ( ) ;
@@ -314,10 +281,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
314
281
& self ,
315
282
associated_ty_id : chalk_solve:: rust_ir:: AssociatedTyValueId < RustInterner < ' tcx > > ,
316
283
) -> Arc < chalk_solve:: rust_ir:: AssociatedTyValue < RustInterner < ' tcx > > > {
317
- let def_id = match associated_ty_id. 0 {
318
- RustDefId :: AssocTy ( def_id) => def_id,
319
- _ => bug ! ( "Did not use `AssocTy` variant when expecting associated type." ) ,
320
- } ;
284
+ let def_id = associated_ty_id. 0 ;
321
285
let assoc_item = self . tcx . associated_item ( def_id) ;
322
286
let impl_id = match assoc_item. container {
323
287
AssocItemContainer :: TraitContainer ( def_id) => def_id,
@@ -332,8 +296,8 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
332
296
let ty = self . tcx . type_of ( def_id) ;
333
297
334
298
Arc :: new ( chalk_solve:: rust_ir:: AssociatedTyValue {
335
- impl_id : chalk_ir:: ImplId ( RustDefId :: Impl ( impl_id) ) ,
336
- associated_ty_id : chalk_ir:: AssocTypeId ( RustDefId :: AssocTy ( def_id) ) ,
299
+ impl_id : chalk_ir:: ImplId ( impl_id) ,
300
+ associated_ty_id : chalk_ir:: AssocTypeId ( def_id) ,
337
301
value : chalk_ir:: Binders :: new (
338
302
binders,
339
303
chalk_solve:: rust_ir:: AssociatedTyValueBound { ty : ty. lower_into ( & self . interner ) } ,
@@ -381,25 +345,14 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
381
345
match well_known {
382
346
chalk_solve:: rust_ir:: WellKnownTrait :: SizedTrait => match ty {
383
347
Apply ( apply) => match apply. name {
384
- chalk_ir:: TypeName :: Adt ( chalk_ir:: AdtId ( rust_def_id) ) => {
385
- use rustc_middle:: traits:: ChalkRustDefId :: * ;
386
- match rust_def_id {
387
- Adt ( adt_def_id) => {
388
- let adt_def = self . tcx . adt_def ( adt_def_id) ;
389
- match adt_def. adt_kind ( ) {
390
- ty:: AdtKind :: Struct | ty:: AdtKind :: Union => None ,
391
- ty:: AdtKind :: Enum => {
392
- let constraint = self . tcx . adt_sized_constraint ( adt_def_id) ;
393
- if !constraint. 0 . is_empty ( ) {
394
- unimplemented ! ( )
395
- } else {
396
- Some ( true )
397
- }
398
- }
399
- }
348
+ chalk_ir:: TypeName :: Adt ( chalk_ir:: AdtId ( adt_def_id) ) => {
349
+ let adt_def = self . tcx . adt_def ( adt_def_id) ;
350
+ match adt_def. adt_kind ( ) {
351
+ ty:: AdtKind :: Struct | ty:: AdtKind :: Union => None ,
352
+ ty:: AdtKind :: Enum => {
353
+ let constraint = self . tcx . adt_sized_constraint ( adt_def_id) ;
354
+ if constraint. 0 . len ( ) > 0 { unimplemented ! ( ) } else { Some ( true ) }
400
355
}
401
-
402
- FnDef ( _) | Trait ( _) | Impl ( _) | AssocTy ( _) | Opaque ( _) => panic ! ( ) ,
403
356
}
404
357
}
405
358
_ => None ,
@@ -414,24 +367,14 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
414
367
chalk_solve:: rust_ir:: WellKnownTrait :: CopyTrait
415
368
| chalk_solve:: rust_ir:: WellKnownTrait :: CloneTrait => match ty {
416
369
Apply ( apply) => match apply. name {
417
- chalk_ir:: TypeName :: Adt ( chalk_ir:: AdtId ( rust_def_id) ) => {
418
- use rustc_middle:: traits:: ChalkRustDefId :: * ;
419
- match rust_def_id {
420
- Adt ( adt_def_id) => {
421
- let adt_def = self . tcx . adt_def ( adt_def_id) ;
422
- match adt_def. adt_kind ( ) {
423
- ty:: AdtKind :: Struct | ty:: AdtKind :: Union => None ,
424
- ty:: AdtKind :: Enum => {
425
- let constraint = self . tcx . adt_sized_constraint ( adt_def_id) ;
426
- if !constraint. 0 . is_empty ( ) {
427
- unimplemented ! ( )
428
- } else {
429
- Some ( true )
430
- }
431
- }
432
- }
370
+ chalk_ir:: TypeName :: Adt ( chalk_ir:: AdtId ( adt_def_id) ) => {
371
+ let adt_def = self . tcx . adt_def ( adt_def_id) ;
372
+ match adt_def. adt_kind ( ) {
373
+ ty:: AdtKind :: Struct | ty:: AdtKind :: Union => None ,
374
+ ty:: AdtKind :: Enum => {
375
+ let constraint = self . tcx . adt_sized_constraint ( adt_def_id) ;
376
+ if constraint. 0 . len ( ) > 0 { unimplemented ! ( ) } else { Some ( true ) }
433
377
}
434
- FnDef ( _) | Trait ( _) | Impl ( _) | AssocTy ( _) | Opaque ( _) => panic ! ( ) ,
435
378
}
436
379
}
437
380
_ => None ,
@@ -460,40 +403,20 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
460
403
) -> Option < chalk_ir:: TraitId < RustInterner < ' tcx > > > {
461
404
use chalk_solve:: rust_ir:: WellKnownTrait :: * ;
462
405
let t = match well_known_trait {
463
- SizedTrait => self
464
- . tcx
465
- . lang_items ( )
466
- . sized_trait ( )
467
- . map ( |t| chalk_ir:: TraitId ( RustDefId :: Trait ( t) ) )
468
- . unwrap ( ) ,
469
- CopyTrait => self
470
- . tcx
471
- . lang_items ( )
472
- . copy_trait ( )
473
- . map ( |t| chalk_ir:: TraitId ( RustDefId :: Trait ( t) ) )
474
- . unwrap ( ) ,
475
- CloneTrait => self
476
- . tcx
477
- . lang_items ( )
478
- . clone_trait ( )
479
- . map ( |t| chalk_ir:: TraitId ( RustDefId :: Trait ( t) ) )
480
- . unwrap ( ) ,
481
- DropTrait => self
482
- . tcx
483
- . lang_items ( )
484
- . drop_trait ( )
485
- . map ( |t| chalk_ir:: TraitId ( RustDefId :: Trait ( t) ) )
486
- . unwrap ( ) ,
406
+ SizedTrait => {
407
+ self . tcx . lang_items ( ) . sized_trait ( ) . map ( |t| chalk_ir:: TraitId ( t) ) . unwrap ( )
408
+ }
409
+ CopyTrait => self . tcx . lang_items ( ) . copy_trait ( ) . map ( |t| chalk_ir:: TraitId ( t) ) . unwrap ( ) ,
410
+ CloneTrait => {
411
+ self . tcx . lang_items ( ) . clone_trait ( ) . map ( |t| chalk_ir:: TraitId ( t) ) . unwrap ( )
412
+ }
413
+ DropTrait => self . tcx . lang_items ( ) . drop_trait ( ) . map ( |t| chalk_ir:: TraitId ( t) ) . unwrap ( ) ,
487
414
} ;
488
415
Some ( t)
489
416
}
490
417
491
418
fn is_object_safe ( & self , trait_id : chalk_ir:: TraitId < RustInterner < ' tcx > > ) -> bool {
492
- let def_id: DefId = match trait_id. 0 {
493
- RustDefId :: Trait ( def_id) => def_id,
494
- _ => bug ! ( "Did not use `Trait` variant when expecting trait." ) ,
495
- } ;
496
- self . tcx . is_object_safe ( def_id)
419
+ self . tcx . is_object_safe ( trait_id. 0 )
497
420
}
498
421
}
499
422
0 commit comments