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> =
32    rustc_middle::ty::GenericPredicates<'tcx>;
33#[cfg(rapx_ge_100)]
34pub type GenericPredicatesC<'tcx> =
35    rustc_middle::ty::GenericClauses<'tcx>;
36
37/// Fetch generic predicates/clauses for a definition.
38#[cfg(not(rapx_ge_100))]
39#[inline]
40pub fn predicates_of<'tcx>(
41    tcx: TyCtxt<'tcx>,
42    def_id: DefId,
43) -> GenericPredicatesC<'tcx> {
44    tcx.predicates_of(def_id)
45}
46#[cfg(rapx_ge_100)]
47#[inline]
48pub fn predicates_of<'tcx>(
49    tcx: TyCtxt<'tcx>,
50    def_id: DefId,
51) -> GenericPredicatesC<'tcx> {
52    tcx.clauses_of(def_id)
53}
54
55// ── GenericArgsRef::get (Binder wrapping in rustc ≥ 1.99) ─────────────
56
57#[cfg(not(rapx_ge_99))]
58pub fn args_get<'tcx>(
59    args: &rustc_middle::ty::GenericArgsRef<'tcx>,
60    index: usize,
61) -> Option<&'tcx rustc_middle::ty::GenericArg<'tcx>> {
62    args.get(index)
63}
64#[cfg(rapx_ge_99)]
65pub fn args_get<'tcx>(
66    args: &rustc_middle::ty::Binder<
67        'tcx,
68        &'tcx rustc_middle::ty::GenericArgs<'tcx>,
69    >,
70    index: usize,
71) -> Option<rustc_middle::ty::GenericArg<'tcx>> {
72    args.skip_binder().get(index).copied()
73}
74
75// ── skip_norm_wip (rustc_type_ir) ──────────────────────────────────────
76
77/// `TyKind::skip_norm_wip` was introduced to handle the `Wip` alias
78/// variant.  On older toolchains that do not have `AliasRelation::Wip`,
79/// the method is not needed and we provide an identity fallback.
80#[cfg(not(rapx_has_skip_norm_wip))]
81pub trait SkipNormWip: Sized {
82    fn skip_norm_wip(self) -> Self {
83        self
84    }
85}
86#[cfg(not(rapx_has_skip_norm_wip))]
87impl<T: Sized> SkipNormWip for T {}
88
89// ── get_all_attrs (deprecated but still the only way) ───────────────────
90
91use rustc_hir::Attribute;
92
93#[allow(deprecated)]
94#[inline]
95pub fn get_all_attrs<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> &'tcx [Attribute] {
96    tcx.get_all_attrs(def_id)
97}
98
99#[cfg(not(rapx_ge_99))]
100pub fn attribute_to_string<'tcx>(tcx: TyCtxt<'tcx>, attr: &rustc_hir::Attribute) -> String {
101    rustc_hir_pretty::attribute_to_string(&tcx, attr)
102}
103#[cfg(rapx_ge_99)]
104pub fn attribute_to_string(_tcx: TyCtxt<'_>, attr: &rustc_hir::Attribute) -> String {
105    struct Ann;
106    impl rustc_hir_pretty::PpAnn for Ann {}
107    rustc_hir_pretty::attribute_to_string(&Ann, attr)
108}