]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/values.rs
derive various Lift impl instead of hand rolling them
[rust.git] / compiler / rustc_middle / src / values.rs
1 use rustc_middle::ty::{self, AdtSizedConstraint, Ty, TyCtxt};
2 use rustc_query_system::Value;
3
4 impl<'tcx> Value<TyCtxt<'tcx>> for Ty<'_> {
5     fn from_cycle_error(tcx: TyCtxt<'tcx>) -> Self {
6         // SAFETY: This is never called when `Self` is not `Ty<'tcx>`.
7         // FIXME: Represent the above fact in the trait system somehow.
8         unsafe { std::mem::transmute::<Ty<'tcx>, Ty<'_>>(tcx.ty_error()) }
9     }
10 }
11
12 impl<'tcx> Value<TyCtxt<'tcx>> for ty::SymbolName<'_> {
13     fn from_cycle_error(tcx: TyCtxt<'tcx>) -> Self {
14         // SAFETY: This is never called when `Self` is not `SymbolName<'tcx>`.
15         // FIXME: Represent the above fact in the trait system somehow.
16         unsafe {
17             std::mem::transmute::<ty::SymbolName<'tcx>, ty::SymbolName<'_>>(ty::SymbolName::new(
18                 tcx, "<error>",
19             ))
20         }
21     }
22 }
23
24 impl<'tcx> Value<TyCtxt<'tcx>> for AdtSizedConstraint<'_> {
25     fn from_cycle_error(tcx: TyCtxt<'tcx>) -> Self {
26         // SAFETY: This is never called when `Self` is not `AdtSizedConstraint<'tcx>`.
27         // FIXME: Represent the above fact in the trait system somehow.
28         unsafe {
29             std::mem::transmute::<AdtSizedConstraint<'tcx>, AdtSizedConstraint<'_>>(
30                 AdtSizedConstraint(tcx.intern_type_list(&[tcx.ty_error()])),
31             )
32         }
33     }
34 }
35
36 impl<'tcx> Value<TyCtxt<'tcx>> for ty::Binder<'_, ty::FnSig<'_>> {
37     fn from_cycle_error(tcx: TyCtxt<'tcx>) -> Self {
38         let err = tcx.ty_error();
39         // FIXME(compiler-errors): It would be nice if we could get the
40         // query key, so we could at least generate a fn signature that
41         // has the right arity.
42         let fn_sig = ty::Binder::dummy(tcx.mk_fn_sig(
43             [].into_iter(),
44             err,
45             false,
46             rustc_hir::Unsafety::Normal,
47             rustc_target::spec::abi::Abi::Rust,
48         ));
49
50         // SAFETY: This is never called when `Self` is not `ty::Binder<'tcx, ty::FnSig<'tcx>>`.
51         // FIXME: Represent the above fact in the trait system somehow.
52         unsafe { std::mem::transmute::<ty::PolyFnSig<'tcx>, ty::Binder<'_, ty::FnSig<'_>>>(fn_sig) }
53     }
54 }