1use std::fmt::{self, Debug, Display, Formatter};
2
3use rustc_abi::{HasDataLayout, Size};
4use rustc_hir::def::DefKind;
5use rustc_hir::def_id::DefId;
6use rustc_macros::{Lift, StableHash, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
7use rustc_span::{DUMMY_SP, RemapPathScopeComponents, Span, Symbol, bug};
8use rustc_type_ir::TypeVisitableExt;
9
10use super::interpret::ReportedErrorInfo;
11use crate::mir::interpret::{AllocId, AllocRange, ErrorHandled, GlobalAlloc, Scalar, alloc_range};
12use crate::mir::{Promoted, pretty_print_const_value};
13use crate::ty::print::{pretty_print_const, with_no_trimmed_paths};
14use crate::ty::{self, ConstKind, GenericArgsRef, ScalarInt, Ty, TyCtxt};
15
16#[derive(#[automatically_derived]impl<'tcx> ::core::marker::Copy for ConstAlloc<'tcx> {
}Copy, #[automatically_derived]#[doc(hidden)]unsafe impl<'tcx>
::core::clone::TrivialClone for ConstAlloc<'tcx> {
}
#[automatically_derived]impl<'tcx> ::core::clone::Clone for ConstAlloc<'tcx> {
#[inline]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<AllocId>;
let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
*self
}
}Clone, const _: () =
{
impl<'tcx> ::rustc_data_structures::stable_hash::StableHash for
ConstAlloc<'tcx> {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
ConstAlloc { alloc_id: ref __binding_0, ty: ref __binding_1
} => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for ConstAlloc<'tcx> {
fn encode(&self, __encoder: &mut __E) {
let ConstAlloc {
alloc_id: ref __binding_0, ty: ref __binding_1 } = *self;
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
}
};TyEncodable, const _: () =
{
impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for ConstAlloc<'tcx> {
fn decode(__decoder: &mut __D) -> Self {
ConstAlloc {
alloc_id: ::rustc_serialize::Decodable::decode(__decoder),
ty: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};TyDecodable, #[automatically_derived]impl<'tcx> ::core::fmt::Debug for ConstAlloc<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "ConstAlloc",
"alloc_id", &self.alloc_id, "ty", &&self.ty)
}
}Debug, #[automatically_derived]impl<'tcx> ::core::cmp::Eq for ConstAlloc<'tcx> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<AllocId>;
let _: ::core::cmp::AssertParamIsEq<Ty<'tcx>>;
}
}Eq, #[automatically_derived]impl<'tcx> ::core::marker::StructuralPartialEq for
ConstAlloc<'tcx> {
}
#[automatically_derived]impl<'tcx> ::core::cmp::PartialEq for ConstAlloc<'tcx>
{
#[inline]
fn eq(&self, other: &Self) -> bool {
self.alloc_id == other.alloc_id && self.ty == other.ty
}
}PartialEq)]
23pub struct ConstAlloc<'tcx> {
24 pub alloc_id: AllocId,
27 pub ty: Ty<'tcx>,
28}
29
30#[derive(#[automatically_derived]impl ::core::marker::Copy for ConstValue { }Copy, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
for ConstValue {
}
#[automatically_derived]impl ::core::clone::Clone for ConstValue {
#[inline]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<Scalar>;
let _: ::core::clone::AssertParamIsClone<AllocId>;
let _: ::core::clone::AssertParamIsClone<u64>;
let _: ::core::clone::AssertParamIsClone<Size>;
*self
}
}Clone, #[automatically_derived]impl ::core::fmt::Debug for ConstValue {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Self::Scalar(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f, "Scalar",
&__self_0),
Self::ZeroSized =>
::core::fmt::Formatter::write_str(f, "ZeroSized"),
Self::Slice { alloc_id: __self_0, meta: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f, "Slice",
"alloc_id", __self_0, "meta", &__self_1),
Self::Indirect { alloc_id: __self_0, offset: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"Indirect", "alloc_id", __self_0, "offset", &__self_1),
}
}
}Debug, #[automatically_derived]impl ::core::cmp::Eq for ConstValue {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<Scalar>;
let _: ::core::cmp::AssertParamIsEq<AllocId>;
let _: ::core::cmp::AssertParamIsEq<u64>;
let _: ::core::cmp::AssertParamIsEq<Size>;
}
}Eq, #[automatically_derived]impl ::core::marker::StructuralPartialEq for
ConstValue {
}
#[automatically_derived]impl ::core::cmp::PartialEq for ConstValue {
#[inline]
fn eq(&self, other: &Self) -> bool {
::core::intrinsics::discriminant_value(self) ==
::core::intrinsics::discriminant_value(other) &&
match (self, other) {
(Self::Scalar(__self_0), Self::Scalar(__arg1_0)) =>
__self_0 == __arg1_0,
(Self::Slice { alloc_id: __self_0, meta: __self_1 },
Self::Slice { alloc_id: __arg1_0, meta: __arg1_1 }) =>
__self_1 == __arg1_1 && __self_0 == __arg1_0,
(Self::Indirect { alloc_id: __self_0, offset: __self_1 },
Self::Indirect { alloc_id: __arg1_0, offset: __arg1_1 }) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
_ => true,
}
}
}PartialEq, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for ConstValue {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
ConstValue::Scalar(ref __binding_0) => { 0usize }
ConstValue::ZeroSized => { 1usize }
ConstValue::Slice {
alloc_id: ref __binding_0, meta: ref __binding_1 } => {
2usize
}
ConstValue::Indirect {
alloc_id: ref __binding_0, offset: ref __binding_1 } => {
3usize
}
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
ConstValue::Scalar(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
ConstValue::ZeroSized => {}
ConstValue::Slice {
alloc_id: ref __binding_0, meta: ref __binding_1 } => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
ConstValue::Indirect {
alloc_id: ref __binding_0, offset: ref __binding_1 } => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
}
}
}
};TyEncodable, const _: () =
{
impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for ConstValue {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => {
ConstValue::Scalar(::rustc_serialize::Decodable::decode(__decoder))
}
1usize => { ConstValue::ZeroSized }
2usize => {
ConstValue::Slice {
alloc_id: ::rustc_serialize::Decodable::decode(__decoder),
meta: ::rustc_serialize::Decodable::decode(__decoder),
}
}
3usize => {
ConstValue::Indirect {
alloc_id: ::rustc_serialize::Decodable::decode(__decoder),
offset: ::rustc_serialize::Decodable::decode(__decoder),
}
}
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `ConstValue`, expected 0..4, actual {0}",
n));
}
}
}
}
};TyDecodable, #[automatically_derived]impl ::core::hash::Hash for ConstValue {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
state);
match self {
Self::Scalar(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
Self::Slice { alloc_id: __self_0, meta: __self_1 } => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state)
}
Self::Indirect { alloc_id: __self_0, offset: __self_1 } => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state)
}
_ => {}
}
}
}Hash)]
33#[derive(const _: () =
{
impl ::rustc_data_structures::stable_hash::StableHash for ConstValue {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
match *self {
ConstValue::Scalar(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
ConstValue::ZeroSized => {}
ConstValue::Slice {
alloc_id: ref __binding_0, meta: ref __binding_1 } => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
ConstValue::Indirect {
alloc_id: ref __binding_0, offset: ref __binding_1 } => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash)]
34pub enum ConstValue {
35 Scalar(Scalar),
39
40 ZeroSized,
42
43 Slice {
50 alloc_id: AllocId,
53 meta: u64,
56 },
57
58 Indirect {
62 alloc_id: AllocId,
69 offset: Size,
71 },
72}
73
74#[cfg(target_pointer_width = "64")]
75const _: [(); 24] = [(); ::std::mem::size_of::<ConstValue>()];rustc_data_structures::static_assert_size!(ConstValue, 24);
76
77impl ConstValue {
78 #[inline]
79 pub fn try_to_scalar(&self) -> Option<Scalar> {
80 match *self {
81 ConstValue::Indirect { .. } | ConstValue::Slice { .. } | ConstValue::ZeroSized => None,
82 ConstValue::Scalar(val) => Some(val),
83 }
84 }
85
86 pub fn try_to_scalar_int(&self) -> Option<ScalarInt> {
87 self.try_to_scalar()?.try_to_scalar_int().ok()
88 }
89
90 pub fn try_to_bits(&self, size: Size) -> Option<u128> {
91 Some(self.try_to_scalar_int()?.to_bits(size))
92 }
93
94 pub fn try_to_bool(&self) -> Option<bool> {
95 self.try_to_scalar_int()?.try_into().ok()
96 }
97
98 pub fn try_to_target_usize(&self, tcx: TyCtxt<'_>) -> Option<u64> {
99 Some(self.try_to_scalar_int()?.to_target_usize(tcx))
100 }
101
102 pub fn try_to_bits_for_ty<'tcx>(
103 &self,
104 tcx: TyCtxt<'tcx>,
105 typing_env: ty::TypingEnv<'tcx>,
106 ty: Ty<'tcx>,
107 ) -> Option<u128> {
108 let size = tcx.layout_of(typing_env.as_query_input(ty)).ok()?.size;
109 self.try_to_bits(size)
110 }
111
112 pub fn from_bool(b: bool) -> Self {
113 ConstValue::Scalar(Scalar::from_bool(b))
114 }
115
116 pub fn from_u64(i: u64) -> Self {
117 ConstValue::Scalar(Scalar::from_u64(i))
118 }
119
120 pub fn from_u128(i: u128) -> Self {
121 ConstValue::Scalar(Scalar::from_u128(i))
122 }
123
124 pub fn from_target_usize(i: u64, cx: &impl HasDataLayout) -> Self {
125 ConstValue::Scalar(Scalar::from_target_usize(i, cx))
126 }
127
128 pub fn try_get_slice_bytes_for_diagnostics<'tcx>(
130 &self,
131 tcx: TyCtxt<'tcx>,
132 ) -> Option<&'tcx [u8]> {
133 let (alloc_id, start, len) = match self {
134 ConstValue::Scalar(_) | ConstValue::ZeroSized => {
135 ::rustc_span::macros::bug_impl(None,
format_args!("`try_get_slice_bytes` on non-slice constant"),
Location::caller())bug!("`try_get_slice_bytes` on non-slice constant")
136 }
137 &ConstValue::Slice { alloc_id, meta } => (alloc_id, 0, meta),
138 &ConstValue::Indirect { alloc_id, offset } => {
139 let a = tcx.global_alloc(alloc_id).unwrap_memory().inner();
142 let ptr_size = tcx.data_layout.pointer_size();
143 if a.size() < offset + 2 * ptr_size {
144 return None;
146 }
147 let ptr = a
149 .read_scalar(
150 &tcx,
151 alloc_range(offset, ptr_size),
152 true,
153 )
154 .ok()?;
155 let ptr = ptr.to_pointer(&tcx);
156 let len = a
157 .read_scalar(
158 &tcx,
159 alloc_range(offset + ptr_size, ptr_size),
160 false,
161 )
162 .ok()?;
163 let len = len.to_target_usize(&tcx).discard_err()?;
164 if len == 0 {
165 return Some(&[]);
166 }
167 let (inner_prov, offset) =
169 ptr.into_pointer_or_addr().ok()?.prov_and_relative_offset();
170 (inner_prov.alloc_id(), offset.bytes(), len)
171 }
172 };
173
174 let data = tcx.global_alloc(alloc_id).unwrap_memory();
175
176 let start = start.try_into().unwrap();
178 let end = start + usize::try_from(len).unwrap();
179 Some(data.inner().inspect_with_uninit_and_ptr_outside_interpreter(start..end))
180 }
181
182 pub fn all_bytes_uninit(&self, tcx: TyCtxt<'_>) -> bool {
184 let ConstValue::Indirect { alloc_id, .. } = self else {
185 return false;
186 };
187 let alloc = tcx.global_alloc(*alloc_id);
188 let GlobalAlloc::Memory(alloc) = alloc else {
189 return false;
190 };
191 let init_mask = alloc.0.init_mask();
192 let init_range = init_mask.is_range_initialized(AllocRange {
193 start: Size::ZERO,
194 size: Size::from_bytes(alloc.0.len()),
195 });
196 if let Err(range) = init_range {
197 if range.size == alloc.0.size() {
198 return true;
199 }
200 }
201 false
202 }
203}
204
205#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl<'tcx>
::core::clone::TrivialClone for Const<'tcx> {
}
#[automatically_derived]impl<'tcx> ::core::clone::Clone for Const<'tcx> {
#[inline]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
let _: ::core::clone::AssertParamIsClone<ty::Const<'tcx>>;
let _: ::core::clone::AssertParamIsClone<UnevaluatedConst<'tcx>>;
let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
let _: ::core::clone::AssertParamIsClone<ConstValue>;
let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
*self
}
}Clone, #[automatically_derived]impl<'tcx> ::core::marker::Copy for Const<'tcx> { }Copy, #[automatically_derived]impl<'tcx> ::core::marker::StructuralPartialEq for
Const<'tcx> {
}
#[automatically_derived]impl<'tcx> ::core::cmp::PartialEq for Const<'tcx> {
#[inline]
fn eq(&self, other: &Self) -> bool {
::core::intrinsics::discriminant_value(self) ==
::core::intrinsics::discriminant_value(other) &&
match (self, other) {
(Self::Ty(__self_0, __self_1), Self::Ty(__arg1_0, __arg1_1))
=> __self_0 == __arg1_0 && __self_1 == __arg1_1,
(Self::Unevaluated(__self_0, __self_1),
Self::Unevaluated(__arg1_0, __arg1_1)) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
(Self::Val(__self_0, __self_1), Self::Val(__arg1_0, __arg1_1))
=> __self_0 == __arg1_0 && __self_1 == __arg1_1,
_ => unsafe { ::core::intrinsics::unreachable() }
}
}
}PartialEq, #[automatically_derived]impl<'tcx> ::core::cmp::Eq for Const<'tcx> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<Ty<'tcx>>;
let _: ::core::cmp::AssertParamIsEq<ty::Const<'tcx>>;
let _: ::core::cmp::AssertParamIsEq<UnevaluatedConst<'tcx>>;
let _: ::core::cmp::AssertParamIsEq<Ty<'tcx>>;
let _: ::core::cmp::AssertParamIsEq<ConstValue>;
let _: ::core::cmp::AssertParamIsEq<Ty<'tcx>>;
}
}Eq, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for Const<'tcx> {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
Const::Ty(ref __binding_0, ref __binding_1) => { 0usize }
Const::Unevaluated(ref __binding_0, ref __binding_1) => {
1usize
}
Const::Val(ref __binding_0, ref __binding_1) => { 2usize }
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
Const::Ty(ref __binding_0, ref __binding_1) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
Const::Unevaluated(ref __binding_0, ref __binding_1) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
Const::Val(ref __binding_0, ref __binding_1) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
}
}
}
};TyEncodable, const _: () =
{
impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for Const<'tcx> {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => {
Const::Ty(::rustc_serialize::Decodable::decode(__decoder),
::rustc_serialize::Decodable::decode(__decoder))
}
1usize => {
Const::Unevaluated(::rustc_serialize::Decodable::decode(__decoder),
::rustc_serialize::Decodable::decode(__decoder))
}
2usize => {
Const::Val(::rustc_serialize::Decodable::decode(__decoder),
::rustc_serialize::Decodable::decode(__decoder))
}
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `Const`, expected 0..3, actual {0}",
n));
}
}
}
}
};TyDecodable, #[automatically_derived]impl<'tcx> ::core::hash::Hash for Const<'tcx> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&::core::intrinsics::discriminant_value(self),
state);
match self {
Self::Ty(__self_0, __self_1) => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state)
}
Self::Unevaluated(__self_0, __self_1) => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state)
}
Self::Val(__self_0, __self_1) => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state)
}
}
}
}Hash, const _: () =
{
impl<'tcx> ::rustc_data_structures::stable_hash::StableHash for
Const<'tcx> {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
match *self {
Const::Ty(ref __binding_0, ref __binding_1) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
Const::Unevaluated(ref __binding_0, ref __binding_1) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
Const::Val(ref __binding_0, ref __binding_1) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash, #[automatically_derived]impl<'tcx> ::core::fmt::Debug for Const<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Self::Ty(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f, "Ty",
__self_0, &__self_1),
Self::Unevaluated(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"Unevaluated", __self_0, &__self_1),
Self::Val(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f, "Val",
__self_0, &__self_1),
}
}
}Debug)]
209#[derive(const _: () =
{
impl<'tcx>
::rustc_middle::ty::TypeFoldable<::rustc_middle::ty::TyCtxt<'tcx>>
for Const<'tcx> {
fn try_fold_with<__F: ::rustc_middle::ty::FallibleTypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
__folder: &mut __F) -> Result<Self, __F::Error> {
Ok(match self {
Const::Ty(__binding_0, __binding_1) => {
Const::Ty(::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_0,
__folder)?,
::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_1,
__folder)?)
}
Const::Unevaluated(__binding_0, __binding_1) => {
Const::Unevaluated(::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_0,
__folder)?,
::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_1,
__folder)?)
}
Const::Val(__binding_0, __binding_1) => {
Const::Val(::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_0,
__folder)?,
::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_1,
__folder)?)
}
})
}
fn fold_with<__F: ::rustc_middle::ty::TypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
__folder: &mut __F) -> Self {
match self {
Const::Ty(__binding_0, __binding_1) => {
Const::Ty(::rustc_middle::ty::TypeFoldable::fold_with(__binding_0,
__folder),
::rustc_middle::ty::TypeFoldable::fold_with(__binding_1,
__folder))
}
Const::Unevaluated(__binding_0, __binding_1) => {
Const::Unevaluated(::rustc_middle::ty::TypeFoldable::fold_with(__binding_0,
__folder),
::rustc_middle::ty::TypeFoldable::fold_with(__binding_1,
__folder))
}
Const::Val(__binding_0, __binding_1) => {
Const::Val(::rustc_middle::ty::TypeFoldable::fold_with(__binding_0,
__folder),
::rustc_middle::ty::TypeFoldable::fold_with(__binding_1,
__folder))
}
}
}
}
};TypeFoldable, const _: () =
{
impl<'tcx>
::rustc_middle::ty::TypeVisitable<::rustc_middle::ty::TyCtxt<'tcx>>
for Const<'tcx> {
fn visit_with<__V: ::rustc_middle::ty::TypeVisitor<::rustc_middle::ty::TyCtxt<'tcx>>>(&self,
__visitor: &mut __V) -> __V::Result {
match *self {
Const::Ty(ref __binding_0, ref __binding_1) => {
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_0,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_1,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
}
Const::Unevaluated(ref __binding_0, ref __binding_1) => {
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_0,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_1,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
}
Const::Val(ref __binding_0, ref __binding_1) => {
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_0,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_1,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
}
}
<__V::Result as ::rustc_middle::ty::VisitorResult>::output()
}
}
};TypeVisitable, const _: () =
{
impl<'tcx, '__lifted>
::rustc_middle::ty::Lift<::rustc_middle::ty::TyCtxt<'__lifted>>
for Const<'tcx> {
type Lifted = Const<'__lifted>;
fn lift_to_interner(self,
__tcx: ::rustc_middle::ty::TyCtxt<'__lifted>)
-> Const<'__lifted> {
match self {
Const::Ty(__binding_0, __binding_1) => {
Const::Ty(__tcx.lift(__binding_0), __tcx.lift(__binding_1))
}
Const::Unevaluated(__binding_0, __binding_1) => {
Const::Unevaluated(__tcx.lift(__binding_0),
__tcx.lift(__binding_1))
}
Const::Val(__binding_0, __binding_1) => {
Const::Val(__tcx.lift(__binding_0), __tcx.lift(__binding_1))
}
}
}
}
};Lift)]
210pub enum Const<'tcx> {
211 Ty(Ty<'tcx>, ty::Const<'tcx>),
219
220 Unevaluated(UnevaluatedConst<'tcx>, Ty<'tcx>),
227
228 Val(ConstValue, Ty<'tcx>),
231}
232
233impl<'tcx> Const<'tcx> {
234 pub fn from_unevaluated(
237 tcx: TyCtxt<'tcx>,
238 def_id: DefId,
239 ) -> ty::EarlyBinder<'tcx, Const<'tcx>> {
240 ty::EarlyBinder::bind(
241 tcx,
242 Const::Unevaluated(
243 UnevaluatedConst {
244 def: def_id,
245 args: ty::GenericArgs::identity_for_item(tcx, def_id),
246 promoted: None,
247 },
248 tcx.type_of(def_id).skip_binder(),
249 ),
250 )
251 }
252
253 #[inline(always)]
254 pub fn ty(&self) -> Ty<'tcx> {
255 match self {
256 Const::Ty(ty, ct) => {
257 match ct.kind() {
258 ty::ConstKind::Value(cv) => cv.ty,
262 _ => *ty,
263 }
264 }
265 Const::Val(_, ty) | Const::Unevaluated(_, ty) => *ty,
266 }
267 }
268
269 #[inline]
272 pub fn is_required_const(&self) -> bool {
273 match self {
274 Const::Ty(_, c) => match c.kind() {
275 ty::ConstKind::Value(_) => false, _ => true,
277 },
278 Const::Val(..) => false, Const::Unevaluated(..) => true,
280 }
281 }
282
283 #[inline]
284 pub fn try_to_scalar(self) -> Option<Scalar> {
285 match self {
286 Const::Ty(_, c) => c.try_to_scalar(),
287 Const::Val(val, _) => val.try_to_scalar(),
288 Const::Unevaluated(..) => None,
289 }
290 }
291
292 #[inline]
293 pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
294 match self {
296 Const::Val(ConstValue::Scalar(Scalar::Int(x)), _) => Some(x),
297 Const::Ty(_, c) => c.try_to_leaf(),
298 _ => None,
299 }
300 }
301
302 #[inline]
303 pub fn try_to_bits(self, size: Size) -> Option<u128> {
304 Some(self.try_to_scalar_int()?.to_bits(size))
305 }
306
307 #[inline]
308 pub fn try_to_bool(self) -> Option<bool> {
309 self.try_to_scalar_int()?.try_into().ok()
310 }
311
312 #[inline]
313 pub fn eval(
314 self,
315 tcx: TyCtxt<'tcx>,
316 typing_env: ty::TypingEnv<'tcx>,
317 span: Span,
318 ) -> Result<ConstValue, ErrorHandled> {
319 match self {
320 Const::Ty(_, c) => {
321 if let Err(e) = c.error_reported() {
322 return Err(ReportedErrorInfo::non_const_eval_error(e).into());
323 }
324
325 if c.has_non_region_param() || c.has_non_region_placeholders() {
328 return Err(ErrorHandled::TooGeneric(span));
329 }
330
331 match c.kind() {
332 ConstKind::Value(cv) => Ok(tcx.valtree_to_const_val(cv)),
333 ConstKind::Expr(_) => {
334 ::rustc_span::macros::bug_impl(None,
format_args!("Normalization of `ty::ConstKind::Expr` is unimplemented"),
Location::caller())bug!("Normalization of `ty::ConstKind::Expr` is unimplemented")
335 }
336 _ => Err(ReportedErrorInfo::non_const_eval_error(
337 tcx.dcx().delayed_bug("Unevaluated `ty::Const` in MIR body"),
338 )
339 .into()),
340 }
341 }
342 Const::Unevaluated(uneval, _) => {
343 tcx.const_eval_resolve(typing_env, uneval, span)
345 }
346 Const::Val(val, _ty) => Ok(val),
347 }
348 }
349
350 #[inline]
351 pub fn try_eval_scalar(
352 self,
353 tcx: TyCtxt<'tcx>,
354 typing_env: ty::TypingEnv<'tcx>,
355 ) -> Option<Scalar> {
356 if let Const::Ty(_, c) = self {
357 c.try_to_scalar()
360 } else {
361 self.eval(tcx, typing_env, DUMMY_SP).ok()?.try_to_scalar()
362 }
363 }
364
365 #[inline]
366 pub fn try_eval_scalar_int(
367 self,
368 tcx: TyCtxt<'tcx>,
369 typing_env: ty::TypingEnv<'tcx>,
370 ) -> Option<ScalarInt> {
371 self.try_eval_scalar(tcx, typing_env)?.try_to_scalar_int().ok()
372 }
373
374 #[inline]
375 pub fn try_eval_bits(
376 &self,
377 tcx: TyCtxt<'tcx>,
378 typing_env: ty::TypingEnv<'tcx>,
379 ) -> Option<u128> {
380 let int = self.try_eval_scalar_int(tcx, typing_env)?;
381 let size = tcx.layout_of(typing_env.as_query_input(self.ty())).ok()?.size;
382 Some(int.to_bits(size))
383 }
384
385 #[inline]
387 pub fn eval_bits(self, tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>) -> u128 {
388 self.try_eval_bits(tcx, typing_env)
389 .unwrap_or_else(|| ::rustc_span::macros::bug_impl(None,
format_args!("expected bits of {0:#?}, got {1:#?}", self.ty(), self),
Location::caller())bug!("expected bits of {:#?}, got {:#?}", self.ty(), self))
390 }
391
392 #[inline]
393 pub fn try_eval_target_usize(
394 self,
395 tcx: TyCtxt<'tcx>,
396 typing_env: ty::TypingEnv<'tcx>,
397 ) -> Option<u64> {
398 Some(self.try_eval_scalar_int(tcx, typing_env)?.to_target_usize(tcx))
399 }
400
401 #[inline]
402 pub fn eval_target_usize(self, tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>) -> u64 {
404 self.try_eval_target_usize(tcx, typing_env)
405 .unwrap_or_else(|| ::rustc_span::macros::bug_impl(None,
format_args!("expected usize, got {0:#?}", self), Location::caller())bug!("expected usize, got {:#?}", self))
406 }
407
408 #[inline]
409 pub fn try_eval_bool(self, tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>) -> Option<bool> {
410 self.try_eval_scalar_int(tcx, typing_env)?.try_into().ok()
411 }
412
413 #[inline]
414 pub fn from_value(val: ConstValue, ty: Ty<'tcx>) -> Self {
415 Self::Val(val, ty)
416 }
417
418 #[inline]
419 pub fn from_ty_value(tcx: TyCtxt<'tcx>, val: ty::Value<'tcx>) -> Self {
420 Self::Ty(val.ty, ty::Const::new_value(tcx, val.valtree, val.ty))
421 }
422
423 pub fn from_bits(
424 tcx: TyCtxt<'tcx>,
425 bits: u128,
426 typing_env: ty::TypingEnv<'tcx>,
427 ty: Ty<'tcx>,
428 ) -> Self {
429 let size = tcx
430 .layout_of(typing_env.as_query_input(ty))
431 .unwrap_or_else(|e| ::rustc_span::macros::bug_impl(None,
format_args!("could not compute layout for {0:?}: {1:?}", ty, e),
Location::caller())bug!("could not compute layout for {ty:?}: {e:?}"))
432 .size;
433 let cv = ConstValue::Scalar(Scalar::from_uint(bits, size));
434
435 Self::Val(cv, ty)
436 }
437
438 #[inline]
439 pub fn from_bool(tcx: TyCtxt<'tcx>, v: bool) -> Self {
440 let cv = ConstValue::from_bool(v);
441 Self::Val(cv, tcx.types.bool)
442 }
443
444 #[inline]
445 pub fn zero_sized(ty: Ty<'tcx>) -> Self {
446 let cv = ConstValue::ZeroSized;
447 Self::Val(cv, ty)
448 }
449
450 pub fn from_usize(tcx: TyCtxt<'tcx>, n: u64) -> Self {
451 let ty = tcx.types.usize;
452 let typing_env = ty::TypingEnv::fully_monomorphized();
453 Self::from_bits(tcx, n as u128, typing_env, ty)
454 }
455
456 #[inline]
457 pub fn from_scalar(_tcx: TyCtxt<'tcx>, s: Scalar, ty: Ty<'tcx>) -> Self {
458 let val = ConstValue::Scalar(s);
459 Self::Val(val, ty)
460 }
461}
462
463#[derive(#[automatically_derived]impl<'tcx> ::core::marker::Copy for
UnevaluatedConst<'tcx> {
}Copy, #[automatically_derived]#[doc(hidden)]unsafe impl<'tcx>
::core::clone::TrivialClone for UnevaluatedConst<'tcx> {
}
#[automatically_derived]impl<'tcx> ::core::clone::Clone for
UnevaluatedConst<'tcx> {
#[inline]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<DefId>;
let _: ::core::clone::AssertParamIsClone<GenericArgsRef<'tcx>>;
let _: ::core::clone::AssertParamIsClone<Option<Promoted>>;
*self
}
}Clone, #[automatically_derived]impl<'tcx> ::core::fmt::Debug for
UnevaluatedConst<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f,
"UnevaluatedConst", "def", &self.def, "args", &self.args,
"promoted", &&self.promoted)
}
}Debug, #[automatically_derived]impl<'tcx> ::core::cmp::Eq for UnevaluatedConst<'tcx>
{
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<DefId>;
let _: ::core::cmp::AssertParamIsEq<GenericArgsRef<'tcx>>;
let _: ::core::cmp::AssertParamIsEq<Option<Promoted>>;
}
}Eq, #[automatically_derived]impl<'tcx> ::core::marker::StructuralPartialEq for
UnevaluatedConst<'tcx> {
}
#[automatically_derived]impl<'tcx> ::core::cmp::PartialEq for
UnevaluatedConst<'tcx> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.def == other.def && self.args == other.args &&
self.promoted == other.promoted
}
}PartialEq, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for UnevaluatedConst<'tcx> {
fn encode(&self, __encoder: &mut __E) {
let UnevaluatedConst {
def: ref __binding_0,
args: ref __binding_1,
promoted: ref __binding_2 } = *self;
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_2,
__encoder);
}
}
};TyEncodable, const _: () =
{
impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for UnevaluatedConst<'tcx> {
fn decode(__decoder: &mut __D) -> Self {
UnevaluatedConst {
def: ::rustc_serialize::Decodable::decode(__decoder),
args: ::rustc_serialize::Decodable::decode(__decoder),
promoted: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};TyDecodable)]
465#[derive(#[automatically_derived]impl<'tcx> ::core::hash::Hash for
UnevaluatedConst<'tcx> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.def, state);
::core::hash::Hash::hash(&self.args, state);
::core::hash::Hash::hash(&self.promoted, state)
}
}Hash, const _: () =
{
impl<'tcx> ::rustc_data_structures::stable_hash::StableHash for
UnevaluatedConst<'tcx> {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
UnevaluatedConst {
def: ref __binding_0,
args: ref __binding_1,
promoted: ref __binding_2 } => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
{ __binding_2.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash, const _: () =
{
impl<'tcx>
::rustc_middle::ty::TypeFoldable<::rustc_middle::ty::TyCtxt<'tcx>>
for UnevaluatedConst<'tcx> {
fn try_fold_with<__F: ::rustc_middle::ty::FallibleTypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
__folder: &mut __F) -> Result<Self, __F::Error> {
Ok(match self {
UnevaluatedConst {
def: __binding_0, args: __binding_1, promoted: __binding_2 }
=> {
UnevaluatedConst {
def: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_0,
__folder)?,
args: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_1,
__folder)?,
promoted: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_2,
__folder)?,
}
}
})
}
fn fold_with<__F: ::rustc_middle::ty::TypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
__folder: &mut __F) -> Self {
match self {
UnevaluatedConst {
def: __binding_0, args: __binding_1, promoted: __binding_2 }
=> {
UnevaluatedConst {
def: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_0,
__folder),
args: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_1,
__folder),
promoted: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_2,
__folder),
}
}
}
}
}
};TypeFoldable, const _: () =
{
impl<'tcx>
::rustc_middle::ty::TypeVisitable<::rustc_middle::ty::TyCtxt<'tcx>>
for UnevaluatedConst<'tcx> {
fn visit_with<__V: ::rustc_middle::ty::TypeVisitor<::rustc_middle::ty::TyCtxt<'tcx>>>(&self,
__visitor: &mut __V) -> __V::Result {
match *self {
UnevaluatedConst {
def: ref __binding_0,
args: ref __binding_1,
promoted: ref __binding_2 } => {
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_0,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_1,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_2,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
}
}
<__V::Result as ::rustc_middle::ty::VisitorResult>::output()
}
}
};TypeVisitable, const _: () =
{
impl<'tcx, '__lifted>
::rustc_middle::ty::Lift<::rustc_middle::ty::TyCtxt<'__lifted>>
for UnevaluatedConst<'tcx> {
type Lifted = UnevaluatedConst<'__lifted>;
fn lift_to_interner(self,
__tcx: ::rustc_middle::ty::TyCtxt<'__lifted>)
-> UnevaluatedConst<'__lifted> {
match self {
UnevaluatedConst {
def: __binding_0, args: __binding_1, promoted: __binding_2 }
=> {
UnevaluatedConst {
def: __tcx.lift(__binding_0),
args: __tcx.lift(__binding_1),
promoted: __tcx.lift(__binding_2),
}
}
}
}
}
};Lift)]
466pub struct UnevaluatedConst<'tcx> {
467 pub def: DefId,
468 pub args: GenericArgsRef<'tcx>,
469 pub promoted: Option<Promoted>,
470}
471
472impl<'tcx> UnevaluatedConst<'tcx> {
473 #[inline]
474 pub fn shrink(self, tcx: TyCtxt<'tcx>) -> ty::AliasConst<'tcx> {
475 {
match (&self.promoted, &None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};assert_eq!(self.promoted, None);
476
477 let kind = match tcx.def_kind(self.def) {
478 DefKind::AssocConst => {
479 if let DefKind::Impl { of_trait: false } = tcx.def_kind(tcx.parent(self.def)) {
480 ty::AliasConstKind::InherentImpl { def_id: self.def }
481 } else {
482 ty::AliasConstKind::Projection { def_id: self.def }
483 }
484 }
485 DefKind::Const => ty::AliasConstKind::Free { def_id: self.def },
486 DefKind::AnonConst => ty::AliasConstKind::Anon { def_id: self.def },
487 kind => ::rustc_span::macros::bug_impl(None,
format_args!("unexpected DefKind in MIR UnevaluatedConst: {0:?}", kind),
Location::caller())bug!("unexpected DefKind in MIR UnevaluatedConst: {kind:?}"),
488 };
489
490 ty::AliasConst::new(tcx, kind, self.args)
491 }
492}
493
494impl<'tcx> UnevaluatedConst<'tcx> {
495 #[inline]
496 pub fn new(def: DefId, args: GenericArgsRef<'tcx>) -> UnevaluatedConst<'tcx> {
497 UnevaluatedConst { def, args, promoted: Default::default() }
498 }
499
500 #[inline]
501 pub fn from_instance(instance: ty::Instance<'tcx>) -> Self {
502 UnevaluatedConst::new(instance.def_id(), instance.args)
503 }
504}
505
506impl<'tcx> Display for Const<'tcx> {
507 fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
508 match *self {
509 Const::Ty(_, c) => pretty_print_const(c, fmt, true),
510 Const::Val(val, ty) => pretty_print_const_value(val, ty, fmt),
511 Const::Unevaluated(c, _ty) => {
513 ty::tls::with(move |tcx| {
514 let c = tcx.lift(c);
515 let instance =
517 {
let _guard = NoTrimmedGuard::new();
tcx.def_path_str_with_args(c.def, c.args)
}with_no_trimmed_paths!(tcx.def_path_str_with_args(c.def, c.args));
518 fmt.write_fmt(format_args!("{0}", instance))write!(fmt, "{instance}")?;
519 if let Some(promoted) = c.promoted {
520 fmt.write_fmt(format_args!("::{0:?}", promoted))write!(fmt, "::{promoted:?}")?;
521 }
522 Ok(())
523 })
524 }
525 }
526 }
527}
528
529impl<'tcx> TyCtxt<'tcx> {
533 pub fn span_as_caller_location(self, span: Span) -> ConstValue {
534 let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
535 let caller = self.sess.source_map().lookup_char_pos(topmost.lo());
536 self.const_caller_location(
537 Symbol::intern(
538 &caller.file.name.display(RemapPathScopeComponents::MACRO).to_string_lossy(),
539 ),
540 caller.line as u32,
541 caller.col_display as u32 + 1,
542 )
543 }
544}