Skip to content

Commit d2aefbb

Browse files
Add ConstnessAnd that implements ToPredicate
1 parent ab3081a commit d2aefbb

File tree

21 files changed

+155
-74
lines changed

21 files changed

+155
-74
lines changed

src/librustc/traits/engine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::infer::InferCtxt;
22
use crate::traits::Obligation;
3-
use crate::ty::{self, ToPredicate, Ty, TyCtxt};
3+
use crate::ty::{self, ToPredicate, Ty, TyCtxt, WithConstness};
44
use rustc_hir::def_id::DefId;
55

66
use super::{ChalkFulfillmentContext, FulfillmentContext, FulfillmentError};
@@ -33,7 +33,7 @@ pub trait TraitEngine<'tcx>: 'tcx {
3333
cause,
3434
recursion_depth: 0,
3535
param_env,
36-
predicate: trait_ref.to_predicate(),
36+
predicate: trait_ref.without_const().to_predicate(),
3737
},
3838
);
3939
}

src/librustc/traits/error_reporting/mod.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ use crate::ty::error::ExpectedFound;
1919
use crate::ty::fast_reject;
2020
use crate::ty::fold::TypeFolder;
2121
use crate::ty::SubtypePredicate;
22-
use crate::ty::{self, AdtKind, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, TypeFoldable};
22+
use crate::ty::{
23+
self, AdtKind, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness,
24+
};
2325

2426
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2527
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
@@ -130,15 +132,15 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
130132
}
131133

132134
let (cond, error) = match (cond, error) {
133-
(&ty::Predicate::Trait(..), &ty::Predicate::Trait(ref error)) => (cond, error),
135+
(&ty::Predicate::Trait(..), &ty::Predicate::Trait(ref error, _)) => (cond, error),
134136
_ => {
135137
// FIXME: make this work in other cases too.
136138
return false;
137139
}
138140
};
139141

140142
for implication in super::elaborate_predicates(self.tcx, vec![cond.clone()]) {
141-
if let ty::Predicate::Trait(implication) = implication {
143+
if let ty::Predicate::Trait(implication, _) = implication {
142144
let error = error.to_poly_trait_ref();
143145
let implication = implication.to_poly_trait_ref();
144146
// FIXME: I'm just not taking associated types at all here.
@@ -530,7 +532,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
530532
return;
531533
}
532534
match obligation.predicate {
533-
ty::Predicate::Trait(ref trait_predicate) => {
535+
ty::Predicate::Trait(ref trait_predicate, _) => {
534536
let trait_predicate = self.resolve_vars_if_possible(trait_predicate);
535537

536538
if self.tcx.sess.has_errors() && trait_predicate.references_error() {
@@ -583,7 +585,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
583585
"{}",
584586
message.unwrap_or_else(|| format!(
585587
"the trait bound `{}` is not satisfied{}",
586-
trait_ref.to_predicate(),
588+
trait_ref.without_const().to_predicate(),
587589
post_message,
588590
))
589591
);
@@ -695,7 +697,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
695697
trait_pred
696698
});
697699
let unit_obligation = Obligation {
698-
predicate: ty::Predicate::Trait(predicate),
700+
predicate: ty::Predicate::Trait(
701+
predicate,
702+
ast::Constness::NotConst,
703+
),
699704
..obligation.clone()
700705
};
701706
if self.predicate_may_hold(&unit_obligation) {
@@ -988,7 +993,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
988993
) -> PredicateObligation<'tcx> {
989994
let new_trait_ref =
990995
ty::TraitRef { def_id, substs: self.tcx.mk_substs_trait(output_ty, &[]) };
991-
Obligation::new(cause, param_env, new_trait_ref.to_predicate())
996+
Obligation::new(cause, param_env, new_trait_ref.without_const().to_predicate())
992997
}
993998
}
994999

@@ -1076,7 +1081,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10761081
}
10771082

10781083
let mut err = match predicate {
1079-
ty::Predicate::Trait(ref data) => {
1084+
ty::Predicate::Trait(ref data, _) => {
10801085
let trait_ref = data.to_poly_trait_ref();
10811086
let self_ty = trait_ref.self_ty();
10821087
debug!("self_ty {:?} {:?} trait_ref {:?}", self_ty, self_ty.kind, trait_ref);
@@ -1269,8 +1274,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
12691274
)
12701275
.value;
12711276

1272-
let obligation =
1273-
Obligation::new(ObligationCause::dummy(), param_env, cleaned_pred.to_predicate());
1277+
let obligation = Obligation::new(
1278+
ObligationCause::dummy(),
1279+
param_env,
1280+
cleaned_pred.without_const().to_predicate(),
1281+
);
12741282

12751283
self.predicate_may_hold(&obligation)
12761284
})

