Skip to content

Commit 1af00cf

Browse files
Uplift TraitPredicate
1 parent 251c1cd commit 1af00cf

File tree

13 files changed

+115
-91
lines changed

13 files changed

+115
-91
lines changed

compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use rustc_hir as hir;
22
use rustc_infer::infer::TyCtxtInferExt;
33
use rustc_macros::{LintDiagnostic, Subdiagnostic};
4-
use rustc_middle::ty::{
5-
self, fold::BottomUpFolder, print::TraitPredPrintModifiersAndPath, Ty, TypeFoldable,
6-
};
4+
use rustc_middle::ty::print::{PrintTraitPredicateExt as _, TraitPredPrintModifiersAndPath};
5+
use rustc_middle::ty::{self, fold::BottomUpFolder, Ty, TypeFoldable};
76
use rustc_session::{declare_lint, declare_lint_pass};
87
use rustc_span::{symbol::kw, Span};
98
use rustc_trait_selection::traits;

compiler/rustc_middle/src/ty/mod.rs

-31
Original file line numberDiff line numberDiff line change
@@ -299,37 +299,6 @@ impl fmt::Display for ImplPolarity {
299299
}
300300
}
301301

302-
/// Polarity for a trait predicate. May either be negative or positive.
303-
/// Distinguished from [`ImplPolarity`] since we never compute goals with
304-
/// "reservation" level.
305-
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable, Debug)]
306-
#[derive(TypeFoldable, TypeVisitable)]
307-
pub enum PredicatePolarity {
308-
/// `Type: Trait`
309-
Positive,
310-
/// `Type: !Trait`
311-
Negative,
312-
}
313-
314-
impl PredicatePolarity {
315-
/// Flips polarity by turning `Positive` into `Negative` and `Negative` into `Positive`.
316-
pub fn flip(&self) -> PredicatePolarity {
317-
match self {
318-
PredicatePolarity::Positive => PredicatePolarity::Negative,
319-
PredicatePolarity::Negative => PredicatePolarity::Positive,
320-
}
321-
}
322-
}
323-
324-
impl fmt::Display for PredicatePolarity {
325-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
326-
match self {
327-
Self::Positive => f.write_str("positive"),
328-
Self::Negative => f.write_str("negative"),
329-
}
330-
}
331-
}
332-
333302
#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable, Debug)]
334303
#[derive(TypeFoldable, TypeVisitable)]
335304
pub enum Asyncness {

compiler/rustc_middle/src/ty/predicate.rs

+2-29
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_hir::def_id::DefId;
55
use rustc_macros::{HashStable, Lift, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
66
use rustc_type_ir::ClauseKind as IrClauseKind;
77
use rustc_type_ir::PredicateKind as IrPredicateKind;
8+
use rustc_type_ir::TraitPredicate as IrTraitPredicate;
89
use rustc_type_ir::TraitRef as IrTraitRef;
910
use std::cmp::Ordering;
1011

@@ -15,6 +16,7 @@ use crate::ty::{
1516
};
1617

1718
pub type TraitRef<'tcx> = IrTraitRef<TyCtxt<'tcx>>;
19+
pub type TraitPredicate<'tcx> = IrTraitPredicate<TyCtxt<'tcx>>;
1820
pub type ClauseKind<'tcx> = IrClauseKind<TyCtxt<'tcx>>;
1921
pub type PredicateKind<'tcx> = IrPredicateKind<TyCtxt<'tcx>>;
2022

@@ -578,37 +580,8 @@ impl<'tcx> Clause<'tcx> {
578580
}
579581
}
580582

581-
#[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
582-
#[derive(HashStable, TypeFoldable, TypeVisitable, Lift)]
583-
pub struct TraitPredicate<'tcx> {
584-
pub trait_ref: TraitRef<'tcx>,
585-
586-
/// If polarity is Positive: we are proving that the trait is implemented.
587-
///
588-
/// If polarity is Negative: we are proving that a negative impl of this trait
589-
/// exists. (Note that coherence also checks whether negative impls of supertraits
590-
/// exist via a series of predicates.)
591-
///
592-
/// If polarity is Reserved: that's a bug.
593-
pub polarity: PredicatePolarity,
594-
}
595-
596583
pub type PolyTraitPredicate<'tcx> = ty::Binder<'tcx, TraitPredicate<'tcx>>;
597584

