1#![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::{BinOpKind, Block, ConstBlock, Expr, ExprKind, HirId, PatExpr, PatExprKind, QPath, TyKind, UnOp};
17use rustc_lexer::{FrontmatterAllowed, tokenize};
18use rustc_lint::LateContext;
19use rustc_middle::mir::ConstValue;
20use rustc_middle::mir::interpret::{Scalar, alloc_range};
21use rustc_middle::ty::{self, FloatTy, IntTy, ScalarInt, Ty, TyCtxt, TypeckResults, UintTy};
22use rustc_middle::{bug, mir, span_bug};
23use rustc_span::{Symbol, SyntaxContext};
24use std::cell::Cell;
25use std::cmp::Ordering;
26use std::hash::{Hash, Hasher};
27use std::iter;
28
29#[derive(Debug, Clone)]
31pub enum Constant {
32 Adt(ConstValue),
33 Str(String),
35 Binary(Vec<u8>),
37 Char(char),
39 Int(u128),
41 F16(u16),
44 F32(f32),
46 F64(f64),
48 F128(u128),
51 Bool(bool),
53 Vec(Vec<Self>),
55 Repeat(Box<Self>, u64),
57 Tuple(Vec<Self>),
59 RawPtr(u128),
61 Ref(Box<Self>),
63 Err,
65}
66
67trait IntTypeBounds: Sized {
68 type Output: PartialOrd;
69
70 fn min_max(self) -> Option<(Self::Output, Self::Output)>;
71 fn bits(self) -> Self::Output;
72 fn ensure_fits(self, val: Self::Output) -> Option<Self::Output> {
73 let (min, max) = self.min_max()?;
74 (min <= val && val <= max).then_some(val)
75 }
76}
77impl IntTypeBounds for UintTy {
78 type Output = u128;
79 fn min_max(self) -> Option<(Self::Output, Self::Output)> {
80 Some(match self {
81 UintTy::U8 => (u8::MIN.into(), u8::MAX.into()),
82 UintTy::U16 => (u16::MIN.into(), u16::MAX.into()),
83 UintTy::U32 => (u32::MIN.into(), u32::MAX.into()),
84 UintTy::U64 => (u64::MIN.into(), u64::MAX.into()),
85 UintTy::U128 => (u128::MIN, u128::MAX),
86 UintTy::Usize => (usize::MIN.try_into().ok()?, usize::MAX.try_into().ok()?),
87 })
88 }
89 fn bits(self) -> Self::Output {
90 match self {
91 UintTy::U8 => 8,
92 UintTy::U16 => 16,
93 UintTy::U32 => 32,
94 UintTy::U64 => 64,
95 UintTy::U128 => 128,
96 UintTy::Usize => usize::BITS.into(),
97 }
98 }
99}
100impl IntTypeBounds for IntTy {
101 type Output = i128;
102 fn min_max(self) -> Option<(Self::Output, Self::Output)> {
103 Some(match self {
104 IntTy::I8 => (i8::MIN.into(), i8::MAX.into()),
105 IntTy::I16 => (i16::MIN.into(), i16::MAX.into()),
106 IntTy::I32 => (i32::MIN.into(), i32::MAX.into()),
107 IntTy::I64 => (i64::MIN.into(), i64::MAX.into()),
108 IntTy::I128 => (i128::MIN, i128::MAX),
109 IntTy::Isize => (isize::MIN.try_into().ok()?, isize::MAX.try_into().ok()?),
110 })
111 }
112 fn bits(self) -> Self::Output {
113 match self {
114 IntTy::I8 => 8,
115 IntTy::I16 => 16,
116 IntTy::I32 => 32,
117 IntTy::I64 => 64,
118 IntTy::I128 => 128,
119 IntTy::Isize => isize::BITS.into(),
120 }
121 }
122}
123
124impl PartialEq for Constant {
125 fn eq(&self, other: &Self) -> bool {
126 match (self, other) {
127 (Self::Str(ls), Self::Str(rs)) => ls == rs,
128 (Self::Binary(l), Self::Binary(r)) => l == r,
129 (&Self::Char(l), &Self::Char(r)) => l == r,
130 (&Self::Int(l), &Self::Int(r)) => l == r,
131 (&Self::F64(l), &Self::F64(r)) => {
132 l.to_bits() == r.to_bits() && !l.is_nan()
134 },
135 (&Self::F32(l), &Self::F32(r)) => {
136 l.to_bits() == r.to_bits() && !l.is_nan()
138 },
139 (&Self::Bool(l), &Self::Bool(r)) => l == r,
140 (&Self::Vec(ref l), &Self::Vec(ref r)) | (&Self::Tuple(ref l), &Self::Tuple(ref r)) => l == r,
141 (Self::Repeat(lv, ls), Self::Repeat(rv, rs)) => ls == rs && lv == rv,
142 (Self::Ref(lb), Self::Ref(rb)) => *lb == *rb,
143 _ => false,
145 }
146 }
147}
148
149impl Hash for Constant {
150 fn hash<H>(&self, state: &mut H)
151 where
152 H: Hasher,
153 {
154 std::mem::discriminant(self).hash(state);
155 match *self {
156 Self::Adt(ref elem) => {
157 elem.hash(state);
158 },
159 Self::Str(ref s) => {
160 s.hash(state);
161 },
162 Self::Binary(ref b) => {
163 b.hash(state);
164 },
165 Self::Char(c) => {
166 c.hash(state);
167 },
168 Self::Int(i) => {
169 i.hash(state);
170 },
171 Self::F16(f) => {
172 f.hash(state);
174 },
175 Self::F32(f) => {
176 f64::from(f).to_bits().hash(state);
177 },
178 Self::F64(f) => {
179 f.to_bits().hash(state);
180 },
181 Self::F128(f) => {
182 f.hash(state);
183 },
184 Self::Bool(b) => {
185 b.hash(state);
186 },
187 Self::Vec(ref v) | Self::Tuple(ref v) => {
188 v.hash(state);
189 },
190 Self::Repeat(ref c, l) => {
191 c.hash(state);
192 l.hash(state);
193 },
194 Self::RawPtr(u) => {
195 u.hash(state);
196 },
197 Self::Ref(ref r) => {
198 r.hash(state);
199 },
200 Self::Err => {},
201 }
202 }
203}
204
205impl Constant {
206 pub fn partial_cmp(tcx: TyCtxt<'_>, cmp_type: Ty<'_>, left: &Self, right: &Self) -> Option<Ordering> {
207 match (left, right) {
208 (Self::Str(ls), Self::Str(rs)) => Some(ls.cmp(rs)),
209 (Self::Char(l), Self::Char(r)) => Some(l.cmp(r)),
210 (&Self::Int(l), &Self::Int(r)) => match *cmp_type.kind() {
211 ty::Int(int_ty) => Some(sext(tcx, l, int_ty).cmp(&sext(tcx, r, int_ty))),
212 ty::Uint(_) => Some(l.cmp(&r)),
213 _ => bug!("Not an int type"),
214 },
215 (&Self::F64(l), &Self::F64(r)) => l.partial_cmp(&r),
216 (&Self::F32(l), &Self::F32(r)) => l.partial_cmp(&r),
217 (Self::Bool(l), Self::Bool(r)) => Some(l.cmp(r)),
218 (Self::Tuple(l), Self::Tuple(r)) if l.len() == r.len() => match *cmp_type.kind() {
219 ty::Tuple(tys) if tys.len() == l.len() => l
220 .iter()
221 .zip(r)
222 .zip(tys)
223 .map(|((li, ri), cmp_type)| Self::partial_cmp(tcx, cmp_type, li, ri))
224 .find(|r| r.is_none_or(|o| o != Ordering::Equal))
225 .unwrap_or_else(|| Some(l.len().cmp(&r.len()))),
226 _ => None,
227 },
228 (Self::Vec(l), Self::Vec(r)) => {
229 let cmp_type = cmp_type.builtin_index()?;
230 iter::zip(l, r)
231 .map(|(li, ri)| Self::partial_cmp(tcx, cmp_type, li, ri))
232 .find(|r| r.is_none_or(|o| o != Ordering::Equal))
233 .unwrap_or_else(|| Some(l.len().cmp(&r.len())))
234 },
235 (Self::Repeat(lv, ls), Self::Repeat(rv, rs)) => {
236 match Self::partial_cmp(
237 tcx,
238 match *cmp_type.kind() {
239 ty::Array(ty, _) => ty,
240 _ => return None,
241 },
242 lv,
243 rv,
244 ) {
245 Some(Ordering::Equal) => Some(ls.cmp(rs)),
246 x => x,
247 }
248 },
249 (Self::Ref(lb), Self::Ref(rb)) => Self::partial_cmp(
250 tcx,
251 match *cmp_type.kind() {
252 ty::Ref(_, ty, _) => ty,
253 _ => return None,
254 },
255 lb,
256 rb,
257 ),
258 _ => None,
260 }
261 }
262
263 pub fn int_value(&self, tcx: TyCtxt<'_>, val_type: Ty<'_>) -> Option<FullInt> {
265 if let Constant::Int(const_int) = *self {
266 match *val_type.kind() {
267 ty::Int(ity) => Some(FullInt::S(sext(tcx, const_int, ity))),
268 ty::Uint(_) => Some(FullInt::U(const_int)),
269 _ => None,
270 }
271 } else {
272 None
273 }
274 }
275
276 #[must_use]
277 pub fn peel_refs(mut self) -> Self {
278 while let Constant::Ref(r) = self {
279 self = *r;
280 }
281 self
282 }
283
284 fn parse_f16(s: &str) -> Self {
285 let f: Half = s.parse().unwrap();
286 Self::F16(f.to_bits().try_into().unwrap())
287 }
288
289 fn parse_f128(s: &str) -> Self {
290 let f: Quad = s.parse().unwrap();
291 Self::F128(f.to_bits())
292 }
293
294 pub fn new_numeric_min<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<Self> {
295 match *ty.kind() {
296 ty::Uint(_) => Some(Self::Int(0)),
297 ty::Int(ty) => {
298 let val = match ty.normalize(tcx.sess.target.pointer_width) {
299 IntTy::I8 => i128::from(i8::MIN),
300 IntTy::I16 => i128::from(i16::MIN),
301 IntTy::I32 => i128::from(i32::MIN),
302 IntTy::I64 => i128::from(i64::MIN),
303 IntTy::I128 => i128::MIN,
304 IntTy::Isize => return None,
305 };
306 Some(Self::Int(val.cast_unsigned()))
307 },
308 ty::Char => Some(Self::Char(char::MIN)),
309 ty::Float(FloatTy::F32) => Some(Self::F32(f32::NEG_INFINITY)),
310 ty::Float(FloatTy::F64) => Some(Self::F64(f64::NEG_INFINITY)),
311 _ => None,
312 }
313 }
314
315 pub fn new_numeric_max<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<Self> {
316 match *ty.kind() {
317 ty::Uint(ty) => Some(Self::Int(match ty.normalize(tcx.sess.target.pointer_width) {
318 UintTy::U8 => u128::from(u8::MAX),
319 UintTy::U16 => u128::from(u16::MAX),
320 UintTy::U32 => u128::from(u32::MAX),
321 UintTy::U64 => u128::from(u64::MAX),
322 UintTy::U128 => u128::MAX,
323 UintTy::Usize => return None,
324 })),
325 ty::Int(ty) => {
326 let val = match ty.normalize(tcx.sess.target.pointer_width) {
327 IntTy::I8 => i128::from(i8::MAX),
328 IntTy::I16 => i128::from(i16::MAX),
329 IntTy::I32 => i128::from(i32::MAX),
330 IntTy::I64 => i128::from(i64::MAX),
331 IntTy::I128 => i128::MAX,
332 IntTy::Isize => return None,
333 };
334 Some(Self::Int(val.cast_unsigned()))
335 },
336 ty::Char => Some(Self::Char(char::MAX)),
337 ty::Float(FloatTy::F32) => Some(Self::F32(f32::INFINITY)),
338 ty::Float(FloatTy::F64) => Some(Self::F64(f64::INFINITY)),
339 _ => None,
340 }
341 }
342
343 pub fn is_numeric_min<'tcx>(&self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
344 match (self, ty.kind()) {
345 (&Self::Int(x), &ty::Uint(_)) => x == 0,
346 (&Self::Int(x), &ty::Int(ty)) => {
347 let limit = match ty.normalize(tcx.sess.target.pointer_width) {
348 IntTy::I8 => i128::from(i8::MIN),
349 IntTy::I16 => i128::from(i16::MIN),
350 IntTy::I32 => i128::from(i32::MIN),
351 IntTy::I64 => i128::from(i64::MIN),
352 IntTy::I128 => i128::MIN,
353 IntTy::Isize => return false,
354 };
355 x.cast_signed() == limit
356 },
357 (&Self::Char(x), &ty::Char) => x == char::MIN,
358 (&Self::F32(x), &ty::Float(FloatTy::F32)) => x == f32::NEG_INFINITY,
359 (&Self::F64(x), &ty::Float(FloatTy::F64)) => x == f64::NEG_INFINITY,
360 _ => false,
361 }
362 }
363
364 pub fn is_numeric_max<'tcx>(&self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
365 match (self, ty.kind()) {
366 (&Self::Int(x), &ty::Uint(ty)) => {
367 let limit = match ty.normalize(tcx.sess.target.pointer_width) {
368 UintTy::U8 => u128::from(u8::MAX),
369 UintTy::U16 => u128::from(u16::MAX),
370 UintTy::U32 => u128::from(u32::MAX),
371 UintTy::U64 => u128::from(u64::MAX),
372 UintTy::U128 => u128::MAX,
373 UintTy::Usize => return false,
374 };
375 x == limit
376 },
377 (&Self::Int(x), &ty::Int(ty)) => {
378 let limit = match ty.normalize(tcx.sess.target.pointer_width) {
379 IntTy::I8 => i128::from(i8::MAX),
380 IntTy::I16 => i128::from(i16::MAX),
381 IntTy::I32 => i128::from(i32::MAX),
382 IntTy::I64 => i128::from(i64::MAX),
383 IntTy::I128 => i128::MAX,
384 IntTy::Isize => return false,
385 };
386 x.cast_signed() == limit
387 },
388 (&Self::Char(x), &ty::Char) => x == char::MAX,
389 (&Self::F32(x), &ty::Float(FloatTy::F32)) => x == f32::INFINITY,
390 (&Self::F64(x), &ty::Float(FloatTy::F64)) => x == f64::INFINITY,
391 _ => false,
392 }
393 }
394
395 pub fn is_pos_infinity(&self) -> bool {
396 match *self {
397 Constant::F32(x) => x == f32::INFINITY,
399 Constant::F64(x) => x == f64::INFINITY,
400 _ => false,
401 }
402 }
403
404 pub fn is_neg_infinity(&self) -> bool {
405 match *self {
406 Constant::F32(x) => x == f32::NEG_INFINITY,
408 Constant::F64(x) => x == f64::NEG_INFINITY,
409 _ => false,
410 }
411 }
412}
413
414pub fn lit_to_mir_constant(lit: &LitKind, ty: Option<Ty<'_>>) -> Constant {
416 match *lit {
417 LitKind::Str(ref is, _) => Constant::Str(is.to_string()),
418 LitKind::Byte(b) => Constant::Int(u128::from(b)),
419 LitKind::ByteStr(ref s, _) | LitKind::CStr(ref s, _) => Constant::Binary(s.as_byte_str().to_vec()),
420 LitKind::Char(c) => Constant::Char(c),
421 LitKind::Int(n, _) => Constant::Int(n.get()),
422 LitKind::Float(ref is, LitFloatType::Suffixed(fty)) => match fty {
423 FloatTy::F16 => Constant::parse_f16(is.as_str()),
425 FloatTy::F32 => Constant::F32(is.as_str().parse().unwrap()),
426 FloatTy::F64 => Constant::F64(is.as_str().parse().unwrap()),
427 FloatTy::F128 => Constant::parse_f128(is.as_str()),
428 },
429 LitKind::Float(ref is, LitFloatType::Unsuffixed) => match ty.expect("type of float is known").kind() {
430 ty::Float(FloatTy::F16) => Constant::parse_f16(is.as_str()),
431 ty::Float(FloatTy::F32) => Constant::F32(is.as_str().parse().unwrap()),
432 ty::Float(FloatTy::F64) => Constant::F64(is.as_str().parse().unwrap()),
433 ty::Float(FloatTy::F128) => Constant::parse_f128(is.as_str()),
434 _ => bug!(),
435 },
436 LitKind::Bool(b) => Constant::Bool(b),
437 LitKind::Err(_) => Constant::Err,
438 }
439}
440
441#[derive(Clone, Copy)]
443pub enum ConstantSource {
444 Local,
446 NonLocal,
449}
450impl ConstantSource {
451 pub fn is_local(self) -> bool {
452 matches!(self, Self::Local)
453 }
454}
455
456#[derive(Copy, Clone, Debug, Eq)]
457pub enum FullInt {
458 S(i128),
459 U(u128),
460}
461
462impl PartialEq for FullInt {
463 fn eq(&self, other: &Self) -> bool {
464 self.cmp(other) == Ordering::Equal
465 }
466}
467
468impl PartialOrd for FullInt {
469 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
470 Some(self.cmp(other))
471 }
472}
473
474impl Ord for FullInt {
475 fn cmp(&self, other: &Self) -> Ordering {
476 use FullInt::{S, U};
477
478 fn cmp_s_u(s: i128, u: u128) -> Ordering {
479 u128::try_from(s).map_or(Ordering::Less, |x| x.cmp(&u))
480 }
481
482 match (*self, *other) {
483 (S(s), S(o)) => s.cmp(&o),
484 (U(s), U(o)) => s.cmp(&o),
485 (S(s), U(o)) => cmp_s_u(s, o),
486 (U(s), S(o)) => cmp_s_u(o, s).reverse(),
487 }
488 }
489}
490
491pub struct ConstEvalCtxt<'tcx> {
497 tcx: TyCtxt<'tcx>,
498 typing_env: ty::TypingEnv<'tcx>,
499 typeck: &'tcx TypeckResults<'tcx>,
500 source: Cell<ConstantSource>,
501 ctxt: Cell<SyntaxContext>,
502}
503
504impl<'tcx> ConstEvalCtxt<'tcx> {
505 pub fn new(cx: &LateContext<'tcx>) -> Self {
508 Self {
509 tcx: cx.tcx,
510 typing_env: cx.typing_env(),
511 typeck: cx.typeck_results(),
512 source: Cell::new(ConstantSource::Local),
513 ctxt: Cell::new(SyntaxContext::root()),
514 }
515 }
516
517 pub fn with_env(tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>, typeck: &'tcx TypeckResults<'tcx>) -> Self {
519 Self {
520 tcx,
521 typing_env,
522 typeck,
523 source: Cell::new(ConstantSource::Local),
524 ctxt: Cell::new(SyntaxContext::root()),
525 }
526 }
527
528 pub fn eval_with_source(&self, e: &Expr<'_>, ctxt: SyntaxContext) -> Option<(Constant, ConstantSource)> {
531 self.source.set(ConstantSource::Local);
532 self.ctxt.set(ctxt);
533 self.expr(e).map(|c| (c, self.source.get()))
534 }
535
536 pub fn eval(&self, e: &Expr<'_>) -> Option<Constant> {
538 self.expr(e)
539 }
540
541 pub fn eval_local(&self, e: &Expr<'_>, ctxt: SyntaxContext) -> Option<Constant> {
548 match self.eval_with_source(e, ctxt) {
549 Some((x, ConstantSource::Local)) => Some(x),
550 _ => None,
551 }
552 }
553
554 pub fn eval_full_int(&self, e: &Expr<'_>, ctxt: SyntaxContext) -> Option<FullInt> {
561 match self.eval_with_source(e, ctxt) {
562 Some((x, ConstantSource::Local)) => x.int_value(self.tcx, self.typeck.expr_ty(e)),
563 _ => None,
564 }
565 }
566
567 pub fn eval_pat_expr(&self, pat_expr: &PatExpr<'_>) -> Option<Constant> {
568 match &pat_expr.kind {
569 PatExprKind::Lit { lit, negated } => {
570 let ty = self.typeck.node_type_opt(pat_expr.hir_id);
571 let val = lit_to_mir_constant(&lit.node, ty);
572 if *negated {
573 self.constant_negate(&val, ty?)
574 } else {
575 Some(val)
576 }
577 },
578 PatExprKind::ConstBlock(ConstBlock { body, .. }) => self.expr(self.tcx.hir_body(*body).value),
579 PatExprKind::Path(qpath) => self.qpath(qpath, pat_expr.hir_id),
580 }
581 }
582
583 fn check_ctxt(&self, ctxt: SyntaxContext) {
584 if self.ctxt.get() != ctxt {
585 self.source.set(ConstantSource::NonLocal);
586 }
587 }
588
589 fn qpath(&self, qpath: &QPath<'_>, hir_id: HirId) -> Option<Constant> {
590 self.fetch_path(qpath, hir_id)
591 .and_then(|c| mir_to_const(self.tcx, c, self.typeck.node_type(hir_id)))
592 }
593
594 fn expr(&self, e: &Expr<'_>) -> Option<Constant> {
596 self.check_ctxt(e.span.ctxt());
597 match e.kind {
598 ExprKind::ConstBlock(ConstBlock { body, .. }) => self.expr(self.tcx.hir_body(body).value),
599 ExprKind::DropTemps(e) => self.expr(e),
600 ExprKind::Path(ref qpath) => self.qpath(qpath, e.hir_id),
601 ExprKind::Block(block, _) => {
602 self.check_ctxt(block.span.ctxt());
603 self.block(block)
604 },
605 ExprKind::Lit(lit) => {
606 self.check_ctxt(lit.span.ctxt());
607 Some(lit_to_mir_constant(&lit.node, self.typeck.expr_ty_opt(e)))
608 },
609 ExprKind::Array(vec) => self.multi(vec).map(Constant::Vec),
610 ExprKind::Tup(tup) => self.multi(tup).map(Constant::Tuple),
611 ExprKind::Repeat(value, _) => {
612 let n = match self.typeck.expr_ty(e).kind() {
613 ty::Array(_, n) => n.try_to_target_usize(self.tcx)?,
614 _ => span_bug!(e.span, "typeck error"),
615 };
616 self.expr(value).map(|v| Constant::Repeat(Box::new(v), n))
617 },
618 ExprKind::Unary(op, operand) => self.expr(operand).and_then(|o| match op {
619 UnOp::Not => self.constant_not(&o, self.typeck.expr_ty(e)),
620 UnOp::Neg => self.constant_negate(&o, self.typeck.expr_ty(e)),
621 UnOp::Deref => Some(if let Constant::Ref(r) = o { *r } else { o }),
622 }),
623 ExprKind::If(cond, then, ref otherwise) => self.ifthenelse(cond, then, *otherwise),
624 ExprKind::Binary(op, left, right) => {
625 self.check_ctxt(e.span.ctxt());
626 self.binop(op.node, left, right)
627 },
628 ExprKind::Call(callee, []) => {
629 if let ExprKind::Path(qpath) = &callee.kind
631 && let Some(did) = self.typeck.qpath_res(qpath, callee.hir_id).opt_def_id()
632 {
633 match self.tcx.get_diagnostic_name(did) {
634 Some(sym::i8_legacy_fn_max_value) => Some(Constant::Int(i8::MAX as u128)),
635 Some(sym::i16_legacy_fn_max_value) => Some(Constant::Int(i16::MAX as u128)),
636 Some(sym::i32_legacy_fn_max_value) => Some(Constant::Int(i32::MAX as u128)),
637 Some(sym::i64_legacy_fn_max_value) => Some(Constant::Int(i64::MAX as u128)),
638 Some(sym::i128_legacy_fn_max_value) => Some(Constant::Int(i128::MAX as u128)),
639 _ => None,
640 }
641 } else {
642 None
643 }
644 },
645 ExprKind::Index(arr, index, _) => self.index(arr, index),
646 ExprKind::AddrOf(_, _, inner) => self.expr(inner).map(|r| Constant::Ref(Box::new(r))),
647 ExprKind::Field(base, ref field)
648 if let base_ty = self.typeck.expr_ty(base)
649 && match self.typeck.expr_adjustments(base) {
650 [] => true,
651 [.., a] => a.target == base_ty,
652 }
653 && let Some(Constant::Adt(constant)) = self.expr(base)
654 && let ty::Adt(adt_def, _) = *base_ty.kind()
655 && adt_def.is_struct()
656 && let Some((desired_field, ty)) =
657 field_of_struct(adt_def, self.tcx, constant, base_ty, field.name) =>
658 {
659 self.check_ctxt(field.span.ctxt());
660 mir_to_const(self.tcx, desired_field, ty)
661 },
662 _ => None,
663 }
664 }
665
666 pub fn eval_is_empty(&self, e: &Expr<'_>) -> Option<bool> {
670 match e.kind {
671 ExprKind::ConstBlock(ConstBlock { body, .. }) => self.eval_is_empty(self.tcx.hir_body(body).value),
672 ExprKind::DropTemps(e) => self.eval_is_empty(e),
673 ExprKind::Lit(lit) => {
674 if is_direct_expn_of(e.span, sym::cfg).is_some() {
675 None
676 } else {
677 match &lit.node {
678 LitKind::Str(is, _) => Some(is.is_empty()),
679 LitKind::ByteStr(s, _) | LitKind::CStr(s, _) => Some(s.as_byte_str().is_empty()),
680 _ => None,
681 }
682 }
683 },
684 ExprKind::Array(vec) => self.multi(vec).map(|v| v.is_empty()),
685 ExprKind::Repeat(..) => {
686 if let ty::Array(_, n) = self.typeck.expr_ty(e).kind() {
687 Some(n.try_to_target_usize(self.tcx)? == 0)
688 } else {
689 span_bug!(e.span, "typeck error");
690 }
691 },
692 _ => None,
693 }
694 }
695
696 #[expect(clippy::cast_possible_wrap)]
697 fn constant_not(&self, o: &Constant, ty: Ty<'_>) -> Option<Constant> {
698 use self::Constant::{Bool, Int};
699 match *o {
700 Bool(b) => Some(Bool(!b)),
701 Int(value) => {
702 let value = !value;
703 match *ty.kind() {
704 ty::Int(ity) => Some(Int(unsext(self.tcx, value as i128, ity))),
705 ty::Uint(ity) => Some(Int(clip(self.tcx, value, ity))),
706 _ => None,
707 }
708 },
709 _ => None,
710 }
711 }
712
713 fn constant_negate(&self, o: &Constant, ty: Ty<'_>) -> Option<Constant> {
714 use self::Constant::{F32, F64, Int};
715 match *o {
716 Int(value) => {
717 let ty::Int(ity) = *ty.kind() else { return None };
718 let (min, _) = ity.min_max()?;
719 let value = sext(self.tcx, value, ity);
721
722 if value == min {
724 return None;
725 }
726
727 let value = value.checked_neg()?;
728 Some(Int(unsext(self.tcx, value, ity)))
730 },
731 F32(f) => Some(F32(-f)),
732 F64(f) => Some(F64(-f)),
733 _ => None,
734 }
735 }
736
737 fn multi(&self, vec: &[Expr<'_>]) -> Option<Vec<Constant>> {
740 vec.iter().map(|elem| self.expr(elem)).collect::<Option<_>>()
741 }
742
743 #[expect(clippy::too_many_lines)]
745 fn fetch_path(&self, qpath: &QPath<'_>, id: HirId) -> Option<ConstValue> {
746 let did = match *qpath {
751 QPath::Resolved(None, path)
752 if path.span.ctxt() == self.ctxt.get()
753 && path.segments.iter().all(|s| self.ctxt.get() == s.ident.span.ctxt())
754 && let Res::Def(DefKind::Const, did) = path.res
755 && (matches!(
756 self.tcx.get_diagnostic_name(did),
757 Some(
758 sym::f32_legacy_const_digits
759 | sym::f32_legacy_const_epsilon
760 | sym::f32_legacy_const_infinity
761 | sym::f32_legacy_const_mantissa_dig
762 | sym::f32_legacy_const_max
763 | sym::f32_legacy_const_max_10_exp
764 | sym::f32_legacy_const_max_exp
765 | sym::f32_legacy_const_min
766 | sym::f32_legacy_const_min_10_exp
767 | sym::f32_legacy_const_min_exp
768 | sym::f32_legacy_const_min_positive
769 | sym::f32_legacy_const_nan
770 | sym::f32_legacy_const_neg_infinity
771 | sym::f32_legacy_const_radix
772 | sym::f64_legacy_const_digits
773 | sym::f64_legacy_const_epsilon
774 | sym::f64_legacy_const_infinity
775 | sym::f64_legacy_const_mantissa_dig
776 | sym::f64_legacy_const_max
777 | sym::f64_legacy_const_max_10_exp
778 | sym::f64_legacy_const_max_exp
779 | sym::f64_legacy_const_min
780 | sym::f64_legacy_const_min_10_exp
781 | sym::f64_legacy_const_min_exp
782 | sym::f64_legacy_const_min_positive
783 | sym::f64_legacy_const_nan
784 | sym::f64_legacy_const_neg_infinity
785 | sym::f64_legacy_const_radix
786 | sym::u8_legacy_const_min
787 | sym::u16_legacy_const_min
788 | sym::u32_legacy_const_min
789 | sym::u64_legacy_const_min
790 | sym::u128_legacy_const_min
791 | sym::usize_legacy_const_min
792 | sym::u8_legacy_const_max
793 | sym::u16_legacy_const_max
794 | sym::u32_legacy_const_max
795 | sym::u64_legacy_const_max
796 | sym::u128_legacy_const_max
797 | sym::i8_legacy_const_min
798 | sym::i16_legacy_const_min
799 | sym::i32_legacy_const_min
800 | sym::i64_legacy_const_min
801 | sym::i128_legacy_const_min
802 | sym::i8_legacy_const_max
803 | sym::i16_legacy_const_max
804 | sym::i32_legacy_const_max
805 | sym::i64_legacy_const_max
806 | sym::i128_legacy_const_max
807 )
808 ) || self.tcx.opt_parent(did).is_some_and(|parent| {
809 parent.is_diag_item(&self.tcx, sym::f16_consts_mod)
810 || parent.is_diag_item(&self.tcx, sym::f32_consts_mod)
811 || parent.is_diag_item(&self.tcx, sym::f64_consts_mod)
812 || parent.is_diag_item(&self.tcx, sym::f128_consts_mod)
813 })) =>
814 {
815 did
816 },
817 QPath::TypeRelative(ty, const_name)
818 if let TyKind::Path(QPath::Resolved(None, ty_path)) = ty.kind
819 && let [.., ty_name] = ty_path.segments
820 && (matches!(
821 ty_name.ident.name,
822 sym::i8
823 | sym::i16
824 | sym::i32
825 | sym::i64
826 | sym::i128
827 | sym::u8
828 | sym::u16
829 | sym::u32
830 | sym::u64
831 | sym::u128
832 | sym::f32
833 | sym::f64
834 | sym::char
835 ) || (ty_name.ident.name == sym::usize && const_name.ident.name == sym::MIN))
836 && const_name.ident.span.ctxt() == self.ctxt.get()
837 && ty.span.ctxt() == self.ctxt.get()
838 && ty_name.ident.span.ctxt() == self.ctxt.get()
839 && matches!(ty_path.res, Res::PrimTy(_))
840 && let Some((DefKind::AssocConst, did)) = self.typeck.type_dependent_def(id) =>
841 {
842 did
843 },
844 _ if let Res::Def(DefKind::Const | DefKind::AssocConst, did) = self.typeck.qpath_res(qpath, id) => {
845 self.source.set(ConstantSource::NonLocal);
846 did
847 },
848 _ => return None,
849 };
850
851 self.tcx
852 .const_eval_resolve(
853 self.typing_env,
854 mir::UnevaluatedConst::new(did, self.typeck.node_args(id)),
855 qpath.span(),
856 )
857 .ok()
858 }
859
860 fn index(&self, lhs: &'_ Expr<'_>, index: &'_ Expr<'_>) -> Option<Constant> {
861 let lhs = self.expr(lhs);
862 let index = self.expr(index);
863
864 match (lhs, index) {
865 (Some(Constant::Vec(vec)), Some(Constant::Int(index))) => match vec.get(index as usize) {
866 Some(Constant::F16(x)) => Some(Constant::F16(*x)),
867 Some(Constant::F32(x)) => Some(Constant::F32(*x)),
868 Some(Constant::F64(x)) => Some(Constant::F64(*x)),
869 Some(Constant::F128(x)) => Some(Constant::F128(*x)),
870 _ => None,
871 },
872 (Some(Constant::Vec(vec)), _) => {
873 if !vec.is_empty() && vec.iter().all(|x| *x == vec[0]) {
874 match vec.first() {
875 Some(Constant::F16(x)) => Some(Constant::F16(*x)),
876 Some(Constant::F32(x)) => Some(Constant::F32(*x)),
877 Some(Constant::F64(x)) => Some(Constant::F64(*x)),
878 Some(Constant::F128(x)) => Some(Constant::F128(*x)),
879 _ => None,
880 }
881 } else {
882 None
883 }
884 },
885 _ => None,
886 }
887 }
888
889 fn block(&self, block: &Block<'_>) -> Option<Constant> {
891 if block.stmts.is_empty()
892 && let Some(expr) = block.expr
893 {
894 let span = block.span.data();
896 if span.ctxt == SyntaxContext::root() {
897 if let Some(expr_span) = walk_span_to_context(expr.span, span.ctxt)
898 && let expr_lo = expr_span.lo()
899 && expr_lo >= span.lo
900 && let Some(src) = (span.lo..expr_lo).get_source_range(&self.tcx)
901 && let Some(src) = src.as_str()
902 {
903 use rustc_lexer::TokenKind::{BlockComment, LineComment, OpenBrace, Semi, Whitespace};
904 if !tokenize(src, FrontmatterAllowed::No)
905 .map(|t| t.kind)
906 .filter(|t| !matches!(t, Whitespace | LineComment { .. } | BlockComment { .. } | Semi))
907 .eq([OpenBrace])
908 {
909 self.source.set(ConstantSource::NonLocal);
910 }
911 } else {
912 self.source.set(ConstantSource::NonLocal);
914 }
915 }
916
917 self.expr(expr)
918 } else {
919 None
920 }
921 }
922
923 fn ifthenelse(&self, cond: &Expr<'_>, then: &Expr<'_>, otherwise: Option<&Expr<'_>>) -> Option<Constant> {
924 if let Some(Constant::Bool(b)) = self.expr(cond) {
925 if b {
926 self.expr(then)
927 } else {
928 otherwise.as_ref().and_then(|expr| self.expr(expr))
929 }
930 } else {
931 None
932 }
933 }
934
935 fn binop(&self, op: BinOpKind, left: &Expr<'_>, right: &Expr<'_>) -> Option<Constant> {
936 let l = self.expr(left)?;
937 let r = self.expr(right);
938 match (l, r) {
939 (Constant::Int(l), Some(Constant::Int(r))) => match *self.typeck.expr_ty_opt(left)?.kind() {
940 ty::Int(ity) => {
941 let (ty_min_value, _) = ity.min_max()?;
942 let bits = ity.bits();
943 let l = sext(self.tcx, l, ity);
944 let r = sext(self.tcx, r, ity);
945
946 if let BinOpKind::Div | BinOpKind::Rem = op
949 && l == ty_min_value
950 && r == -1
951 {
952 return None;
953 }
954
955 let zext = |n: i128| Constant::Int(unsext(self.tcx, n, ity));
956 match op {
957 BinOpKind::Add => l.checked_add(r).and_then(|n| ity.ensure_fits(n)).map(zext),
960 BinOpKind::Sub => l.checked_sub(r).and_then(|n| ity.ensure_fits(n)).map(zext),
961 BinOpKind::Mul => l.checked_mul(r).and_then(|n| ity.ensure_fits(n)).map(zext),
962 BinOpKind::Div if r != 0 => l.checked_div(r).map(zext),
963 BinOpKind::Rem if r != 0 => l.checked_rem(r).map(zext),
964 BinOpKind::Shr if r < bits && !r.is_negative() => l.checked_shr(r.try_into().ok()?).map(zext),
967 BinOpKind::Shl if r < bits && !r.is_negative() => l.checked_shl(r.try_into().ok()?).map(zext),
968 BinOpKind::BitXor => Some(zext(l ^ r)),
969 BinOpKind::BitOr => Some(zext(l | r)),
970 BinOpKind::BitAnd => Some(zext(l & r)),
971 BinOpKind::Eq => Some(Constant::Bool(l == r)),
973 BinOpKind::Ne => Some(Constant::Bool(l != r)),
974 BinOpKind::Lt => Some(Constant::Bool(l < r)),
975 BinOpKind::Le => Some(Constant::Bool(l <= r)),
976 BinOpKind::Ge => Some(Constant::Bool(l >= r)),
977 BinOpKind::Gt => Some(Constant::Bool(l > r)),
978 _ => None,
979 }
980 },
981 ty::Uint(ity) => {
982 let bits = ity.bits();
983
984 match op {
985 BinOpKind::Add => l.checked_add(r).and_then(|n| ity.ensure_fits(n)).map(Constant::Int),
986 BinOpKind::Sub => l.checked_sub(r).and_then(|n| ity.ensure_fits(n)).map(Constant::Int),
987 BinOpKind::Mul => l.checked_mul(r).and_then(|n| ity.ensure_fits(n)).map(Constant::Int),
988 BinOpKind::Div => l.checked_div(r).map(Constant::Int),
989 BinOpKind::Rem => l.checked_rem(r).map(Constant::Int),
990 BinOpKind::Shr if r < bits => l.checked_shr(r.try_into().ok()?).map(Constant::Int),
991 BinOpKind::Shl if r < bits => l.checked_shl(r.try_into().ok()?).map(Constant::Int),
992 BinOpKind::BitXor => Some(Constant::Int(l ^ r)),
993 BinOpKind::BitOr => Some(Constant::Int(l | r)),
994 BinOpKind::BitAnd => Some(Constant::Int(l & r)),
995 BinOpKind::Eq => Some(Constant::Bool(l == r)),
996 BinOpKind::Ne => Some(Constant::Bool(l != r)),
997 BinOpKind::Lt => Some(Constant::Bool(l < r)),
998 BinOpKind::Le => Some(Constant::Bool(l <= r)),
999 BinOpKind::Ge => Some(Constant::Bool(l >= r)),
1000 BinOpKind::Gt => Some(Constant::Bool(l > r)),
1001 _ => None,
1002 }
1003 },
1004 _ => None,
1005 },
1006 (Constant::F32(l), Some(Constant::F32(r))) => match op {
1008 BinOpKind::Add => Some(Constant::F32(l + r)),
1009 BinOpKind::Sub => Some(Constant::F32(l - r)),
1010 BinOpKind::Mul => Some(Constant::F32(l * r)),
1011 BinOpKind::Div => Some(Constant::F32(l / r)),
1012 BinOpKind::Rem => Some(Constant::F32(l % r)),
1013 BinOpKind::Eq => Some(Constant::Bool(l == r)),
1014 BinOpKind::Ne => Some(Constant::Bool(l != r)),
1015 BinOpKind::Lt => Some(Constant::Bool(l < r)),
1016 BinOpKind::Le => Some(Constant::Bool(l <= r)),
1017 BinOpKind::Ge => Some(Constant::Bool(l >= r)),
1018 BinOpKind::Gt => Some(Constant::Bool(l > r)),
1019 _ => None,
1020 },
1021 (Constant::F64(l), Some(Constant::F64(r))) => match op {
1022 BinOpKind::Add => Some(Constant::F64(l + r)),
1023 BinOpKind::Sub => Some(Constant::F64(l - r)),
1024 BinOpKind::Mul => Some(Constant::F64(l * r)),
1025 BinOpKind::Div => Some(Constant::F64(l / r)),
1026 BinOpKind::Rem => Some(Constant::F64(l % r)),
1027 BinOpKind::Eq => Some(Constant::Bool(l == r)),
1028 BinOpKind::Ne => Some(Constant::Bool(l != r)),
1029 BinOpKind::Lt => Some(Constant::Bool(l < r)),
1030 BinOpKind::Le => Some(Constant::Bool(l <= r)),
1031 BinOpKind::Ge => Some(Constant::Bool(l >= r)),
1032 BinOpKind::Gt => Some(Constant::Bool(l > r)),
1033 _ => None,
1034 },
1035 (l, r) => match (op, l, r) {
1036 (BinOpKind::And, Constant::Bool(false), _) => Some(Constant::Bool(false)),
1037 (BinOpKind::Or, Constant::Bool(true), _) => Some(Constant::Bool(true)),
1038 (BinOpKind::And, Constant::Bool(true), Some(r)) | (BinOpKind::Or, Constant::Bool(false), Some(r)) => {
1039 Some(r)
1040 },
1041 (BinOpKind::BitXor, Constant::Bool(l), Some(Constant::Bool(r))) => Some(Constant::Bool(l ^ r)),
1042 (BinOpKind::BitAnd, Constant::Bool(l), Some(Constant::Bool(r))) => Some(Constant::Bool(l & r)),
1043 (BinOpKind::BitOr, Constant::Bool(l), Some(Constant::Bool(r))) => Some(Constant::Bool(l | r)),
1044 _ => None,
1045 },
1046 }
1047 }
1048}
1049
1050pub fn mir_to_const<'tcx>(tcx: TyCtxt<'tcx>, val: ConstValue, ty: Ty<'tcx>) -> Option<Constant> {
1051 match (val, ty.kind()) {
1052 (_, &ty::Adt(adt_def, _)) if adt_def.is_struct() => Some(Constant::Adt(val)),
1053 (ConstValue::Scalar(Scalar::Int(int)), _) => match ty.kind() {
1054 ty::Bool => Some(Constant::Bool(int == ScalarInt::TRUE)),
1055 ty::Uint(_) | ty::Int(_) => Some(Constant::Int(int.to_bits(int.size()))),
1056 ty::Float(FloatTy::F16) => Some(Constant::F16(int.into())),
1057 ty::Float(FloatTy::F32) => Some(Constant::F32(f32::from_bits(int.into()))),
1058 ty::Float(FloatTy::F64) => Some(Constant::F64(f64::from_bits(int.into()))),
1059 ty::Float(FloatTy::F128) => Some(Constant::F128(int.into())),
1060 ty::RawPtr(_, _) => Some(Constant::RawPtr(int.to_bits(int.size()))),
1061 _ => None,
1062 },
1063 (_, ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Str) => {
1064 let data = val.try_get_slice_bytes_for_diagnostics(tcx)?;
1065 String::from_utf8(data.to_owned()).ok().map(Constant::Str)
1066 },
1067 (ConstValue::Indirect { alloc_id, offset }, ty::Array(sub_type, len)) => {
1068 let alloc = tcx.global_alloc(alloc_id).unwrap_memory().inner();
1069 let len = len.try_to_target_usize(tcx)?;
1070 let ty::Float(flt) = sub_type.kind() else {
1071 return None;
1072 };
1073 let size = Size::from_bits(flt.bit_width());
1074 let mut res = Vec::new();
1075 for idx in 0..len {
1076 let range = alloc_range(offset + size * idx, size);
1077 let val = alloc.read_scalar(&tcx, range, false).ok()?;
1078 res.push(match flt {
1079 FloatTy::F16 => Constant::F16(val.to_u16().discard_err()?),
1080 FloatTy::F32 => Constant::F32(f32::from_bits(val.to_u32().discard_err()?)),
1081 FloatTy::F64 => Constant::F64(f64::from_bits(val.to_u64().discard_err()?)),
1082 FloatTy::F128 => Constant::F128(val.to_u128().discard_err()?),
1083 });
1084 }
1085 Some(Constant::Vec(res))
1086 },
1087 _ => None,
1088 }
1089}
1090
1091fn field_of_struct<'tcx>(
1092 adt_def: ty::AdtDef<'tcx>,
1093 tcx: TyCtxt<'tcx>,
1094 value: ConstValue,
1095 ty: Ty<'tcx>,
1096 field: Symbol,
1097) -> Option<(ConstValue, Ty<'tcx>)> {
1098 if let Some(dc) = tcx.try_destructure_mir_constant_for_user_output(value, ty)
1099 && let Some(dc_variant) = dc.variant
1100 && let Some(variant) = adt_def.variants().get(dc_variant)
1101 && let Some(field_idx) = variant.fields.iter().position(|el| el.name == field)
1102 {
1103 dc.fields.get(field_idx).copied()
1104 } else {
1105 None
1106 }
1107}
1108
1109pub fn integer_const(cx: &LateContext<'_>, expr: &Expr<'_>, ctxt: SyntaxContext) -> Option<u128> {
1116 if let Some(Constant::Int(value)) = ConstEvalCtxt::new(cx).eval_local(expr, ctxt) {
1117 Some(value)
1118 } else {
1119 None
1120 }
1121}
1122
1123#[inline]
1130pub fn is_zero_integer_const(cx: &LateContext<'_>, expr: &Expr<'_>, ctxt: SyntaxContext) -> bool {
1131 integer_const(cx, expr, ctxt) == Some(0)
1132}