src/librustc/traits/error_reporting/suggestions.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::{
66
use crate::infer::InferCtxt;
77
use crate::traits::object_safety::object_safety_violations;
88
use crate::ty::TypeckTables;
9-
use crate::ty::{self, AdtKind, DefIdTree, ToPredicate, Ty, TyCtxt, TypeFoldable};
9+
use crate::ty::{self, AdtKind, DefIdTree, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness};
1010

1111
use rustc_errors::{
1212
error_code, pluralize, struct_span_err, Applicability, DiagnosticBuilder, Style,
@@ -50,7 +50,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
5050
} else {
5151
" where"
5252
},
53-
trait_ref.to_predicate(),
53+
trait_ref.without_const().to_predicate(),
5454
),
5555
Applicability::MachineApplicable,
5656
);
@@ -340,8 +340,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
340340
let new_self_ty = self.tcx.mk_imm_ref(self.tcx.lifetimes.re_static, self_ty);
341341
let substs = self.tcx.mk_substs_trait(new_self_ty, &[]);
342342
let new_trait_ref = ty::TraitRef::new(obligation.parent_trait_ref.def_id(), substs);
343-
let new_obligation =
344-
Obligation::new(ObligationCause::dummy(), param_env, new_trait_ref.to_predicate());
343+
let new_obligation = Obligation::new(
344+
ObligationCause::dummy(),
345+
param_env,
346+
new_trait_ref.without_const().to_predicate(),
347+
);
345348
if self.predicate_must_hold_modulo_regions(&new_obligation) {
346349
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
347350
// We have a very specific type of error, where just borrowing this argument
@@ -1122,7 +1125,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
11221125
// the type. The last generator has information about where the bound was introduced. At
11231126
// least one generator should be present for this diagnostic to be modified.
11241127
let (mut trait_ref, mut target_ty) = match obligation.predicate {
1125-
ty::Predicate::Trait(p) => {
1128+
ty::Predicate::Trait(p, _) => {
11261129
(Some(p.skip_binder().trait_ref), Some(p.skip_binder().self_ty()))
11271130
}
11281131
_ => (None, None),
@@ -1545,7 +1548,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15451548
err.note(&format!("required because it appears within the type `{}`", ty));
15461549
obligated_types.push(ty);
15471550

1548-
let parent_predicate = parent_trait_ref.to_predicate();
1551+
let parent_predicate = parent_trait_ref.without_const().to_predicate();
15491552
if !self.is_recursive_obligation(obligated_types, &data.parent_code) {
15501553
self.note_obligation_cause_code(
15511554
err,
@@ -1562,7 +1565,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15621565
parent_trait_ref.print_only_trait_path(),
15631566
parent_trait_ref.skip_binder().self_ty()
15641567
));
1565-
let parent_predicate = parent_trait_ref.to_predicate();
1568+
let parent_predicate = parent_trait_ref.without_const().to_predicate();
15661569
self.note_obligation_cause_code(
15671570
err,
15681571
&parent_predicate,

src/librustc/traits/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::mir::interpret::ErrorHandled;
2929
use crate::ty::error::{ExpectedFound, TypeError};
3030
use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
3131
use crate::ty::subst::{InternalSubsts, SubstsRef};
32-
use crate::ty::{self, AdtKind, GenericParamDefKind, List, ToPredicate, Ty, TyCtxt};
32+
use crate::ty::{self, AdtKind, GenericParamDefKind, List, ToPredicate, Ty, TyCtxt, WithConstness};
3333
use crate::util::common::ErrorReported;
3434
use chalk_engine;
3535
use rustc_hir as hir;
@@ -732,7 +732,7 @@ pub fn type_known_to_meet_bound_modulo_regions<'a, 'tcx>(
732732
param_env,
733733
cause: ObligationCause::misc(span, hir::DUMMY_HIR_ID),
734734
recursion_depth: 0,
735-
predicate: trait_ref.to_predicate(),
735+
predicate: trait_ref.without_const().to_predicate(),
736736
};
737737

738738
let result = infcx.predicate_must_hold_modulo_regions(&obligation);

src/librustc/traits/object_safety.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use super::elaborate_predicates;
1212

1313
use crate::traits::{self, Obligation, ObligationCause};
1414
use crate::ty::subst::{InternalSubsts, Subst};
15-
use crate::ty::{self, Predicate, ToPredicate, Ty, TyCtxt, TypeFoldable};
15+
use crate::ty::{self, Predicate, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness};
1616
use rustc_hir as hir;
1717
use rustc_hir::def_id::DefId;
1818
use rustc_session::lint::builtin::WHERE_CLAUSES_OBJECT_SAFETY;
@@ -585,6 +585,7 @@ fn receiver_is_dispatchable<'tcx>(
585585
def_id: unsize_did,
586586
substs: tcx.mk_substs_trait(tcx.types.self_param, &[unsized_self_ty.into()]),
587587
}
588+
.without_const()
588589
.to_predicate();
589590

590591
// U: Trait<Arg1, ..., ArgN>
@@ -598,7 +599,7 @@ fn receiver_is_dispatchable<'tcx>(
598599
}
599600
});
600601