598-
impl<'tcx> TraitPredicate<'tcx> {
599-
pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self {
600-
Self { trait_ref: self.trait_ref.with_self_ty(tcx, self_ty), ..self }
601-
}
602-
603-
pub fn def_id(self) -> DefId {
604-
self.trait_ref.def_id
605-
}
606-
607-
pub fn self_ty(self) -> Ty<'tcx> {
608-
self.trait_ref.self_ty()
609-
}
610-
}
611-
612585
impl<'tcx> PolyTraitPredicate<'tcx> {
613586
pub fn def_id(self) -> DefId {
614587
// Ok to skip binder since trait `DefId` does not care about regions.

compiler/rustc_middle/src/ty/print/pretty.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -2953,8 +2953,9 @@ impl<'tcx> fmt::Debug for TraitPredPrintModifiersAndPath<'tcx> {
29532953
}
29542954
}
29552955

2956+
#[extension(pub trait PrintTraitPredicateExt<'tcx>)]
29562957
impl<'tcx> ty::TraitPredicate<'tcx> {
2957-
pub fn print_modifiers_and_trait_path(self) -> TraitPredPrintModifiersAndPath<'tcx> {
2958+
fn print_modifiers_and_trait_path(self) -> TraitPredPrintModifiersAndPath<'tcx> {
29582959
TraitPredPrintModifiersAndPath(self)
29592960
}
29602961
}
@@ -3037,6 +3038,15 @@ define_print! {
30373038
p!(write("<{} as {}>", self.self_ty(), self.print_only_trait_path()))
30383039
}
30393040

3041+
ty::TraitPredicate<'tcx> {
3042+
p!(print(self.trait_ref.self_ty()), ": ");
3043+
p!(pretty_print_bound_constness(self.trait_ref));
3044+
if let ty::PredicatePolarity::Negative = self.polarity {
3045+
p!("!");
3046+
}
3047+
p!(print(self.trait_ref.print_trait_sugared()))
3048+
}
3049+
30403050
ty::TypeAndMut<'tcx> {
30413051
p!(write("{}", self.mutbl.prefix_str()), print(self.ty))
30423052
}
@@ -3176,15 +3186,6 @@ define_print_and_forward_display! {
31763186
p!(print(self.b))
31773187
}
31783188

3179-
ty::TraitPredicate<'tcx> {
3180-
p!(print(self.trait_ref.self_ty()), ": ");
3181-
p!(pretty_print_bound_constness(self.trait_ref));
3182-
if let ty::PredicatePolarity::Negative = self.polarity {
3183-
p!("!");
3184-
}
3185-
p!(print(self.trait_ref.print_trait_sugared()))
3186-
}
3187-
31883189
ty::ProjectionPredicate<'tcx> {
31893190
p!(print(self.projection_ty), " == ");
31903191
cx.reset_type_limit();

compiler/rustc_middle/src/ty/structural_impls.rs

-7
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,6 @@ impl fmt::Debug for ty::ParamConst {
158158
}
159159
}
160160

161-
impl<'tcx> fmt::Debug for ty::TraitPredicate<'tcx> {
162-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163-
// FIXME(effects) printing?
164-
write!(f, "TraitPredicate({:?}, polarity:{:?})", self.trait_ref, self.polarity)
165-
}
166-
}
167-
168161
impl<'tcx> fmt::Debug for ty::ProjectionPredicate<'tcx> {
169162
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170163
write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.term)

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ use std::iter;
4545
use crate::infer::InferCtxtExt as _;
4646
use crate::traits::error_reporting::type_err_ctxt_ext::InferCtxtPrivExt;
4747
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
48-
use rustc_middle::ty::print::{with_forced_trimmed_paths, with_no_trimmed_paths};
48+
use rustc_middle::ty::print::{
49+
with_forced_trimmed_paths, with_no_trimmed_paths, PrintTraitPredicateExt as _,
50+
};
4951

