Skip to content

Commit 7cfcefd

Browse files
committed
add projection_ty_from_predicates query
1 parent 8ee206a commit 7cfcefd

File tree

5 files changed

+31
-32
lines changed

5 files changed

+31
-32
lines changed

src/librustc_infer/infer/error_reporting/mod.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -1574,17 +1574,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15741574
.unwrap()
15751575
.def_id;
15761576

1577-
let mut projection_ty = None;
1578-
for (predicate, _) in self.tcx.predicates_of(def_id).predicates {
1579-
if let ty::PredicateAtom::Projection(projection_predicate) =
1580-
predicate.skip_binders()
1581-
{
1582-
if item_def_id == projection_predicate.projection_ty.item_def_id {
1583-
projection_ty = Some(projection_predicate.projection_ty);
1584-
break;
1585-
}
1586-
}
1587-
}
1577+
let projection_ty = self.tcx.projection_ty_from_predicates((def_id, item_def_id));
15881578
if let Some(projection_ty) = projection_ty {
15891579
let projection_query = self.canonicalize_query(
15901580
&ParamEnvAnd { param_env: self.tcx.param_env(def_id), value: projection_ty },

src/librustc_middle/query/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ rustc_queries! {
173173
desc { |tcx| "finding projection predicates for `{}`", tcx.def_path_str(key) }
174174
}
175175

176+
query projection_ty_from_predicates(key: (DefId, DefId)) -> Option<ty::ProjectionTy<'tcx>> {
177+
desc { |tcx| "finding projection type inside predicates of `{}`", tcx.def_path_str(key.0) }
178+
}
179+
176180
query native_libraries(_: CrateNum) -> Lrc<Vec<NativeLib>> {
177181
desc { "looking up the native libraries of a linked crate" }
178182
}

src/librustc_typeck/check/expr.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -1523,15 +1523,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15231523
let item_def_id =
15241524
self.tcx.associated_items(future_trait).in_definition_order().next().unwrap().def_id;
15251525

1526-
let mut projection_ty = None;
1527-
for (predicate, _) in self.tcx.predicates_of(def_id).predicates {
1528-
if let ty::PredicateAtom::Projection(projection_predicate) = predicate.skip_binders() {
1529-
if item_def_id == projection_predicate.projection_ty.item_def_id {
1530-
projection_ty = Some(projection_predicate.projection_ty);
1531-
break;
1532-
}
1533-
}
1534-
}
1526+
let projection_ty = self.tcx.projection_ty_from_predicates((def_id, item_def_id));
15351527
debug!("suggest_await_on_field_access: projection_ty={:?}", projection_ty);
15361528

15371529
let cause = self.misc(expr.span);

src/librustc_typeck/check/method/suggest.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
870870
call: &hir::Expr<'_>,
871871
span: Span,
872872
) {
873-
if let ty::Opaque(def_id, _substs) = ty.kind {
873+
if let ty::Opaque(def_id, _) = ty.kind {
874874
let future_trait = self.tcx.require_lang_item(LangItem::Future, None);
875875
// Future::Output
876876
let item_def_id = self
@@ -881,17 +881,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
881881
.unwrap()
882882
.def_id;
883883

884-
let mut projection_ty = None;
885-
for (predicate, _) in self.tcx.predicates_of(def_id).predicates {
886-
if let ty::PredicateAtom::Projection(projection_predicate) =
887-
predicate.skip_binders()
888-
{
889-
if item_def_id == projection_predicate.projection_ty.item_def_id {
890-
projection_ty = Some(projection_predicate.projection_ty);
891-
break;
892-
}
893-
}
894-
}
884+
let projection_ty = self.tcx.projection_ty_from_predicates((def_id, item_def_id));
895885
let cause = self.misc(span);
896886
let mut selcx = SelectionContext::new(&self.infcx);
897887
let mut obligations = vec![];

src/librustc_typeck/collect.rs

+23
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub fn provide(providers: &mut Providers) {
7070
generics_of,
7171
predicates_of,
7272
predicates_defined_on,
73+
projection_ty_from_predicates,
7374
explicit_predicates_of,
7475
super_predicates_of,
7576
type_param_predicates,
@@ -2051,6 +2052,28 @@ fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicat
20512052
result
20522053
}
20532054

2055+
fn projection_ty_from_predicates(
2056+
tcx: TyCtxt<'tcx>,
2057+
key: (
2058+
// ty_def_id
2059+
DefId,
2060+
// def_id of `N` in `<T as Trait>::N`
2061+
DefId,
2062+
),
2063+
) -> Option<ty::ProjectionTy<'tcx>> {
2064+
let (ty_def_id, item_def_id) = key;
2065+
let mut projection_ty = None;
2066+
for (predicate, _) in tcx.predicates_of(ty_def_id).predicates {
2067+
if let ty::PredicateAtom::Projection(projection_predicate) = predicate.skip_binders() {
2068+
if item_def_id == projection_predicate.projection_ty.item_def_id {
2069+
projection_ty = Some(projection_predicate.projection_ty);
2070+
break;
2071+
}
2072+
}
2073+
}
2074+
projection_ty
2075+
}
2076+
20542077
fn trait_associated_item_predicates(
20552078
tcx: TyCtxt<'tcx>,
20562079
def_id: DefId,

0 commit comments

Comments
 (0)