Skip to content

Commit 1151d62

Browse files
committed
split ignore_qualifiers
1 parent c6c0d17 commit 1151d62

File tree

42 files changed

+196
-181
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+196
-181
lines changed

src/librustc_infer/infer/outlives/env.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::infer::{GenericKind, InferCtxt};
33
use crate::traits::query::OutlivesBound;
44
use rustc_data_structures::fx::FxHashMap;
55
use rustc_hir as hir;
6-
use rustc_middle::ty::{self, TyCtxt};
6+
use rustc_middle::ty;
77

88
use super::explicit_outlives_bounds;
99

@@ -69,15 +69,15 @@ pub struct OutlivesEnvironment<'tcx> {
6969
pub type RegionBoundPairs<'tcx> = Vec<(ty::Region<'tcx>, GenericKind<'tcx>)>;
7070

7171
impl<'a, 'tcx> OutlivesEnvironment<'tcx> {
72-
pub fn new(tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self {
72+
pub fn new(param_env: ty::ParamEnv<'tcx>) -> Self {
7373
let mut env = OutlivesEnvironment {
7474
param_env,
7575
free_region_map: Default::default(),
7676
region_bound_pairs_map: Default::default(),
7777
region_bound_pairs_accum: vec![],
7878
};
7979

80-
env.add_outlives_bounds(None, explicit_outlives_bounds(tcx, param_env));
80+
env.add_outlives_bounds(None, explicit_outlives_bounds(param_env));
8181

8282
env
8383
}

src/librustc_infer/infer/outlives/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ pub mod obligations;
55
pub mod verify;
66

77
use rustc_middle::traits::query::OutlivesBound;
8-
use rustc_middle::ty::{self, TyCtxt};
8+
use rustc_middle::ty;
99

1010
pub fn explicit_outlives_bounds<'tcx>(
11-
tcx: TyCtxt<'tcx>,
1211
param_env: ty::ParamEnv<'tcx>,
1312
) -> impl Iterator<Item = OutlivesBound<'tcx>> + 'tcx {
1413
debug!("explicit_outlives_bounds()");

src/librustc_infer/infer/outlives/verify.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,8 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
331331
compare_ty: impl Fn(Ty<'tcx>) -> bool,
332332
predicates: impl Iterator<Item = ty::Predicate<'tcx>>,
333333
) -> impl Iterator<Item = ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>> {
334-
let tcx = self.tcx;
335334
predicates
336-
.filter_map(move |p| p.to_opt_type_outlives(tcx))
335+
.filter_map(|p| p.to_opt_type_outlives())
337336
.filter_map(|p| p.no_bound_vars())
338337
.filter(move |p| compare_ty(p.0))
339338
}

src/librustc_infer/traits/util.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,14 @@ fn predicate_obligation<'tcx>(
131131
}
132132

133133
impl Elaborator<'tcx> {
134-
pub fn filter_to_traits(self) -> FilterToTraits<'tcx, Self> {
135-
FilterToTraits::new(self.visited.tcx, self)
134+
pub fn filter_to_traits(self) -> FilterToTraits<Self> {
135+
FilterToTraits::new(self)
136136
}
137137

138138
fn elaborate(&mut self, obligation: &PredicateObligation<'tcx>) {
139139
let tcx = self.visited.tcx;
140140

141-
match obligation.predicate.ignore_qualifiers(tcx).skip_binder().kind() {
141+
match obligation.predicate.ignore_qualifiers().skip_binder().kind() {
142142
ty::PredicateKind::ForAll(_) => {
143143
bug!("unexpected predicate: {:?}", obligation.predicate)
144144
}
@@ -275,7 +275,7 @@ impl Iterator for Elaborator<'tcx> {
275275
// Supertrait iterator
276276
///////////////////////////////////////////////////////////////////////////
277277

278-
pub type Supertraits<'tcx> = FilterToTraits<'tcx, Elaborator<'tcx>>;
278+
pub type Supertraits<'tcx> = FilterToTraits<Elaborator<'tcx>>;
279279

280280
pub fn supertraits<'tcx>(
281281
tcx: TyCtxt<'tcx>,
@@ -297,23 +297,22 @@ pub fn transitive_bounds<'tcx>(
297297

298298
/// A filter around an iterator of predicates that makes it yield up
299299
/// just trait references.
300-
pub struct FilterToTraits<'tcx, I> {
301-
tcx: TyCtxt<'tcx>,
300+
pub struct FilterToTraits<I> {
302301
base_iterator: I,
303302
}
304303

305-
impl<'tcx, I> FilterToTraits<'tcx, I> {
306-
fn new(tcx: TyCtxt<'tcx>, base: I) -> FilterToTraits<'tcx, I> {
307-
FilterToTraits { tcx, base_iterator: base }
304+
impl<I> FilterToTraits<I> {
305+
fn new(base: I) -> FilterToTraits<I> {
306+
FilterToTraits { base_iterator: base }
308307
}
309308
}
310309

311-
impl<'tcx, I: Iterator<Item = PredicateObligation<'tcx>>> Iterator for FilterToTraits<'tcx, I> {
310+
impl<'tcx, I: Iterator<Item = PredicateObligation<'tcx>>> Iterator for FilterToTraits<I> {
312311
type Item = ty::PolyTraitRef<'tcx>;
313312

314313
fn next(&mut self) -> Option<ty::PolyTraitRef<'tcx>> {
315314
while let Some(obligation) = self.base_iterator.next() {
316-
if let Some(data) = obligation.predicate.to_opt_poly_trait_ref(self.tcx) {
315+
if let Some(data) = obligation.predicate.to_opt_poly_trait_ref() {
317316
return Some(data);
318317
}
319318
}

src/librustc_lint/builtin.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ impl<'tcx> LateLintPass<'tcx> for TrivialConstraints {
12101210
for &(predicate, span) in predicates.predicates {
12111211
// We don't actually look inside of the predicate,
12121212
// so it is safe to skip this binder here.
1213-
let predicate_kind_name = match predicate.ignore_qualifiers(cx.tcx).skip_binder().kind() {
1213+
let predicate_kind_name = match predicate.ignore_qualifiers().skip_binder().kind() {
12141214
Trait(..) => "Trait",
12151215
TypeOutlives(..) |
12161216
RegionOutlives(..) => "Lifetime",
@@ -1495,13 +1495,12 @@ declare_lint_pass!(ExplicitOutlivesRequirements => [EXPLICIT_OUTLIVES_REQUIREMEN
14951495

14961496
impl ExplicitOutlivesRequirements {
14971497
fn lifetimes_outliving_lifetime<'tcx>(
1498-
tcx: TyCtxt<'tcx>,
14991498
inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)],
15001499
index: u32,
15011500
) -> Vec<ty::Region<'tcx>> {
15021501
inferred_outlives
15031502
.iter()
1504-
.filter_map(|(pred, _)| match pred.ignore_qualifiers(tcx).skip_binder().kind() {
1503+
.filter_map(|(pred, _)| match pred.ignore_qualifiers().skip_binder().kind() {
15051504
&ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => match a {
15061505
ty::ReEarlyBound(ebr) if ebr.index == index => Some(b),
15071506
_ => None,
@@ -1512,13 +1511,12 @@ impl ExplicitOutlivesRequirements {
15121511
}
15131512

15141513
fn lifetimes_outliving_type<'tcx>(
1515-
tcx: TyCtxt<'tcx>,
15161514
inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)],
15171515
index: u32,
15181516
) -> Vec<ty::Region<'tcx>> {
15191517
inferred_outlives
15201518
.iter()
1521-
.filter_map(|(pred, _)| match pred.ignore_qualifiers(tcx).skip_binder().kind() {
1519+
.filter_map(|(pred, _)| match pred.ignore_qualifiers().skip_binder().kind() {
15221520
&ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(a, b)) => {
15231521
a.is_param(index).then_some(b)
15241522
}
@@ -1539,10 +1537,10 @@ impl ExplicitOutlivesRequirements {
15391537

15401538
match param.kind {
15411539
hir::GenericParamKind::Lifetime { .. } => {
1542-
Self::lifetimes_outliving_lifetime(tcx, inferred_outlives, index)
1540+
Self::lifetimes_outliving_lifetime(inferred_outlives, index)
15431541
}
15441542
hir::GenericParamKind::Type { .. } => {
1545-
Self::lifetimes_outliving_type(tcx, inferred_outlives, index)
1543+
Self::lifetimes_outliving_type(inferred_outlives, index)
15461544
}
15471545
hir::GenericParamKind::Const { .. } => Vec::new(),
15481546
}
@@ -1694,11 +1692,7 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
16941692
cx.tcx.named_region(predicate.lifetime.hir_id)
16951693
{
16961694
(
1697-
Self::lifetimes_outliving_lifetime(
1698-
cx.tcx,
1699-
inferred_outlives,
1700-
index,
1701-
),
1695+
Self::lifetimes_outliving_lifetime(inferred_outlives, index),
17021696
&predicate.bounds,
17031697
predicate.span,
17041698
)
@@ -1714,11 +1708,7 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
17141708
if let Res::Def(DefKind::TyParam, def_id) = path.res {
17151709
let index = ty_generics.param_def_id_to_index[&def_id];
17161710
(
1717-
Self::lifetimes_outliving_type(
1718-
cx.tcx,
1719-
inferred_outlives,
1720-
index,
1721-
),
1711+
Self::lifetimes_outliving_type(inferred_outlives, index),
17221712
&predicate.bounds,
17231713
predicate.span,
17241714
)

src/librustc_lint/unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
148148
for (predicate, _) in cx.tcx.predicates_of(def).predicates {
149149
// We only look at the `DefId`, so it is safe to skip the binder here.
150150
if let ty::PredicateKind::Trait(ref poly_trait_predicate, _) =
151-
predicate.ignore_qualifiers(cx.tcx).skip_binder().kind()
151+
predicate.ignore_qualifiers().skip_binder().kind()
152152
{
153153
let def_id = poly_trait_predicate.trait_ref.def_id;
154154
let descr_pre =

src/librustc_middle/ty/mod.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,30 @@ impl<'tcx> Predicate<'tcx> {
10501050
}
10511051

10521052
/// Skips `PredicateKind::ForAll`.
1053-
pub fn ignore_qualifiers(self, tcx: TyCtxt<'tcx>) -> Binder<Predicate<'tcx>> {
1053+
pub fn ignore_qualifiers(self) -> Binder<Predicate<'tcx>> {
1054+
match self.kind() {
1055+
&PredicateKind::ForAll(binder) => binder,
1056+
ty::PredicateKind::Projection(..)
1057+
| ty::PredicateKind::Trait(..)
1058+
| ty::PredicateKind::Subtype(..)
1059+
| ty::PredicateKind::WellFormed(..)
1060+
| ty::PredicateKind::ObjectSafe(..)
1061+
| ty::PredicateKind::ClosureKind(..)
1062+
| ty::PredicateKind::TypeOutlives(..)
1063+
| ty::PredicateKind::ConstEvaluatable(..)
1064+
| ty::PredicateKind::ConstEquate(..)
1065+
| ty::PredicateKind::RegionOutlives(..) => Binder::dummy(self),
1066+
}
1067+
}
1068+
1069+
/// Skips `PredicateKind::ForAll`, while allowing for unbound variables.
1070+
///
1071+
/// This method requires the `TyCtxt` as it has to shift the unbound variables
1072+
/// outwards.
1073+
///
1074+
/// Do not use this method if you may end up just skipping the binder, as this
1075+
/// would leave the unbound variables at an incorrect binding level.
1076+
pub fn ignore_qualifiers_with_unbound_vars(self, tcx: TyCtxt<'tcx>) -> Binder<Predicate<'tcx>> {
10541077
match self.kind() {
10551078
&PredicateKind::ForAll(binder) => binder,
10561079
ty::PredicateKind::Projection(..)
@@ -1226,7 +1249,7 @@ impl<'tcx> Predicate<'tcx> {
12261249
// from the substitution and the value being substituted into, and
12271250
// this trick achieves that).
12281251
let substs = trait_ref.skip_binder().substs;
1229-
let pred = *self.ignore_qualifiers(tcx).skip_binder();
1252+
let pred = *self.ignore_qualifiers().skip_binder();
12301253
let new = pred.subst(tcx, substs);
12311254
if new != pred { new.potentially_qualified(tcx, PredicateKind::ForAll) } else { self }
12321255
}
@@ -1427,8 +1450,8 @@ impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
14271450
}
14281451

14291452
impl<'tcx> Predicate<'tcx> {
1430-
pub fn to_opt_poly_trait_ref(self, tcx: TyCtxt<'tcx>) -> Option<PolyTraitRef<'tcx>> {
1431-
self.ignore_qualifiers(tcx)
1453+
pub fn to_opt_poly_trait_ref(self) -> Option<PolyTraitRef<'tcx>> {
1454+
self.ignore_qualifiers()
14321455
.map_bound(|pred| match pred.kind() {
14331456
&PredicateKind::Trait(ref t, _) => Some(t.trait_ref),
14341457
PredicateKind::Projection(..)
@@ -1445,11 +1468,8 @@ impl<'tcx> Predicate<'tcx> {
14451468
.transpose()
14461469
}
14471470

1448-
pub fn to_opt_type_outlives(
1449-
self,
1450-
tcx: TyCtxt<'tcx>,
1451-
) -> Option<PolyTypeOutlivesPredicate<'tcx>> {
1452-
self.ignore_qualifiers(tcx)
1471+
pub fn to_opt_type_outlives(self) -> Option<PolyTypeOutlivesPredicate<'tcx>> {
1472+
self.ignore_qualifiers()
14531473
.map_bound(|pred| match pred.kind() {
14541474
&PredicateKind::TypeOutlives(data) => Some(data),
14551475
PredicateKind::Trait(..)

src/librustc_middle/ty/print/pretty.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,16 @@ pub trait PrettyPrinter<'tcx>:
572572
let mut is_sized = false;
573573
p!(write("impl"));
574574
for predicate in bounds.predicates {
575-
if let Some(trait_ref) = predicate.to_opt_poly_trait_ref(self.tcx()) {
575+
// Note: We can't use `to_opt_poly_trait_ref` here as `predicate`
576+
// may contain unbound variables. We therefore do this manually.
577+
//
578+
// FIXME(lcnr): Find out why exactly this is the case :)
579+
if let ty::PredicateKind::Trait(pred, _) = predicate
580+
.ignore_qualifiers_with_unbound_vars(self.tcx())
581+
.skip_binder()
582+
.kind()
583+
{
584+
let trait_ref = ty::Binder::bind(pred.trait_ref);
576585
// Don't print +Sized, but rather +?Sized if absent.
577586
if Some(trait_ref.def_id()) == self.tcx().lang_items().sized_trait() {
578587
is_sized = true;

src/librustc_mir/borrow_check/diagnostics/region_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
590590
let mut found = false;
591591
for predicate in bounds.predicates {
592592
if let ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(_, r)) =
593-
predicate.ignore_qualifiers(self.infcx.tcx).skip_binder().kind()
593+
predicate.ignore_qualifiers().skip_binder().kind()
594594
{
595595
if let ty::RegionKind::ReStatic = r {
596596
found = true;

src/librustc_mir/borrow_check/type_check/free_region_relations.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ impl UniversalRegionRelationsBuilder<'cx, 'tcx> {
274274

275275
// Insert the facts we know from the predicates. Why? Why not.
276276
let param_env = self.param_env;
277-
self.add_outlives_bounds(outlives::explicit_outlives_bounds(self.infcx.tcx, param_env));
277+
self.add_outlives_bounds(outlives::explicit_outlives_bounds(param_env));
278278

279279
// Finally:
280280
// - outlives is reflexive, so `'r: 'r` for every region `'r`

0 commit comments

Comments
 (0)