Rollup merge of #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`
This commit is contained in:
Matthias Krüger 2024-03-22 20:31:30 +01:00 committed by GitHub
commit 80306927cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
35 changed files with 163 additions and 120 deletions

View File

@ -82,7 +82,7 @@ pub(super) fn prove_trait_ref(
) {
self.prove_predicate(
ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::Trait(
ty::TraitPredicate { trait_ref, polarity: ty::ImplPolarity::Positive },
ty::TraitPredicate { trait_ref, polarity: ty::PredicatePolarity::Positive },
))),
locations,
category,

View File

@ -42,7 +42,7 @@ pub fn push_trait_bound(
tcx: TyCtxt<'tcx>,
trait_ref: ty::PolyTraitRef<'tcx>,
span: Span,
polarity: ty::ImplPolarity,
polarity: ty::PredicatePolarity,
) {
self.push_trait_bound_inner(tcx, trait_ref, span, polarity);
}
@ -52,7 +52,7 @@ fn push_trait_bound_inner(
tcx: TyCtxt<'tcx>,
trait_ref: ty::PolyTraitRef<'tcx>,
span: Span,
polarity: ty::ImplPolarity,
polarity: ty::PredicatePolarity,
) {
self.clauses.push((
trait_ref

View File

@ -1322,7 +1322,7 @@ fn check_impl<'tcx>(
trait_ref,
);
let trait_pred =
ty::TraitPredicate { trait_ref, polarity: ty::ImplPolarity::Positive };
ty::TraitPredicate { trait_ref, polarity: ty::PredicatePolarity::Positive };
let mut obligations = traits::wf::trait_obligations(
wfcx.infcx,
wfcx.param_env,

View File

@ -551,7 +551,7 @@ fn infringing_fields_error(
}
if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(ty::TraitPredicate {
trait_ref,
polarity: ty::ImplPolarity::Positive,
polarity: ty::PredicatePolarity::Positive,
..
})) = error_predicate.kind().skip_binder()
{

View File

@ -624,7 +624,7 @@ pub(super) fn implied_predicates_with_filter(
for &(pred, span) in implied_bounds {
debug!("superbound: {:?}", pred);
if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
&& bound.polarity == ty::ImplPolarity::Positive
&& bound.polarity == ty::PredicatePolarity::Positive
{
tcx.at(span).super_predicates_of(bound.def_id());
}
@ -634,7 +634,7 @@ pub(super) fn implied_predicates_with_filter(
for &(pred, span) in implied_bounds {
debug!("superbound: {:?}", pred);
if let ty::ClauseKind::Trait(bound) = pred.kind().skip_binder()
&& bound.polarity == ty::ImplPolarity::Positive
&& bound.polarity == ty::PredicatePolarity::Positive
{
tcx.at(span).implied_predicates_of(bound.def_id());
}

View File

@ -140,16 +140,16 @@ pub(crate) fn lower_poly_bounds<'hir, I: Iterator<Item = &'hir hir::GenericBound
hir::GenericBound::Trait(poly_trait_ref, modifier) => {
let (constness, polarity) = match modifier {
hir::TraitBoundModifier::Const => {
(ty::BoundConstness::Const, ty::ImplPolarity::Positive)
(ty::BoundConstness::Const, ty::PredicatePolarity::Positive)
}
hir::TraitBoundModifier::MaybeConst => {
(ty::BoundConstness::ConstIfConst, ty::ImplPolarity::Positive)
(ty::BoundConstness::ConstIfConst, ty::PredicatePolarity::Positive)
}
hir::TraitBoundModifier::None => {
(ty::BoundConstness::NotConst, ty::ImplPolarity::Positive)
(ty::BoundConstness::NotConst, ty::PredicatePolarity::Positive)
}
hir::TraitBoundModifier::Negative => {
(ty::BoundConstness::NotConst, ty::ImplPolarity::Negative)
(ty::BoundConstness::NotConst, ty::PredicatePolarity::Negative)
}
hir::TraitBoundModifier::Maybe => continue,
};

View File

@ -673,7 +673,7 @@ pub(crate) fn lower_poly_trait_ref(
trait_ref: &hir::TraitRef<'tcx>,
span: Span,
constness: ty::BoundConstness,
polarity: ty::ImplPolarity,
polarity: ty::PredicatePolarity,
self_ty: Ty<'tcx>,
bounds: &mut Bounds<'tcx>,
only_self_bounds: OnlySelfBounds,
@ -710,7 +710,7 @@ pub(crate) fn lower_poly_trait_ref(
// Don't register additional associated type bounds for negative bounds,
// since we should have emitten an error for them earlier, and they will
// not be well-formed!
if polarity == ty::ImplPolarity::Negative {
if polarity != ty::PredicatePolarity::Positive {
assert!(
self.tcx().dcx().has_errors().is_some(),
"negative trait bounds should not have bindings",

View File

@ -43,7 +43,7 @@ pub(super) fn lower_trait_object_ty(
&trait_bound.trait_ref,
trait_bound.span,
ty::BoundConstness::NotConst,
ty::ImplPolarity::Positive,
ty::PredicatePolarity::Positive,
dummy_self,
&mut bounds,
// True so we don't populate `bounds` with associated type bounds, even
@ -60,7 +60,7 @@ pub(super) fn lower_trait_object_ty(
let bound_pred = pred.kind();
match bound_pred.skip_binder() {
ty::ClauseKind::Trait(trait_pred) => {
assert_eq!(trait_pred.polarity, ty::ImplPolarity::Positive);
assert_eq!(trait_pred.polarity, ty::PredicatePolarity::Positive);
trait_bounds.push((bound_pred.rebind(trait_pred.trait_ref), span));
}
ty::ClauseKind::Projection(proj) => {

View File

@ -3100,7 +3100,7 @@ fn find_and_report_unsatisfied_index_impl(
cause.clone().derived_cause(
ty::Binder::dummy(ty::TraitPredicate {
trait_ref: impl_trait_ref,
polarity: ty::ImplPolarity::Positive,
polarity: ty::PredicatePolarity::Positive,
}),
|derived| {
traits::ImplDerivedObligation(Box::new(

View File

@ -875,7 +875,7 @@ pub fn report_no_match_method_error(
match pred.kind().skip_binder() {
ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
Some(pred.def_id()) == self.tcx.lang_items().sized_trait()
&& pred.polarity == ty::ImplPolarity::Positive
&& pred.polarity == ty::PredicatePolarity::Positive
}
_ => false,
}
@ -3367,7 +3367,7 @@ enum Introducer {
"inherent impls can't be candidates, only trait impls can be",
)
})
.filter(|header| header.polarity == ty::ImplPolarity::Negative)
.filter(|header| header.polarity != ty::ImplPolarity::Positive)
.any(|header| {
let imp = header.trait_ref.instantiate_identity();
let imp_simp =

View File

@ -306,7 +306,7 @@ fn foo(&self, x: T) -> T { x }
.any(|(pred, _span)| match pred.kind().skip_binder() {
ty::ClauseKind::Trait(trait_predicate)
if trait_predicate.polarity
== ty::ImplPolarity::Positive =>
== ty::PredicatePolarity::Positive =>
{
trait_predicate.def_id() == def_id
}
@ -420,7 +420,7 @@ fn foo(&self, x: T) -> T { x }
else {
continue;
};
if trait_predicate.polarity != ty::ImplPolarity::Positive {
if trait_predicate.polarity != ty::PredicatePolarity::Positive {
continue;
}
let def_id = trait_predicate.def_id();

View File

@ -209,7 +209,7 @@ pub fn new(
}
impl<'tcx> PolyTraitObligation<'tcx> {
pub fn polarity(&self) -> ty::ImplPolarity {
pub fn polarity(&self) -> ty::PredicatePolarity {
self.predicate.skip_binder().polarity
}

View File

@ -270,7 +270,7 @@ fn elaborate(&mut self, elaboratable: &O) {
match bound_clause.skip_binder() {
ty::ClauseKind::Trait(data) => {
// Negative trait bounds do not imply any supertrait bounds
if data.polarity == ty::ImplPolarity::Negative {
if data.polarity != ty::PredicatePolarity::Positive {
return;
}
// Get predicates implied by the trait, or only super predicates if we only care about self predicates.

View File

@ -726,7 +726,7 @@ fn type_implements_negative_copy_modulo_regions<'tcx>(
param_env: ty::ParamEnv<'tcx>,
) -> bool {
let trait_ref = ty::TraitRef::new(tcx, tcx.require_lang_item(hir::LangItem::Copy, None), [ty]);
let pred = ty::TraitPredicate { trait_ref, polarity: ty::ImplPolarity::Negative };
let pred = ty::TraitPredicate { trait_ref, polarity: ty::PredicatePolarity::Negative };
let obligation = traits::Obligation {
cause: traits::ObligationCause::dummy(),
param_env,

View File

@ -27,8 +27,8 @@
use crate::ty::{
self, AdtDef, AdtDefData, AdtKind, Binder, Clause, Const, ConstData, GenericParamDefKind,
ImplPolarity, List, ParamConst, ParamTy, PolyExistentialPredicate, PolyFnSig, Predicate,
PredicateKind, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyVid,
TypeVisitable, Visibility,
PredicateKind, PredicatePolarity, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty,
TyKind, TyVid, TypeVisitable, Visibility,
};
use crate::ty::{GenericArg, GenericArgs, GenericArgsRef};
use rustc_ast::{self as ast, attr};
@ -1526,7 +1526,7 @@ fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
nop_slice_lift! {ty::ValTree<'a> => ty::ValTree<'tcx>}
TrivialLiftImpls! {
ImplPolarity, Promoted
ImplPolarity, PredicatePolarity, Promoted
}
macro_rules! sty_debug_print {
@ -1833,7 +1833,7 @@ pub fn ty_is_opaque_future(self, ty: Ty<'_>) -> bool {
return false;
};
trait_predicate.trait_ref.def_id == future_trait
&& trait_predicate.polarity == ImplPolarity::Positive
&& trait_predicate.polarity == PredicatePolarity::Positive
})
}

View File

@ -32,7 +32,7 @@ pub fn new(a_is_expected: bool, a: T, b: T) -> Self {
pub enum TypeError<'tcx> {
Mismatch,
ConstnessMismatch(ExpectedFound<ty::BoundConstness>),
PolarityMismatch(ExpectedFound<ty::ImplPolarity>),
PolarityMismatch(ExpectedFound<ty::PredicatePolarity>),
UnsafetyMismatch(ExpectedFound<hir::Unsafety>),
AbiMismatch(ExpectedFound<abi::Abi>),
Mutability,

View File

@ -280,17 +280,6 @@ pub enum ImplPolarity {
Reservation,
}
impl ImplPolarity {
/// Flips polarity by turning `Positive` into `Negative` and `Negative` into `Positive`.
pub fn flip(&self) -> Option<ImplPolarity> {
match self {
ImplPolarity::Positive => Some(ImplPolarity::Negative),
ImplPolarity::Negative => Some(ImplPolarity::Positive),
ImplPolarity::Reservation => None,
}
}
}
impl fmt::Display for ImplPolarity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
@ -301,6 +290,37 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
}
}
/// Polarity for a trait predicate. May either be negative or positive.
/// Distinguished from [`ImplPolarity`] since we never compute goals with
/// "reservation" level.
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable, Debug)]
#[derive(TypeFoldable, TypeVisitable)]
pub enum PredicatePolarity {
/// `Type: Trait`
Positive,
/// `Type: !Trait`
Negative,
}
impl PredicatePolarity {
/// Flips polarity by turning `Positive` into `Negative` and `Negative` into `Positive`.
pub fn flip(&self) -> PredicatePolarity {
match self {
PredicatePolarity::Positive => PredicatePolarity::Negative,
PredicatePolarity::Negative => PredicatePolarity::Positive,
}
}
}
impl fmt::Display for PredicatePolarity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Positive => f.write_str("positive"),
Self::Negative => f.write_str("negative"),
}
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable, Debug)]
#[derive(TypeFoldable, TypeVisitable)]
pub enum Asyncness {

View File

@ -11,7 +11,7 @@
use crate::ty::visit::TypeVisitableExt;
use crate::ty::{
self, AliasTy, Binder, DebruijnIndex, DebugWithInfcx, EarlyBinder, GenericArg, GenericArgs,
GenericArgsRef, ImplPolarity, Term, Ty, TyCtxt, TypeFlags, WithCachedTypeInfo,
GenericArgsRef, PredicatePolarity, Term, Ty, TyCtxt, TypeFlags, WithCachedTypeInfo,
};
pub type ClauseKind<'tcx> = IrClauseKind<TyCtxt<'tcx>>;
@ -70,7 +70,7 @@ pub fn flip_polarity(self, tcx: TyCtxt<'tcx>) -> Option<Predicate<'tcx>> {
polarity,
})) => Some(PredicateKind::Clause(ClauseKind::Trait(TraitPredicate {
trait_ref,
polarity: polarity.flip()?,
polarity: polarity.flip(),
}))),
_ => None,
@ -663,7 +663,7 @@ pub struct TraitPredicate<'tcx> {
/// exist via a series of predicates.)
///
/// If polarity is Reserved: that's a bug.
pub polarity: ImplPolarity,
pub polarity: PredicatePolarity,
}
pub type PolyTraitPredicate<'tcx> = ty::Binder<'tcx, TraitPredicate<'tcx>>;
@ -693,7 +693,7 @@ pub fn self_ty(self) -> ty::Binder<'tcx, Ty<'tcx>> {
}
#[inline]
pub fn polarity(self) -> ImplPolarity {
pub fn polarity(self) -> PredicatePolarity {
self.skip_binder().polarity
}
}
@ -907,7 +907,7 @@ fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> {
impl<'tcx> ToPredicate<'tcx, TraitPredicate<'tcx>> for TraitRef<'tcx> {
#[inline(always)]
fn to_predicate(self, _tcx: TyCtxt<'tcx>) -> TraitPredicate<'tcx> {
TraitPredicate { trait_ref: self, polarity: ImplPolarity::Positive }
TraitPredicate { trait_ref: self, polarity: PredicatePolarity::Positive }
}
}
@ -940,7 +940,7 @@ impl<'tcx> ToPredicate<'tcx, PolyTraitPredicate<'tcx>> for Binder<'tcx, TraitRef
fn to_predicate(self, _: TyCtxt<'tcx>) -> PolyTraitPredicate<'tcx> {
self.map_bound(|trait_ref| TraitPredicate {
trait_ref,
polarity: ty::ImplPolarity::Positive,
polarity: ty::PredicatePolarity::Positive,
})
}
}

View File

@ -995,11 +995,11 @@ fn pretty_print_opaque_impl_type(
// Don't print `+ Sized`, but rather `+ ?Sized` if absent.
if Some(trait_ref.def_id()) == tcx.lang_items().sized_trait() {
match pred.polarity {
ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => {
ty::PredicatePolarity::Positive => {
has_sized_bound = true;
continue;
}
ty::ImplPolarity::Negative => has_negative_sized_bound = true,
ty::PredicatePolarity::Negative => has_negative_sized_bound = true,
}
}
@ -1020,7 +1020,7 @@ fn pretty_print_opaque_impl_type(
self.insert_trait_and_projection(
trait_ref,
ty::ImplPolarity::Positive,
ty::PredicatePolarity::Positive,
Some(proj_ty),
&mut traits,
&mut fn_traits,
@ -1085,7 +1085,7 @@ fn pretty_print_opaque_impl_type(
_ => {
if entry.has_fn_once {
traits
.entry((fn_once_trait_ref, ty::ImplPolarity::Positive))
.entry((fn_once_trait_ref, ty::PredicatePolarity::Positive))
.or_default()
.extend(
// Group the return ty with its def id, if we had one.
@ -1095,10 +1095,10 @@ fn pretty_print_opaque_impl_type(
);
}
if let Some(trait_ref) = entry.fn_mut_trait_ref {
traits.entry((trait_ref, ty::ImplPolarity::Positive)).or_default();
traits.entry((trait_ref, ty::PredicatePolarity::Positive)).or_default();
}
if let Some(trait_ref) = entry.fn_trait_ref {
traits.entry((trait_ref, ty::ImplPolarity::Positive)).or_default();
traits.entry((trait_ref, ty::PredicatePolarity::Positive)).or_default();
}
}
}
@ -1114,7 +1114,7 @@ fn pretty_print_opaque_impl_type(
self.wrap_binder(&trait_ref, |trait_ref, cx| {
define_scoped_cx!(cx);
if polarity == ty::ImplPolarity::Negative {
if polarity == ty::PredicatePolarity::Negative {
p!("!");
}
p!(print(trait_ref.print_only_trait_name()));
@ -1223,10 +1223,10 @@ fn pretty_print_opaque_impl_type(
fn insert_trait_and_projection(
&mut self,
trait_ref: ty::PolyTraitRef<'tcx>,
polarity: ty::ImplPolarity,
polarity: ty::PredicatePolarity,
proj_ty: Option<(DefId, ty::Binder<'tcx, Term<'tcx>>)>,
traits: &mut FxIndexMap<
(ty::PolyTraitRef<'tcx>, ty::ImplPolarity),
(ty::PolyTraitRef<'tcx>, ty::PredicatePolarity),
FxIndexMap<DefId, ty::Binder<'tcx, Term<'tcx>>>,
>,
fn_traits: &mut FxIndexMap<ty::PolyTraitRef<'tcx>, OpaqueFnEntry<'tcx>>,
@ -1236,7 +1236,7 @@ fn insert_trait_and_projection(
// If our trait_ref is FnOnce or any of its children, project it onto the parent FnOnce
// super-trait ref and record it there.
// We skip negative Fn* bounds since they can't use parenthetical notation anyway.
if polarity == ty::ImplPolarity::Positive
if polarity == ty::PredicatePolarity::Positive
&& let Some(fn_once_trait) = self.tcx().lang_items().fn_once_trait()
{
// If we have a FnOnce, then insert it into
@ -3139,7 +3139,7 @@ macro_rules! define_print_and_forward_display {
TraitPredPrintModifiersAndPath<'tcx> {
p!(pretty_print_bound_constness(self.0.trait_ref));
if let ty::ImplPolarity::Negative = self.0.polarity {
if let ty::PredicatePolarity::Negative = self.0.polarity {
p!("!")
}
p!(print(self.0.trait_ref.print_only_trait_path()));
@ -3172,7 +3172,7 @@ macro_rules! define_print_and_forward_display {
ty::TraitPredicate<'tcx> {
p!(print(self.trait_ref.self_ty()), ": ");
p!(pretty_print_bound_constness(self.trait_ref));
if let ty::ImplPolarity::Negative = self.polarity {
if let ty::PredicatePolarity::Negative = self.polarity {
p!("!");
}
p!(print(self.trait_ref.print_trait_sugared()))

View File

@ -769,12 +769,12 @@ fn relate<R: TypeRelation<'tcx>>(
}
}
impl<'tcx> Relate<'tcx> for ty::ImplPolarity {
impl<'tcx> Relate<'tcx> for ty::PredicatePolarity {
fn relate<R: TypeRelation<'tcx>>(
_relation: &mut R,
a: ty::ImplPolarity,
b: ty::ImplPolarity,
) -> RelateResult<'tcx, ty::ImplPolarity> {
a: ty::PredicatePolarity,
b: ty::PredicatePolarity,
) -> RelateResult<'tcx, ty::PredicatePolarity> {
if a != b { Err(TypeError::PolarityMismatch(expected_found(a, b))) } else { Ok(a) }
}
}

View File

@ -719,6 +719,18 @@ fn stable(&self, _: &mut Tables<'_>) -> Self::T {
}
}
impl<'tcx> Stable<'tcx> for ty::PredicatePolarity {
type T = stable_mir::ty::PredicatePolarity;
fn stable(&self, _: &mut Tables<'_>) -> Self::T {
use rustc_middle::ty::PredicatePolarity::*;
match self {
Positive => stable_mir::ty::PredicatePolarity::Positive,
Negative => stable_mir::ty::PredicatePolarity::Negative,
}
}
}
impl<'tcx> Stable<'tcx> for ty::Region<'tcx> {
type T = stable_mir::ty::Region;

View File

@ -55,17 +55,23 @@ fn consider_impl_candidate(
// An upper bound of the certainty of this goal, used to lower the certainty
// of reservation impl to ambiguous during coherence.
let impl_polarity = impl_trait_header.polarity;
let maximal_certainty = match impl_polarity {
ty::ImplPolarity::Positive | ty::ImplPolarity::Negative => {
match impl_polarity == goal.predicate.polarity {
true => Certainty::Yes,
false => return Err(NoSolution),
}
}
ty::ImplPolarity::Reservation => match ecx.solver_mode() {
SolverMode::Normal => return Err(NoSolution),
let maximal_certainty = match (impl_polarity, goal.predicate.polarity) {
// In intercrate mode, this is ambiguous. But outside of intercrate,
// it's not a real impl.
(ty::ImplPolarity::Reservation, _) => match ecx.solver_mode() {
SolverMode::Coherence => Certainty::AMBIGUOUS,
SolverMode::Normal => return Err(NoSolution),
},
// Impl matches polarity
(ty::ImplPolarity::Positive, ty::PredicatePolarity::Positive)
| (ty::ImplPolarity::Negative, ty::PredicatePolarity::Negative) => Certainty::Yes,
// Impl doesn't match polarity
(ty::ImplPolarity::Positive, ty::PredicatePolarity::Negative)
| (ty::ImplPolarity::Negative, ty::PredicatePolarity::Positive) => {
return Err(NoSolution);
}
};
ecx.probe_trait_candidate(CandidateSource::Impl(impl_def_id)).enter(|ecx| {
@ -123,7 +129,7 @@ fn consider_auto_trait_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
if goal.predicate.polarity != ty::ImplPolarity::Positive {
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
return Err(NoSolution);
}
@ -168,7 +174,7 @@ fn consider_trait_alias_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
if goal.predicate.polarity != ty::ImplPolarity::Positive {
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
return Err(NoSolution);
}
@ -191,7 +197,7 @@ fn consider_builtin_sized_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
if goal.predicate.polarity != ty::ImplPolarity::Positive {
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
return Err(NoSolution);
}
@ -205,7 +211,7 @@ fn consider_builtin_copy_clone_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
if goal.predicate.polarity != ty::ImplPolarity::Positive {
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
return Err(NoSolution);
}
@ -219,7 +225,7 @@ fn consider_builtin_pointer_like_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
if goal.predicate.polarity != ty::ImplPolarity::Positive {
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
return Err(NoSolution);
}
@ -251,7 +257,7 @@ fn consider_builtin_fn_ptr_trait_candidate(
let self_ty = goal.predicate.self_ty();
match goal.predicate.polarity {
// impl FnPtr for FnPtr {}
ty::ImplPolarity::Positive => {
ty::PredicatePolarity::Positive => {
if self_ty.is_fn_ptr() {
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
} else {
@ -259,7 +265,7 @@ fn consider_builtin_fn_ptr_trait_candidate(
}
}
// impl !FnPtr for T where T != FnPtr && T is rigid {}
ty::ImplPolarity::Negative => {
ty::PredicatePolarity::Negative => {
// If a type is rigid and not a fn ptr, then we know for certain
// that it does *not* implement `FnPtr`.
if !self_ty.is_fn_ptr() && self_ty.is_known_rigid() {
@ -268,10 +274,6 @@ fn consider_builtin_fn_ptr_trait_candidate(
Err(NoSolution)
}
}
// FIXME: Goal polarity should be split from impl polarity
ty::ImplPolarity::Reservation => {
bug!("we never expect a `Reservation` polarity in a trait goal")
}
}
}
@ -280,7 +282,7 @@ fn consider_builtin_fn_trait_candidates(
goal: Goal<'tcx, Self>,
goal_kind: ty::ClosureKind,
) -> QueryResult<'tcx> {
if goal.predicate.polarity != ty::ImplPolarity::Positive {
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
return Err(NoSolution);
}
@ -316,7 +318,7 @@ fn consider_builtin_async_fn_trait_candidates(
goal: Goal<'tcx, Self>,
goal_kind: ty::ClosureKind,
) -> QueryResult<'tcx> {
if goal.predicate.polarity != ty::ImplPolarity::Positive {
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
return Err(NoSolution);
}
@ -386,7 +388,7 @@ fn consider_builtin_tuple_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
if goal.predicate.polarity != ty::ImplPolarity::Positive {
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
return Err(NoSolution);
}
@ -401,7 +403,7 @@ fn consider_builtin_pointee_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
if goal.predicate.polarity != ty::ImplPolarity::Positive {
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
return Err(NoSolution);
}
@ -412,7 +414,7 @@ fn consider_builtin_future_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
if goal.predicate.polarity != ty::ImplPolarity::Positive {
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
return Err(NoSolution);
}
@ -436,7 +438,7 @@ fn consider_builtin_iterator_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
if goal.predicate.polarity != ty::ImplPolarity::Positive {
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
return Err(NoSolution);
}
@ -460,7 +462,7 @@ fn consider_builtin_fused_iterator_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
if goal.predicate.polarity != ty::ImplPolarity::Positive {
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
return Err(NoSolution);
}
@ -482,7 +484,7 @@ fn consider_builtin_async_iterator_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
if goal.predicate.polarity != ty::ImplPolarity::Positive {
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
return Err(NoSolution);
}
@ -506,7 +508,7 @@ fn consider_builtin_coroutine_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
if goal.predicate.polarity != ty::ImplPolarity::Positive {
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
return Err(NoSolution);
}
@ -537,7 +539,7 @@ fn consider_builtin_discriminant_kind_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
if goal.predicate.polarity != ty::ImplPolarity::Positive {
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
return Err(NoSolution);
}
@ -549,7 +551,7 @@ fn consider_builtin_destruct_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
if goal.predicate.polarity != ty::ImplPolarity::Positive {
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
return Err(NoSolution);
}
@ -564,7 +566,7 @@ fn consider_builtin_transmute_candidate(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> QueryResult<'tcx> {
if goal.predicate.polarity != ty::ImplPolarity::Positive {
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
return Err(NoSolution);
}
@ -601,7 +603,7 @@ fn consider_structural_builtin_unsize_candidates(
ecx: &mut EvalCtxt<'_, 'tcx>,
goal: Goal<'tcx, Self>,
) -> Vec<(CanonicalResponse<'tcx>, BuiltinImplSource)> {
if goal.predicate.polarity != ty::ImplPolarity::Positive {
if goal.predicate.polarity != ty::PredicatePolarity::Positive {
return vec![];
}

View File

@ -8,7 +8,7 @@
use crate::traits::project::ProjectAndUnifyResult;
use rustc_infer::infer::DefineOpaqueTypes;
use rustc_middle::mir::interpret::ErrorHandled;
use rustc_middle::ty::{ImplPolarity, Region, RegionVid};
use rustc_middle::ty::{Region, RegionVid};
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
@ -96,9 +96,9 @@ pub fn find_auto_trait_generics<A>(
ty::TraitPredicate {
trait_ref,
polarity: if polarity {
ImplPolarity::Positive
ty::PredicatePolarity::Positive
} else {
ImplPolarity::Negative
ty::PredicatePolarity::Negative
},
},
));
@ -258,7 +258,7 @@ fn evaluate_predicates(
trait_ref: ty::TraitRef::new(infcx.tcx, trait_did, [ty]),
// Auto traits are positive
polarity: ty::ImplPolarity::Positive,
polarity: ty::PredicatePolarity::Positive,
}));
let computed_preds = param_env.caller_bounds().iter().map(|c| c.as_predicate());
@ -295,7 +295,7 @@ fn evaluate_predicates(
}) = impl_source
{
// Blame 'tidy' for the weird bracket placement.
if infcx.tcx.impl_polarity(*impl_def_id) == ty::ImplPolarity::Negative {
if infcx.tcx.impl_polarity(*impl_def_id) != ty::ImplPolarity::Positive {
debug!(
"evaluate_nested_obligations: found explicit negative impl\
{:?}, bailing out",

View File

@ -206,7 +206,7 @@ fn type_implements_fn_trait(
&self,
param_env: ty::ParamEnv<'tcx>,
ty: ty::Binder<'tcx, Ty<'tcx>>,
polarity: ty::ImplPolarity,
polarity: ty::PredicatePolarity,
) -> Result<(ty::ClosureKind, ty::Binder<'tcx, Ty<'tcx>>), ()> {
self.commit_if_ok(|_| {
for trait_def_id in [

View File

@ -245,7 +245,7 @@ fn suggest_restricting_param_bound(
associated_ty: Option<(&'static str, Ty<'tcx>)>,
mut body_id: LocalDefId,
) {
if trait_pred.skip_binder().polarity == ty::ImplPolarity::Negative {
if trait_pred.skip_binder().polarity != ty::PredicatePolarity::Positive {
return;
}
@ -4057,7 +4057,7 @@ fn look_for_iterator_item_mistakes<G: EmissionGuarantee>(
span,
[*ty],
),
polarity: ty::ImplPolarity::Positive,
polarity: ty::PredicatePolarity::Positive,
});
let Some(generics) = node.generics() else {
continue;
@ -4802,7 +4802,7 @@ pub(super) fn get_explanation_based_on_obligation<'tcx>(
Some(desc) => format!(" {desc}"),
None => String::new(),
};
if let ty::ImplPolarity::Positive = trait_predicate.polarity() {
if let ty::PredicatePolarity::Positive = trait_predicate.polarity() {
format!(
"{pre_message}the trait `{}` is not implemented for{desc} `{}`{post}",
trait_predicate.print_modifiers_and_trait_path(),

View File

@ -1907,7 +1907,7 @@ fn find_similar_impl_candidates(
.all_impls(trait_pred.def_id())
.filter_map(|def_id| {
let imp = self.tcx.impl_trait_header(def_id).unwrap();
if imp.polarity == ty::ImplPolarity::Negative
if imp.polarity != ty::ImplPolarity::Positive
|| !self.tcx.is_user_visible_dep(def_id.krate)
{
return None;

View File

@ -584,7 +584,7 @@ fn virtual_call_violations_for_method<'tcx>(
// implement auto traits if the underlying type does as well.
if let ty::ClauseKind::Trait(ty::TraitPredicate {
trait_ref: pred_trait_ref,
polarity: ty::ImplPolarity::Positive,
polarity: ty::PredicatePolarity::Positive,
}) = pred.kind().skip_binder()
&& pred_trait_ref.self_ty() == tcx.types.self_param
&& tcx.trait_is_auto(pred_trait_ref.def_id)

View File

@ -56,7 +56,7 @@ pub(super) fn assemble_candidates<'o>(
let mut candidates = SelectionCandidateSet { vec: Vec::new(), ambiguous: false };
// Negative trait predicates have different rules than positive trait predicates.
if obligation.polarity() == ty::ImplPolarity::Negative {
if obligation.polarity() == ty::PredicatePolarity::Negative {
self.assemble_candidates_for_trait_alias(obligation, &mut candidates);
self.assemble_candidates_from_impls(obligation, &mut candidates);
self.assemble_candidates_from_caller_bounds(stack, &mut candidates)?;

View File

@ -1460,7 +1460,7 @@ fn confirm_const_destruct_candidate(
cause.span,
[nested_ty.into(), host_effect_param],
),
polarity: ty::ImplPolarity::Positive,
polarity: ty::PredicatePolarity::Positive,
}),
&mut nested,
);
@ -1485,7 +1485,7 @@ fn confirm_const_destruct_candidate(
cause.span,
[nested_ty.into(), host_effect_param],
),
polarity: ty::ImplPolarity::Positive,
polarity: ty::PredicatePolarity::Positive,
});
nested.push(Obligation::with_depth(

View File

@ -1418,10 +1418,13 @@ fn filter_impls(
for candidate in candidates {
if let ImplCandidate(def_id) = candidate {
if ty::ImplPolarity::Reservation == tcx.impl_polarity(def_id)
|| obligation.polarity() == tcx.impl_polarity(def_id)
{
result.push(candidate);
match (tcx.impl_polarity(def_id), obligation.polarity()) {
(ty::ImplPolarity::Reservation, _)
| (ty::ImplPolarity::Positive, ty::PredicatePolarity::Positive)
| (ty::ImplPolarity::Negative, ty::PredicatePolarity::Negative) => {
result.push(candidate);
}
_ => {}
}
} else {
result.push(candidate);

View File

@ -363,7 +363,7 @@ fn compute_trait_pred(&mut self, trait_pred: ty::TraitPredicate<'tcx>, elaborate
// Negative trait predicates don't require supertraits to hold, just
// that their args are WF.
if trait_pred.polarity == ty::ImplPolarity::Negative {
if trait_pred.polarity == ty::PredicatePolarity::Negative {
self.compute_negative_trait_pred(trait_ref);
return;
}

View File

@ -1332,7 +1332,7 @@ pub enum AliasRelationDirection {
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TraitPredicate {
pub trait_ref: TraitRef,
pub polarity: ImplPolarity,
pub polarity: PredicatePolarity,
}
#[derive(Clone, Debug, Eq, PartialEq)]
@ -1354,6 +1354,12 @@ pub enum ImplPolarity {
Reservation,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PredicatePolarity {
Positive,
Negative,
}
pub trait IndexedVal {
fn to_val(index: usize) -> Self;

View File

@ -11,7 +11,7 @@
use rustc_middle::hir::nested_filter;
use rustc_middle::traits::Reveal;
use rustc_middle::ty::{
self, ClauseKind, GenericArgKind, GenericParamDefKind, ImplPolarity, ParamEnv, ToPredicate, TraitPredicate, Ty,
self, ClauseKind, GenericArgKind, GenericParamDefKind, ParamEnv, ToPredicate, TraitPredicate, Ty,
TyCtxt,
};
use rustc_session::declare_lint_pass;
@ -502,7 +502,7 @@ fn param_env_for_derived_eq(tcx: TyCtxt<'_>, did: DefId, eq_trait_id: DefId) ->
params.iter().filter(|&&(_, needs_eq)| needs_eq).map(|&(param, _)| {
ClauseKind::Trait(TraitPredicate {
trait_ref: ty::TraitRef::new(tcx, eq_trait_id, [tcx.mk_param_from_def(param)]),
polarity: ImplPolarity::Positive,
polarity: ty::PredicatePolarity::Positive,
})
.to_predicate(tcx)
}),

View File

@ -9,7 +9,7 @@
use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::{
self, Binder, ClosureArgs, ClosureKind, FnSig, GenericArg, GenericArgKind, ImplPolarity, List, Region, RegionKind,
self, Binder, ClosureArgs, ClosureKind, FnSig, GenericArg, GenericArgKind, List, Region, RegionKind,
Ty, TypeVisitableExt, TypeckResults,
};
use rustc_session::declare_lint_pass;
@ -173,7 +173,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if let Ok((ClosureKind::FnMut, _)) = cx.tcx.infer_ctxt().build().type_implements_fn_trait(
cx.param_env,
Binder::bind_with_vars(callee_ty_adjusted, List::empty()),
ImplPolarity::Positive,
ty::PredicatePolarity::Positive,
) && path_to_local(callee).map_or(false, |l| {
local_used_in(cx, l, args) || local_used_after_expr(cx, l, expr)
}) {

View File

@ -18,7 +18,7 @@
use rustc_middle::mir::Mutability;
use rustc_middle::ty::adjustment::{Adjust, Adjustment, OverloadedDeref};
use rustc_middle::ty::{
self, ClauseKind, GenericArg, GenericArgKind, GenericArgsRef, ImplPolarity, ParamTy, ProjectionPredicate,
self, ClauseKind, GenericArg, GenericArgKind, GenericArgsRef, ParamTy, ProjectionPredicate,
TraitPredicate, Ty,
};
use rustc_span::{sym, Symbol};
@ -669,7 +669,7 @@ fn check_borrow_predicate<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
&& let Some(borrow_id) = cx.tcx.get_diagnostic_item(sym::Borrow)
&& cx.tcx.predicates_of(method_def_id).predicates.iter().any(|(pred, _)| {
if let ClauseKind::Trait(trait_pred) = pred.kind().skip_binder()
&& trait_pred.polarity == ImplPolarity::Positive
&& trait_pred.polarity == ty::PredicatePolarity::Positive
&& trait_pred.trait_ref.def_id == borrow_id
{
true