Skip to content

Commit 6622172

Browse files
committed
Auto merge of #54278 - eddyb:spanned-generic-predicates, r=nikomatsakis
rustc: keep a Span for each predicate in ty::GenericPredicates. This should allow finer-grained diagnostics, including migration suggestions for #54090. (Note that I haven't changed most of the users of `predicates_of` to use the new spans) r? @nikomatsakis
2 parents a0a6e43 + 7020326 commit 6622172

33 files changed

+272
-297
lines changed

src/librustc/infer/outlives/verify.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,15 @@ impl<'cx, 'gcx, 'tcx> VerifyBoundCx<'cx, 'gcx, 'tcx> {
297297
let tcx = self.tcx;
298298
let assoc_item = tcx.associated_item(assoc_item_def_id);
299299
let trait_def_id = assoc_item.container.assert_trait();
300-
let trait_predicates = tcx.predicates_of(trait_def_id);
300+
let trait_predicates = tcx.predicates_of(trait_def_id).predicates
301+
.into_iter()
302+
.map(|(p, _)| p)
303+
.collect();
301304
let identity_substs = Substs::identity_for_item(tcx, assoc_item_def_id);
302305
let identity_proj = tcx.mk_projection(assoc_item_def_id, identity_substs);
303306
self.collect_outlives_from_predicate_list(
304307
move |ty| ty == identity_proj,
305-
traits::elaborate_predicates(tcx, trait_predicates.predicates),
308+
traits::elaborate_predicates(tcx, trait_predicates),
306309
).map(|b| b.1)
307310
}
308311

src/librustc/traits/coherence.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ fn with_fresh_ty_vars<'cx, 'gcx, 'tcx>(selcx: &mut SelectionContext<'cx, 'gcx, '
9696

9797
let header = ty::ImplHeader {
9898
impl_def_id,
99-
self_ty: tcx.type_of(impl_def_id),
100-
trait_ref: tcx.impl_trait_ref(impl_def_id),
101-
predicates: tcx.predicates_of(impl_def_id).predicates
102-
}.subst(tcx, impl_substs);
99+
self_ty: tcx.type_of(impl_def_id).subst(tcx, impl_substs),
100+
trait_ref: tcx.impl_trait_ref(impl_def_id).subst(tcx, impl_substs),
101+
predicates: tcx.predicates_of(impl_def_id).instantiate(tcx, impl_substs).predicates,
102+
};
103103

104104
let Normalized { value: mut header, obligations } =
105105
traits::normalize(selcx, param_env, ObligationCause::dummy(), &header);

