Skip to main content

rapx/
compat.rs

1/// Centralized compatibility shims for compiler API differences across nightly
2/// versions.
3///
4/// When a type or function moves between modules or changes interface across
5/// rustc versions, add a re-export here (gated by `#[cfg]`).  All other
6/// modules import from this crate instead of writing their own `#[cfg]` blocks.
7///
8/// # How to add a new compat item
9///
10/// 1. Add a cfg flag in `build.rs` (e.g. `rustc_foo_moved`).
11/// 2. Register it with `emit_check_cfg` in `build.rs`.
12/// 3. Add the re-export below.
13/// 4. Update call-sites to `use crate::compat::Foo;`.
14pub use rustc_hash::FxHashMap;
15pub use rustc_hash::FxHashSet;
16
17/// `Spanned` was moved from `rustc_span::source_map` to `rustc_span` root
18/// in rustc 1.97.
19#[cfg(rapx_ge_99)]
20pub use rustc_span::Spanned;
21#[cfg(not(rapx_ge_99))]
22pub use rustc_span::source_map::Spanned;
23
24// ── predicates_of / clauses_of (rustc ≥ 1.100) ────────────────────────
25
26use rustc_hir::def_id::DefId;
27use rustc_middle::ty::TyCtxt;
28
29/// Type alias for the return type of `predicates_of`.
30#[cfg(not(rapx_ge_100))]
31pub type GenericPredicatesC<'tcx> = rustc_middle::ty::GenericPredicates<'tcx>;
32#[cfg(rapx_ge_100)]
33pub type GenericPredicatesC<'tcx> = rustc_middle::ty::GenericClauses<'tcx>;
34
35/// Fetch generic predicates/clauses for a definition.
36#[cfg(not(rapx_ge_100))]
37#[inline]
38pub fn predicates_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> GenericPredicatesC<'tcx> {
39    tcx.predicates_of(def_id)
40}
41#[cfg(rapx_ge_100)]
42#[inline]
43pub fn predicates_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> GenericPredicatesC<'tcx> {
44    tcx.clauses_of(def_id)
45}
46
47// ── GenericArgsRef::get (Binder wrapping in rustc ≥ 1.99) ─────────────
48
49#[cfg(not(rapx_ge_99))]
50pub fn args_get<'tcx>(
51    args: &rustc_middle::ty::GenericArgsRef<'tcx>,
52    index: usize,
53) -> Option<&'tcx rustc_middle::ty::GenericArg<'tcx>> {
54    args.get(index)
55}
56#[cfg(rapx_ge_99)]
57pub fn args_get<'tcx>(
58    args: &rustc_middle::ty::Binder<'tcx, &'tcx rustc_middle::ty::GenericArgs<'tcx>>,
59    index: usize,
60) -> Option<rustc_middle::ty::GenericArg<'tcx>> {
61    args.skip_binder().get(index).copied()
62}
63
64// ── skip_norm_wip (rustc_type_ir) ──────────────────────────────────────
65
66/// `TyKind::skip_norm_wip` was introduced to handle the `Wip` alias
67/// variant.  On older toolchains that do not have `AliasRelation::Wip`,
68/// the method is not needed and we provide an identity fallback.
69#[cfg(not(rapx_has_skip_norm_wip))]
70pub trait SkipNormWip: Sized {
71    fn skip_norm_wip(self) -> Self {
72        self
73    }
74}
75#[cfg(not(rapx_has_skip_norm_wip))]
76impl<T: Sized> SkipNormWip for T {}
77
78// ── get_all_attrs (deprecated but still the only way) ───────────────────
79
80#[cfg(rapx_has_attr_ir)]
81use rustc_attr_ir::Attribute;
82#[cfg(not(rapx_has_attr_ir))]
83use rustc_hir::Attribute;
84
85#[allow(deprecated)]
86#[inline]
87pub fn get_all_attrs<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx [Attribute] {
88    tcx.get_all_attrs(def_id)
89}
90
91#[cfg(not(rapx_ge_99))]
92pub fn attribute_to_string<'tcx>(tcx: TyCtxt<'tcx>, attr: &Attribute) -> String {
93    rustc_hir_pretty::attribute_to_string(&tcx, attr)
94}
95#[cfg(rapx_ge_99)]
96pub fn attribute_to_string(_tcx: TyCtxt<'_>, attr: &Attribute) -> String {
97    struct Ann;
98    impl rustc_hir_pretty::PpAnn for Ann {}
99    rustc_hir_pretty::attribute_to_string(&Ann, attr)
100}