Skip to content

Commit ca8f4c8

Browse files
authored
Rollup merge of #104911 - spastorino:inferred_outlives_crate-return-clause, r=oli-obk
Make inferred_outlives_crate return Clause r? ``@oli-obk``
2 parents 3539cf9 + 537488e commit ca8f4c8

File tree

9 files changed

+49
-39
lines changed

9 files changed

+49
-39
lines changed

compiler/rustc_hir_analysis/src/collect.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs}
3333
use rustc_middle::mir::mono::Linkage;
3434
use rustc_middle::ty::query::Providers;
3535
use rustc_middle::ty::util::{Discr, IntTypeExt};
36-
use rustc_middle::ty::{self, AdtKind, Const, DefIdTree, IsSuggestable, Ty, TyCtxt};
36+
use rustc_middle::ty::{self, AdtKind, Const, DefIdTree, IsSuggestable, ToPredicate, Ty, TyCtxt};
3737
use rustc_session::lint;
3838
use rustc_session::parse::feature_err;
3939
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -1366,12 +1366,14 @@ fn predicates_defined_on(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicate
13661366
"predicates_defined_on: inferred_outlives_of({:?}) = {:?}",
13671367
def_id, inferred_outlives,
13681368
);
1369+
let inferred_outlives_iter =
1370+
inferred_outlives.iter().map(|(clause, span)| ((*clause).to_predicate(tcx), *span));
13691371
if result.predicates.is_empty() {
1370-
result.predicates = inferred_outlives;
1372+
result.predicates = tcx.arena.alloc_from_iter(inferred_outlives_iter);
13711373
} else {
1372-
result.predicates = tcx
1373-
.arena
1374-
.alloc_from_iter(result.predicates.iter().chain(inferred_outlives).copied());
1374+
result.predicates = tcx.arena.alloc_from_iter(
1375+
result.predicates.into_iter().copied().chain(inferred_outlives_iter),
1376+
);
13751377
}
13761378
}
13771379

compiler/rustc_hir_analysis/src/outlives/mod.rs

+8-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_hir as hir;
33
use rustc_hir::def_id::DefId;
44
use rustc_middle::ty::query::Providers;
55
use rustc_middle::ty::subst::GenericArgKind;
6-
use rustc_middle::ty::{self, CratePredicatesMap, ToPredicate, TyCtxt};
6+
use rustc_middle::ty::{self, CratePredicatesMap, TyCtxt};
77
use rustc_span::symbol::sym;
88
use rustc_span::Span;
99

@@ -17,7 +17,7 @@ pub fn provide(providers: &mut Providers) {
1717
*providers = Providers { inferred_outlives_of, inferred_outlives_crate, ..*providers };
1818
}
1919

