Skip to content

Commit 13303bb

Browse files
authored
Merge pull request #251 from nikomatsakis/simplify-rust-ir
simplify rust-ir
2 parents 774c7f6 + 6e1d848 commit 13303bb

File tree

12 files changed

+109
-96
lines changed

12 files changed

+109
-96
lines changed

chalk-parse/src/ast.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ impl fmt::Display for Kind {
128128
#[derive(Clone, PartialEq, Eq, Debug)]
129129
pub struct Impl {
130130
pub parameter_kinds: Vec<ParameterKind>,
131-
pub trait_ref: PolarizedTraitRef,
131+
pub trait_ref: TraitRef,
132+
pub polarity: Polarity,
132133
pub where_clauses: Vec<QuantifiedWhereClause>,
133134
pub assoc_ty_values: Vec<AssocTyValue>,
134135
pub impl_type: ImplType,
@@ -185,17 +186,20 @@ pub struct TraitRef {
185186
}
186187

187188
#[derive(Clone, PartialEq, Eq, Debug)]
188-
pub enum PolarizedTraitRef {
189-
Positive(TraitRef),
190-
Negative(TraitRef),
189+
pub enum Polarity {
190+
/// `impl Foo for Bar`
191+
Positive,
192+
193+
/// `impl !Foo for Bar`
194+
Negative,
191195
}
192196

193-
impl PolarizedTraitRef {
194-
pub fn from_bool(polarity: bool, trait_ref: TraitRef) -> PolarizedTraitRef {
197+
impl Polarity {
198+
pub fn from_bool(polarity: bool) -> Polarity {
195199
if polarity {
196-
PolarizedTraitRef::Positive(trait_ref)
200+
Polarity::Positive
197201
} else {
198-
PolarizedTraitRef::Negative(trait_ref)
202+
Polarity::Negative
199203
}
200204
}
201205
}

chalk-parse/src/parser.lalrpop

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,11 @@ Impl: Impl = {
136136
args.extend(a);
137137
Impl {
138138
parameter_kinds: p,
139-
trait_ref: PolarizedTraitRef::from_bool(mark.is_none(), TraitRef {
139+
polarity: Polarity::from_bool(mark.is_none()),
140+
trait_ref: TraitRef {
140141
trait_name: t,
141142
args: args,
142-
}),
143+
},
143144
where_clauses: w,
144145
assoc_ty_values: assoc,
145146
impl_type: external.map(|_| ImplType::External).unwrap_or(ImplType::Local),

chalk-rust-ir/src/lib.rs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,26 @@ pub enum LangItem {}
1818

1919
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
2020
pub struct ImplDatum {
21+
pub polarity: Polarity,
2122
pub binders: Binders<ImplDatumBound>,
23+
pub impl_type: ImplType,
2224
}
2325

2426
impl ImplDatum {
2527
pub fn is_positive(&self) -> bool {
26-
match self.binders.value.trait_ref {
27-
PolarizedTraitRef::Positive(_) => true,
28-
PolarizedTraitRef::Negative(_) => false,
29-
}
28+
self.polarity.is_positive()
29+
}
30+
31+
pub fn trait_id(&self) -> TraitId {
32+
self.binders.value.trait_ref.trait_id
3033
}
3134
}
3235

3336
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
3437
pub struct ImplDatumBound {
35-
pub trait_ref: PolarizedTraitRef,
38+
pub trait_ref: TraitRef,
3639
pub where_clauses: Vec<QuantifiedWhereClause>,
3740
pub associated_ty_values: Vec<AssociatedTyValue>,
38-
pub impl_type: ImplType,
3941
}
4042

4143
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
@@ -77,15 +79,20 @@ pub struct StructFlags {
7779
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
7880
pub struct TraitDatum {
7981
pub binders: Binders<TraitDatumBound>,
82+
83+
/// "Flags" indicate special kinds of traits, like auto traits.
84+
/// In Rust syntax these are represented in different ways, but in
85+
/// chalk we add annotations like `#[auto]`.
86+
pub flags: TraitFlags,
8087
}
8188

8289
impl TraitDatum {
8390
pub fn is_auto_trait(&self) -> bool {
84-
self.binders.value.flags.auto
91+
self.flags.auto
8592
}
8693

8794
pub fn is_non_enumerable_trait(&self) -> bool {
88-
self.binders.value.flags.non_enumerable
95+
self.flags.non_enumerable
8996
}
9097
}
9198

@@ -110,11 +117,6 @@ pub struct TraitDatumBound {
110117
/// ```
111118
pub where_clauses: Vec<QuantifiedWhereClause>,
112119

113-
/// "Flags" indicate special kinds of traits, like auto traits.
114-
/// In Rust syntax these are represented in different ways, but in
115-
/// chalk we add annotations like `#[auto]`.
116-
pub flags: TraitFlags,
117-
118120
/// The id of each associated type defined in the trait.
119121
pub associated_ty_ids: Vec<TypeId>,
120122
}
@@ -400,25 +402,17 @@ pub enum TypeSort {
400402
Trait,
401403
}
402404

403-
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
404-
pub enum PolarizedTraitRef {
405-
Positive(TraitRef),
406-
Negative(TraitRef),
405+
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
406+
pub enum Polarity {
407+
Positive,
408+
Negative,
407409
}
408410

409-
enum_fold!(PolarizedTraitRef[] { Positive(a), Negative(a) });
410-
411-
impl PolarizedTraitRef {
411+
impl Polarity {
412412
pub fn is_positive(&self) -> bool {
413413
match *self {
414-
PolarizedTraitRef::Positive(_) => true,
415-
PolarizedTraitRef::Negative(_) => false,
416-
}
417-
}
418-
419-
pub fn trait_ref(&self) -> &TraitRef {
420-
match *self {
421-
PolarizedTraitRef::Positive(ref tr) | PolarizedTraitRef::Negative(ref tr) => tr,
414+
Polarity::Positive => true,
415+
Polarity::Negative => false,
422416
}
423417
}
424418
}

chalk-solve/src/clauses/program_clauses.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl ToProgramClauses for ImplDatum {
3232
clauses.push(
3333
self.binders
3434
.map_ref(|bound| ProgramClauseImplication {
35-
consequence: bound.trait_ref.trait_ref().clone().cast(),
35+
consequence: bound.trait_ref.clone().cast(),
3636
conditions: bound.where_clauses.iter().cloned().casted().collect(),
3737
})
3838
.cast(),
@@ -94,7 +94,6 @@ impl ToProgramClauses for AssociatedTyValue {
9494
.binders
9595
.value
9696
.trait_ref
97-
.trait_ref()
9897
.shifted_in(self.value.len());
9998

10099
let all_parameters: Vec<_> = self
@@ -519,7 +518,7 @@ impl ToProgramClauses for TraitDatum {
519518
impl_may_exist
520519
}));
521520

522-
if !self.binders.value.flags.upstream {
521+
if !self.flags.upstream {
523522
let impl_allowed = self
524523
.binders
525524
.map_ref(|bound_datum| ProgramClauseImplication {
@@ -549,7 +548,7 @@ impl ToProgramClauses for TraitDatum {
549548

550549
// Fundamental traits can be reasoned about negatively without any ambiguity, so no
551550
// need for this rule if the trait is fundamental.
552-
if !self.binders.value.flags.fundamental {
551+
if !self.flags.fundamental {
553552
let impl_may_exist = self
554553
.binders
555554
.map_ref(|bound_datum| ProgramClauseImplication {

chalk-solve/src/coherence/orphan.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub fn perform_orphan_check(
2626
.binders
2727
.map_ref(|bound_impl| {
2828
// Ignoring the polarization of the impl's polarized trait ref
29-
DomainGoal::LocalImplAllowed(bound_impl.trait_ref.trait_ref().clone())
29+
DomainGoal::LocalImplAllowed(bound_impl.trait_ref.clone())
3030
})
3131
.cast();
3232

@@ -38,7 +38,7 @@ pub fn perform_orphan_check(
3838
debug!("overlaps = {:?}", is_allowed);
3939

4040
if !is_allowed {
41-
let trait_id = impl_datum.binders.value.trait_ref.trait_ref().trait_id;
41+
let trait_id = impl_datum.trait_id();
4242
let trait_name = db.type_name(trait_id.into());
4343
Err(CoherenceError::FailedOrphanCheck(trait_name))?;
4444
}

chalk-solve/src/coherence/solve.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ where
1818
) -> Result<(), CoherenceError> {
1919
// Ignore impls for marker traits as they are allowed to overlap.
2020
let trait_datum = self.db.trait_datum(self.trait_id);
21-
if trait_datum.binders.value.flags.marker {
21+
if trait_datum.flags.marker {
2222
return Ok(());
2323
}
2424

@@ -29,9 +29,7 @@ where
2929
let rhs = &self.db.impl_datum(r_id);
3030

3131
// Two negative impls never overlap.
32-
if !lhs.binders.value.trait_ref.is_positive()
33-
&& !rhs.binders.value.trait_ref.is_positive()
34-
{
32+
if !lhs.is_positive() && !rhs.is_positive() {
3533
continue;
3634
}
3735

@@ -171,9 +169,7 @@ where
171169
);
172170

173171
// Negative impls cannot specialize.
174-
if !less_special.binders.value.trait_ref.is_positive()
175-
|| !more_special.binders.value.trait_ref.is_positive()
176-
{
172+
if !less_special.is_positive() || !more_special.is_positive() {
177173
return false;
178174
}
179175

@@ -228,5 +224,5 @@ where
228224
}
229225

230226
fn params(impl_datum: &ImplDatum) -> &[Parameter] {
231-
&impl_datum.binders.value.trait_ref.trait_ref().parameters
227+
&impl_datum.binders.value.trait_ref.parameters
232228
}

chalk-solve/src/wf.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,9 @@ where
184184
pub fn verify_trait_impl(&self, impl_id: ImplId) -> Result<(), WfError> {
185185
let impl_datum = self.db.impl_datum(impl_id);
186186

187-
let trait_ref = match impl_datum.binders.value.trait_ref {
188-
PolarizedTraitRef::Positive(ref trait_ref) => trait_ref,
189-
_ => return Ok(()),
190-
};
187+
if !impl_datum.is_positive() {
188+
return Ok(());
189+
}
191190

192191
// We retrieve all the input types of the where clauses appearing on the trait impl,
193192
// e.g. in:
@@ -217,6 +216,7 @@ where
217216
// }
218217
// ```
219218
let mut header_input_types = Vec::new();
219+
let trait_ref = &impl_datum.binders.value.trait_ref;
220220
trait_ref.fold(&mut header_input_types);
221221

222222
// Associated type values are special because they can be parametric (independently of
@@ -341,7 +341,7 @@ where
341341
if is_legal {
342342
Ok(())
343343
} else {
344-
let trait_ref = impl_datum.binders.value.trait_ref.trait_ref();
344+
let trait_ref = &impl_datum.binders.value.trait_ref;
345345
let name = self.db.type_name(trait_ref.trait_id.into());
346346
Err(WfError::IllFormedTraitImpl(name))
347347
}

src/db.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl RustIrDatabase for ChalkDatabase {
9393
.impl_data
9494
.iter()
9595
.filter(|(_, impl_datum)| {
96-
let trait_ref = impl_datum.binders.value.trait_ref.trait_ref();
96+
let trait_ref = &impl_datum.binders.value.trait_ref;
9797
trait_id == trait_ref.trait_id && {
9898
assert_eq!(trait_ref.parameters.len(), parameters.len());
9999
<[_] as CouldMatch<[_]>>::could_match(&parameters, &trait_ref.parameters)
@@ -109,9 +109,7 @@ impl RustIrDatabase for ChalkDatabase {
109109
.impl_data
110110
.iter()
111111
.filter(|(_, impl_datum)| {
112-
let impl_trait_id = impl_datum.binders.value.trait_ref.trait_ref().trait_id;
113-
let impl_type = impl_datum.binders.value.impl_type;
114-
impl_trait_id == trait_id && impl_type == ImplType::Local
112+
impl_datum.trait_id() == trait_id && impl_datum.impl_type == ImplType::Local
115113
})
116114
.map(|(&impl_id, _)| impl_id)
117115
.collect()
@@ -126,7 +124,7 @@ impl RustIrDatabase for ChalkDatabase {
126124
.impl_data
127125
.values()
128126
.any(|impl_datum| {
129-
let impl_trait_ref = impl_datum.binders.value.trait_ref.trait_ref();
127+
let impl_trait_ref = &impl_datum.binders.value.trait_ref;
130128
impl_trait_ref.trait_id == auto_trait_id
131129
&& match impl_trait_ref.parameters[0].assert_ty_ref() {
132130
Ty::Apply(apply) => match apply.name {

0 commit comments

Comments
 (0)