601-
ty::TraitRef { def_id: unsize_did, substs }.to_predicate()
602+
ty::TraitRef { def_id: unsize_did, substs }.without_const().to_predicate()
602603
};
603604

604605
let caller_bounds: Vec<Predicate<'tcx>> = param_env
@@ -620,6 +621,7 @@ fn receiver_is_dispatchable<'tcx>(
620621
def_id: dispatch_from_dyn_did,
621622
substs: tcx.mk_substs_trait(receiver_ty, &[unsized_receiver_ty.into()]),
622623
}
624+
.without_const()
623625
.to_predicate();
624626

625627
Obligation::new(ObligationCause::dummy(), param_env, predicate)

src/librustc/traits/project.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1616
use crate::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime};
1717
use crate::ty::fold::{TypeFoldable, TypeFolder};
1818
use crate::ty::subst::{InternalSubsts, Subst};
19-
use crate::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt};
19+
use crate::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, WithConstness};
2020
use rustc_data_structures::snapshot_map::{Snapshot, SnapshotMap};
2121
use rustc_hir::def_id::DefId;
2222
use rustc_macros::HashStable;
@@ -738,7 +738,12 @@ fn get_paranoid_cache_value_obligation<'a, 'tcx>(
738738
depth: usize,
739739
) -> PredicateObligation<'tcx> {
740740
let trait_ref = projection_ty.trait_ref(infcx.tcx).to_poly_trait_ref();
741-
Obligation { cause, recursion_depth: depth, param_env, predicate: trait_ref.to_predicate() }
741+
Obligation {
742+
cause,
743+
recursion_depth: depth,
744+
param_env,
745+
predicate: trait_ref.without_const().to_predicate(),
746+
}
742747
}
743748

744749
/// If we are projecting `<T as Trait>::Item`, but `T: Trait` does not
@@ -772,7 +777,7 @@ fn normalize_to_error<'a, 'tcx>(
772777
cause,
773778
recursion_depth: depth,
774779
param_env,
775-
predicate: trait_ref.to_predicate(),
780+
predicate: trait_ref.without_const().to_predicate(),
776781
};
777782
let tcx = selcx.infcx().tcx;
778783
let def_id = projection_ty.item_def_id;

src/librustc/traits/select.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use crate::middle::lang_items;
3737
use crate::ty::fast_reject;
3838
use crate::ty::relate::TypeRelation;
3939
use crate::ty::subst::{Subst, SubstsRef};
40-
use crate::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, TypeFoldable};
40+
use crate::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness};
4141
use rustc_hir::def_id::DefId;
4242

4343
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -3368,7 +3368,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
33683368
tcx.require_lang_item(lang_items::SizedTraitLangItem, None),
33693369
tcx.mk_substs_trait(source, &[]),
33703370
);
3371-
nested.push(predicate_to_obligation(tr.to_predicate()));
3371+
nested.push(predicate_to_obligation(tr.without_const().to_predicate()));
33723372

33733373
// If the type is `Foo + 'a`, ensure that the type
33743374
// being cast to `Foo + 'a` outlives `'a`:

src/librustc/traits/util.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use smallvec::SmallVec;
44

