Skip to main content

clippy_utils/
consts.rs

1//! A simple const eval API, for use on arbitrary HIR expressions.
2//!
3//! This cannot use rustc's const eval, aka miri, as arbitrary HIR expressions cannot be lowered to
4//! executable MIR bodies, so we have to do this instead.
5#![expect(clippy::float_cmp)]
6
7use crate::res::MaybeDef;
8use crate::source::{SpanRangeExt, walk_span_to_context};
9use crate::{clip, is_direct_expn_of, sext, sym, unsext};
10
11use rustc_abi::Size;
12use rustc_apfloat::Float;
13use rustc_apfloat::ieee::{Half, Quad};
14use rustc_ast::ast::{LitFloatType, LitKind};
15use rustc_hir::def::{DefKind, Res};
16use rustc_hir::{
17    BinOpKind, Block, ConstArgKind, ConstBlock, ConstItemRhs, Expr, ExprKind, HirId, PatExpr, PatExprKind, QPath,
18    TyKind, UnOp,
19};
20use rustc_lexer::{FrontmatterAllowed, tokenize};
21use rustc_lint::LateContext;
22use rustc_middle::mir::ConstValue;
23use rustc_middle::mir::interpret::{Scalar, alloc_range};
24use rustc_middle::ty::{self, FloatTy, IntTy, ScalarInt, Ty, TyCtxt, TypeckResults, UintTy};
25use rustc_middle::{bug, mir, span_bug};
26use rustc_span::{Symbol, SyntaxContext};
27use std::cell::Cell;
28use std::cmp::Ordering;
29use std::hash::{Hash, Hasher};
30use std::iter;
31
32/// A `LitKind`-like enum to fold constant `Expr`s into.
33#[derive(Debug, Clone)]
34pub enum Constant {
35    Adt(ConstValue),
36    /// A `String` (e.g., "abc").
37    Str(String),
38    /// A binary string (e.g., `b"abc"`).
39    Binary(Vec<u8>),
40    /// A single `char` (e.g., `'a'`).
41    Char(char),
42    /// An integer's bit representation.
43    Int(u128),
44    /// An `f16` bitcast to a `u16`.
45    // FIXME(f16_f128): use `f16` once builtins are available on all host tools platforms.
46    F16(u16),
47    /// An `f32`.
48    F32(f32),
49    /// An `f64`.
50    F64(f64),
51    /// An `f128` bitcast to a `u128`.
52    // FIXME(f16_f128): use `f128` once builtins are available on all host tools platforms.
53    F128(u128),
54    /// `true` or `false`.
55    Bool(bool),
56    /// An array of constants.
57    Vec(Vec<Self>),
58    /// Also an array, but with only one constant, repeated N times.
59    Repeat(Box<Self>, u64),
60    /// A tuple of constants.
61    Tuple(Vec<Self>),
62    /// A raw pointer.
63    RawPtr(u128),
64    /// A reference
65    Ref(Box<Self>),
66    /// A literal with syntax error.
67    Err,
68}
69
70trait IntTypeBounds: Sized {
71    type Output: PartialOrd;
72
73    fn min_max(self) -> Option<(Self::Output, Self::Output)>;
74    fn bits(self) -> Self::Output;
75    fn ensure_fits(self, val: Self::Output) -> Option<Self::Output> {
76        let (min, max) = self.min_max()?;
77        (min <= val && val <= max).then_some(val)
78    }
79}
80impl IntTypeBounds for UintTy {
81    type Output = u128;
82    fn min_max(self) -> Option<(Self::Output, Self::Output)> {
83        Some(match self {
84            UintTy::U8 => (u8::MIN.into(), u8::MAX.into()),
85            UintTy::U16 => (u16::MIN.into(), u16::MAX.into()),
86            UintTy::U32 => (u32::MIN.into(), u32::MAX.into()),
87            UintTy::U64 => (u64::MIN.into(), u64::MAX.into()),
88            UintTy::U128 => (u128::MIN, u128::MAX),
89            UintTy::Usize => (usize::MIN.try_into().ok()?, usize::MAX.try_into().ok()?),
90        })
91    }
92    fn bits(self) -> Self::Output {
93        match self {
94            UintTy::U8 => 8,
95            UintTy::U16 => 16,
96            UintTy::U32 => 32,
97            UintTy::U64 => 64,
98            UintTy::U128 => 128,
99            UintTy::Usize => usize::BITS.into(),
100        }
101    }
102}
103impl IntTypeBounds for IntTy {
104    type Output = i128;
105    fn min_max(self) -> Option<(Self::Output, Self::Output)> {
106        Some(match self {
107            IntTy::I8 => (i8::MIN.into(), i8::MAX.into()),
108            IntTy::I16 => (i16::MIN.into(), i16::MAX.into()),
109            IntTy::I32 => (i32::MIN.into(), i32::MAX.into()),
110            IntTy::I64 => (i64::MIN.into(), i64::MAX.into()),
111            IntTy::I128 => (i128::MIN, i128::MAX),
112            IntTy::Isize => (isize::MIN.try_into().ok()?, isize::MAX.try_into().ok()?),
113        })
114    }
115    fn bits(self) -> Self::Output {
116        match self {
117            IntTy::I8 => 8,
118            IntTy::I16 => 16,
119            IntTy::I32 => 32,
120            IntTy::I64 => 64,
121            IntTy::I128 => 128,
122            IntTy::Isize => isize::BITS.into(),
123        }
124    }
125}
126
127impl PartialEq for Constant {
128    fn eq(&self, other: &Self) -> bool {
129        match (self, other) {
130            (Self::Str(ls), Self::Str(rs)) => ls == rs,
131            (Self::Binary(l), Self::Binary(r)) => l == r,
132            (&Self::Char(l), &Self::Char(r)) => l == r,
133            (&Self::Int(l), &Self::Int(r)) => l == r,
134            (&Self::F64(l), &Self::F64(r)) => {
135                // `to_bits` is required to catch non-matching `0.0` and `-0.0`.
136                l.to_bits() == r.to_bits() && !l.is_nan()
137            },
138            (&Self::F32(l), &Self::F32(r)) => {
139                // `to_bits` is required to catch non-matching `0.0` and `-0.0`.
140                l.to_bits() == r.to_bits() && !l.is_nan()
141            },
142            (&Self::Bool(l), &Self::Bool(r)) => l == r,
143            (&Self::Vec(ref l), &Self::Vec(ref r)) | (&Self::Tuple(ref l), &Self::Tuple(ref r)) => l == r,
144            (Self::Repeat(lv, ls), Self::Repeat(rv, rs)) => ls == rs && lv == rv,
145            (Self::Ref(lb), Self::Ref(rb)) => *lb == *rb,
146            // TODO: are there inter-type equalities?
147            _ => false,
148        }
149    }
150}
151
152impl Hash for Constant {
153    fn hash<H>(&self, state: &mut H)
154    where
155        H: Hasher,
156    {
157        std::mem::discriminant(self).hash(state);
158        match *self {
159            Self::Adt(ref elem) => {
160                elem.hash(state);
161            },
162            Self::Str(ref s) => {
163                s.hash(state);
164            },
165            Self::Binary(ref b) => {
166                b.hash(state);
167            },
168            Self::Char(c) => {
169                c.hash(state);
170            },
171            Self::Int(i) => {
172                i.hash(state);
173            },
174            Self::F16(f) => {
175                // FIXME(f16_f128): once conversions to/from `f128` are available on all platforms,
176                f.hash(state);
177            },
178            Self::F32(f) => {
179                f64::from(f).to_bits().hash(state);
180            },
181            Self::F64(f) => {
182                f.to_bits().hash(state);
183            },
184            Self::F128(f) => {
185                f.hash(state);
186            },
187            Self::Bool(b) => {
188                b.hash(state);
189            },
190            Self::Vec(ref v) | Self::Tuple(ref v) => {
191                v.hash(state);
192            },
193            Self::Repeat(ref c, l) => {
194                c.hash(state);
195                l.hash(state);
196            },
197            Self::RawPtr(u) => {
198                u.hash(state);
199            },
200            Self::Ref(ref r) => {
201                r.hash(state);
202            },
203            Self::Err => {},
204        }
205    }
206}
207
208impl Constant {
209    pub fn partial_cmp(tcx: TyCtxt<'_>, cmp_type: Ty<'_>, left: &Self, right: &Self) -> Option<Ordering> {
210        match (left, right) {
211            (Self::Str(ls), Self::Str(rs)) => Some(ls.cmp(rs)),
212            (Self::Char(l), Self::Char(r)) => Some(l.cmp(r)),
213            (&Self::Int(l), &Self::Int(r)) => match *cmp_type.kind() {
214                ty::Int(int_ty) => Some(sext(tcx, l, int_ty).cmp(&sext(tcx, r, int_ty))),
215                ty::Uint(_) => Some(l.cmp(&r)),
216                _ => bug!("Not an int type"),
217            },
218            (&Self::F64(l), &Self::F64(r)) => l.partial_cmp(&r),
219            (&Self::F32(l), &Self::F32(r)) => l.partial_cmp(&r),
220            (Self::Bool(l), Self::Bool(r)) => Some(l.cmp(r)),
221            (Self::Tuple(l), Self::Tuple(r)) if l.len() == r.len() => match *cmp_type.kind() {
222                ty::Tuple(tys) if tys.len() == l.len() => l
223                    .iter()
224                    .zip(r)
225                    .zip(tys)
226                    .map(|((li, ri), cmp_type)| Self::partial_cmp(tcx, cmp_type, li, ri))
227                    .find(|r| r.is_none_or(|o| o != Ordering::Equal))
228                    .unwrap_or_else(|| Some(l.len().cmp(&r.len()))),
229                _ => None,
230            },
231            (Self::Vec(l), Self::Vec(r)) => {
232                let cmp_type = cmp_type.builtin_index()?;
233                iter::zip(l, r)
234                    .map(|(li, ri)| Self::partial_cmp(tcx, cmp_type, li, ri))
235                    .find(|r| r.is_none_or(|o| o != Ordering::Equal))
236                    .unwrap_or_else(|| Some(l.len().cmp(&r.len())))
237            },
238            (Self::Repeat(lv, ls), Self::Repeat(rv, rs)) => {
239                match Self::partial_cmp(
240                    tcx,
241                    match *cmp_type.kind() {
242                        ty::Array(ty, _) => ty,
243                        _ => return None,
244                    },
245                    lv,
246                    rv,
247                ) {
248                    Some(Ordering::Equal) => Some(ls.cmp(rs)),
249                    x => x,
250                }
251            },
252            (Self::Ref(lb), Self::Ref(rb)) => Self::partial_cmp(
253                tcx,
254                match *cmp_type.kind() {
255                    ty::Ref(_, ty, _) => ty,
256                    _ => return None,
257                },
258                lb,
259                rb,
260            ),
261            // TODO: are there any useful inter-type orderings?
262            _ => None,
263        }
264    }
265
266    /// Returns the integer value or `None` if `self` or `val_type` is not integer type.
267    pub fn int_value(&self, tcx: TyCtxt<'_>, val_type: Ty<'_>) -> Option<FullInt> {
268        if let Constant::Int(const_int) = *self {
269            match *val_type.kind() {
270                ty::Int(ity) => Some(FullInt::S(sext(tcx, const_int, ity))),
271                ty::Uint(_) => Some(FullInt::U(const_int)),
272                _ => None,
273            }
274        } else {
275            None
276        }
277    }
278
279    #[must_use]
280    pub fn peel_refs(mut self) -> Self {
281        while let Constant::Ref(r) = self {
282            self = *r;
283        }
284        self
285    }
286
287    fn parse_f16(s: &str) -> Self {
288        let f: Half = s.parse().unwrap();
289        Self::F16(f.to_bits().try_into().unwrap())
290    }
291
292    fn parse_f128(s: &str) -> Self {
293        let f: Quad = s.parse().unwrap();
294        Self::F128(f.to_bits())
295    }
296
297    pub fn new_numeric_min<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<Self> {
298        match *ty.kind() {
299            ty::Uint(_) => Some(Self::Int(0)),
300            ty::Int(ty) => {
301                let val = match ty.normalize(tcx.sess.target.pointer_width) {
302                    IntTy::I8 => i128::from(i8::MIN),
303                    IntTy::I16 => i128::from(i16::MIN),
304                    IntTy::I32 => i128::from(i32::MIN),
305                    IntTy::I64 => i128::from(i64::MIN),
306                    IntTy::I128 => i128::MIN,
307                    IntTy::Isize => return None,
308                };
309                Some(Self::Int(val.cast_unsigned()))
310            },
311            ty::Char => Some(Self::Char(char::MIN)),
312            ty::Float(FloatTy::F32) => Some(Self::F32(f32::NEG_INFINITY)),
313            ty::Float(FloatTy::F64) => Some(Self::F64(f64::NEG_INFINITY)),
314            _ => None,
315        }
316    }
317
318    pub fn new_numeric_max<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<Self> {
319        match *ty.kind() {
320            ty::Uint(ty) => Some(Self::Int(match ty.normalize(tcx.sess.target.pointer_width) {
321                UintTy::U8 => u128::from(u8::MAX),
322                UintTy::U16 => u128::from(u16::MAX),
323                UintTy::U32 => u128::from(u32::MAX),
324                UintTy::U64 => u128::from(u64::MAX),
325                UintTy::U128 => u128::MAX,
326                UintTy::Usize => return None,
327            })),
328            ty::Int(ty) => {
329                let val = match ty.normalize(tcx.sess.target.pointer_width) {
330                    IntTy::I8 => i128::from(i8::MAX),
331                    IntTy::I16 => i128::from(i16::MAX),
332                    IntTy::I32 => i128::from(i32::MAX),
333                    IntTy::I64 => i128::from(i64::MAX),
334                    IntTy::I128 => i128::MAX,
335                    IntTy::Isize => return None,
336                };
337                Some(Self::Int(val.cast_unsigned()))
338            },
339            ty::Char => Some(Self::Char(char::MAX)),
340            ty::Float(FloatTy::F32) => Some(Self::F32(f32::INFINITY)),
341            ty::Float(FloatTy::F64) => Some(Self::F64(f64::INFINITY)),
342            _ => None,
343        }
344    }
345
346    pub fn is_numeric_min<'tcx>(&self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
347        match (self, ty.kind()) {
348            (&Self::Int(x), &ty::Uint(_)) => x == 0,
349            (&Self::Int(x), &ty::Int(ty)) => {
350                let limit = match ty.normalize(tcx.sess.target.pointer_width) {
351                    IntTy::I8 => i128::from(i8::MIN),
352                    IntTy::I16 => i128::from(i16::MIN),
353                    IntTy::I32 => i128::from(i32::MIN),
354                    IntTy::I64 => i128::from(i64::MIN),
355                    IntTy::I128 => i128::MIN,
356                    IntTy::Isize => return false,
357                };
358                x.cast_signed() == limit
359            },
360            (&Self::Char(x), &ty::Char) => x == char::MIN,
361            (&Self::F32(x), &ty::Float(FloatTy::F32)) => x == f32::NEG_INFINITY,
362            (&Self::F64(x), &ty::Float(FloatTy::F64)) => x == f64::NEG_INFINITY,
363            _ => false,
364        }
365    }
366
367    pub fn is_numeric_max<'tcx>(&self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
368        match (self, ty.kind()) {
369            (&Self::Int(x), &ty::Uint(ty)) => {
370                let limit = match ty.normalize(tcx.sess.target.pointer_width) {
371                    UintTy::U8 => u128::from(u8::MAX),
372                    UintTy::U16 => u128::from(u16::MAX),
373                    UintTy::U32 => u128::from(u32::MAX),
374                    UintTy::U64 => u128::from(u64::MAX),
375                    UintTy::U128 => u128::MAX,
376                    UintTy::Usize => return false,
377                };
378                x == limit
379            },
380            (&Self::Int(x), &ty::Int(ty)) => {
381                let limit = match ty.normalize(tcx.sess.target.pointer_width) {
382                    IntTy::I8 => i128::from(i8::MAX),
383                    IntTy::I16 => i128::from(i16::MAX),
384                    IntTy::I32 => i128::from(i32::MAX),
385                    IntTy::I64 => i128::from(i64::MAX),
386                    IntTy::I128 => i128::MAX,
387                    IntTy::Isize => return false,
388                };
389                x.cast_signed() == limit
390            },
391            (&Self::Char(x), &ty::Char) => x == char::MAX,
392            (&Self::F32(x), &ty::Float(FloatTy::F32)) => x == f32::INFINITY,
393            (&Self::F64(x), &ty::Float(FloatTy::F64)) => x == f64::INFINITY,
394            _ => false,
395        }
396    }
397
398    pub fn is_pos_infinity(&self) -> bool {
399        match *self {
400            // FIXME(f16_f128): add f16 and f128 when constants are available
401            Constant::F32(x) => x == f32::INFINITY,
402            Constant::F64(x) => x == f64::INFINITY,
403            _ => false,
404        }
405    }
406
407    pub fn is_neg_infinity(&self) -> bool {
408        match *self {
409            // FIXME(f16_f128): add f16 and f128 when constants are available
410            Constant::F32(x) => x == f32::NEG_INFINITY,
411            Constant::F64(x) => x == f64::NEG_INFINITY,
412            _ => false,
413        }
414    }
415}
416
417/// Parses a `LitKind` to a `Constant`.
418pub fn lit_to_mir_constant(lit: &LitKind, ty: Option<Ty<'_>>) -> Constant {
419    match *lit {
420        LitKind::Str(ref is, _) => Constant::Str(is.to_string()),
421        LitKind::Byte(b) => Constant::Int(u128::from(b)),
422        LitKind::ByteStr(ref s, _) | LitKind::CStr(ref s, _) => Constant::Binary(s.as_byte_str().to_vec()),
423        LitKind::Char(c) => Constant::Char(c),
424        LitKind::Int(n, _) => Constant::Int(n.get()),
425        LitKind::Float(ref is, LitFloatType::Suffixed(fty)) => match fty {
426            // FIXME(f16_f128): just use `parse()` directly when available for `f16`/`f128`
427            FloatTy::F16 => Constant::parse_f16(is.as_str()),
428            FloatTy::F32 => Constant::F32(is.as_str().parse().unwrap()),
429            FloatTy::F64 => Constant::F64(is.as_str().parse().unwrap()),
430            FloatTy::F128 => Constant::parse_f128(is.as_str()),
431        },
432        LitKind::Float(ref is, LitFloatType::Unsuffixed) => match ty.expect("type of float is known").kind() {
433            ty::Float(FloatTy::F16) => Constant::parse_f16(is.as_str()),
434            ty::Float(FloatTy::F32) => Constant::F32(is.as_str().parse().unwrap()),
435            ty::Float(FloatTy::F64) => Constant::F64(is.as_str().parse().unwrap()),
436            ty::Float(FloatTy::F128) => Constant::parse_f128(is.as_str()),
437            _ => bug!(),
438        },
439        LitKind::Bool(b) => Constant::Bool(b),
440        LitKind::Err(_) => Constant::Err,
441    }
442}
443
444/// The source of a constant value.
445#[derive(Clone, Copy)]
446pub enum ConstantSource {
447    /// The value is determined solely from the expression.
448    Local,
449    /// The value is dependent on another definition that may change independently from the local
450    /// expression.
451    NonLocal,
452}
453impl ConstantSource {
454    pub fn is_local(self) -> bool {
455        matches!(self, Self::Local)
456    }
457}
458
459#[derive(Copy, Clone, Debug, Eq)]
460pub enum FullInt {
461    S(i128),
462    U(u128),
463}
464
465impl FullInt {
466    pub fn is_zero(self) -> bool {
467        matches!(self, Self::S(0) | Self::U(0))
468    }
469}
470
471impl PartialEq for FullInt {
472    fn eq(&self, other: &Self) -> bool {
473        self.cmp(other) == Ordering::Equal
474    }
475}
476
477impl PartialOrd for FullInt {
478    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
479        Some(self.cmp(other))
480    }
481}
482
483impl Ord for FullInt {
484    fn cmp(&self, other: &Self) -> Ordering {
485        use FullInt::{S, U};
486
487        fn cmp_s_u(s: i128, u: u128) -> Ordering {
488            u128::try_from(s).map_or(Ordering::Less, |x| x.cmp(&u))
489        }
490
491        match (*self, *other) {
492            (S(s), S(o)) => s.cmp(&o),
493            (U(s), U(o)) => s.cmp(&o),
494            (S(s), U(o)) => cmp_s_u(s, o),
495            (U(s), S(o)) => cmp_s_u(o, s).reverse(),
496        }
497    }
498}
499
500/// Evaluates an expression if it's a builtin integer type.
501pub fn eval_int(cx: &LateContext<'_>, e: &Expr<'_>) -> Option<FullInt> {
502    match e.kind {
503        ExprKind::Lit(lit) if let LitKind::Int(val, _) = lit.node => Some(FullInt::U(val.0)),
504        ExprKind::Unary(UnOp::Neg, e)
505            if let ExprKind::Lit(lit) = e.kind
506                && let LitKind::Int(val, _) = lit.node =>
507        {
508            Some(FullInt::S(val.0.cast_signed().wrapping_neg()))
509        },
510        _ if let ty = cx.typeck_results().expr_ty(e)
511            && let ty::Int(_) | ty::Uint(_) = *ty.kind() =>
512        {
513            ConstEvalCtxt::new(cx).eval(e).and_then(|x| x.int_value(cx.tcx, ty))
514        },
515        _ => None,
516    }
517}
518
519/// The context required to evaluate a constant expression.
520///
521/// This is currently limited to constant folding and reading the value of named constants.
522///
523/// See the module level documentation for some context.
524pub struct ConstEvalCtxt<'tcx> {
525    tcx: TyCtxt<'tcx>,
526    typing_env: ty::TypingEnv<'tcx>,
527    typeck: &'tcx TypeckResults<'tcx>,
528    source: Cell<ConstantSource>,
529    ctxt: Cell<SyntaxContext>,
530}
531
532impl<'tcx> ConstEvalCtxt<'tcx> {
533    /// Creates the evaluation context from the lint context. This requires the lint context to be
534    /// in a body (i.e. `cx.enclosing_body.is_some()`).
535    pub fn new(cx: &LateContext<'tcx>) -> Self {
536        Self {
537            tcx: cx.tcx,
538            typing_env: cx.typing_env(),
539            typeck: cx.typeck_results(),
540            source: Cell::new(ConstantSource::Local),
541            ctxt: Cell::new(SyntaxContext::root()),
542        }
543    }
544
545    /// Creates an evaluation context.
546    pub fn with_env(tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>, typeck: &'tcx TypeckResults<'tcx>) -> Self {
547        Self {
548            tcx,
549            typing_env,
550            typeck,
551            source: Cell::new(ConstantSource::Local),
552            ctxt: Cell::new(SyntaxContext::root()),
553        }
554    }
555
556    /// Attempts to evaluate the expression and returns both the value and whether it's dependant on
557    /// other items.
558    pub fn eval_with_source(&self, e: &Expr<'_>, ctxt: SyntaxContext) -> Option<(Constant, ConstantSource)> {
559        self.source.set(ConstantSource::Local);
560        self.ctxt.set(ctxt);
561        self.expr(e).map(|c| (c, self.source.get()))
562    }
563
564    /// Attempts to evaluate the expression.
565    pub fn eval(&self, e: &Expr<'_>) -> Option<Constant> {
566        self.expr(e)
567    }
568
569    /// Attempts to evaluate the expression without accessing other items.
570    ///
571    /// The context argument is the context used to view the evaluated expression. e.g. when
572    /// evaluating the argument in `f(m!(1))` the context of the call expression should be used.
573    /// This is need so the const evaluator can see the `m` macro and marke the evaluation as
574    /// non-local independant of what the macro expands to.
575    pub fn eval_local(&self, e: &Expr<'_>, ctxt: SyntaxContext) -> Option<Constant> {
576        match self.eval_with_source(e, ctxt) {
577            Some((x, ConstantSource::Local)) => Some(x),
578            _ => None,
579        }
580    }
581
582    /// Attempts to evaluate the expression as an integer without accessing other items.
583    ///
584    /// The context argument is the context used to view the evaluated expression. e.g. when
585    /// evaluating the argument in `f(m!(1))` the context of the call expression should be used.
586    /// This is need so the const evaluator can see the `m` macro and marke the evaluation as
587    /// non-local independant of what the macro expands to.
588    pub fn eval_full_int(&self, e: &Expr<'_>, ctxt: SyntaxContext) -> Option<FullInt> {
589        match self.eval_with_source(e, ctxt) {
590            Some((x, ConstantSource::Local)) => x.int_value(self.tcx, self.typeck.expr_ty(e)),
591            _ => None,
592        }
593    }
594
595    pub fn eval_pat_expr(&self, pat_expr: &PatExpr<'_>) -> Option<Constant> {
596        match &pat_expr.kind {
597            PatExprKind::Lit { lit, negated } => {
598                let ty = self.typeck.node_type_opt(pat_expr.hir_id);
599                let val = lit_to_mir_constant(&lit.node, ty);
600                if *negated {
601                    self.constant_negate(&val, ty?)
602                } else {
603                    Some(val)
604                }
605            },
606            PatExprKind::Path(qpath) => self.qpath(qpath, pat_expr.hir_id),
607        }
608    }
609
610    fn check_ctxt(&self, ctxt: SyntaxContext) {
611        if self.ctxt.get() != ctxt {
612            self.source.set(ConstantSource::NonLocal);
613        }
614    }
615
616    fn qpath(&self, qpath: &QPath<'_>, hir_id: HirId) -> Option<Constant> {
617        self.fetch_path(qpath, hir_id)
618            .and_then(|c| mir_to_const(self.tcx, c, self.typeck.node_type(hir_id)))
619    }
620
621    /// Simple constant folding: Insert an expression, get a constant or none.
622    fn expr(&self, e: &Expr<'_>) -> Option<Constant> {
623        self.check_ctxt(e.span.ctxt());
624        match e.kind {
625            ExprKind::ConstBlock(ConstBlock { body, .. }) => self.expr(self.tcx.hir_body(body).value),
626            ExprKind::DropTemps(e) => self.expr(e),
627            ExprKind::Path(ref qpath) => self.qpath(qpath, e.hir_id),
628            ExprKind::Block(block, _) => {
629                self.check_ctxt(block.span.ctxt());
630                self.block(block)
631            },
632            ExprKind::Lit(lit) => {
633                self.check_ctxt(lit.span.ctxt());
634                Some(lit_to_mir_constant(&lit.node, self.typeck.expr_ty_opt(e)))
635            },
636            ExprKind::Array(vec) => self.multi(vec).map(Constant::Vec),
637            ExprKind::Tup(tup) => self.multi(tup).map(Constant::Tuple),
638            ExprKind::Repeat(value, _) => {
639                let n = match self.typeck.expr_ty(e).kind() {
640                    ty::Array(_, n) => n.try_to_target_usize(self.tcx)?,
641                    _ => span_bug!(e.span, "typeck error"),
642                };
643                self.expr(value).map(|v| Constant::Repeat(Box::new(v), n))
644            },
645            ExprKind::Unary(op, operand) => self.expr(operand).and_then(|o| match op {
646                UnOp::Not => self.constant_not(&o, self.typeck.expr_ty(e)),
647                UnOp::Neg => self.constant_negate(&o, self.typeck.expr_ty(e)),
648                UnOp::Deref => Some(if let Constant::Ref(r) = o { *r } else { o }),
649            }),
650            ExprKind::If(cond, then, ref otherwise) => self.ifthenelse(cond, then, *otherwise),
651            ExprKind::Binary(op, left, right) => {
652                self.check_ctxt(e.span.ctxt());
653                self.binop(op.node, left, right)
654            },
655            ExprKind::Call(callee, []) => {
656                // We only handle a few const functions for now.
657                if let ExprKind::Path(qpath) = &callee.kind
658                    && let Some(did) = self.typeck.qpath_res(qpath, callee.hir_id).opt_def_id()
659                {
660                    match self.tcx.get_diagnostic_name(did) {
661                        Some(sym::i8_legacy_fn_max_value) => Some(Constant::Int(i8::MAX as u128)),
662                        Some(sym::i16_legacy_fn_max_value) => Some(Constant::Int(i16::MAX as u128)),
663                        Some(sym::i32_legacy_fn_max_value) => Some(Constant::Int(i32::MAX as u128)),
664                        Some(sym::i64_legacy_fn_max_value) => Some(Constant::Int(i64::MAX as u128)),
665                        Some(sym::i128_legacy_fn_max_value) => Some(Constant::Int(i128::MAX as u128)),
666                        _ => None,
667                    }
668                } else {
669                    None
670                }
671            },
672            ExprKind::Index(arr, index, _) => self.index(arr, index),
673            ExprKind::AddrOf(_, _, inner) => self.expr(inner).map(|r| Constant::Ref(Box::new(r))),
674            ExprKind::Field(base, ref field)
675                if let base_ty = self.typeck.expr_ty(base)
676                    && match self.typeck.expr_adjustments(base) {
677                        [] => true,
678                        [.., a] => a.target == base_ty,
679                    }
680                    && let Some(Constant::Adt(constant)) = self.expr(base)
681                    && let ty::Adt(adt_def, _) = *base_ty.kind()
682                    && adt_def.is_struct()
683                    && let Some((desired_field, ty)) =
684                        field_of_struct(adt_def, self.tcx, constant, base_ty, field.name) =>
685            {
686                self.check_ctxt(field.span.ctxt());
687                mir_to_const(self.tcx, desired_field, ty)
688            },
689            _ => None,
690        }
691    }
692
693    /// Simple constant folding to determine if an expression is an empty slice, str, array, …
694    /// `None` will be returned if the constness cannot be determined, or if the resolution
695    /// leaves the local crate.
696    pub fn eval_is_empty(&self, e: &Expr<'_>) -> Option<bool> {
697        match e.kind {
698            ExprKind::ConstBlock(ConstBlock { body, .. }) => self.eval_is_empty(self.tcx.hir_body(body).value),
699            ExprKind::DropTemps(e) => self.eval_is_empty(e),
700            ExprKind::Lit(lit) => {
701                if is_direct_expn_of(e.span, sym::cfg).is_some() {
702                    None
703                } else {
704                    match &lit.node {
705                        LitKind::Str(is, _) => Some(is.is_empty()),
706                        LitKind::ByteStr(s, _) | LitKind::CStr(s, _) => Some(s.as_byte_str().is_empty()),
707                        _ => None,
708                    }
709                }
710            },
711            ExprKind::Array(vec) => self.multi(vec).map(|v| v.is_empty()),
712            ExprKind::Repeat(..) => {
713                if let ty::Array(_, n) = self.typeck.expr_ty(e).kind() {
714                    Some(n.try_to_target_usize(self.tcx)? == 0)
715                } else {
716                    span_bug!(e.span, "typeck error");
717                }
718            },
719            _ => None,
720        }
721    }
722
723    #[expect(clippy::cast_possible_wrap)]
724    fn constant_not(&self, o: &Constant, ty: Ty<'_>) -> Option<Constant> {
725        use self::Constant::{Bool, Int};
726        match *o {
727            Bool(b) => Some(Bool(!b)),
728            Int(value) => {
729                let value = !value;
730                match *ty.kind() {
731                    ty::Int(ity) => Some(Int(unsext(self.tcx, value as i128, ity))),
732                    ty::Uint(ity) => Some(Int(clip(self.tcx, value, ity))),
733                    _ => None,
734                }
735            },
736            _ => None,
737        }
738    }
739
740    fn constant_negate(&self, o: &Constant, ty: Ty<'_>) -> Option<Constant> {
741        use self::Constant::{F32, F64, Int};
742        match *o {
743            Int(value) => {
744                let ty::Int(ity) = *ty.kind() else { return None };
745                let (min, _) = ity.min_max()?;
746                // sign extend
747                let value = sext(self.tcx, value, ity);
748
749                // Applying unary - to the most negative value of any signed integer type panics.
750                if value == min {
751                    return None;
752                }
753
754                let value = value.checked_neg()?;
755                // clear unused bits
756                Some(Int(unsext(self.tcx, value, ity)))
757            },
758            F32(f) => Some(F32(-f)),
759            F64(f) => Some(F64(-f)),
760            _ => None,
761        }
762    }
763
764    /// Create `Some(Vec![..])` of all constants, unless there is any
765    /// non-constant part.
766    fn multi(&self, vec: &[Expr<'_>]) -> Option<Vec<Constant>> {
767        vec.iter().map(|elem| self.expr(elem)).collect::<Option<_>>()
768    }
769
770    /// Lookup a possibly constant expression from an `ExprKind::Path` and apply a function on it.
771    #[expect(clippy::too_many_lines)]
772    fn fetch_path(&self, qpath: &QPath<'_>, id: HirId) -> Option<ConstValue> {
773        // Resolve the path to a constant and check if that constant is known to
774        // not change based on the target.
775        //
776        // This should be replaced with an attribute at some point.
777        let did = match *qpath {
778            QPath::Resolved(None, path)
779                if path.span.ctxt() == self.ctxt.get()
780                    && path.segments.iter().all(|s| self.ctxt.get() == s.ident.span.ctxt())
781                    && let Res::Def(DefKind::Const { .. }, did) = path.res
782                    && (matches!(
783                        self.tcx.get_diagnostic_name(did),
784                        Some(
785                            sym::f32_legacy_const_digits
786                                | sym::f32_legacy_const_epsilon
787                                | sym::f32_legacy_const_infinity
788                                | sym::f32_legacy_const_mantissa_dig
789                                | sym::f32_legacy_const_max
790                                | sym::f32_legacy_const_max_10_exp
791                                | sym::f32_legacy_const_max_exp
792                                | sym::f32_legacy_const_min
793                                | sym::f32_legacy_const_min_10_exp
794                                | sym::f32_legacy_const_min_exp
795                                | sym::f32_legacy_const_min_positive
796                                | sym::f32_legacy_const_nan
797                                | sym::f32_legacy_const_neg_infinity
798                                | sym::f32_legacy_const_radix
799                                | sym::f64_legacy_const_digits
800                                | sym::f64_legacy_const_epsilon
801                                | sym::f64_legacy_const_infinity
802                                | sym::f64_legacy_const_mantissa_dig
803                                | sym::f64_legacy_const_max
804                                | sym::f64_legacy_const_max_10_exp
805                                | sym::f64_legacy_const_max_exp
806                                | sym::f64_legacy_const_min
807                                | sym::f64_legacy_const_min_10_exp
808                                | sym::f64_legacy_const_min_exp
809                                | sym::f64_legacy_const_min_positive
810                                | sym::f64_legacy_const_nan
811                                | sym::f64_legacy_const_neg_infinity
812                                | sym::f64_legacy_const_radix
813                                | sym::u8_legacy_const_min
814                                | sym::u16_legacy_const_min
815                                | sym::u32_legacy_const_min
816                                | sym::u64_legacy_const_min
817                                | sym::u128_legacy_const_min
818                                | sym::usize_legacy_const_min
819                                | sym::u8_legacy_const_max
820                                | sym::u16_legacy_const_max
821                                | sym::u32_legacy_const_max
822                                | sym::u64_legacy_const_max
823                                | sym::u128_legacy_const_max
824                                | sym::i8_legacy_const_min
825                                | sym::i16_legacy_const_min
826                                | sym::i32_legacy_const_min
827                                | sym::i64_legacy_const_min
828                                | sym::i128_legacy_const_min
829                                | sym::i8_legacy_const_max
830                                | sym::i16_legacy_const_max
831                                | sym::i32_legacy_const_max
832                                | sym::i64_legacy_const_max
833                                | sym::i128_legacy_const_max
834                        )
835                    ) || self.tcx.opt_parent(did).is_some_and(|parent| {
836                        matches!(
837                            parent.opt_diag_name(&self.tcx),
838                            Some(
839                                sym::f16_consts_mod | sym::f32_consts_mod | sym::f64_consts_mod | sym::f128_consts_mod
840                            )
841                        )
842                    })) =>
843            {
844                did
845            },
846            QPath::TypeRelative(ty, const_name)
847                if let TyKind::Path(QPath::Resolved(None, ty_path)) = ty.kind
848                    && let [.., ty_name] = ty_path.segments
849                    && (matches!(
850                        ty_name.ident.name,
851                        sym::i8
852                            | sym::i16
853                            | sym::i32
854                            | sym::i64
855                            | sym::i128
856                            | sym::u8
857                            | sym::u16
858                            | sym::u32
859                            | sym::u64
860                            | sym::u128
861                            | sym::f32
862                            | sym::f64
863                            | sym::char
864                    ) || (ty_name.ident.name == sym::usize && const_name.ident.name == sym::MIN))
865                    && const_name.ident.span.ctxt() == self.ctxt.get()
866                    && ty.span.ctxt() == self.ctxt.get()
867                    && ty_name.ident.span.ctxt() == self.ctxt.get()
868                    && matches!(ty_path.res, Res::PrimTy(_))
869                    && let Some((DefKind::AssocConst { .. }, did)) = self.typeck.type_dependent_def(id)
870                    && self.tcx.inherent_impl_of_assoc(did).is_some() =>
871            {
872                did
873            },
874            // TODO: revisit when feature `min_generic_const_args` is stabilized. In the meantime,
875            // `TyCtxt::const_eval_resolve()` will trigger an ICE when evaluating the body of the
876            // `type const` definition.
877            _ if let Res::Def(
878                DefKind::Const { is_type_const: false } | DefKind::AssocConst { is_type_const: false },
879                did,
880            ) = self.typeck.qpath_res(qpath, id) =>
881            {
882                self.source.set(ConstantSource::NonLocal);
883                did
884            },
885            _ => return None,
886        };
887
888        self.tcx
889            .const_eval_resolve(
890                self.typing_env,
891                mir::UnevaluatedConst::new(did, self.typeck.node_args(id)),
892                qpath.span(),
893            )
894            .ok()
895    }
896
897    fn index(&self, lhs: &'_ Expr<'_>, index: &'_ Expr<'_>) -> Option<Constant> {
898        let lhs = self.expr(lhs);
899        let index = self.expr(index);
900
901        match (lhs, index) {
902            (Some(Constant::Vec(vec)), Some(Constant::Int(index))) => match vec.get(index as usize) {
903                Some(Constant::F16(x)) => Some(Constant::F16(*x)),
904                Some(Constant::F32(x)) => Some(Constant::F32(*x)),
905                Some(Constant::F64(x)) => Some(Constant::F64(*x)),
906                Some(Constant::F128(x)) => Some(Constant::F128(*x)),
907                _ => None,
908            },
909            (Some(Constant::Vec(vec)), _) => {
910                if !vec.is_empty() && vec.iter().all(|x| *x == vec[0]) {
911                    match vec.first() {
912                        Some(Constant::F16(x)) => Some(Constant::F16(*x)),
913                        Some(Constant::F32(x)) => Some(Constant::F32(*x)),
914                        Some(Constant::F64(x)) => Some(Constant::F64(*x)),
915                        Some(Constant::F128(x)) => Some(Constant::F128(*x)),
916                        _ => None,
917                    }
918                } else {
919                    None
920                }
921            },
922            _ => None,
923        }
924    }
925
926    /// A block can only yield a constant if it has exactly one constant expression.
927    fn block(&self, block: &Block<'_>) -> Option<Constant> {
928        if block.stmts.is_empty()
929            && let Some(expr) = block.expr
930        {
931            // Try to detect any `cfg`ed statements or empty macro expansions.
932            let span = block.span.data();
933            if span.ctxt == SyntaxContext::root() {
934                if let Some(expr_span) = walk_span_to_context(expr.span, span.ctxt)
935                    && let expr_lo = expr_span.lo()
936                    && expr_lo >= span.lo
937                    && let Some(src) = (span.lo..expr_lo).get_source_range(&self.tcx)
938                    && let Some(src) = src.as_str()
939                {
940                    use rustc_lexer::TokenKind::{BlockComment, LineComment, OpenBrace, Semi, Whitespace};
941                    if !tokenize(src, FrontmatterAllowed::No)
942                        .map(|t| t.kind)
943                        .filter(|t| !matches!(t, Whitespace | LineComment { .. } | BlockComment { .. } | Semi))
944                        .eq([OpenBrace])
945                    {
946                        self.source.set(ConstantSource::NonLocal);
947                    }
948                } else {
949                    // Unable to access the source. Assume a non-local dependency.
950                    self.source.set(ConstantSource::NonLocal);
951                }
952            }
953
954            self.expr(expr)
955        } else {
956            None
957        }
958    }
959
960    fn ifthenelse(&self, cond: &Expr<'_>, then: &Expr<'_>, otherwise: Option<&Expr<'_>>) -> Option<Constant> {
961        if let Some(Constant::Bool(b)) = self.expr(cond) {
962            if b {
963                self.expr(then)
964            } else {
965                otherwise.as_ref().and_then(|expr| self.expr(expr))
966            }
967        } else {
968            None
969        }
970    }
971
972    fn binop(&self, op: BinOpKind, left: &Expr<'_>, right: &Expr<'_>) -> Option<Constant> {
973        let l = self.expr(left)?;
974        let r = self.expr(right);
975        match (l, r) {
976            (Constant::Int(l), Some(Constant::Int(r))) => match *self.typeck.expr_ty_opt(left)?.kind() {
977                ty::Int(ity) => {
978                    let (ty_min_value, _) = ity.min_max()?;
979                    let bits = ity.bits();
980                    let l = sext(self.tcx, l, ity);
981                    let r = sext(self.tcx, r, ity);
982
983                    // Using / or %, where the left-hand argument is the smallest integer of a signed integer type and
984                    // the right-hand argument is -1 always panics, even with overflow-checks disabled
985                    if let BinOpKind::Div | BinOpKind::Rem = op
986                        && l == ty_min_value
987                        && r == -1
988                    {
989                        return None;
990                    }
991
992                    let zext = |n: i128| Constant::Int(unsext(self.tcx, n, ity));
993                    match op {
994                        // When +, * or binary - create a value greater than the maximum value, or less than
995                        // the minimum value that can be stored, it panics.
996                        BinOpKind::Add => l.checked_add(r).and_then(|n| ity.ensure_fits(n)).map(zext),
997                        BinOpKind::Sub => l.checked_sub(r).and_then(|n| ity.ensure_fits(n)).map(zext),
998                        BinOpKind::Mul => l.checked_mul(r).and_then(|n| ity.ensure_fits(n)).map(zext),
999                        BinOpKind::Div if r != 0 => l.checked_div(r).map(zext),
1000                        BinOpKind::Rem if r != 0 => l.checked_rem(r).map(zext),
1001                        // Using << or >> where the right-hand argument is greater than or equal to the number of bits
1002                        // in the type of the left-hand argument, or is negative panics.
1003                        BinOpKind::Shr if r < bits && !r.is_negative() => l.checked_shr(r.try_into().ok()?).map(zext),
1004                        BinOpKind::Shl if r < bits && !r.is_negative() => l.checked_shl(r.try_into().ok()?).map(zext),
1005                        BinOpKind::BitXor => Some(zext(l ^ r)),
1006                        BinOpKind::BitOr => Some(zext(l | r)),
1007                        BinOpKind::BitAnd => Some(zext(l & r)),
1008                        // FIXME: f32/f64 currently consider `0.0` and `-0.0` as different.
1009                        BinOpKind::Eq => Some(Constant::Bool(l == r)),
1010                        BinOpKind::Ne => Some(Constant::Bool(l != r)),
1011                        BinOpKind::Lt => Some(Constant::Bool(l < r)),
1012                        BinOpKind::Le => Some(Constant::Bool(l <= r)),
1013                        BinOpKind::Ge => Some(Constant::Bool(l >= r)),
1014                        BinOpKind::Gt => Some(Constant::Bool(l > r)),
1015                        _ => None,
1016                    }
1017                },
1018                ty::Uint(ity) => {
1019                    let bits = ity.bits();
1020
1021                    match op {
1022                        BinOpKind::Add => l.checked_add(r).and_then(|n| ity.ensure_fits(n)).map(Constant::Int),
1023                        BinOpKind::Sub => l.checked_sub(r).and_then(|n| ity.ensure_fits(n)).map(Constant::Int),
1024                        BinOpKind::Mul => l.checked_mul(r).and_then(|n| ity.ensure_fits(n)).map(Constant::Int),
1025                        BinOpKind::Div => l.checked_div(r).map(Constant::Int),
1026                        BinOpKind::Rem => l.checked_rem(r).map(Constant::Int),
1027                        BinOpKind::Shr if r < bits => l.checked_shr(r.try_into().ok()?).map(Constant::Int),
1028                        BinOpKind::Shl if r < bits => l.checked_shl(r.try_into().ok()?).map(Constant::Int),
1029                        BinOpKind::BitXor => Some(Constant::Int(l ^ r)),
1030                        BinOpKind::BitOr => Some(Constant::Int(l | r)),
1031                        BinOpKind::BitAnd => Some(Constant::Int(l & r)),
1032                        BinOpKind::Eq => Some(Constant::Bool(l == r)),
1033                        BinOpKind::Ne => Some(Constant::Bool(l != r)),
1034                        BinOpKind::Lt => Some(Constant::Bool(l < r)),
1035                        BinOpKind::Le => Some(Constant::Bool(l <= r)),
1036                        BinOpKind::Ge => Some(Constant::Bool(l >= r)),
1037                        BinOpKind::Gt => Some(Constant::Bool(l > r)),
1038                        _ => None,
1039                    }
1040                },
1041                _ => None,
1042            },
1043            // FIXME(f16_f128): add these types when binary operations are available on all platforms
1044            (Constant::F32(l), Some(Constant::F32(r))) => match op {
1045                BinOpKind::Add => Some(Constant::F32(l + r)),
1046                BinOpKind::Sub => Some(Constant::F32(l - r)),
1047                BinOpKind::Mul => Some(Constant::F32(l * r)),
1048                BinOpKind::Div => Some(Constant::F32(l / r)),
1049                BinOpKind::Rem => Some(Constant::F32(l % r)),
1050                BinOpKind::Eq => Some(Constant::Bool(l == r)),
1051                BinOpKind::Ne => Some(Constant::Bool(l != r)),
1052                BinOpKind::Lt => Some(Constant::Bool(l < r)),
1053                BinOpKind::Le => Some(Constant::Bool(l <= r)),
1054                BinOpKind::Ge => Some(Constant::Bool(l >= r)),
1055                BinOpKind::Gt => Some(Constant::Bool(l > r)),
1056                _ => None,
1057            },
1058            (Constant::F64(l), Some(Constant::F64(r))) => match op {
1059                BinOpKind::Add => Some(Constant::F64(l + r)),
1060                BinOpKind::Sub => Some(Constant::F64(l - r)),
1061                BinOpKind::Mul => Some(Constant::F64(l * r)),
1062                BinOpKind::Div => Some(Constant::F64(l / r)),
1063                BinOpKind::Rem => Some(Constant::F64(l % r)),
1064                BinOpKind::Eq => Some(Constant::Bool(l == r)),
1065                BinOpKind::Ne => Some(Constant::Bool(l != r)),
1066                BinOpKind::Lt => Some(Constant::Bool(l < r)),
1067                BinOpKind::Le => Some(Constant::Bool(l <= r)),
1068                BinOpKind::Ge => Some(Constant::Bool(l >= r)),
1069                BinOpKind::Gt => Some(Constant::Bool(l > r)),
1070                _ => None,
1071            },
1072            (l, r) => match (op, l, r) {
1073                (BinOpKind::And, Constant::Bool(false), _) => Some(Constant::Bool(false)),
1074                (BinOpKind::Or, Constant::Bool(true), _) => Some(Constant::Bool(true)),
1075                (BinOpKind::And, Constant::Bool(true), Some(r)) | (BinOpKind::Or, Constant::Bool(false), Some(r)) => {
1076                    Some(r)
1077                },
1078                (BinOpKind::BitXor, Constant::Bool(l), Some(Constant::Bool(r))) => Some(Constant::Bool(l ^ r)),
1079                (BinOpKind::BitAnd, Constant::Bool(l), Some(Constant::Bool(r))) => Some(Constant::Bool(l & r)),
1080                (BinOpKind::BitOr, Constant::Bool(l), Some(Constant::Bool(r))) => Some(Constant::Bool(l | r)),
1081                _ => None,
1082            },
1083        }
1084    }
1085}
1086
1087pub fn mir_to_const<'tcx>(tcx: TyCtxt<'tcx>, val: ConstValue, ty: Ty<'tcx>) -> Option<Constant> {
1088    match (val, ty.kind()) {
1089        (_, &ty::Adt(adt_def, _)) if adt_def.is_struct() => Some(Constant::Adt(val)),
1090        (ConstValue::Scalar(Scalar::Int(int)), _) => match ty.kind() {
1091            ty::Bool => Some(Constant::Bool(int == ScalarInt::TRUE)),
1092            ty::Uint(_) | ty::Int(_) => Some(Constant::Int(int.to_bits(int.size()))),
1093            ty::Float(FloatTy::F16) => Some(Constant::F16(int.into())),
1094            ty::Float(FloatTy::F32) => Some(Constant::F32(f32::from_bits(int.into()))),
1095            ty::Float(FloatTy::F64) => Some(Constant::F64(f64::from_bits(int.into()))),
1096            ty::Float(FloatTy::F128) => Some(Constant::F128(int.into())),
1097            ty::RawPtr(_, _) => Some(Constant::RawPtr(int.to_bits(int.size()))),
1098            _ => None,
1099        },
1100        (_, ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Str) => {
1101            let data = val.try_get_slice_bytes_for_diagnostics(tcx)?;
1102            String::from_utf8(data.to_owned()).ok().map(Constant::Str)
1103        },
1104        (ConstValue::Indirect { alloc_id, offset }, ty::Array(sub_type, len)) => {
1105            let alloc = tcx.global_alloc(alloc_id).unwrap_memory().inner();
1106            let len = len.try_to_target_usize(tcx)?;
1107            let ty::Float(flt) = sub_type.kind() else {
1108                return None;
1109            };
1110            let size = Size::from_bits(flt.bit_width());
1111            let mut res = Vec::new();
1112            for idx in 0..len {
1113                let range = alloc_range(offset + size * idx, size);
1114                let val = alloc.read_scalar(&tcx, range, /* read_provenance */ false).ok()?;
1115                res.push(match flt {
1116                    FloatTy::F16 => Constant::F16(val.to_u16().discard_err()?),
1117                    FloatTy::F32 => Constant::F32(f32::from_bits(val.to_u32().discard_err()?)),
1118                    FloatTy::F64 => Constant::F64(f64::from_bits(val.to_u64().discard_err()?)),
1119                    FloatTy::F128 => Constant::F128(val.to_u128().discard_err()?),
1120                });
1121            }
1122            Some(Constant::Vec(res))
1123        },
1124        _ => None,
1125    }
1126}
1127
1128fn field_of_struct<'tcx>(
1129    adt_def: ty::AdtDef<'tcx>,
1130    tcx: TyCtxt<'tcx>,
1131    value: ConstValue,
1132    ty: Ty<'tcx>,
1133    field: Symbol,
1134) -> Option<(ConstValue, Ty<'tcx>)> {
1135    if let Some(dc) = tcx.try_destructure_mir_constant_for_user_output(value, ty)
1136        && let Some(dc_variant) = dc.variant
1137        && let Some(variant) = adt_def.variants().get(dc_variant)
1138        && let Some(field_idx) = variant.fields.iter().position(|el| el.name == field)
1139    {
1140        dc.fields.get(field_idx).copied()
1141    } else {
1142        None
1143    }
1144}
1145
1146/// If `expr` evaluates to an integer constant, return its value.
1147///
1148/// The context argument is the context used to view the evaluated expression. e.g. when evaluating
1149/// the argument in `f(m!(1))` the context of the call expression should be used. This is need so
1150/// the const evaluator can see the `m` macro and marke the evaluation as non-local independant of
1151/// what the macro expands to.
1152pub fn integer_const(cx: &LateContext<'_>, expr: &Expr<'_>, ctxt: SyntaxContext) -> Option<u128> {
1153    if let Some(Constant::Int(value)) = ConstEvalCtxt::new(cx).eval_local(expr, ctxt) {
1154        Some(value)
1155    } else {
1156        None
1157    }
1158}
1159
1160/// Check if `expr` evaluates to an integer constant of 0.
1161///
1162/// The context argument is the context used to view the evaluated expression. e.g. when evaluating
1163/// the argument in `f(m!(1))` the context of the call expression should be used. This is need so
1164/// the const evaluator can see the `m` macro and marke the evaluation as non-local independant of
1165/// what the macro expands to.
1166#[inline]
1167pub fn is_zero_integer_const(cx: &LateContext<'_>, expr: &Expr<'_>, ctxt: SyntaxContext) -> bool {
1168    integer_const(cx, expr, ctxt) == Some(0)
1169}
1170
1171pub fn const_item_rhs_to_expr<'tcx>(tcx: TyCtxt<'tcx>, ct_rhs: ConstItemRhs<'tcx>) -> Option<&'tcx Expr<'tcx>> {
1172    match ct_rhs {
1173        ConstItemRhs::Body(body_id) => Some(tcx.hir_body(body_id).value),
1174        ConstItemRhs::TypeConst(const_arg) => match const_arg.kind {
1175            ConstArgKind::Anon(anon) => Some(tcx.hir_body(anon.body).value),
1176            ConstArgKind::Struct(..)
1177            | ConstArgKind::Tup(..)
1178            | ConstArgKind::Literal { .. }
1179            | ConstArgKind::TupleCall(..)
1180            | ConstArgKind::Array(..)
1181            | ConstArgKind::Path(_)
1182            | ConstArgKind::Error(..)
1183            | ConstArgKind::Infer(..) => None,
1184        },
1185    }
1186}