src/librustc/traits/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -816,11 +816,10 @@ fn substitute_normalize_and_test_predicates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx
816816
key: (DefId, &'tcx Substs<'tcx>))
817817
-> bool
818818
{
819-
use ty::subst::Subst;
820819
debug!("substitute_normalize_and_test_predicates(key={:?})",
821820
key);
822821

823-
let predicates = tcx.predicates_of(key.0).predicates.subst(tcx, key.1);
822+
let predicates = tcx.predicates_of(key.0).instantiate(tcx, key.1).predicates;
824823
let result = normalize_and_test_predicates(tcx, predicates);
825824

826825
debug!("substitute_normalize_and_test_predicates(key={:?}) = {:?}",

src/librustc/traits/object_safety.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
179179
predicates
180180
.predicates
181181
.into_iter()
182-
.map(|predicate| predicate.subst_supertrait(self, &trait_ref))
182+
.map(|(predicate, _)| predicate.subst_supertrait(self, &trait_ref))
183183
.any(|predicate| {
184184
match predicate {
185185
ty::Predicate::Trait(ref data) => {
@@ -311,7 +311,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
311311
if self.predicates_of(method.def_id).predicates.into_iter()
312312
// A trait object can't claim to live more than the concrete type,
313313
// so outlives predicates will always hold.
314-
.filter(|p| p.to_opt_type_outlives().is_none())
314+
.filter(|(p, _)| p.to_opt_type_outlives().is_none())
315315
.collect::<Vec<_>>()
316316
// Do a shallow visit so that `contains_illegal_self_type_reference`
317317
// may apply it's custom visiting.

src/librustc/traits/select.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3401,7 +3401,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
34013401
// that order.
34023402
let predicates = tcx.predicates_of(def_id);
34033403
assert_eq!(predicates.parent, None);
3404-
let mut predicates: Vec<_> = predicates.predicates.iter().flat_map(|predicate| {
3404+
let mut predicates: Vec<_> = predicates.predicates.iter().flat_map(|(predicate, _)| {
34053405
let predicate = normalize_with_depth(self, param_env, cause.clone(), recursion_depth,
34063406
&predicate.subst(tcx, substs));
34073407
predicate.obligations.into_iter().chain(

src/librustc/traits/specialize/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ fn to_pretty_impl_header(tcx: TyCtxt, impl_def_id: DefId) -> Option<String> {
428428
let mut pretty_predicates = Vec::with_capacity(
429429
predicates.len() + types_without_default_bounds.len());
430430

431-
for p in predicates {
431+
for (p, _) in predicates {
432432
if let Some(poly_trait_ref) = p.to_opt_poly_trait_ref() {
433433
if Some(poly_trait_ref.def_id()) == sized_trait {
434434
types_without_default_bounds.remove(poly_trait_ref.self_ty());

src/librustc/traits/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl<'cx, 'gcx, 'tcx> Elaborator<'cx, 'gcx, 'tcx> {
137137
let mut predicates: Vec<_> =
138138
predicates.predicates
139139
.iter()
140-
.map(|p| p.subst_supertrait(tcx, &data.to_poly_trait_ref()))
140+
.map(|(p, _)| p.subst_supertrait(tcx, &data.to_poly_trait_ref()))
141141
.collect();
142142

143143
debug!("super_predicates: data={:?} predicates={:?}",
@@ -311,7 +311,7 @@ impl<'cx, 'gcx, 'tcx> Iterator for SupertraitDefIds<'cx, 'gcx, 'tcx> {
311311
self.stack.extend(
312312
predicates.predicates
313313
.iter()
314-
.filter_map(|p| p.to_opt_poly_trait_ref())
314+
.filter_map(|(p, _)| p.to_opt_poly_trait_ref())
315315
.map(|t| t.def_id())
316316
.filter(|&super_def_id| visited.insert(super_def_id)));
317317
Some(def_id)

src/librustc/ty/codec.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ pub fn encode_predicates<'tcx, E, C>(encoder: &mut E,
109109
{
110110
predicates.parent.encode(encoder)?;
111111
predicates.predicates.len().encode(encoder)?;
112-
for predicate in &predicates.predicates {
113-
encode_with_shorthand(encoder, predicate, &cache)?
112+
for (predicate, span) in &predicates.predicates {
113+
encode_with_shorthand(encoder, predicate, &cache)?;
114+
span.encode(encoder)?;
114115
}
115116
Ok(())
116117
}
@@ -178,15 +179,16 @@ pub fn decode_predicates<'a, 'tcx, D>(decoder: &mut D)
178179
parent: Decodable::decode(decoder)?,
179180
predicates: (0..decoder.read_usize()?).map(|_| {
180181
// Handle shorthands first, if we have an usize > 0x80.
181-
if decoder.positioned_at_shorthand() {
182+
let predicate = if decoder.positioned_at_shorthand() {
182183
let pos = decoder.read_usize()?;
183184
assert!(pos >= SHORTHAND_OFFSET);
184185
let shorthand = pos - SHORTHAND_OFFSET;
185186

186187
decoder.with_position(shorthand, ty::Predicate::decode)
187188
} else {
188189
ty::Predicate::decode(decoder)
189-
}
190+
}?;
191+
Ok((predicate, Decodable::decode(decoder)?))
190192
})
191193
.collect::<Result<Vec<_>, _>>()?,
192194
})

src/librustc/ty/mod.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ impl<'a, 'gcx, 'tcx> Generics {
982982
#[derive(Clone, Default)]
983983
pub struct GenericPredicates<'tcx> {
984984
pub parent: Option<DefId>,
985-
pub predicates: Vec<Predicate<'tcx>>,
985+
pub predicates: Vec<(Predicate<'tcx>, Span)>,
986986
}
987987

988988
impl<'tcx> serialize::UseSpecializedEncodable for GenericPredicates<'tcx> {}
@@ -998,7 +998,7 @@ impl<'a, 'gcx, 'tcx> GenericPredicates<'tcx> {
998998
pub fn instantiate_own(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>, substs: &Substs<'tcx>)
999999
-> InstantiatedPredicates<'tcx> {
10001000
InstantiatedPredicates {
1001-
predicates: self.predicates.subst(tcx, substs)
1001+
predicates: self.predicates.iter().map(|(p, _)| p.subst(tcx, substs)).collect(),
10021002
}
10031003
}
10041004

@@ -1008,7 +1008,9 @@ impl<'a, 'gcx, 'tcx> GenericPredicates<'tcx> {
10081008
if let Some(def_id) = self.parent {
10091009
tcx.predicates_of(def_id).instantiate_into(tcx, instantiated, substs);
10101010
}
1011-
instantiated.predicates.extend(self.predicates.iter().map(|p| p.subst(tcx, substs)))
1011+
instantiated.predicates.extend(
1012+
self.predicates.iter().map(|(p, _)| p.subst(tcx, substs)),
1013+
);
10121014
}
10131015

10141016
pub fn instantiate_identity(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>)
@@ -1023,7 +1025,7 @@ impl<'a, 'gcx, 'tcx> GenericPredicates<'tcx> {
10231025
if let Some(def_id) = self.parent {
10241026
tcx.predicates_of(def_id).instantiate_identity_into(tcx, instantiated);
10251027
}
1026-
instantiated.predicates.extend(&self.predicates)
1028+
instantiated.predicates.extend(self.predicates.iter().map(|&(p, _)| p))
10271029
}
10281030

10291031
pub fn instantiate_supertrait(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>,
@@ -1032,7 +1034,7 @@ impl<'a, 'gcx, 'tcx> GenericPredicates<'tcx> {
10321034
{
10331035
assert_eq!(self.parent, None);
10341036
InstantiatedPredicates {
1035-
predicates: self.predicates.iter().map(|pred| {
1037+
predicates: self.predicates.iter().map(|(pred, _)| {
10361038
pred.subst_supertrait(tcx, poly_trait_ref)
10371039
}).collect()
10381040
}
@@ -2351,7 +2353,7 @@ impl<'a, 'gcx, 'tcx> AdtDef {
23512353
substs: tcx.mk_substs_trait(ty, &[])
23522354
}).to_predicate();
23532355
let predicates = tcx.predicates_of(self.did).predicates;
2354-
if predicates.into_iter().any(|p| p == sized_predicate) {
2356+
if predicates.into_iter().any(|(p, _)| p == sized_predicate) {
23552357
vec![]
23562358
} else {
23572359
vec![ty]

src/librustc_lint/builtin.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1736,8 +1736,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TrivialConstraints {
17361736
if cx.tcx.features().trivial_bounds {
17371737
let def_id = cx.tcx.hir.local_def_id(item.id);
17381738
let predicates = cx.tcx.predicates_of(def_id);
1739-
for predicate in &predicates.predicates {
1740-
let predicate_kind_name = match *predicate {
1739+
for &(predicate, span) in &predicates.predicates {
1740+
let predicate_kind_name = match predicate {
17411741
Trait(..) => "Trait",
17421742
TypeOutlives(..) |
17431743
RegionOutlives(..) => "Lifetime",
@@ -1755,7 +1755,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TrivialConstraints {
17551755
if predicate.is_global() {
17561756
cx.span_lint(
17571757
TRIVIAL_BOUNDS,
1758-
item.span,
1758+
span,
17591759
&format!("{} bound {} does not depend on any type \
17601760
or lifetime parameters", predicate_kind_name, predicate),
17611761
);

src/librustc_mir/transform/qualify_min_const_fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn is_min_const_fn(
1515
let mut current = def_id;
1616
loop {
1717
let predicates = tcx.predicates_of(current);
18-
for predicate in &predicates.predicates {
18+
for (predicate, _) in &predicates.predicates {
1919
match predicate {
2020
| Predicate::RegionOutlives(_)
2121
| Predicate::TypeOutlives(_)

src/librustc_privacy/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ impl<'b, 'a, 'tcx> ReachEverythingInTheInterfaceVisitor<'b, 'a, 'tcx> {
434434

435435
fn predicates(&mut self) -> &mut Self {
436436
let predicates = self.ev.tcx.predicates_of(self.item_def_id);
437-
for predicate in &predicates.predicates {
437+
for (predicate, _) in &predicates.predicates {
438438
predicate.visit_with(self);
439439
match predicate {
440440
&ty::Predicate::Trait(poly_predicate) => {
@@ -781,7 +781,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
781781
if self.check_trait_ref(*principal.skip_binder()) {
782782
return;
783783
}
784-
for poly_predicate in projections {
784+
for (poly_predicate, _) in projections {
785785
let tcx = self.tcx;
786786
if self.check_trait_ref(poly_predicate.skip_binder().projection_ty.trait_ref(tcx)) {
787787
return;
@@ -956,7 +956,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for TypePrivacyVisitor<'a, 'tcx> {
956956
}
957957
}
958958
ty::Opaque(def_id, ..) => {
959-
for predicate in &self.tcx.predicates_of(def_id).predicates {
959+
for (predicate, _) in &self.tcx.predicates_of(def_id).predicates {
960960
let trait_ref = match *predicate {
961961
ty::Predicate::Trait(ref poly_trait_predicate) => {
962962
Some(poly_trait_predicate.skip_binder().trait_ref)
@@ -1387,7 +1387,7 @@ impl<'a, 'tcx: 'a> SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
13871387
// for the inferred outlives rules; see
13881388
// `src/test/ui/rfc-2093-infer-outlives/privacy.rs`.
13891389
let predicates = self.tcx.explicit_predicates_of(self.item_def_id);
1390-
for predicate in &predicates.predicates {
1390+
for (predicate, _) in &predicates.predicates {
13911391
predicate.visit_with(self);
13921392
match predicate {
13931393
&ty::Predicate::Trait(poly_predicate) => {

src/librustc_traits/lowering.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,10 @@ fn program_clauses_for_trait<'a, 'tcx>(
260260

261261
let clauses = iter::once(Clause::ForAll(ty::Binder::dummy(implemented_from_env)));
262262

263-
let where_clauses = &tcx.predicates_defined_on(def_id).predicates;
263+
let where_clauses = &tcx.predicates_defined_on(def_id).predicates
264+
.into_iter()
265+
.map(|(wc, _)| wc.lower())
266+
.collect::<Vec<_>>();
264267

265268
// Rule Implied-Bound-From-Trait
266269
//
@@ -273,8 +276,8 @@ fn program_clauses_for_trait<'a, 'tcx>(
273276

274277
// `FromEnv(WC) :- FromEnv(Self: Trait<P1..Pn>)`, for each where clause WC
275278
let implied_bound_clauses = where_clauses
276-
.into_iter()
277-
.map(|wc| wc.lower())
279+
.iter()
280+
.cloned()
278281

279282
// `FromEnv(WC) :- FromEnv(Self: Trait<P1..Pn>)`
280283
.map(|wc| wc.map_bound(|goal| ProgramClause {
@@ -296,8 +299,8 @@ fn program_clauses_for_trait<'a, 'tcx>(
296299
let wf_conditions = iter::once(ty::Binder::dummy(trait_pred.lower()))
297300
.chain(
298301
where_clauses
299-
.into_iter()
300-
.map(|wc| wc.lower())
302+
.iter()
303+
.cloned()
301304
.map(|wc| wc.map_bound(|goal| goal.into_well_formed_goal()))
302305
);
303306

@@ -338,7 +341,10 @@ fn program_clauses_for_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId
338341
let trait_pred = ty::TraitPredicate { trait_ref }.lower();
339342

340343
// `WC`
341-
let where_clauses = tcx.predicates_of(def_id).predicates.lower();
344+
let where_clauses = tcx.predicates_of(def_id).predicates
345+
.into_iter()
346+
.map(|(wc, _)| wc.lower())
347+
.collect::<Vec<_>>();
342348

343349
// `Implemented(A0: Trait<A1..An>) :- WC`
344350
let clause = ProgramClause {
@@ -370,7 +376,10 @@ pub fn program_clauses_for_type_def<'a, 'tcx>(
370376
let ty = tcx.type_of(def_id);
371377

372378
// `WC`
373-
let where_clauses = tcx.predicates_of(def_id).predicates.lower();
379+
let where_clauses = tcx.predicates_of(def_id).predicates
380+
.into_iter()
381+
.map(|(wc, _)| wc.lower())
382+
.collect::<Vec<_>>();
374383

375384
// `WellFormed(Ty<...>) :- WC1, ..., WCm`
376385
let well_formed = ProgramClause {

0 commit comments

Comments
 (0)