]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/parameterized.rs
Auto merge of #106399 - estebank:type-err-span-label, r=nagisa
[rust.git] / compiler / rustc_middle / src / ty / parameterized.rs
1 use rustc_data_structures::fx::FxHashMap;
2 use rustc_hir::def_id::DefIndex;
3 use rustc_index::vec::{Idx, IndexVec};
4
5 use crate::ty;
6
7 pub trait ParameterizedOverTcx: 'static {
8     type Value<'tcx>;
9 }
10
11 impl<T: ParameterizedOverTcx> ParameterizedOverTcx for &'static [T] {
12     type Value<'tcx> = &'tcx [T::Value<'tcx>];
13 }
14
15 impl<T: ParameterizedOverTcx> ParameterizedOverTcx for Option<T> {
16     type Value<'tcx> = Option<T::Value<'tcx>>;
17 }
18
19 impl<A: ParameterizedOverTcx, B: ParameterizedOverTcx> ParameterizedOverTcx for (A, B) {
20     type Value<'tcx> = (A::Value<'tcx>, B::Value<'tcx>);
21 }
22
23 impl<I: Idx + 'static, T: ParameterizedOverTcx> ParameterizedOverTcx for IndexVec<I, T> {
24     type Value<'tcx> = IndexVec<I, T::Value<'tcx>>;
25 }
26
27 impl<I: 'static, T: ParameterizedOverTcx> ParameterizedOverTcx for FxHashMap<I, T> {
28     type Value<'tcx> = FxHashMap<I, T::Value<'tcx>>;
29 }
30
31 impl<T: ParameterizedOverTcx> ParameterizedOverTcx for ty::Binder<'static, T> {
32     type Value<'tcx> = ty::Binder<'tcx, T::Value<'tcx>>;
33 }
34
35 impl<T: ParameterizedOverTcx> ParameterizedOverTcx for ty::EarlyBinder<T> {
36     type Value<'tcx> = ty::EarlyBinder<T::Value<'tcx>>;
37 }
38
39 #[macro_export]
40 macro_rules! trivially_parameterized_over_tcx {
41     ($($ty:ty),+ $(,)?) => {
42         $(
43             impl $crate::ty::ParameterizedOverTcx for $ty {
44                 #[allow(unused_lifetimes)]
45                 type Value<'tcx> = $ty;
46             }
47         )*
48     }
49 }
50
51 trivially_parameterized_over_tcx! {
52     usize,
53     (),
54     u32,
55     bool,
56     std::string::String,
57     crate::metadata::ModChild,
58     crate::middle::codegen_fn_attrs::CodegenFnAttrs,
59     crate::middle::exported_symbols::SymbolExportInfo,
60     crate::middle::resolve_lifetime::ObjectLifetimeDefault,
61     crate::mir::ConstQualifs,
62     ty::AssocItemContainer,
63     ty::DeducedParamAttrs,
64     ty::Generics,
65     ty::ImplPolarity,
66     ty::ReprOptions,
67     ty::TraitDef,
68     ty::UnusedGenericParams,
69     ty::Visibility<DefIndex>,
70     ty::adjustment::CoerceUnsizedInfo,
71     ty::fast_reject::SimplifiedType,
72     rustc_ast::Attribute,
73     rustc_ast::DelimArgs,
74     rustc_attr::ConstStability,
75     rustc_attr::DefaultBodyStability,
76     rustc_attr::Deprecation,
77     rustc_attr::Stability,
78     rustc_hir::Constness,
79     rustc_hir::Defaultness,
80     rustc_hir::GeneratorKind,
81     rustc_hir::IsAsync,
82     rustc_hir::LangItem,
83     rustc_hir::def::DefKind,
84     rustc_hir::def_id::DefIndex,
85     rustc_hir::definitions::DefKey,
86     rustc_index::bit_set::BitSet<u32>,
87     rustc_index::bit_set::FiniteBitSet<u32>,
88     rustc_session::cstore::ForeignModule,
89     rustc_session::cstore::LinkagePreference,
90     rustc_session::cstore::NativeLib,
91     rustc_span::DebuggerVisualizerFile,
92     rustc_span::ExpnData,
93     rustc_span::ExpnHash,
94     rustc_span::ExpnId,
95     rustc_span::SourceFile,
96     rustc_span::Span,
97     rustc_span::Symbol,
98     rustc_span::def_id::DefPathHash,
99     rustc_span::hygiene::SyntaxContextData,
100     rustc_span::symbol::Ident,
101     rustc_type_ir::Variance,
102 }
103
104 // HACK(compiler-errors): This macro rule can only take a fake path,
105 // not a real, due to parsing ambiguity reasons.
106 #[macro_export]
107 macro_rules! parameterized_over_tcx {
108     ($($($fake_path:ident)::+),+ $(,)?) => {
109         $(
110             impl $crate::ty::ParameterizedOverTcx for $($fake_path)::+<'static> {
111                 type Value<'tcx> = $($fake_path)::+<'tcx>;
112             }
113         )*
114     }
115 }
116
117 parameterized_over_tcx! {
118     crate::middle::exported_symbols::ExportedSymbol,
119     crate::mir::Body,
120     crate::mir::GeneratorLayout,
121     ty::Ty,
122     ty::FnSig,
123     ty::GenericPredicates,
124     ty::TraitRef,
125     ty::Const,
126     ty::Predicate,
127     ty::Clause,
128     ty::GeneratorDiagnosticData,
129 }