Skip to content

Commit ab3081a

Browse files
Add constness field to ty::Predicate::Trait
1 parent 1a3bd57 commit ab3081a

File tree

27 files changed

+104
-55
lines changed

27 files changed

+104
-55
lines changed

src/librustc/traits/auto_trait.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,10 @@ impl AutoTraitFinder<'tcx> {
337337
&Err(SelectionError::Unimplemented) => {
338338
if self.is_param_no_infer(pred.skip_binder().trait_ref.substs) {
339339
already_visited.remove(&pred);
340-
self.add_user_pred(&mut user_computed_preds, ty::Predicate::Trait(pred));
340+
self.add_user_pred(
341+
&mut user_computed_preds,
342+
ty::Predicate::Trait(pred, ast::Constness::NotConst),
343+
);
341344
predicates.push_back(pred);
342345
} else {
343346
debug!(
@@ -405,7 +408,7 @@ impl AutoTraitFinder<'tcx> {
405408
let mut should_add_new = true;
406409
user_computed_preds.retain(|&old_pred| {
407410
match (&new_pred, old_pred) {
408-
(&ty::Predicate::Trait(new_trait), ty::Predicate::Trait(old_trait)) => {
411+
(&ty::Predicate::Trait(new_trait, _), ty::Predicate::Trait(old_trait, _)) => {
409412
if new_trait.def_id() == old_trait.def_id() {
410413
let new_substs = new_trait.skip_binder().trait_ref.substs;
411414
let old_substs = old_trait.skip_binder().trait_ref.substs;
@@ -627,7 +630,7 @@ impl AutoTraitFinder<'tcx> {
627630
// We check this by calling is_of_param on the relevant types
628631
// from the various possible predicates
629632
match &predicate {
630-
&ty::Predicate::Trait(p) => {
633+
&ty::Predicate::Trait(p, _) => {
631634
if self.is_param_no_infer(p.skip_binder().trait_ref.substs)
632635
&& !only_projections
633636
&& is_new_pred

src/librustc/traits/fulfill.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
331331
}
332332

333333
match obligation.predicate {
334-
ty::Predicate::Trait(ref data) => {
334+
ty::Predicate::Trait(ref data, _) => {
335335
let trait_obligation = obligation.with(data.clone());
336336

337337
if data.is_global() {

src/librustc/traits/object_safety.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ fn predicates_reference_self(tcx: TyCtxt<'_>, trait_def_id: DefId, supertraits_o
234234
.map(|(predicate, _)| predicate.subst_supertrait(tcx, &trait_ref))
235235
.any(|predicate| {
236236
match predicate {
237-
ty::Predicate::Trait(ref data) => {
237+
ty::Predicate::Trait(ref data, _) => {
238238
// In the case of a trait predicate, we can skip the "self" type.
239239
data.skip_binder().input_types().skip(1).any(has_self_ty)
240240
}
@@ -285,7 +285,7 @@ fn generics_require_sized_self(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
285285
let predicates = tcx.predicates_of(def_id);
286286
let predicates = predicates.instantiate_identity(tcx).predicates;
287287
elaborate_predicates(tcx, predicates).any(|predicate| match predicate {
288-
ty::Predicate::Trait(ref trait_pred) => {
288+
ty::Predicate::Trait(ref trait_pred, _) => {
289289
trait_pred.def_id() == sized_def_id && trait_pred.skip_binder().self_ty().is_param(0)
290290
}
291291
ty::Predicate::Projection(..)

src/librustc/traits/query/type_op/prove_predicate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl<'tcx> super::QueryTypeOp<'tcx> for ProvePredicate<'tcx> {
2424
// `&T`, accounts for about 60% percentage of the predicates
2525
// we have to prove. No need to canonicalize and all that for
2626
// such cases.
27-
if let Predicate::Trait(trait_ref) = key.value.predicate {
27+
if let Predicate::Trait(trait_ref, _) = key.value.predicate {
2828
if let Some(sized_def_id) = tcx.lang_items().sized_trait() {
2929
if trait_ref.def_id() == sized_def_id {
3030
if trait_ref.skip_binder().self_ty().is_trivially_sized(tcx) {

src/librustc/traits/select.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use std::cmp;
5151
use std::fmt::{self, Display};
5252
use std::iter;
5353
use std::rc::Rc;
54-
use syntax::attr;
54+
use syntax::{ast, attr};
5555

5656
pub struct SelectionContext<'cx, 'tcx> {
5757
infcx: &'cx InferCtxt<'cx, 'tcx>,
@@ -718,7 +718,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
718718
}
719719

720720
match obligation.predicate {
721-
ty::Predicate::Trait(ref t) => {
721+
ty::Predicate::Trait(ref t, _) => {
722722
debug_assert!(!t.has_escaping_bound_vars());
723723
let obligation = obligation.with(t.clone());
724724
self.evaluate_trait_predicate_recursively(previous_stack, obligation)
@@ -945,7 +945,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
945945
// trait refs. This is important because it's only a cycle
946946
// if the regions match exactly.
947947
let cycle = stack.iter().skip(1).take_while(|s| s.depth >= cycle_depth);
948-
let cycle = cycle.map(|stack| ty::Predicate::Trait(stack.obligation.predicate));
948+
let cycle = cycle.map(|stack| {
949+
ty::Predicate::Trait(stack.obligation.predicate, ast::Constness::NotConst)
950+
});
949951
if self.coinductive_match(cycle) {
950952
debug!("evaluate_stack({:?}) --> recursive, coinductive", stack.fresh_trait_ref);
951953
Some(EvaluatedToOk)
@@ -1060,7 +1062,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
10601062

10611063
fn coinductive_predicate(&self, predicate: ty::Predicate<'tcx>) -> bool {
10621064
let result = match predicate {
1063-
ty::Predicate::Trait(ref data) => self.tcx().trait_is_auto(data.def_id()),
1065+
ty::Predicate::Trait(ref data, _) => self.tcx().trait_is_auto(data.def_id()),
10641066
_ => false,
10651067
};
10661068
debug!("coinductive_predicate({:?}) = {:?}", predicate, result);

src/librustc/traits/util.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use super::{Normalized, Obligation, ObligationCause, PredicateObligation, Select
1313

1414
fn anonymize_predicate<'tcx>(tcx: TyCtxt<'tcx>, pred: &ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
1515
match *pred {
16-
ty::Predicate::Trait(ref data) => {
17-
ty::Predicate::Trait(tcx.anonymize_late_bound_regions(data))
16+
ty::Predicate::Trait(ref data, constness) => {
17+
ty::Predicate::Trait(tcx.anonymize_late_bound_regions(data), constness)
1818
}
1919

2020
ty::Predicate::RegionOutlives(ref data) => {
@@ -127,7 +127,7 @@ impl Elaborator<'tcx> {
127127
fn elaborate(&mut self, predicate: &ty::Predicate<'tcx>) {
128128
let tcx = self.visited.tcx;
129129
match *predicate {
130-
ty::Predicate::Trait(ref data) => {
130+
ty::Predicate::Trait(ref data, _) => {
131131
// Get predicates declared on the trait.
132132
let predicates = tcx.super_predicates_of(data.def_id());
133133

@@ -471,7 +471,7 @@ impl<'tcx, I: Iterator<Item = ty::Predicate<'tcx>>> Iterator for FilterToTraits<
471471

472472
fn next(&mut self) -> Option<ty::PolyTraitRef<'tcx>> {
473473
while let Some(pred) = self.base_iterator.next() {
474-
if let ty::Predicate::Trait(data) = pred {
474+
if let ty::Predicate::Trait(data, _) = pred {
475475
return Some(data.to_poly_trait_ref());
476476
}
477477
}

src/librustc/traits/wf.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub fn predicate_obligations<'a, 'tcx>(
6262

6363
// (*) ok to skip binders, because wf code is prepared for it
6464
match *predicate {
65-
ty::Predicate::Trait(ref t) => {
65+
ty::Predicate::Trait(ref t, _) => {
6666
wf.compute_trait_ref(&t.skip_binder().trait_ref, Elaborate::None); // (*)
6767
}
6868
ty::Predicate::RegionOutlives(..) => {}
@@ -245,7 +245,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
245245
}
246246
}
247247
}
248-
ty::Predicate::Trait(proj) => {
248+
ty::Predicate::Trait(proj, _) => {
249249
// An associated item obligation born out of the `trait` failed to be met.
250250
// Point at the `impl` that failed the obligation, the associated item that
251251
// needed to meet the obligation, and the definition of that associated item,

src/librustc/ty/fold.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
150150
}
151151
}
152152

153+
impl TypeFoldable<'tcx> for syntax::ast::Constness {
154+
fn super_fold_with<F: TypeFolder<'tcx>>(&self, _: &mut F) -> Self {
155+
*self
156+
}
157+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, _: &mut V) -> bool {
158+
false
159+
}
160+
}
161+
153162
/// The `TypeFolder` trait defines the actual *folding*. There is a
154163
/// method defined for every foldable type. Each of these has a
155164
/// default implementation that does an "identity" fold. Within each

src/librustc/ty/mod.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,11 @@ pub enum Predicate<'tcx> {
10681068
/// Corresponds to `where Foo: Bar<A, B, C>`. `Foo` here would be
10691069
/// the `Self` type of the trait reference and `A`, `B`, and `C`
10701070
/// would be the type parameters.
1071-
Trait(PolyTraitPredicate<'tcx>),
1071+
///
1072+
/// A trait predicate will have `Constness::Const` if it originates
1073+
/// from a bound on a `const fn` without the `?const` opt-out (e.g.,
1074+
/// `const fn foobar<Foo: Bar>() {}`).
1075+
Trait(PolyTraitPredicate<'tcx>, ast::Constness),
10721076

10731077
/// `where 'a: 'b`
10741078
RegionOutlives(PolyRegionOutlivesPredicate<'tcx>),
@@ -1191,8 +1195,8 @@ impl<'tcx> Predicate<'tcx> {
11911195

11921196
let substs = &trait_ref.skip_binder().substs;
11931197
match *self {
1194-
Predicate::Trait(ref binder) => {
1195-
Predicate::Trait(binder.map_bound(|data| data.subst(tcx, substs)))
1198+
Predicate::Trait(ref binder, constness) => {
1199+
Predicate::Trait(binder.map_bound(|data| data.subst(tcx, substs)), constness)
11961200
}
11971201
Predicate::Subtype(ref binder) => {
11981202
Predicate::Subtype(binder.map_bound(|data| data.subst(tcx, substs)))
@@ -1338,13 +1342,16 @@ pub trait ToPredicate<'tcx> {
13381342

13391343
impl<'tcx> ToPredicate<'tcx> for TraitRef<'tcx> {
13401344
fn to_predicate(&self) -> Predicate<'tcx> {
1341-
ty::Predicate::Trait(ty::Binder::dummy(ty::TraitPredicate { trait_ref: self.clone() }))
1345+
ty::Predicate::Trait(
1346+
ty::Binder::dummy(ty::TraitPredicate { trait_ref: self.clone() }),
1347+
ast::Constness::NotConst,
1348+
)
13421349
}
13431350
}
13441351

13451352
impl<'tcx> ToPredicate<'tcx> for PolyTraitRef<'tcx> {
13461353
fn to_predicate(&self) -> Predicate<'tcx> {
1347-
ty::Predicate::Trait(self.to_poly_trait_predicate())
1354+
ty::Predicate::Trait(self.to_poly_trait_predicate(), ast::Constness::NotConst)
13481355
}
13491356
}
13501357

@@ -1413,7 +1420,7 @@ impl<'tcx> Predicate<'tcx> {
14131420
/// with depth 0 are bound by the predicate.
14141421
pub fn walk_tys(&'a self) -> impl Iterator<Item = Ty<'tcx>> + 'a {
14151422
match *self {
1416-
ty::Predicate::Trait(ref data) => {
1423+
ty::Predicate::Trait(ref data, _) => {
14171424
WalkTysIter::InputTypes(data.skip_binder().input_types())
14181425
}
14191426
ty::Predicate::Subtype(binder) => {
@@ -1439,7 +1446,7 @@ impl<'tcx> Predicate<'tcx> {
14391446

14401447
pub fn to_opt_poly_trait_ref(&self) -> Option<PolyTraitRef<'tcx>> {
14411448
match *self {
1442-
Predicate::Trait(ref t) => Some(t.to_poly_trait_ref()),
1449+
Predicate::Trait(ref t, _) => Some(t.to_poly_trait_ref()),
14431450
Predicate::Projection(..)
14441451
| Predicate::Subtype(..)
14451452
| Predicate::RegionOutlives(..)

src/librustc/ty/print/pretty.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1791,7 +1791,12 @@ define_print_and_forward_display! {
17911791

17921792
ty::Predicate<'tcx> {
17931793
match *self {
1794-
ty::Predicate::Trait(ref data) => p!(print(data)),
1794+
ty::Predicate::Trait(ref data, constness) => {
1795+
if let ast::Constness::Const = constness {
1796+
p!(write("const "));
1797+
}
1798+
p!(print(data))
1799+
}
17951800
ty::Predicate::Subtype(ref predicate) => p!(print(predicate)),
17961801
ty::Predicate::RegionOutlives(ref predicate) => p!(print(predicate)),
17971802
ty::Predicate::TypeOutlives(ref predicate) => p!(print(predicate)),

0 commit comments

Comments
 (0)