Skip to content

Commit d1895b9

Browse files
committed
Renaming to avoid conflicts, add to AST, add tests.
1 parent ec90c38 commit d1895b9

File tree

10 files changed

+78
-21
lines changed

10 files changed

+78
-21
lines changed

chalk-parse/src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ pub enum WhereClause {
193193
UnifyTys { a: Ty, b: Ty },
194194
UnifyLifetimes { a: Lifetime, b: Lifetime },
195195
TraitInScope { trait_name: Identifier },
196+
Derefs { source: Ty, target: Ty },
196197
}
197198

198199
pub struct QuantifiedWhereClause {

chalk-parse/src/parser.lalrpop

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Goal1: Box<Goal> = {
3838
ExternalKeyword: () = "extern";
3939
AutoKeyword: () = "#" "[" "auto" "]";
4040
MarkerKeyword: () = "#" "[" "marker" "]";
41-
DerefKeyword: () = "#" "[" "deref" "]";
41+
DerefLangItem: () = "#" "[" "lang_deref" "]";
4242

4343
StructDefn: StructDefn = {
4444
<external:ExternalKeyword?> "struct" <n:Id><p:Angle<ParameterKind>>
@@ -55,7 +55,7 @@ StructDefn: StructDefn = {
5555
};
5656

5757
TraitDefn: TraitDefn = {
58-
<external:ExternalKeyword?> <auto:AutoKeyword?> <marker:MarkerKeyword?> <deref:DerefKeyword?> "trait" <n:Id><p:Angle<ParameterKind>>
58+
<external:ExternalKeyword?> <auto:AutoKeyword?> <marker:MarkerKeyword?> <deref:DerefLangItem?> "trait" <n:Id><p:Angle<ParameterKind>>
5959
<w:QuantifiedWhereClauses> "{" <a:AssocTyDefn*> "}" => TraitDefn
6060
{
6161
name: n,
@@ -241,6 +241,7 @@ WhereClause: WhereClause = {
241241
},
242242

243243
"InScope" "(" <t:Id> ")" => WhereClause::TraitInScope { trait_name: t },
244+
"Derefs" "(" <source:Ty> "," <target:Ty> ")" => WhereClause::Derefs { source, target },
244245
};
245246

246247
QuantifiedWhereClause: QuantifiedWhereClause = {

src/fold/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ enum_fold!(PolarizedTraitRef[] { Positive(a), Negative(a) });
425425
enum_fold!(ParameterKind[T,L] { Ty(a), Lifetime(a) } where T: Fold, L: Fold);
426426
enum_fold!(WhereClauseAtom[] { Implemented(a), ProjectionEq(a) });
427427
enum_fold!(DomainGoal[] { Holds(a), WellFormed(a), FromEnv(a), Normalize(a), UnselectedNormalize(a),
428-
WellFormedTy(a), FromEnvTy(a), InScope(a), Deref(a) });
428+
WellFormedTy(a), FromEnvTy(a), InScope(a), Derefs(a) });
429429
enum_fold!(LeafGoal[] { EqGoal(a), DomainGoal(a) });
430430
enum_fold!(Constraint[] { LifetimeEq(a, b) });
431431
enum_fold!(Goal[] { Quantified(qkind, subgoal), Implies(wc, subgoal), And(g1, g2), Not(g),
@@ -571,7 +571,7 @@ struct_fold!(AssociatedTyValueBound { ty, where_clauses });
571571
struct_fold!(Environment { clauses });
572572
struct_fold!(InEnvironment[F] { environment, goal } where F: Fold<Result = F>);
573573
struct_fold!(EqGoal { a, b });
574-
struct_fold!(Deref { source, target });
574+
struct_fold!(Derefs { source, target });
575575
struct_fold!(ProgramClauseImplication {
576576
consequence,
577577
conditions,

src/ir/debug.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ impl Debug for DomainGoal {
205205
DomainGoal::WellFormedTy(t) => write!(fmt, "WellFormed({:?})", t),
206206
DomainGoal::FromEnvTy(t) => write!(fmt, "FromEnv({:?})", t),
207207
DomainGoal::InScope(n) => write!(fmt, "InScope({:?})", n),
208-
DomainGoal::Deref(n) => write!(fmt, "Deref({:?})", n),
208+
DomainGoal::Derefs(n) => write!(fmt, "Derefs({:?})", n),
209209
}
210210
}
211211
}

src/ir/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ pub enum WhereClauseAtom {
464464
}
465465

466466
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
467-
pub struct Deref {
467+
pub struct Derefs {
468468
pub source: Ty,
469469
pub target: Ty,
470470
}
@@ -532,13 +532,12 @@ pub enum DomainGoal {
532532

533533
InScope(ItemId),
534534

535-
/// Whether a type can Deref into another. Right now this is just:
535+
/// Whether a type can deref into another. Right now this is just:
536536
/// ```notrust
537-
/// Deref(T, U) :- Implemented(T: Deref<Target = U>)
537+
/// Derefs(T, U) :- Implemented(T: Deref<Target = U>)
538538
/// ```
539-
/// In Rust there are also raw pointers which can be deref'd
540-
/// but do not implement Deref.
541-
Deref(Deref)
539+
/// In Rust there are also raw pointers which can be deref'd but do not implement Deref.
540+
Derefs(Derefs)
542541
}
543542

544543
pub type QuantifiedDomainGoal = Binders<DomainGoal>;

src/lower/mod.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,12 @@ impl LowerWhereClause<ir::DomainGoal> for WhereClause {
484484

485485
ir::DomainGoal::InScope(id)
486486
}
487+
WhereClause::Derefs { source, target } => {
488+
ir::DomainGoal::Derefs(ir::Derefs {
489+
source: source.lower(env)?,
490+
target: target.lower(env)?
491+
})
492+
}
487493
})
488494
}
489495
}
@@ -511,7 +517,8 @@ impl LowerWhereClause<ir::LeafGoal> for WhereClause {
511517
| WhereClause::TyWellFormed { .. }
512518
| WhereClause::TraitRefWellFormed { .. }
513519
| WhereClause::TyFromEnv { .. }
514-
| WhereClause::TraitRefFromEnv { .. } => {
520+
| WhereClause::TraitRefFromEnv { .. }
521+
| WhereClause::Derefs { .. } => {
515522
let g: ir::DomainGoal = self.lower(env)?;
516523
g.cast()
517524
}
@@ -1039,8 +1046,8 @@ impl ir::Program {
10391046
);
10401047
program_clauses.extend(self.default_impl_data.iter().map(|d| d.to_program_clause()));
10411048

1042-
// Adds clause that defines Deref:
1043-
// forall<T, U> { Deref(T, U) :- ProjectionEq(<T as Deref>::Target = U>) }
1049+
// Adds clause that defines the Derefs domain goal:
1050+
// forall<T, U> { Derefs(T, U) :- ProjectionEq(<T as Deref>::Target = U>) }
10441051
if let Some(trait_id) = self.lang_items.get(&ir::LangItem::DerefTrait) {
10451052
// Find `Deref::Target`.
10461053
let associated_ty_id = self.associated_ty_data.values()
@@ -1052,7 +1059,7 @@ impl ir::Program {
10521059
program_clauses.push(ir::Binders {
10531060
binders: vec![ir::ParameterKind::Ty(()), ir::ParameterKind::Ty(())],
10541061
value: ir::ProgramClauseImplication {
1055-
consequence: ir::DomainGoal::Deref(ir::Deref { source: t(), target: u() }),
1062+
consequence: ir::DomainGoal::Derefs(ir::Derefs { source: t(), target: u() }),
10561063
conditions: vec![ir::ProjectionEq {
10571064
projection: ir::ProjectionTy {
10581065
associated_ty_id,

src/lower/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -855,14 +855,14 @@ fn higher_ranked_cyclic_requirements() {
855855
fn deref_trait() {
856856
lowering_success! {
857857
program {
858-
#[deref] trait Deref { type Target; }
858+
#[lang_deref] trait Deref { type Target; }
859859
}
860860
}
861861

862862
lowering_error! {
863863
program {
864-
#[deref] trait Deref { }
865-
#[deref] trait DerefDupe { }
864+
#[lang_deref] trait Deref { }
865+
#[lang_deref] trait DerefDupe { }
866866
} error_msg {
867867
"Duplicate lang item `DerefTrait`"
868868
}

src/lower/wf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl FoldInputTypes for DomainGoal {
139139
DomainGoal::FromEnv(..) |
140140
DomainGoal::WellFormedTy(..) |
141141
DomainGoal::FromEnvTy(..) |
142-
DomainGoal::Deref(..) => panic!("unexpected where clause"),
142+
DomainGoal::Derefs(..) => panic!("unexpected where clause"),
143143

144144
DomainGoal::InScope(..) => (),
145145
}

src/solve/test/mod.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,3 +2058,52 @@ fn higher_ranked_implied_bounds() {
20582058
}
20592059
}
20602060
}
2061+
2062+
#[test]
2063+
fn deref_goal() {
2064+
test! {
2065+
program {
2066+
#[lang_deref]
2067+
trait Deref { type Target; }
2068+
struct Foo { }
2069+
struct Bar { }
2070+
struct Baz { }
2071+
impl Deref for Foo { type Target = Bar; }
2072+
}
2073+
2074+
goal {
2075+
Derefs(Foo, Bar)
2076+
} yields {
2077+
"Unique"
2078+
}
2079+
2080+
goal {
2081+
Derefs(Foo, Baz)
2082+
} yields {
2083+
"No possible solution"
2084+
}
2085+
}
2086+
2087+
test! {
2088+
program {
2089+
#[lang_deref]
2090+
trait Deref { type Target; }
2091+
struct Arc<T> { }
2092+
struct i32 { }
2093+
struct u64 { }
2094+
impl<T> Deref for Arc<T> { type Target = T; }
2095+
}
2096+
2097+
goal {
2098+
Derefs(Arc<i32>, i32)
2099+
} yields {
2100+
"Unique"
2101+
}
2102+
2103+
goal {
2104+
Derefs(Arc<i32>, u64)
2105+
} yields {
2106+
"No possible solution"
2107+
}
2108+
}
2109+
}

src/zip.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ struct_zip!(ProjectionEq { projection, ty });
182182
struct_zip!(UnselectedNormalize { projection, ty });
183183
struct_zip!(EqGoal { a, b });
184184
struct_zip!(ProgramClauseImplication { consequence, conditions });
185-
struct_zip!(Deref { source, target });
185+
struct_zip!(Derefs { source, target });
186186

187187
impl Zip for Environment {
188188
fn zip_with<Z: Zipper>(zipper: &mut Z, a: &Self, b: &Self) -> Fallible<()> {
@@ -226,7 +226,7 @@ enum_zip!(DomainGoal {
226226
WellFormedTy,
227227
FromEnvTy,
228228
InScope,
229-
Deref
229+
Derefs
230230
});
231231
enum_zip!(LeafGoal { DomainGoal, EqGoal });
232232
enum_zip!(ProgramClause { Implies, ForAll });

0 commit comments

Comments
 (0)