Skip to content

Commit 8dced11

Browse files
committed
rustc: add Spans to inferred_outlives_of predicates.
1 parent cd9e444 commit 8dced11

File tree

5 files changed

+39
-23
lines changed

5 files changed

+39
-23
lines changed

src/librustc/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ rustc_queries! {
191191

192192
/// Returns the inferred outlives predicates (e.g., for `struct
193193
/// Foo<'a, T> { x: &'a T }`, this would return `T: 'a`).
194-
query inferred_outlives_of(_: DefId) -> &'tcx [ty::Predicate<'tcx>] {}
194+
query inferred_outlives_of(_: DefId) -> &'tcx [(ty::Predicate<'tcx>, Span)] {}
195195

196196
/// Maps from the `DefId` of a trait to the list of
197197
/// super-predicates. This is a subset of the full list of

src/librustc/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ pub struct CratePredicatesMap<'tcx> {
11361136
/// For each struct with outlive bounds, maps to a vector of the
11371137
/// predicate of its outlive bounds. If an item has no outlives
11381138
/// bounds, it will have no entry.
1139-
pub predicates: FxHashMap<DefId, &'tcx [ty::Predicate<'tcx>]>,
1139+
pub predicates: FxHashMap<DefId, &'tcx [(ty::Predicate<'tcx>, Span)]>,
11401140
}
11411141

11421142
impl<'tcx> AsRef<Predicate<'tcx>> for Predicate<'tcx> {

