Skip to content

Commit 7f2708c

Browse files
committed
Remove RustDefId
1 parent 2544b8c commit 7f2708c

File tree

5 files changed

+86
-183
lines changed

5 files changed

+86
-183
lines changed

src/librustc_middle/traits/chalk.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,6 @@ use std::cmp::Ordering;
1616
use std::fmt;
1717
use std::hash::{Hash, Hasher};
1818

19-
/// Since Chalk doesn't have full support for all Rust builtin types yet, we
20-
/// need to use an enum here, rather than just `DefId`.
21-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
22-
pub enum RustDefId {
23-
Adt(DefId),
24-
25-
FnDef(DefId),
26-
Trait(DefId),
27-
Impl(DefId),
28-
AssocTy(DefId),
29-
Opaque(DefId),
30-
}
31-
3219
#[derive(Copy, Clone)]
3320
pub struct RustInterner<'tcx> {
3421
pub tcx: TyCtxt<'tcx>,
@@ -86,8 +73,8 @@ impl<'tcx> chalk_ir::interner::Interner for RustInterner<'tcx> {
8673
type InternedQuantifiedWhereClauses = Vec<chalk_ir::QuantifiedWhereClause<Self>>;
8774
type InternedVariableKinds = Vec<chalk_ir::VariableKind<Self>>;
8875
type InternedCanonicalVarKinds = Vec<chalk_ir::CanonicalVarKind<Self>>;
89-
type DefId = RustDefId;
90-
type InternedAdtId = RustDefId;
76+
type DefId = DefId;
77+
type InternedAdtId = DefId;
9178
type Identifier = ();
9279

9380
fn debug_program_clause_implication(

src/librustc_middle/traits/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ pub use self::SelectionError::*;
3232
pub use self::Vtable::*;
3333

3434
pub use self::chalk::{
35-
ChalkEnvironmentAndGoal, ChalkEnvironmentClause, RustDefId as ChalkRustDefId,
36-
RustInterner as ChalkRustInterner,
35+
ChalkEnvironmentAndGoal, ChalkEnvironmentClause, RustInterner as ChalkRustInterner,
3736
};
3837

3938
/// Depending on the stage of compilation, we want projection to be

src/librustc_traits/chalk/db.rs

Lines changed: 73 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! either the `TyCtxt` (for information about types) or
77
//! `crate::chalk::lowering` (to lower rustc types into Chalk types).
88
9-
use rustc_middle::traits::{ChalkRustDefId as RustDefId, ChalkRustInterner as RustInterner};
9+
use rustc_middle::traits::ChalkRustInterner as RustInterner;
1010
use rustc_middle::ty::subst::{InternalSubsts, Subst, SubstsRef};
1111
use rustc_middle::ty::{self, AssocItemContainer, AssocKind, TyCtxt};
1212

@@ -39,10 +39,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
3939
&self,
4040
assoc_type_id: chalk_ir::AssocTypeId<RustInterner<'tcx>>,
4141
) -> 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;
4643
let assoc_item = self.tcx.associated_item(def_id);
4744
let trait_def_id = match assoc_item.container {
4845
AssocItemContainer::TraitContainer(def_id) => def_id,
@@ -64,7 +61,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
6461
.filter_map(|wc| LowerInto::<Option<chalk_ir::QuantifiedWhereClause<RustInterner<'tcx>>>>::lower_into(wc, &self.interner)).collect();
6562

6663
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),
6865
id: assoc_type_id,
6966
name: (),
7067
binders: chalk_ir::Binders::new(
@@ -78,10 +75,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
7875
&self,
7976
trait_id: chalk_ir::TraitId<RustInterner<'tcx>>,
8077
) -> 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;
8579
let trait_def = self.tcx.trait_def(def_id);
8680

8781
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
125119
&self,
126120
struct_id: chalk_ir::AdtId<RustInterner<'tcx>>,
127121
) -> 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);
131124

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);
134127

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
137139
.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()
170147
}
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;
174163
}
175164

176165
fn fn_def_datum(
177166
&self,
178167
fn_def_id: chalk_ir::FnDefId<RustInterner<'tcx>>,
179168
) -> 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;
184170
let bound_vars = bound_vars_for_item(self.tcx, def_id);
185171
let binders = binders_for(&self.interner, bound_vars);
186172

@@ -214,10 +200,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
214200
&self,
215201
impl_id: chalk_ir::ImplId<RustInterner<'tcx>>,
216202
) -> 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;
221204
let bound_vars = bound_vars_for_item(self.tcx, def_id);
222205
let binders = binders_for(&self.interner, bound_vars);
223206

@@ -248,10 +231,7 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
248231
trait_id: chalk_ir::TraitId<RustInterner<'tcx>>,
249232
parameters: &[chalk_ir::GenericArg<RustInterner<'tcx>>],
250233
) -> 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;
255235

256236
// FIXME(chalk): use TraitDef::for_each_relevant_impl, but that will
257237
// 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
270250
parameters[0].assert_ty_ref(&self.interner).could_match(&self.interner, &lowered_ty)
271251
});
272252

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();
276254
impls
277255
}
278256

@@ -281,19 +259,8 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
281259
auto_trait_id: chalk_ir::TraitId<RustInterner<'tcx>>,
282260
struct_id: chalk_ir::AdtId<RustInterner<'tcx>>,
283261
) -> 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;
297264
let all_impls = self.tcx.all_impls(trait_def_id);
298265
for impl_def_id in all_impls {
299266
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
314281
&self,
315282
associated_ty_id: chalk_solve::rust_ir::AssociatedTyValueId<RustInterner<'tcx>>,
316283
) -> 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;
321285
let assoc_item = self.tcx.associated_item(def_id);
322286
let impl_id = match assoc_item.container {
323287
AssocItemContainer::TraitContainer(def_id) => def_id,
@@ -332,8 +296,8 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
332296
let ty = self.tcx.type_of(def_id);
333297

334298
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),
337301
value: chalk_ir::Binders::new(
338302
binders,
339303
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
381345
match well_known {
382346
chalk_solve::rust_ir::WellKnownTrait::SizedTrait => match ty {
383347
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) }
400355
}
401-
402-
FnDef(_) | Trait(_) | Impl(_) | AssocTy(_) | Opaque(_) => panic!(),
403356
}
404357
}
405358
_ => None,
@@ -414,24 +367,14 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
414367
chalk_solve::rust_ir::WellKnownTrait::CopyTrait
415368
| chalk_solve::rust_ir::WellKnownTrait::CloneTrait => match ty {
416369
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) }
433377
}
434-
FnDef(_) | Trait(_) | Impl(_) | AssocTy(_) | Opaque(_) => panic!(),
435378
}
436379
}
437380
_ => None,
@@ -460,40 +403,20 @@ impl<'tcx> chalk_solve::RustIrDatabase<RustInterner<'tcx>> for RustIrDatabase<'t
460403
) -> Option<chalk_ir::TraitId<RustInterner<'tcx>>> {
461404
use chalk_solve::rust_ir::WellKnownTrait::*;
462405
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(),
487414
};
488415
Some(t)
489416
}
490417

491418
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)
497420
}
498421
}
499422

0 commit comments

Comments
 (0)