Skip to content

Commit 7d93920

Browse files
committed
Implement Chalk lowering rule Normalize-From-Impl
1 parent b2a7b94 commit 7d93920

File tree

6 files changed

+81
-5
lines changed

6 files changed

+81
-5
lines changed

src/librustc/ich/impls_ty.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,7 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for traits::DomainGoal<'tcx>
13731373
FromEnv(where_clause) => where_clause.hash_stable(hcx, hasher),
13741374

13751375
WellFormedTy(ty) => ty.hash_stable(hcx, hasher),
1376+
Normalize(projection) => projection.hash_stable(hcx, hasher),
13761377
FromEnvTy(ty) => ty.hash_stable(hcx, hasher),
13771378
RegionOutlives(predicate) => predicate.hash_stable(hcx, hasher),
13781379
TypeOutlives(predicate) => predicate.hash_stable(hcx, hasher),

src/librustc/traits/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ pub enum DomainGoal<'tcx> {
267267
WellFormed(WhereClauseAtom<'tcx>),
268268
FromEnv(WhereClauseAtom<'tcx>),
269269
WellFormedTy(Ty<'tcx>),
270+
Normalize(ty::ProjectionPredicate<'tcx>),
270271
FromEnvTy(Ty<'tcx>),
271272
RegionOutlives(ty::RegionOutlivesPredicate<'tcx>),
272273
TypeOutlives(ty::TypeOutlivesPredicate<'tcx>),

src/librustc/traits/structural_impls.rs

+2
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ impl<'tcx> fmt::Display for traits::DomainGoal<'tcx> {
449449
FromEnv(Implemented(trait_ref)) => write!(fmt, "FromEnv({})", trait_ref),
450450
FromEnv(ProjectionEq(projection)) => write!(fmt, "FromEnv({})", projection),
451451
WellFormedTy(ty) => write!(fmt, "WellFormed({})", ty),
452+
Normalize(projection) => write!(fmt, "Normalize({})", projection),
452453
FromEnvTy(ty) => write!(fmt, "FromEnv({})", ty),
453454
RegionOutlives(predicate) => write!(fmt, "RegionOutlives({})", predicate),
454455
TypeOutlives(predicate) => write!(fmt, "TypeOutlives({})", predicate),
@@ -537,6 +538,7 @@ EnumTypeFoldableImpl! {
537538
(traits::DomainGoal::WellFormed)(wc),
538539
(traits::DomainGoal::FromEnv)(wc),
539540
(traits::DomainGoal::WellFormedTy)(ty),
541+
(traits::DomainGoal::Normalize)(projection),
540542
(traits::DomainGoal::FromEnvTy)(ty),
541543
(traits::DomainGoal::RegionOutlives)(predicate),
542544
(traits::DomainGoal::TypeOutlives)(predicate),

src/librustc_traits/lowering.rs

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

124134
// FIXME: other constructions e.g. traits, associated types...
125135
_ => Lrc::new(vec![]),
@@ -229,6 +239,53 @@ fn program_clauses_for_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId
229239
Lrc::new(vec![Clause::ForAll(ty::Binder::dummy(clause))])
230240
}
231241

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

src/test/ui/chalkify/lower_impl.rs

+9
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

+7-1
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)