20-
fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[(ty::Predicate<'_>, Span)] {
20+
fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[(ty::Clause<'_>, Span)] {
2121
let id = tcx.hir().local_def_id_to_hir_id(item_def_id.expect_local());
2222

2323
if matches!(tcx.def_kind(item_def_id), hir::def::DefKind::AnonConst) && tcx.lazy_normalization()
@@ -50,12 +50,10 @@ fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[(ty::Predicate
5050
if tcx.has_attr(item_def_id, sym::rustc_outlives) {
5151
let mut pred: Vec<String> = predicates
5252
.iter()
53-
.map(|(out_pred, _)| match out_pred.kind().skip_binder() {
54-
ty::PredicateKind::Clause(ty::Clause::RegionOutlives(p)) => {
55-
p.to_string()
56-
}
57-
ty::PredicateKind::Clause(ty::Clause::TypeOutlives(p)) => p.to_string(),
58-
err => bug!("unexpected predicate {:?}", err),
53+
.map(|(out_pred, _)| match out_pred {
54+
ty::Clause::RegionOutlives(p) => p.to_string(),
55+
ty::Clause::TypeOutlives(p) => p.to_string(),
56+
err => bug!("unexpected clause {:?}", err),
5957
})
6058
.collect();
6159
pred.sort();
@@ -103,19 +101,11 @@ fn inferred_outlives_crate(tcx: TyCtxt<'_>, (): ()) -> CratePredicatesMap<'_> {
103101
|(ty::OutlivesPredicate(kind1, region2), &span)| {
104102
match kind1.unpack() {
105103
GenericArgKind::Type(ty1) => Some((
106-
ty::Binder::dummy(ty::PredicateKind::Clause(ty::Clause::TypeOutlives(
107-
ty::OutlivesPredicate(ty1, *region2),
108-
)))
109-
.to_predicate(tcx),
104+
ty::Clause::TypeOutlives(ty::OutlivesPredicate(ty1, *region2)),
110105
span,
111106
)),
112107
GenericArgKind::Lifetime(region1) => Some((
113-
ty::Binder::dummy(ty::PredicateKind::Clause(
114-
ty::Clause::RegionOutlives(ty::OutlivesPredicate(
115-
region1, *region2,
116-
)),
117-
))
118-
.to_predicate(tcx),
108+
ty::Clause::RegionOutlives(ty::OutlivesPredicate(region1, *region2)),
119109
span,
120110
)),
121111
GenericArgKind::Const(_) => {

compiler/rustc_lint/src/builtin.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -2046,16 +2046,13 @@ declare_lint_pass!(ExplicitOutlivesRequirements => [EXPLICIT_OUTLIVES_REQUIREMEN
20462046

20472047
impl ExplicitOutlivesRequirements {
20482048
fn lifetimes_outliving_lifetime<'tcx>(
2049-
inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)],
2049+
inferred_outlives: &'tcx [(ty::Clause<'tcx>, Span)],
20502050
def_id: DefId,
20512051
) -> Vec<ty::Region<'tcx>> {
20522052
inferred_outlives
20532053
.iter()
2054-
.filter_map(|(pred, _)| match pred.kind().skip_binder() {
2055-
ty::PredicateKind::Clause(ty::Clause::RegionOutlives(ty::OutlivesPredicate(
2056-
a,
2057-
b,
2058-
))) => match *a {
2054+
.filter_map(|(clause, _)| match *clause {
2055+
ty::Clause::RegionOutlives(ty::OutlivesPredicate(a, b)) => match *a {
20592056
ty::ReEarlyBound(ebr) if ebr.def_id == def_id => Some(b),
20602057
_ => None,
20612058
},
@@ -2065,16 +2062,15 @@ impl ExplicitOutlivesRequirements {
20652062
}
20662063

20672064
fn lifetimes_outliving_type<'tcx>(
2068-
inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)],
2065+
inferred_outlives: &'tcx [(ty::Clause<'tcx>, Span)],
20692066
index: u32,
20702067
) -> Vec<ty::Region<'tcx>> {
20712068
inferred_outlives
20722069
.iter()
2073-
.filter_map(|(pred, _)| match pred.kind().skip_binder() {
2074-
ty::PredicateKind::Clause(ty::Clause::TypeOutlives(ty::OutlivesPredicate(
2075-
a,
2076-
b,
2077-
))) => a.is_param(index).then_some(b),
2070+
.filter_map(|(clause, _)| match *clause {
2071+
ty::Clause::TypeOutlives(ty::OutlivesPredicate(a, b)) => {
2072+
a.is_param(index).then_some(b)
2073+
}
20782074
_ => None,
20792075
})
20802076
.collect()

compiler/rustc_metadata/src/rmeta/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ define_tables! {
353353
explicit_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'static>>>,
354354
generics_of: Table<DefIndex, LazyValue<ty::Generics>>,
355355
// As an optimization, a missing entry indicates an empty `&[]`.
356-
inferred_outlives_of: Table<DefIndex, LazyArray<(ty::Predicate<'static>, Span)>>,
356+
inferred_outlives_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
357357
super_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'static>>>,
358358
type_of: Table<DefIndex, LazyValue<Ty<'static>>>,
359359
variances_of: Table<DefIndex, LazyArray<ty::Variance>>,

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ rustc_queries! {
562562

563563
/// Returns the inferred outlives predicates (e.g., for `struct
564564
/// Foo<'a, T> { x: &'a T }`, this would return `T: 'a`).
565-
query inferred_outlives_of(key: DefId) -> &'tcx [(ty::Predicate<'tcx>, Span)] {
565+
query inferred_outlives_of(key: DefId) -> &'tcx [(ty::Clause<'tcx>, Span)] {
566566
desc { |tcx| "computing inferred outlives predicates of `{}`", tcx.def_path_str(key) }
567567
cache_on_disk_if { key.is_local() }
568568
separate_provide_extern

compiler/rustc_middle/src/ty/codec.rs

+8
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,14 @@ impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D>
345345
}
346346
}
347347

348+
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D> for [(ty::Clause<'tcx>, Span)] {
349+
fn decode(decoder: &mut D) -> &'tcx Self {
350+
decoder.interner().arena.alloc_from_iter(
351+
(0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::<Vec<_>>(),
352+
)
353+
}
354+
}
355+
348356
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D>
349357
for ty::List<ty::BoundVariableKind>
350358
{

compiler/rustc_middle/src/ty/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ pub struct CratePredicatesMap<'tcx> {
734734
/// For each struct with outlive bounds, maps to a vector of the
735735
/// predicate of its outlive bounds. If an item has no outlives
736736
/// bounds, it will have no entry.
737-
pub predicates: FxHashMap<DefId, &'tcx [(Predicate<'tcx>, Span)]>,
737+
pub predicates: FxHashMap<DefId, &'tcx [(Clause<'tcx>, Span)]>,
738738
}
739739

740740
impl<'tcx> Predicate<'tcx> {
@@ -1167,6 +1167,13 @@ impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for Binder<'tcx, PredicateKind<'tc
11671167
}
11681168
}
11691169

1170+
impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for Clause<'tcx> {
1171+
#[inline(always)]
1172+
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
1173+
tcx.mk_predicate(ty::Binder::dummy(ty::PredicateKind::Clause(self)))
1174+
}
1175+
}
1176+
11701177
impl<'tcx> ToPredicate<'tcx, Predicate<'tcx>> for Binder<'tcx, TraitRef<'tcx>> {
11711178
#[inline(always)]
11721179
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {

compiler/rustc_middle/src/ty/parameterized.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_index::vec::{Idx, IndexVec};
55
use crate::middle::exported_symbols::ExportedSymbol;
66
use crate::mir::Body;
77
use crate::ty::{
8-
self, Const, FnSig, GeneratorDiagnosticData, GenericPredicates, Predicate, TraitRef, Ty,
8+
self, Clause, Const, FnSig, GeneratorDiagnosticData, GenericPredicates, Predicate, TraitRef, Ty,
99
};
1010

1111
pub trait ParameterizedOverTcx: 'static {
@@ -121,6 +121,7 @@ parameterized_over_tcx! {
121121
TraitRef,
122122
Const,
123123
Predicate,
124+
Clause,
124125
GeneratorDiagnosticData,
125126
Body,
126127
ExportedSymbol,

compiler/rustc_query_impl/src/on_disk_cache.rs

+6
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,12 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [(ty::Predicate<'tcx>
818818
}
819819
}
820820

821+
impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [(ty::Clause<'tcx>, Span)] {
822+
fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
823+
RefDecodable::decode(d)
824+
}
825+
}
826+
821827
impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [rustc_ast::InlineAsmTemplatePiece] {
822828
fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
823829
RefDecodable::decode(d)

0 commit comments

Comments
 (0)