5052
use itertools::EitherOrBoth;
5153
use itertools::Itertools;

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ use rustc_middle::ty::abstract_const::NotConstEvaluatable;
3838
use rustc_middle::ty::error::{ExpectedFound, TypeError};
3939
use rustc_middle::ty::fold::{BottomUpFolder, TypeFolder, TypeSuperFoldable};
4040
use rustc_middle::ty::print::{
41-
with_forced_trimmed_paths, FmtPrinter, Print, PrintTraitRefExt as _,
41+
with_forced_trimmed_paths, FmtPrinter, Print, PrintTraitPredicateExt as _,
42+
PrintTraitRefExt as _,
4243
};
4344
use rustc_middle::ty::{
4445
self, SubtypePredicate, ToPolyTraitRef, ToPredicate, TraitRef, Ty, TyCtxt, TypeFoldable,

compiler/rustc_type_ir/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ rustc_ast_ir = { path = "../rustc_ast_ir" }
1111
rustc_data_structures = { path = "../rustc_data_structures", optional = true }
1212
rustc_index = { path = "../rustc_index", default-features = false }
1313
rustc_macros = { path = "../rustc_macros", optional = true }
14-
rustc_type_ir_macros = { path = "../rustc_type_ir_macros" }
1514
rustc_serialize = { path = "../rustc_serialize", optional = true }
1615
rustc_span = { path = "../rustc_span", optional = true }
17-
smallvec = { version = "1.8.1" }
16+
rustc_type_ir_macros = { path = "../rustc_type_ir_macros" }
17+
smallvec = { version = "1.8.1", default-features = false }
1818
# tidy-alphabetical-end
1919

2020
[features]

compiler/rustc_type_ir/src/interner.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use std::hash::Hash;
55
use crate::inherent::*;
66
use crate::ir_print::IrPrint;
77
use crate::visit::{Flags, TypeSuperVisitable, TypeVisitable};
8-
use crate::{CanonicalVarInfo, DebugWithInfcx, TraitRef};
8+
use crate::{CanonicalVarInfo, DebugWithInfcx, TraitPredicate, TraitRef};
99

10-
pub trait Interner: Sized + Copy + IrPrint<TraitRef<Self>> {
10+
pub trait Interner: Sized + Copy + IrPrint<TraitRef<Self>> + IrPrint<TraitPredicate<Self>> {
1111
type DefId: Copy + Debug + Hash + Eq;
1212
type DefiningOpaqueTypes: Copy + Debug + Hash + Default + Eq + TypeVisitable<Self>;
1313
type AdtDef: Copy + Debug + Hash + Eq;

compiler/rustc_type_ir/src/ir_print.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::fmt;
22

3-
use crate::{Interner, TraitRef};
3+
use crate::{Interner, TraitPredicate, TraitRef};
44

55
pub trait IrPrint<T> {
66
fn print(t: &T, fmt: &mut fmt::Formatter<'_>) -> fmt::Result;
@@ -15,14 +15,22 @@ macro_rules! define_display_via_print {
1515
<I as IrPrint<$ty<I>>>::print(self, fmt)
1616
}
1717
}
18+
)*
19+
}
20+
}
1821

22+
macro_rules! define_debug_via_print {
23+
($($ty:ident),+ $(,)?) => {
24+
$(
1925
impl<I: Interner> fmt::Debug for $ty<I> {
2026
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
21-
<I as IrPrint<$ty<I>>>::print_debug(self, fmt)
27+
<I as IrPrint<$ty<I>>>::print(self, fmt)
2228
}
2329
}
2430
)*
2531
}
2632
}
2733

28-
define_display_via_print!(TraitRef,);
34+
define_display_via_print!(TraitRef, TraitPredicate,);
35+
36+
define_debug_via_print!(TraitRef,);

compiler/rustc_type_ir/src/macros.rs

