Skip to content

Commit 5a73fd5

Browse files
committed
Implement Chalk lowering rule Normalize-From-Impl
1 parent 7360d6d commit 5a73fd5

File tree

6 files changed

+81
-5
lines changed

6 files changed

+81
-5
lines changed

src/librustc/ich/impls_ty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,7 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::DomainGoal<'tcx>
13881388
FromEnv(where_clause) => where_clause.hash_stable(hcx, hasher),
13891389

13901390
WellFormedTy(ty) => ty.hash_stable(hcx, hasher),
1391+
Normalize(projection) => projection.hash_stable(hcx, hasher),
13911392
FromEnvTy(ty) => ty.hash_stable(hcx, hasher),
13921393
RegionOutlives(predicate) => predicate.hash_stable(hcx, hasher),
13931394
TypeOutlives(predicate) => predicate.hash_stable(hcx, hasher),

src/librustc/traits/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ pub enum DomainGoal<'tcx> {
266266
WellFormed(WhereClauseAtom<'tcx>),
267267
FromEnv(WhereClauseAtom<'tcx>),
268268
WellFormedTy(Ty<'tcx>),
269+
Normalize(ty::ProjectionPredicate<'tcx>),
269270
FromEnvTy(Ty<'tcx>),
270271
RegionOutlives(ty::RegionOutlivesPredicate<'tcx>),
271272
TypeOutlives(ty::TypeOutlivesPredicate<'tcx>),

src/librustc/traits/structural_impls.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ impl<'tcx> fmt::Display for traits::DomainGoal<'tcx> {
450450
FromEnv(Implemented(trait_ref)) => write!(fmt, "FromEnv({})", trait_ref),
451451
FromEnv(ProjectionEq(projection)) => write!(fmt, "FromEnv({})", projection),
452452
WellFormedTy(ty) => write!(fmt, "WellFormed({})", ty),
453+
Normalize(projection) => write!(fmt, "Normalize({})", projection),
453454
FromEnvTy(ty) => write!(fmt, "FromEnv({})", ty),
454455
RegionOutlives(predicate) => write!(fmt, "RegionOutlives({})", predicate),
455456
TypeOutlives(predicate) => write!(fmt, "TypeOutlives({})", predicate),
@@ -538,6 +539,7 @@ EnumTypeFoldableImpl! {
538539
(traits::DomainGoal::WellFormed)(wc),
539540
(traits::DomainGoal::FromEnv)(wc),
540541
(traits::DomainGoal::WellFormedTy)(ty),
542+
(traits::DomainGoal::Normalize)(projection),
541543
(traits::DomainGoal::FromEnvTy)(ty),
542544
(traits::DomainGoal::RegionOutlives)(predicate),
543545
(traits::DomainGoal::TypeOutlives)(predicate),

src/librustc_traits/lowering.rs

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,20 @@ crate fn program_clauses_for<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefI
118118
-> Lrc<&'tcx Slice<Clause<'tcx>>>
119119
{
120120
let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
121-
let item = tcx.hir.expect_item(node_id);
122-
match item.node {
123-
hir::ItemTrait(..) => program_clauses_for_trait(tcx, def_id),
124-
hir::ItemImpl(..) => program_clauses_for_impl(tcx, def_id),
121+
let node = tcx.hir.find(node_id).unwrap();
122+
match node {
123+
hir::map::Node::NodeItem(item) => match item.node {
124+
hir::ItemTrait(..) => program_clauses_for_trait(tcx, def_id),
125+
hir::ItemImpl(..) => program_clauses_for_impl(tcx, def_id),
126+
_ => Lrc::new(vec![]),
127+
}
128+
hir::map::Node::NodeImplItem(item) => {
129+
if let hir::ImplItemKind::Type(..) = item.node {
130+
program_clauses_for_associated_type(tcx, def_id)
131+
} else {
132+
Lrc::new(vec![])
133+
}
134+
},
125135

126136
// FIXME: other constructions e.g. traits, associated types...
127137
_ => Lrc::new(tcx.mk_clauses(iter::empty::<Clause>())),
@@ -233,6 +243,53 @@ fn program_clauses_for_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId
233243
Lrc::new(tcx.mk_clauses(iter::once(Clause::ForAll(ty::Binder::dummy(clause)))))
234244
}
235245

246+
pub fn program_clauses_for_associated_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_id: DefId)
247+
-> Lrc<Vec<Clause<'tcx>>> {
248+
// Rule Normalize-From-Impl (see rustc guide)
249+
//
250+
// ```impl<P0..Pn> Trait<A1..An> for A0
251+
// where WC
252+
// {
253+
// type AssocType<Pn+1..Pm> where WC1 = T;
254+
// }```
255+
//
256+
// ```
257+
// forall<P0..Pm> {
258+
// forall<Pn+1..Pm> {
259+
// Normalize(<A0 as Trait<A1..An>>::AssocType<Pn+1..Pm> -> T) :-
260+
// WC && WC1
261+
// }
262+
// }
263+
// ```
264+
265+
let item = tcx.associated_item(item_id);
266+
debug_assert_eq!(item.kind, ty::AssociatedKind::Type);
267+
let impl_id = if let ty::AssociatedItemContainer::ImplContainer(impl_id) = item.container {
268+
impl_id
269+
} else {
270+
bug!()
271+
};
272+
// `A0 as Trait<A1..An>`
273+
let trait_ref = tcx.impl_trait_ref(impl_id).unwrap();
274+
// `T`
275+
let ty = tcx.type_of(item_id);
276+
// `WC`
277+
let impl_where_clauses = tcx.predicates_of(impl_id).predicates.lower();
278+
// `WC1`
279+
let item_where_clauses = tcx.predicates_of(item_id).predicates.lower();
280+
// `WC && WC1`
281+
let mut where_clauses = vec![];
282+
where_clauses.extend(impl_where_clauses);
283+
where_clauses.extend(item_where_clauses);
284+
// `<A0 as Trait<A1..An>>::AssocType<Pn+1..Pm>`
285+
let projection_ty = ty::ProjectionTy::from_ref_and_name(tcx, trait_ref, item.name);
286+
// `Normalize(<A0 as Trait<A1..An>>::AssocType<Pn+1..Pm> -> T)`
287+
let normalize_goal = DomainGoal::Normalize(ty::ProjectionPredicate { projection_ty, ty });
288+
// `Normalize(... -> T) :- WC && WC1`
289+
let clause = Clause::Implies(where_clauses, normalize_goal);
290+
Lrc::new(vec![clause])
291+
}
292+
236293
pub fn dump_program_clauses<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
237294
if !tcx.features().rustc_attrs {
238295
return;

src/test/ui/chalkify/lower_impl.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ trait Foo { }
1515
#[rustc_dump_program_clauses] //~ ERROR Implemented(T: Foo) :-
1616
impl<T: 'static> Foo for T where T: Iterator<Item = i32> { }
1717

18+
trait Bar {
19+
type Assoc;
20+
}
21+
22+
impl<T> Bar for T where T: Iterator<Item = i32> {
23+
#[rustc_dump_program_clauses] //~ ERROR Normalize(<T as Bar>::Assoc == std::vec::Vec<T>) :-
24+
type Assoc = Vec<T>;
25+
}
26+
1827
fn main() {
1928
println!("hello");
2029
}

src/test/ui/chalkify/lower_impl.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@ error: Implemented(T: Foo) :- ProjectionEq(<T as std::iter::Iterator>::Item == i
44
LL | #[rustc_dump_program_clauses] //~ ERROR Implemented(T: Foo) :-
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error: aborting due to previous error
7+
error: Normalize(<T as Bar>::Assoc == std::vec::Vec<T>) :- ProjectionEq(<T as std::iter::Iterator>::Item == i32), Implemented(T: std::iter::Iterator), Implemented(T: std::marker::Sized).
8+
--> $DIR/lower_impl.rs:23:5
9+
|
10+
LL | #[rustc_dump_program_clauses] //~ ERROR Normalize(<T as Bar>::Assoc == std::vec::Vec<T>) :-
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
814

0 commit comments

Comments
 (0)