Skip to content

Commit b8ec922

Browse files
authored
Unrolled build for rust-lang#122839
Rollup merge of rust-lang#122839 - compiler-errors:predicate-polarity, r=lcnr Split out `PredicatePolarity` from `ImplPolarity` Because having to deal with a third `Reservation` level in all the trait solver code is kind of weird. r? `@lcnr` or `@oli-obk`
2 parents 85e449a + d677a2d commit b8ec922

File tree

35 files changed

+159
-116
lines changed

35 files changed

+159
-116
lines changed

compiler/rustc_borrowck/src/type_check/canonical.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
8282
) {
8383
self.prove_predicate(
8484
ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::Trait(
85-
ty::TraitPredicate { trait_ref, polarity: ty::ImplPolarity::Positive },
85+
ty::TraitPredicate { trait_ref, polarity: ty::PredicatePolarity::Positive },
8686
))),
8787
locations,
8888
category,

compiler/rustc_hir_analysis/src/bounds.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<'tcx> Bounds<'tcx> {
4242
tcx: TyCtxt<'tcx>,
4343
trait_ref: ty::PolyTraitRef<'tcx>,
4444
span: Span,
45-
polarity: ty::ImplPolarity,
45+
polarity: ty::PredicatePolarity,
4646
) {
4747
self.push_trait_bound_inner(tcx, trait_ref, span, polarity);
4848
}
@@ -52,7 +52,7 @@ impl<'tcx> Bounds<'tcx> {
5252
tcx: TyCtxt<'tcx>,
5353
trait_ref: ty::PolyTraitRef<'tcx>,
5454
span: Span,
55-
polarity: ty::ImplPolarity,
55+
polarity: ty::PredicatePolarity,
5656
) {
5757
self.clauses.push((
5858
trait_ref

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,7 @@ fn check_impl<'tcx>(
13221322
trait_ref,
13231323
);
13241324
let trait_pred =
1325-
ty::TraitPredicate { trait_ref, polarity: ty::ImplPolarity::Positive };
1325+
ty::TraitPredicate { trait_ref, polarity: ty::PredicatePolarity::Positive };
13261326
let mut obligations = traits::wf::trait_obligations(
13271327
wfcx.infcx,
13281328
wfcx.param_env,

compiler/rustc_hir_analysis/src/coherence/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ fn infringing_fields_error(
554554
}
555555
if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(ty::TraitPredicate {
556556
trait_ref,
557-
polarity: ty::ImplPolarity::Positive,
557+
polarity: ty::PredicatePolarity::Positive,
558558
..
559559
})) = error_predicate.kind().skip_binder()
560560
{

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ pub(super) fn implied_predicates_with_filter(
624624
for &(pred, span) in implied_bounds {
625625
debug!("superbound: {:?}", pred);
626626
if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
627-
&& bound.polarity == ty::ImplPolarity::Positive
627+
&& bound.polarity == ty::PredicatePolarity::Positive
628628
{
629629
tcx.at(span).super_predicates_of(bound.def_id());
630630
}
@@ -634,7 +634,7 @@ pub(super) fn implied_predicates_with_filter(
634634
for &(pred, span) in implied_bounds {
635635
debug!("superbound: {:?}", pred);
636636
if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
637-
&& bound.polarity == ty::ImplPolarity::Positive
637+
&& bound.polarity == ty::PredicatePolarity::Positive
638638
{
639639
tcx.at(span).implied_predicates_of(bound.def_id());
640640
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,16 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
140140
hir::GenericBound::Trait(poly_trait_ref, modifier) => {
141141
let (constness, polarity) = match modifier {
142142
hir::TraitBoundModifier::Const => {
143-
(ty::BoundConstness::Const, ty::ImplPolarity::Positive)
143+
(ty::BoundConstness::Const, ty::PredicatePolarity::Positive)
144144
}
145145
hir::TraitBoundModifier::MaybeConst => {
146-
(ty::BoundConstness::ConstIfConst, ty::ImplPolarity::Positive)
146+
(ty::BoundConstness::ConstIfConst, ty::PredicatePolarity::Positive)
147147
}
148148
hir::TraitBoundModifier::None => {
149-
(ty::BoundConstness::NotConst, ty::ImplPolarity::Positive)
149+
(ty::BoundConstness::NotConst, ty::PredicatePolarity::Positive)
150150
}
151151
hir::TraitBoundModifier::Negative => {
152-
(ty::BoundConstness::NotConst, ty::ImplPolarity::Negative)
152+
(ty::BoundConstness::NotConst, ty::PredicatePolarity::Negative)
153153
}
154154
hir::TraitBoundModifier::Maybe => continue,
155155
};

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
673673
trait_ref: &hir::TraitRef<'tcx>,
674674
span: Span,
675675
constness: ty::BoundConstness,
676-
polarity: ty::ImplPolarity,
676+
polarity: ty::PredicatePolarity,
677677
self_ty: Ty<'tcx>,
678678
bounds: &mut Bounds<'tcx>,
679679
only_self_bounds: OnlySelfBounds,
@@ -710,7 +710,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
710710
// Don't register additional associated type bounds for negative bounds,
711711
// since we should have emitten an error for them earlier, and they will
712712
// not be well-formed!
713-
if polarity == ty::ImplPolarity::Negative {
713+
if polarity != ty::PredicatePolarity::Positive {
714714
assert!(
715715
self.tcx().dcx().has_errors().is_some(),
716716
"negative trait bounds should not have bindings",

compiler/rustc_hir_analysis/src/hir_ty_lowering/object_safety.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
4343
&trait_bound.trait_ref,
4444
trait_bound.span,
4545
ty::BoundConstness::NotConst,
46-
ty::ImplPolarity::Positive,
46+
ty::PredicatePolarity::Positive,
4747
dummy_self,
4848
&mut bounds,
4949
// True so we don't populate `bounds` with associated type bounds, even
@@ -60,7 +60,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
6060
let bound_pred = pred.kind();
6161
match bound_pred.skip_binder() {
6262
ty::ClauseKind::Trait(trait_pred) => {
63-
assert_eq!(trait_pred.polarity, ty::ImplPolarity::Positive);
63+
assert_eq!(trait_pred.polarity, ty::PredicatePolarity::Positive);
6464
trait_bounds.push((bound_pred.rebind(trait_pred.trait_ref), span));
6565
}
6666
ty::ClauseKind::Projection(proj) => {

compiler/rustc_hir_typeck/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3099,7 +3099,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30993099
cause.clone().derived_cause(
31003100
ty::Binder::dummy(ty::TraitPredicate {
31013101
trait_ref: impl_trait_ref,
3102-
polarity: ty::ImplPolarity::Positive,
3102+
polarity: ty::PredicatePolarity::Positive,
31033103
}),
31043104
|derived| {
31053105
traits::ImplDerivedObligation(Box::new(

compiler/rustc_hir_typeck/src/method/suggest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
872872
match pred.kind().skip_binder() {
873873
ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
874874
Some(pred.def_id()) == self.tcx.lang_items().sized_trait()
875-
&& pred.polarity == ty::ImplPolarity::Positive
875+
&& pred.polarity == ty::PredicatePolarity::Positive
876876
}
877877
_ => false,
878878
}
@@ -3364,7 +3364,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
33643364
"inherent impls can't be candidates, only trait impls can be",
33653365
)
33663366
})
3367-
.filter(|header| header.polarity == ty::ImplPolarity::Negative)
3367+
.filter(|header| header.polarity != ty::ImplPolarity::Positive)
33683368
.any(|header| {
33693369
let imp = header.trait_ref.instantiate_identity();
33703370
let imp_simp =

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl<T> Trait<T> for X {
306306
.any(|(pred, _span)| match pred.kind().skip_binder() {
307307
ty::ClauseKind::Trait(trait_predicate)
308308
if trait_predicate.polarity
309-
== ty::ImplPolarity::Positive =>
309+
== ty::PredicatePolarity::Positive =>
310310
{
311311
trait_predicate.def_id() == def_id
312312
}
@@ -420,7 +420,7 @@ impl<T> Trait<T> for X {
420420
else {
421421
continue;
422422
};
423-
if trait_predicate.polarity != ty::ImplPolarity::Positive {
423+
if trait_predicate.polarity != ty::PredicatePolarity::Positive {
424424
continue;
425425
}
426426
let def_id = trait_predicate.def_id();

compiler/rustc_infer/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl<'tcx> FulfillmentError<'tcx> {
209209
}
210210

211211
impl<'tcx> PolyTraitObligation<'tcx> {
212-
pub fn polarity(&self) -> ty::ImplPolarity {
212+
pub fn polarity(&self) -> ty::PredicatePolarity {
213213
self.predicate.skip_binder().polarity
214214
}
215215

compiler/rustc_infer/src/traits/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ impl<'tcx, O: Elaboratable<'tcx>> Elaborator<'tcx, O> {
270270
match bound_clause.skip_binder() {
271271
ty::ClauseKind::Trait(data) => {
272272
// Negative trait bounds do not imply any supertrait bounds
273-
if data.polarity == ty::ImplPolarity::Negative {
273+
if data.polarity != ty::PredicatePolarity::Positive {
274274
return;
275275
}
276276
// Get predicates implied by the trait, or only super predicates if we only care about self predicates.

compiler/rustc_lint/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ fn type_implements_negative_copy_modulo_regions<'tcx>(
726726
param_env: ty::ParamEnv<'tcx>,
727727
) -> bool {
728728
let trait_ref = ty::TraitRef::new(tcx, tcx.require_lang_item(hir::LangItem::Copy, None), [ty]);
729-
let pred = ty::TraitPredicate { trait_ref, polarity: ty::ImplPolarity::Negative };
729+
let pred = ty::TraitPredicate { trait_ref, polarity: ty::PredicatePolarity::Negative };
730730
let obligation = traits::Obligation {
731731
cause: traits::ObligationCause::dummy(),
732732
param_env,

compiler/rustc_middle/src/ty/context.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use crate::traits::solve::{
2727
use crate::ty::{
2828
self, AdtDef, AdtDefData, AdtKind, Binder, Clause, Const, ConstData, GenericParamDefKind,
2929
ImplPolarity, List, ParamConst, ParamTy, PolyExistentialPredicate, PolyFnSig, Predicate,
30-
PredicateKind, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyVid,
31-
TypeVisitable, Visibility,
30+
PredicateKind, PredicatePolarity, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty,
31+
TyKind, TyVid, TypeVisitable, Visibility,
3232
};
3333
use crate::ty::{GenericArg, GenericArgs, GenericArgsRef};
3434
use rustc_ast::{self as ast, attr};
@@ -1526,7 +1526,7 @@ macro_rules! nop_slice_lift {
15261526
nop_slice_lift! {ty::ValTree<'a> => ty::ValTree<'tcx>}
15271527

15281528
TrivialLiftImpls! {
1529-
ImplPolarity, Promoted
1529+
ImplPolarity, PredicatePolarity, Promoted
15301530
}
15311531

15321532
macro_rules! sty_debug_print {
@@ -1833,7 +1833,7 @@ impl<'tcx> TyCtxt<'tcx> {
18331833
return false;
18341834
};
18351835
trait_predicate.trait_ref.def_id == future_trait
1836-
&& trait_predicate.polarity == ImplPolarity::Positive
1836+
&& trait_predicate.polarity == PredicatePolarity::Positive
18371837
})
18381838
}
18391839

compiler/rustc_middle/src/ty/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<T> ExpectedFound<T> {
3232
pub enum TypeError<'tcx> {
3333
Mismatch,
3434
ConstnessMismatch(ExpectedFound<ty::BoundConstness>),
35-
PolarityMismatch(ExpectedFound<ty::ImplPolarity>),
35+
PolarityMismatch(ExpectedFound<ty::PredicatePolarity>),
3636
UnsafetyMismatch(ExpectedFound<hir::Unsafety>),
3737
AbiMismatch(ExpectedFound<abi::Abi>),
3838
Mutability,

compiler/rustc_middle/src/ty/mod.rs

+27-7
Original file line numberDiff line numberDiff line change
@@ -280,23 +280,43 @@ pub enum ImplPolarity {
280280
Reservation,
281281
}
282282

283-
impl ImplPolarity {
283+
impl fmt::Display for ImplPolarity {
284+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
285+
match self {
286+
Self::Positive => f.write_str("positive"),
287+
Self::Negative => f.write_str("negative"),
288+
Self::Reservation => f.write_str("reservation"),
289+
}
290+
}
291+
}
292+
293+
/// Polarity for a trait predicate. May either be negative or positive.
294+
/// Distinguished from [`ImplPolarity`] since we never compute goals with
295+
/// "reservation" level.
296+
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable, Debug)]
297+
#[derive(TypeFoldable, TypeVisitable)]
298+
pub enum PredicatePolarity {
299+
/// `Type: Trait`
300+
Positive,
301+
/// `Type: !Trait`
302+
Negative,
303+
}
304+
305+
impl PredicatePolarity {
284306
/// Flips polarity by turning `Positive` into `Negative` and `Negative` into `Positive`.
285-
pub fn flip(&self) -> Option<ImplPolarity> {
307+
pub fn flip(&self) -> PredicatePolarity {
286308
match self {
287-
ImplPolarity::Positive => Some(ImplPolarity::Negative),
288-
ImplPolarity::Negative => Some(ImplPolarity::Positive),
289-
ImplPolarity::Reservation => None,
309+
PredicatePolarity::Positive => PredicatePolarity::Negative,
310+
PredicatePolarity::Negative => PredicatePolarity::Positive,
290311
}
291312
}
292313
}
293314

294-
impl fmt::Display for ImplPolarity {
315+
impl fmt::Display for PredicatePolarity {
295316
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
296317
match self {
297318
Self::Positive => f.write_str("positive"),
298319
Self::Negative => f.write_str("negative"),
299-
Self::Reservation => f.write_str("reservation"),
300320
}
301321
}
302322
}

compiler/rustc_middle/src/ty/predicate.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::cmp::Ordering;
1111
use crate::ty::visit::TypeVisitableExt;
1212
use crate::ty::{
1313
self, AliasTy, Binder, DebruijnIndex, DebugWithInfcx, EarlyBinder, GenericArg, GenericArgs,
14-
GenericArgsRef, ImplPolarity, Term, Ty, TyCtxt, TypeFlags, WithCachedTypeInfo,
14+
GenericArgsRef, PredicatePolarity, Term, Ty, TyCtxt, TypeFlags, WithCachedTypeInfo,
1515
};
1616

1717
pub type ClauseKind<'tcx> = IrClauseKind<TyCtxt<'tcx>>;
@@ -70,7 +70,7 @@ impl<'tcx> Predicate<'tcx> {
7070
polarity,
7171
})) => Some(PredicateKind::Clause(ClauseKind::Trait(TraitPredicate {
7272
trait_ref,
73-
polarity: polarity.flip()?,
73+
polarity: polarity.flip(),
7474
}))),
7575

7676
_ => None,
@@ -663,7 +663,7 @@ pub struct TraitPredicate<'tcx> {
663663
/// exist via a series of predicates.)
664664
///
665665
/// If polarity is Reserved: that's a bug.
666-
pub polarity: ImplPolarity,
666+
pub polarity: PredicatePolarity,
667667
}
668668

669669
pub type PolyTraitPredicate<'tcx> = ty::Binder<'tcx, TraitPredicate<'tcx>>;
@@ -693,7 +693,7 @@ impl<'tcx> PolyTraitPredicate<'tcx> {
693693
}
694694

695695
#[inline]
696-
pub fn polarity(self) -> ImplPolarity {
696+
pub fn polarity(self) -> PredicatePolarity {
697697
self.skip_binder().polarity
698698
}
699699
}
@@ -907,7 +907,7 @@ impl<'tcx> ToPredicate<'tcx> for TraitRef<'tcx> {
907907
impl<'tcx> ToPredicate<'tcx, TraitPredicate<'tcx>> for TraitRef<'tcx> {
908908
#[inline(always)]
909909
fn to_predicate(self, _tcx: TyCtxt<'tcx>) -> TraitPredicate<'tcx> {
910-
TraitPredicate { trait_ref: self, polarity: ImplPolarity::Positive }
910+
TraitPredicate { trait_ref: self, polarity: PredicatePolarity::Positive }
911911
}
912912
}
913913

@@ -940,7 +940,7 @@ impl<'tcx> ToPredicate<'tcx, PolyTraitPredicate<'tcx>> for Binder<'tcx, TraitRef
940940
fn to_predicate(self, _: TyCtxt<'tcx>) -> PolyTraitPredicate<'tcx> {
941941
self.map_bound(|trait_ref| TraitPredicate {
942942
trait_ref,
943-
polarity: ty::ImplPolarity::Positive,
943+
polarity: ty::PredicatePolarity::Positive,
944944
})
945945
}
946946
}

0 commit comments

Comments
 (0)