55
use crate::ty::outlives::Component;
66
use crate::ty::subst::{GenericArg, Subst, SubstsRef};
7-
use crate::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt};
7+
use crate::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, WithConstness};
88
use rustc_data_structures::fx::FxHashSet;
99
use rustc_hir as hir;
1010
use rustc_hir::def_id::DefId;
@@ -99,14 +99,14 @@ pub fn elaborate_trait_ref<'tcx>(
9999
tcx: TyCtxt<'tcx>,
100100
trait_ref: ty::PolyTraitRef<'tcx>,
101101
) -> Elaborator<'tcx> {
102-
elaborate_predicates(tcx, vec![trait_ref.to_predicate()])
102+
elaborate_predicates(tcx, vec![trait_ref.without_const().to_predicate()])
103103
}
104104

105105
pub fn elaborate_trait_refs<'tcx>(
106106
tcx: TyCtxt<'tcx>,
107107
trait_refs: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
108108
) -> Elaborator<'tcx> {
109-
let predicates = trait_refs.map(|trait_ref| trait_ref.to_predicate()).collect();
109+
let predicates = trait_refs.map(|trait_ref| trait_ref.without_const().to_predicate()).collect();
110110
elaborate_predicates(tcx, predicates)
111111
}
112112

@@ -358,7 +358,7 @@ impl<'tcx> TraitAliasExpander<'tcx> {
358358
fn expand(&mut self, item: &TraitAliasExpansionInfo<'tcx>) -> bool {
359359
let tcx = self.tcx;
360360
let trait_ref = item.trait_ref();
361-
let pred = trait_ref.to_predicate();
361+
let pred = trait_ref.without_const().to_predicate();
362362

363363
debug!("expand_trait_aliases: trait_ref={:?}", trait_ref);
364364

@@ -370,13 +370,9 @@ impl<'tcx> TraitAliasExpander<'tcx> {
370370

371371
// Don't recurse if this trait alias is already on the stack for the DFS search.
372372
let anon_pred = anonymize_predicate(tcx, &pred);
373-
if item
374-
.path
375-
.iter()
376-
.rev()
377-
.skip(1)
378-
.any(|(tr, _)| anonymize_predicate(tcx, &tr.to_predicate()) == anon_pred)
379-
{
373+
if item.path.iter().rev().skip(1).any(|(tr, _)| {
374+
anonymize_predicate(tcx, &tr.without_const().to_predicate()) == anon_pred
375+
}) {
380376
return false;
381377
}
382378

@@ -545,7 +541,12 @@ pub fn predicate_for_trait_ref<'tcx>(
545541
trait_ref: ty::TraitRef<'tcx>,
546542
recursion_depth: usize,
547543
) -> PredicateObligation<'tcx> {
548-
Obligation { cause, param_env, recursion_depth, predicate: trait_ref.to_predicate() }
544+
Obligation {
545+
cause,
546+
param_env,
547+
recursion_depth,
548+
predicate: trait_ref.without_const().to_predicate(),
549+
}
549550
}
550551

551552
pub fn predicate_for_trait_def(

src/librustc/traits/wf.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::infer::InferCtxt;
33
use crate::middle::lang_items;
44
use crate::traits::{self, AssocTypeBoundData};
55
use crate::ty::subst::SubstsRef;
6-
use crate::ty::{self, ToPredicate, Ty, TyCtxt, TypeFoldable};
6+
use crate::ty::{self, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness};
77
use rustc_hir as hir;
88
use rustc_hir::def_id::DefId;
99
use rustc_span::symbol::{kw, Ident};
@@ -350,7 +350,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
350350
self.compute_trait_ref(&trait_ref, Elaborate::None);
351351

352352
if !data.has_escaping_bound_vars() {
353-
let predicate = trait_ref.to_predicate();
353+
let predicate = trait_ref.without_const().to_predicate();
354354
let cause = self.cause(traits::ProjectionWf(data));
355355
self.out.push(traits::Obligation::new(cause, self.param_env, predicate));
356356
}
@@ -378,7 +378,11 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
378378
def_id: self.infcx.tcx.require_lang_item(lang_items::SizedTraitLangItem, None),
379379
substs: self.infcx.tcx.mk_substs_trait(subty, &[]),
380380
};
381-
self.out.push(traits::Obligation::new(cause, self.param_env, trait_ref.to_predicate()));
381+
self.out.push(traits::Obligation::new(
382+
cause,
383+
self.param_env,
384+
trait_ref.without_const().to_predicate(),
385+
));
382386
}
383387
}
384388

0 commit comments

Comments
 (0)