]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_middle/src/ty/query/values.rs
New upstream version 1.51.0+dfsg1
[rustc.git] / compiler / rustc_middle / src / ty / query / values.rs
CommitLineData
f035d41b 1use crate::ty::{self, AdtSizedConstraint, Ty, TyCtxt, TyS};
ea8adc8c 2
ea8adc8c 3pub(super) trait Value<'tcx>: Sized {
dc9dc135 4 fn from_cycle_error(tcx: TyCtxt<'tcx>) -> Self;
ea8adc8c
XL
5}
6
7impl<'tcx, T> Value<'tcx> for T {
dc9dc135 8 default fn from_cycle_error(tcx: TyCtxt<'tcx>) -> T {
ea8adc8c
XL
9 tcx.sess.abort_if_errors();
10 bug!("Value::from_cycle_error called without errors");
11 }
12}
13
f035d41b
XL
14impl<'tcx> Value<'tcx> for &'_ TyS<'_> {
15 fn from_cycle_error(tcx: TyCtxt<'tcx>) -> Self {
16 // SAFETY: This is never called when `Self` is not `Ty<'tcx>`.
17 // FIXME: Represent the above fact in the trait system somehow.
18 unsafe { std::mem::transmute::<Ty<'tcx>, Ty<'_>>(tcx.ty_error()) }
ea8adc8c
XL
19 }
20}
21
3dfed10e
XL
22impl<'tcx> Value<'tcx> for ty::SymbolName<'_> {
23 fn from_cycle_error(tcx: TyCtxt<'tcx>) -> Self {
24 // SAFETY: This is never called when `Self` is not `SymbolName<'tcx>`.
25 // FIXME: Represent the above fact in the trait system somehow.
26 unsafe {
27 std::mem::transmute::<ty::SymbolName<'tcx>, ty::SymbolName<'_>>(ty::SymbolName::new(
28 tcx, "<error>",
29 ))
30 }
ea8adc8c
XL
31 }
32}
33
f035d41b 34impl<'tcx> Value<'tcx> for AdtSizedConstraint<'_> {
dc9dc135 35 fn from_cycle_error(tcx: TyCtxt<'tcx>) -> Self {
f035d41b
XL
36 // SAFETY: This is never called when `Self` is not `AdtSizedConstraint<'tcx>`.
37 // FIXME: Represent the above fact in the trait system somehow.
38 unsafe {
39 std::mem::transmute::<AdtSizedConstraint<'tcx>, AdtSizedConstraint<'_>>(
40 AdtSizedConstraint(tcx.intern_type_list(&[tcx.ty_error()])),
41 )
42 }
9fa01778
XL
43 }
44}