|
| 1 | +use std::fmt; |
| 2 | + |
| 3 | +use rustc_data_structures::fx::FxHashMap; |
| 4 | +use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdMap}; |
| 5 | + |
| 6 | +use crate::ty::{Predicate, SimplifiedType, TraitRef, Ty}; |
| 7 | + |
| 8 | +/// A map for the local crate mapping each type to a vector of its |
| 9 | +/// inherent impls. This is not meant to be used outside of coherence; |
| 10 | +/// rather, you should request the vector for a specific type via |
| 11 | +/// `tcx.inherent_impls(def_id)` so as to minimize your dependencies |
| 12 | +/// (constructing this map requires touching the entire crate). |
| 13 | +#[derive(Clone, Debug, Default, HashStable)] |
| 14 | +pub struct CrateInherentImpls { |
| 15 | + pub inherent_impls: LocalDefIdMap<Vec<DefId>>, |
| 16 | + pub incoherent_impls: FxHashMap<SimplifiedType, Vec<LocalDefId>>, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Debug, PartialEq, Eq)] |
| 20 | +pub enum ImplOverlapKind { |
| 21 | + /// These impls are always allowed to overlap. |
| 22 | + Permitted { |
| 23 | + /// Whether or not the impl is permitted due to the trait being a `#[marker]` trait |
| 24 | + marker: bool, |
| 25 | + }, |
| 26 | + /// These impls are allowed to overlap, but that raises |
| 27 | + /// an issue #33140 future-compatibility warning. |
| 28 | + /// |
| 29 | + /// Some background: in Rust 1.0, the trait-object types `Send + Sync` (today's |
| 30 | + /// `dyn Send + Sync`) and `Sync + Send` (now `dyn Sync + Send`) were different. |
| 31 | + /// |
| 32 | + /// The widely-used version 0.1.0 of the crate `traitobject` had accidentally relied |
| 33 | + /// that difference, making what reduces to the following set of impls: |
| 34 | + /// |
| 35 | + /// ```compile_fail,(E0119) |
| 36 | + /// trait Trait {} |
| 37 | + /// impl Trait for dyn Send + Sync {} |
| 38 | + /// impl Trait for dyn Sync + Send {} |
| 39 | + /// ``` |
| 40 | + /// |
| 41 | + /// Obviously, once we made these types be identical, that code causes a coherence |
| 42 | + /// error and a fairly big headache for us. However, luckily for us, the trait |
| 43 | + /// `Trait` used in this case is basically a marker trait, and therefore having |
| 44 | + /// overlapping impls for it is sound. |
| 45 | + /// |
| 46 | + /// To handle this, we basically regard the trait as a marker trait, with an additional |
| 47 | + /// future-compatibility warning. To avoid accidentally "stabilizing" this feature, |
| 48 | + /// it has the following restrictions: |
| 49 | + /// |
| 50 | + /// 1. The trait must indeed be a marker-like trait (i.e., no items), and must be |
| 51 | + /// positive impls. |
| 52 | + /// 2. The trait-ref of both impls must be equal. |
| 53 | + /// 3. The trait-ref of both impls must be a trait object type consisting only of |
| 54 | + /// marker traits. |
| 55 | + /// 4. Neither of the impls can have any where-clauses. |
| 56 | + /// |
| 57 | + /// Once `traitobject` 0.1.0 is no longer an active concern, this hack can be removed. |
| 58 | + Issue33140, |
| 59 | +} |
| 60 | + |
| 61 | +/// The "header" of an impl is everything outside the body: a Self type, a trait |
| 62 | +/// ref (in the case of a trait impl), and a set of predicates (from the |
| 63 | +/// bounds / where-clauses). |
| 64 | +#[derive(Clone, Debug, TypeFoldable, TypeVisitable)] |
| 65 | +pub struct ImplHeader<'tcx> { |
| 66 | + pub impl_def_id: DefId, |
| 67 | + pub self_ty: Ty<'tcx>, |
| 68 | + pub trait_ref: Option<TraitRef<'tcx>>, |
| 69 | + pub predicates: Vec<Predicate<'tcx>>, |
| 70 | +} |
| 71 | + |
| 72 | +#[derive(Copy, Clone, PartialEq, Eq, Debug, TypeFoldable, TypeVisitable)] |
| 73 | +pub enum ImplSubject<'tcx> { |
| 74 | + Trait(TraitRef<'tcx>), |
| 75 | + Inherent(Ty<'tcx>), |
| 76 | +} |
| 77 | + |
| 78 | +#[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable, Debug)] |
| 79 | +#[derive(TypeFoldable, TypeVisitable)] |
| 80 | +pub enum ImplPolarity { |
| 81 | + /// `impl Trait for Type` |
| 82 | + Positive, |
| 83 | + /// `impl !Trait for Type` |
| 84 | + Negative, |
| 85 | + /// `#[rustc_reservation_impl] impl Trait for Type` |
| 86 | + /// |
| 87 | + /// This is a "stability hack", not a real Rust feature. |
| 88 | + /// See #64631 for details. |
| 89 | + Reservation, |
| 90 | +} |
| 91 | + |
| 92 | +impl ImplPolarity { |
| 93 | + /// Flips polarity by turning `Positive` into `Negative` and `Negative` into `Positive`. |
| 94 | + pub fn flip(&self) -> Option<ImplPolarity> { |
| 95 | + match self { |
| 96 | + ImplPolarity::Positive => Some(ImplPolarity::Negative), |
| 97 | + ImplPolarity::Negative => Some(ImplPolarity::Positive), |
| 98 | + ImplPolarity::Reservation => None, |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl fmt::Display for ImplPolarity { |
| 104 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 105 | + match self { |
| 106 | + Self::Positive => f.write_str("positive"), |
| 107 | + Self::Negative => f.write_str("negative"), |
| 108 | + Self::Reservation => f.write_str("reservation"), |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments