Skip to content

Commit 1ca8da4

Browse files
committed
rustc_typeck: compute better spans for inferred_outlives.
1 parent 93cac9c commit 1ca8da4

File tree

5 files changed

+39
-29
lines changed

5 files changed

+39
-29
lines changed

src/librustc_metadata/encoder.rs

+8
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ impl<'tcx> SpecializedEncoder<Span> for EncodeContext<'tcx> {
197197
return TAG_INVALID_SPAN.encode(self)
198198
}
199199

200+
// HACK(eddyb) there's no way to indicate which crate a Span is coming
201+
// from right now, so decoding would fail to find the SourceFile if
202+
// it's not local to the crate the Span is found in.
203+
if self.source_file_cache.is_imported() {
204+
return TAG_INVALID_SPAN.encode(self)
205+
}
206+
200207
TAG_VALID_SPAN.encode(self)?;
201208
span.lo.encode(self)?;
202209

@@ -379,6 +386,7 @@ impl<'tcx> EncodeContext<'tcx> {
379386
.filter(|source_file| {
380387
// No need to re-export imported source_files, as any downstream
381388
// crate will import them from their original source.
389+
// FIXME(eddyb) the `Span` encoding should take that into account.
382390
!source_file.is_imported()
383391
})
384392
.map(|source_file| {

src/librustc_typeck/outlives/explicit.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ impl<'tcx> ExplicitPredicatesMap<'tcx> {
3030
let mut required_predicates = RequiredPredicates::default();
3131

3232
// process predicates and convert to `RequiredPredicates` entry, see below
33-
for (pred, _) in predicates.predicates {
34-
match pred {
33+
for &(predicate, span) in predicates.predicates {
34+
match predicate {
3535
ty::Predicate::TypeOutlives(predicate) => {
3636
let OutlivesPredicate(ref ty, ref reg) = predicate.skip_binder();
37-
insert_outlives_predicate(tcx, (*ty).into(), reg, &mut required_predicates)
37+
insert_outlives_predicate(
38+
tcx,
39+
(*ty).into(),
40+
reg,
41+
span,
42+
&mut required_predicates,
43+
)
3844
}
3945

4046
ty::Predicate::RegionOutlives(predicate) => {
@@ -43,6 +49,7 @@ impl<'tcx> ExplicitPredicatesMap<'tcx> {
4349
tcx,
4450
(*reg1).into(),
4551
reg2,
52+
span,
4653
&mut required_predicates,
4754
)
4855
}

src/librustc_typeck/outlives/implicit_infer.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc::hir::itemlikevisit::ItemLikeVisitor;
44
use rustc::ty::subst::{GenericArg, Subst, GenericArgKind};
55
use rustc::ty::{self, Ty, TyCtxt};
66
use rustc::util::nodemap::FxHashMap;
7+
use syntax_pos::Span;
78

89
use super::explicit::ExplicitPredicatesMap;
910
use super::utils::*;
@@ -79,9 +80,11 @@ impl<'cx, 'tcx> ItemLikeVisitor<'tcx> for InferVisitor<'cx, 'tcx> {
7980
// (struct/enum/union) there will be outlive
8081
// requirements for adt_def.
8182
let field_ty = self.tcx.type_of(field_def.did);
83+
let field_span = self.tcx.def_span(field_def.did);
8284
insert_required_predicates_to_be_wf(
8385
self.tcx,
8486
field_ty,
87+
field_span,
8588
self.global_inferred_outlives,
8689
&mut item_required_predicates,
8790
&mut self.explicit_map,
@@ -118,6 +121,7 @@ impl<'cx, 'tcx> ItemLikeVisitor<'tcx> for InferVisitor<'cx, 'tcx> {
118121
fn insert_required_predicates_to_be_wf<'tcx>(
119122
tcx: TyCtxt<'tcx>,
120123
field_ty: Ty<'tcx>,
124+
field_span: Span,
121125
global_inferred_outlives: &FxHashMap<DefId, RequiredPredicates<'tcx>>,
122126
required_predicates: &mut RequiredPredicates<'tcx>,
123127
explicit_map: &mut ExplicitPredicatesMap<'tcx>,
@@ -130,7 +134,7 @@ fn insert_required_predicates_to_be_wf<'tcx>(
130134
// We also want to calculate potential predicates for the T
131135
ty::Ref(region, rty, _) => {
132136
debug!("Ref");
133-
insert_outlives_predicate(tcx, rty.into(), region, required_predicates);
137+
insert_outlives_predicate(tcx, rty.into(), region, field_span, required_predicates);
134138
}
135139

136140
// For each Adt (struct/enum/union) type `Foo<'a, T>`, we
@@ -158,7 +162,7 @@ fn insert_required_predicates_to_be_wf<'tcx>(
158162
// 'a` holds for `Foo`.
159163
debug!("Adt");
160164
if let Some(unsubstituted_predicates) = global_inferred_outlives.get(&def.did) {
161-
for unsubstituted_predicate in unsubstituted_predicates {
165+
for (unsubstituted_predicate, &span) in unsubstituted_predicates {
162166
// `unsubstituted_predicate` is `U: 'b` in the
163167
// example above. So apply the substitution to
164168
// get `T: 'a` (or `predicate`):
@@ -167,6 +171,7 @@ fn insert_required_predicates_to_be_wf<'tcx>(
167171
tcx,
168172
predicate.0,
169173
predicate.1,
174+
span,
170175
required_predicates,
171176
);
172177
}
@@ -272,7 +277,7 @@ pub fn check_explicit_predicates<'tcx>(
272277
);
273278
let explicit_predicates = explicit_map.explicit_predicates_of(tcx, def_id);
274279

275-
for outlives_predicate in explicit_predicates.iter() {
280+
for (outlives_predicate, &span) in explicit_predicates {
276281
debug!("outlives_predicate = {:?}", &outlives_predicate);
277282

278283
// Careful: If we are inferring the effects of a `dyn Trait<..>`
@@ -320,6 +325,6 @@ pub fn check_explicit_predicates<'tcx>(
320325

321326
let predicate = outlives_predicate.subst(tcx, substs);
322327
debug!("predicate = {:?}", &predicate);
323-
insert_outlives_predicate(tcx, predicate.0.into(), predicate.1, required_predicates);
328+
insert_outlives_predicate(tcx, predicate.0.into(), predicate.1, span, required_predicates);
324329
}
325330
}

src/librustc_typeck/outlives/mod.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -97,32 +97,16 @@ fn inferred_outlives_crate(
9797
let predicates = global_inferred_outlives
9898
.iter()
9999
.map(|(&def_id, set)| {
100-
let def_span = tcx.def_span(def_id);
101-
let generics = tcx.generics_of(def_id);
102100
let predicates = &*tcx.arena.alloc_from_iter(set
103101
.iter()
104102
.filter_map(
105-
|ty::OutlivesPredicate(kind1, region2)| match kind1.unpack() {
103+
|(ty::OutlivesPredicate(kind1, region2), &span)| match kind1.unpack() {
106104
GenericArgKind::Type(ty1) => {
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-
};
114105
Some((ty::Predicate::TypeOutlives(ty::Binder::bind(
115106
ty::OutlivesPredicate(ty1, region2)
116107
)), span))
117108
}
118109
GenericArgKind::Lifetime(region1) => {
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-
};
126110
Some((ty::Predicate::RegionOutlives(
127111
ty::Binder::bind(ty::OutlivesPredicate(region1, region2))
128112
), span))

src/librustc_typeck/outlives/utils.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ use rustc::ty::outlives::Component;
22
use rustc::ty::subst::{GenericArg, GenericArgKind};
33
use rustc::ty::{self, Region, RegionKind, Ty, TyCtxt};
44
use smallvec::smallvec;
5-
use std::collections::BTreeSet;
5+
use std::collections::BTreeMap;
6+
use syntax_pos::Span;
67

78
/// Tracks the `T: 'a` or `'a: 'a` predicates that we have inferred
89
/// must be added to the struct header.
910
pub type RequiredPredicates<'tcx> =
10-
BTreeSet<ty::OutlivesPredicate<GenericArg<'tcx>, ty::Region<'tcx>>>;
11+
BTreeMap<ty::OutlivesPredicate<GenericArg<'tcx>, ty::Region<'tcx>>, Span>;
1112

1213
/// Given a requirement `T: 'a` or `'b: 'a`, deduce the
1314
/// outlives_component and add it to `required_predicates`
1415
pub fn insert_outlives_predicate<'tcx>(
1516
tcx: TyCtxt<'tcx>,
1617
kind: GenericArg<'tcx>,
1718
outlived_region: Region<'tcx>,
19+
span: Span,
1820
required_predicates: &mut RequiredPredicates<'tcx>,
1921
) {
2022
// If the `'a` region is bound within the field type itself, we
@@ -53,6 +55,7 @@ pub fn insert_outlives_predicate<'tcx>(
5355
tcx,
5456
r.into(),
5557
outlived_region,
58+
span,
5659
required_predicates,
5760
);
5861
}
@@ -73,7 +76,8 @@ pub fn insert_outlives_predicate<'tcx>(
7376
// where clause that `U: 'a`.
7477
let ty: Ty<'tcx> = param_ty.to_ty(tcx);
7578
required_predicates
76-
.insert(ty::OutlivesPredicate(ty.into(), outlived_region));
79+
.entry(ty::OutlivesPredicate(ty.into(), outlived_region))
80+
.or_insert(span);
7781
}
7882

7983
Component::Projection(proj_ty) => {
@@ -88,7 +92,8 @@ pub fn insert_outlives_predicate<'tcx>(
8892
// Here we want to add an explicit `where <T as Iterator>::Item: 'a`.
8993
let ty: Ty<'tcx> = tcx.mk_projection(proj_ty.item_def_id, proj_ty.substs);
9094
required_predicates
91-
.insert(ty::OutlivesPredicate(ty.into(), outlived_region));
95+
.entry(ty::OutlivesPredicate(ty.into(), outlived_region))
96+
.or_insert(span);
9297
}
9398

9499
Component::EscapingProjection(_) => {
@@ -117,7 +122,8 @@ pub fn insert_outlives_predicate<'tcx>(
117122
if !is_free_region(tcx, r) {
118123
return;
119124
}
120-
required_predicates.insert(ty::OutlivesPredicate(kind, outlived_region));
125+
required_predicates.entry(ty::OutlivesPredicate(kind, outlived_region))
126+
.or_insert(span);
121127
}
122128

123129
GenericArgKind::Const(_) => {

0 commit comments

Comments
 (0)