src/librustc_lint/builtin.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1518,10 +1518,10 @@ declare_lint_pass!(ExplicitOutlivesRequirements => [EXPLICIT_OUTLIVES_REQUIREMEN
15181518

15191519
impl ExplicitOutlivesRequirements {
15201520
fn lifetimes_outliving_lifetime<'tcx>(
1521-
inferred_outlives: &'tcx [ty::Predicate<'tcx>],
1521+
inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)],
15221522
index: u32,
15231523
) -> Vec<ty::Region<'tcx>> {
1524-
inferred_outlives.iter().filter_map(|pred| {
1524+
inferred_outlives.iter().filter_map(|(pred, _)| {
15251525
match pred {
15261526
ty::Predicate::RegionOutlives(outlives) => {
15271527
let outlives = outlives.skip_binder();
@@ -1538,10 +1538,10 @@ impl ExplicitOutlivesRequirements {
15381538
}
15391539

15401540
fn lifetimes_outliving_type<'tcx>(
1541-
inferred_outlives: &'tcx [ty::Predicate<'tcx>],
1541+
inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)],
15421542
index: u32,
15431543
) -> Vec<ty::Region<'tcx>> {
1544-
inferred_outlives.iter().filter_map(|pred| {
1544+
inferred_outlives.iter().filter_map(|(pred, _)| {
15451545
match pred {
15461546
ty::Predicate::TypeOutlives(outlives) => {
15471547
let outlives = outlives.skip_binder();
@@ -1560,7 +1560,7 @@ impl ExplicitOutlivesRequirements {
15601560
&self,
15611561
param: &'tcx hir::GenericParam,
15621562
tcx: TyCtxt<'tcx>,
1563-
inferred_outlives: &'tcx [ty::Predicate<'tcx>],
1563+
inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)],
15641564
ty_generics: &'tcx ty::Generics,
15651565
) -> Vec<ty::Region<'tcx>> {
15661566
let index = ty_generics.param_def_id_to_index[

src/librustc_typeck/collect.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1970,19 +1970,18 @@ fn predicates_defined_on(
19701970
);
19711971
let inferred_outlives = tcx.inferred_outlives_of(def_id);
19721972
if !inferred_outlives.is_empty() {
1973-
let span = tcx.def_span(def_id);
19741973
debug!(
19751974
"predicates_defined_on: inferred_outlives_of({:?}) = {:?}",
19761975
def_id,
19771976
inferred_outlives,
19781977
);
1979-
result.predicates = tcx.arena.alloc_from_iter(
1980-
result.predicates.iter().copied().chain(
1981-
// FIXME(eddyb) use better spans - maybe add `Span`s
1982-
// to `inferred_outlives_of` predicates as well?
1983-
inferred_outlives.iter().map(|&p| (p, span)),
1984-
),
1985-
);
1978+
if result.predicates.is_empty() {
1979+
result.predicates = inferred_outlives;
1980+
} else {
1981+
result.predicates = tcx.arena.alloc_from_iter(
1982+
result.predicates.iter().chain(inferred_outlives).copied(),
1983+
);
1984+
}
19861985
}
19871986
debug!("predicates_defined_on({:?}) = {:?}", def_id, result);
19881987
result

src/librustc_typeck/outlives/mod.rs

+25-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc::ty::query::Providers;
55
use rustc::ty::subst::GenericArgKind;
66
use rustc::ty::{self, CratePredicatesMap, TyCtxt};
77
use syntax::symbol::sym;
8+
use syntax_pos::Span;
89

910
mod explicit;
1011
mod implicit_infer;
@@ -23,7 +24,7 @@ pub fn provide(providers: &mut Providers<'_>) {
2324
fn inferred_outlives_of(
2425
tcx: TyCtxt<'_>,
2526
item_def_id: DefId,
26-
) -> &[ty::Predicate<'_>] {
27+
) -> &[(ty::Predicate<'_>, Span)] {
2728
let id = tcx
2829
.hir()
2930
.as_local_hir_id(item_def_id)
@@ -43,7 +44,7 @@ fn inferred_outlives_of(
4344
if tcx.has_attr(item_def_id, sym::rustc_outlives) {
4445
let mut pred: Vec<String> = predicates
4546
.iter()
46-
.map(|out_pred| match out_pred {
47+
.map(|(out_pred, _)| match out_pred {
4748
ty::Predicate::RegionOutlives(p) => p.to_string(),
4849
ty::Predicate::TypeOutlives(p) => p.to_string(),
4950
err => bug!("unexpected predicate {:?}", err),
@@ -96,27 +97,43 @@ fn inferred_outlives_crate(
9697
let predicates = global_inferred_outlives
9798
.iter()
9899
.map(|(&def_id, set)| {
99-
let predicates = tcx.arena.alloc_from_iter(set
100+
let def_span = tcx.def_span(def_id);
101+
let generics = tcx.generics_of(def_id);
102+
let predicates = &*tcx.arena.alloc_from_iter(set
100103
.iter()
101104
.filter_map(
102105
|ty::OutlivesPredicate(kind1, region2)| match kind1.unpack() {
103106
GenericArgKind::Type(ty1) => {
104-
Some(ty::Predicate::TypeOutlives(ty::Binder::bind(
107+
// FIXME(eddyb) compute `Span`s in `implicit_infer`.
108+
let span = match &ty1.kind {
109+
ty::Param(p) => {
110+
tcx.def_span(generics.type_param(p, tcx).def_id)
111+
}
112+
_ => def_span,
113+
};
114+
Some((ty::Predicate::TypeOutlives(ty::Binder::bind(
105115
ty::OutlivesPredicate(ty1, region2)
106-
)))
116+
)), span))
107117
}
108118
GenericArgKind::Lifetime(region1) => {
109-
Some(ty::Predicate::RegionOutlives(
119+
// FIXME(eddyb) compute `Span`s in `implicit_infer`.
120+
let span = match region1 {
121+
ty::RegionKind::ReEarlyBound(p) => {
122+
tcx.def_span(generics.region_param(p, tcx).def_id)
123+
}
124+
_ => def_span,
125+
};
126+
Some((ty::Predicate::RegionOutlives(
110127
ty::Binder::bind(ty::OutlivesPredicate(region1, region2))
111-
))
128+
), span))
112129
}
113130
GenericArgKind::Const(_) => {
114131
// Generic consts don't impose any constraints.
115132
None
116133
}
117134
},
118135
));
119-
(def_id, &*predicates)
136+
(def_id, predicates)
120137
}).collect();
121138

122139
tcx.arena.alloc(ty::CratePredicatesMap {

0 commit comments

Comments
 (0)