Skip to content

Commit d703622

Browse files
committed
Auto merge of #49626 - fanzier:chalk-lowering, r=scalexm
Implement Chalk lowering rule Normalize-From-Impl This extends the Chalk lowering pass with the "Normalize-From-Impl" rule for generating program clauses from a trait definition as part of #49177. r? @nikomatsakis
2 parents 8728c7a + b7c4a57 commit d703622

File tree

6 files changed

+87
-5
lines changed

6 files changed

+87
-5
lines changed

src/librustc/ich/impls_ty.rs

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

13891389
WellFormedTy(ty) => ty.hash_stable(hcx, hasher),
1390+
Normalize(projection) => projection.hash_stable(hcx, hasher),
13901391
FromEnvTy(ty) => ty.hash_stable(hcx, hasher),
13911392
RegionOutlives(predicate) => predicate.hash_stable(hcx, hasher),
13921393
TypeOutlives(predicate) => predicate.hash_stable(hcx, hasher),

src/librustc/traits/mod.rs

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

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

+67-4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ impl<'tcx> IntoFromEnvGoal for DomainGoal<'tcx> {
108108
FromEnv(..) |
109109
WellFormedTy(..) |
110110
FromEnvTy(..) |
111+
Normalize(..) |
111112
RegionOutlives(..) |
112113
TypeOutlives(..) => self,
113114
}
@@ -118,10 +119,20 @@ crate fn program_clauses_for<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefI
118119
-> Lrc<&'tcx Slice<Clause<'tcx>>>
119120
{
120121
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),
122+
let node = tcx.hir.find(node_id).unwrap();
123+
match node {
124+
hir::map::Node::NodeItem(item) => match item.node {
125+
hir::ItemTrait(..) => program_clauses_for_trait(tcx, def_id),
126+
hir::ItemImpl(..) => program_clauses_for_impl(tcx, def_id),
127+
_ => Lrc::new(tcx.mk_clauses(iter::empty::<Clause>())),
128+
}
129+
hir::map::Node::NodeImplItem(item) => {
130+
if let hir::ImplItemKind::Type(..) = item.node {
131+
program_clauses_for_associated_type_value(tcx, def_id)
132+
} else {
133+
Lrc::new(tcx.mk_clauses(iter::empty::<Clause>()))
134+
}
135+
},
125136

126137
// FIXME: other constructions e.g. traits, associated types...
127138
_ => Lrc::new(tcx.mk_clauses(iter::empty::<Clause>())),
@@ -233,6 +244,58 @@ fn program_clauses_for_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId
233244
Lrc::new(tcx.mk_clauses(iter::once(Clause::ForAll(ty::Binder::dummy(clause)))))
234245
}
235246

247+
pub fn program_clauses_for_associated_type_value<'a, 'tcx>(
248+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
249+
item_id: DefId,
250+
) -> Lrc<&'tcx Slice<Clause<'tcx>>> {
251+
// Rule Normalize-From-Impl (see rustc guide)
252+
//
253+
// ```impl<P0..Pn> Trait<A1..An> for A0
254+
// {
255+
// type AssocType<Pn+1..Pm> where WC = T;
256+
// }```
257+
//
258+
// ```
259+
// forall<P0..Pm> {
260+
// forall<Pn+1..Pm> {
261+
// Normalize(<A0 as Trait<A1..An>>::AssocType<Pn+1..Pm> -> T) :-
262+
// Implemented(A0: Trait<A1..An>) && WC
263+
// }
264+
// }
265+
// ```
266+
267+
let item = tcx.associated_item(item_id);
268+
debug_assert_eq!(item.kind, ty::AssociatedKind::Type);
269+
let impl_id = if let ty::AssociatedItemContainer::ImplContainer(impl_id) = item.container {
270+
impl_id
271+
} else {
272+
bug!()
273+
};
274+
// `A0 as Trait<A1..An>`
275+
let trait_ref = tcx.impl_trait_ref(impl_id).unwrap();
276+
// `T`
277+
let ty = tcx.type_of(item_id);
278+
// `Implemented(A0: Trait<A1..An>)`
279+
let trait_implemented = ty::Binder::dummy(ty::TraitPredicate { trait_ref }.lower());
280+
// `WC`
281+
let item_where_clauses = tcx.predicates_of(item_id).predicates.lower();
282+
// `Implemented(A0: Trait<A1..An>) && WC`
283+
let mut where_clauses = vec![trait_implemented];
284+
where_clauses.extend(item_where_clauses);
285+
// `<A0 as Trait<A1..An>>::AssocType<Pn+1..Pm>`
286+
let projection_ty = ty::ProjectionTy::from_ref_and_name(tcx, trait_ref, item.name);
287+
// `Normalize(<A0 as Trait<A1..An>>::AssocType<Pn+1..Pm> -> T)`
288+
let normalize_goal = DomainGoal::Normalize(ty::ProjectionPredicate { projection_ty, ty });
289+
// `Normalize(... -> T) :- ...`
290+
let clause = ProgramClause {
291+
goal: normalize_goal,
292+
hypotheses: tcx.mk_goals(
293+
where_clauses.into_iter().map(|wc| Goal::from_poly_domain_goal(wc, tcx))
294+
),
295+
};
296+
Lrc::new(tcx.mk_clauses(iter::once(Clause::ForAll(ty::Binder::dummy(clause)))))
297+
}
298+
236299
pub fn dump_program_clauses<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
237300
if !tcx.features().rustc_attrs {
238301
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>) :- Implemented(T: Bar).
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)