+1
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ TrivialTypeTraversalImpls! {
5353
crate::UniverseIndex,
5454
rustc_ast_ir::Mutability,
5555
rustc_ast_ir::Movability,
56+
crate::PredicatePolarity,
5657
}

compiler/rustc_type_ir/src/trait_ref.rs

+77
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt;
2+
13
use rustc_macros::{HashStable_NoContext, TyDecodable, TyEncodable};
24
use rustc_type_ir_macros::{Lift_Generic, TypeFoldable_Generic, TypeVisitable_Generic};
35

@@ -67,3 +69,78 @@ impl<I: Interner> TraitRef<I> {
6769
self.args.type_at(0)
6870
}
6971
}
72+
73+
#[derive(derivative::Derivative)]
74+
#[derivative(
75+
Clone(bound = ""),
76+
Copy(bound = ""),
77+
Hash(bound = ""),
78+
PartialEq(bound = ""),
79+
Eq(bound = "")
80+
)]
81+
#[derive(TypeVisitable_Generic, TypeFoldable_Generic, Lift_Generic)]
82+
#[cfg_attr(feature = "nightly", derive(TyDecodable, TyEncodable, HashStable_NoContext))]
83+
pub struct TraitPredicate<I: Interner> {
84+
pub trait_ref: TraitRef<I>,
85+
86+
/// If polarity is Positive: we are proving that the trait is implemented.
87+
///
88+
/// If polarity is Negative: we are proving that a negative impl of this trait
89+
/// exists. (Note that coherence also checks whether negative impls of supertraits
90+
/// exist via a series of predicates.)
91+
///
92+
/// If polarity is Reserved: that's a bug.
93+
pub polarity: PredicatePolarity,
94+
}
95+
96+
impl<I: Interner> TraitPredicate<I> {
97+
pub fn with_self_ty(self, interner: I, self_ty: I::Ty) -> Self {
98+
Self { trait_ref: self.trait_ref.with_self_ty(interner, self_ty), polarity: self.polarity }
99+
}
100+
101+
pub fn def_id(self) -> I::DefId {
102+
self.trait_ref.def_id
103+
}
104+
105+
pub fn self_ty(self) -> I::Ty {
106+
self.trait_ref.self_ty()
107+
}
108+
}
109+
110+
impl<I: Interner> fmt::Debug for TraitPredicate<I> {
111+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112+
// FIXME(effects) printing?
113+
write!(f, "TraitPredicate({:?}, polarity:{:?})", self.trait_ref, self.polarity)
114+
}
115+
}
116+
117+
/// Polarity for a trait predicate. May either be negative or positive.
118+
/// Distinguished from [`ImplPolarity`] since we never compute goals with
119+
/// "reservation" level.
120+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
121+
#[cfg_attr(feature = "nightly", derive(TyDecodable, TyEncodable, HashStable_NoContext))]
122+
pub enum PredicatePolarity {
123+
/// `Type: Trait`
124+
Positive,
125+
/// `Type: !Trait`
126+
Negative,
127+
}
128+
129+
impl PredicatePolarity {
130+
/// Flips polarity by turning `Positive` into `Negative` and `Negative` into `Positive`.
131+
pub fn flip(&self) -> PredicatePolarity {
132+
match self {
133+
PredicatePolarity::Positive => PredicatePolarity::Negative,
134+
PredicatePolarity::Negative => PredicatePolarity::Positive,
135+
}
136+
}
137+
}
138+
139+
impl fmt::Display for PredicatePolarity {
140+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141+
match self {
142+
Self::Positive => f.write_str("positive"),
143+
Self::Negative => f.write_str("negative"),
144+
}
145+
}
146+
}

compiler/rustc_type_ir_macros/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use synstructure::decl_derive;
21
use quote::quote;
32
use syn::{parse_quote, visit_mut::VisitMut};
3+
use synstructure::decl_derive;
44

55
decl_derive!(
66
[TypeFoldable_Generic] => type_foldable_derive
@@ -156,4 +156,4 @@ fn type_visitable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::Tok
156156
}
157157
},
158158
)
159-
}
159+
}

0 commit comments

Comments
 (0)