1#![expect(clippy::float_cmp)]
6
7use crate::res::MaybeDef;
8use crate::source::{SpanExt, 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#[derive(Debug, Clone)]
34pub enum Constant {
35 Adt(ConstValue),
36 Str(String),
38 Binary(Vec<u8>),
40 Char(char),
42 Int(u128),
44 F16(u16),
47 F32(f32),
49 F64(f64),
51 F128(u128),
54 Bool(bool),
56 Vec(Vec<Self>),
58 Repeat(Box<Self>, u64),
60 Tuple(Vec<Self>),
62 RawPtr(u128),
64 Ref(Box<Self>),
66 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 l.to_bits() == r.to_bits() && !l.is_nan()
137 },
138 (&Self::F32(l), &Self::F32(r)) => {
139 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 _ => 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 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 _ => None,
263 }
264 }
265
266 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 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 Constant::F32(x) => x == f32::NEG_INFINITY,
411 Constant::F64(x) => x == f64::NEG_INFINITY,
412 _ => false,
413 }
414 }
415}
416
417pub 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 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#[derive(Clone, Copy)]
446pub enum ConstantSource {
447 Local,
449 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
500pub 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
519pub struct ConstEvalCtxt<'tcx> {
525 pub tcx: TyCtxt<'tcx>,
526 pub typing_env: ty::TypingEnv<'tcx>,
527 pub typeck: &'tcx TypeckResults<'tcx>,
528 source: Cell<ConstantSource>,
529 ctxt: Cell<SyntaxContext>,
530}
531
532impl<'tcx> ConstEvalCtxt<'tcx> {
533 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 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 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 pub fn eval(&self, e: &Expr<'_>) -> Option<Constant> {
566 self.expr(e)
567 }
568
569 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 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 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 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 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 let value = sext(self.tcx, value, ity);
748
749 if value == min {
751 return None;
752 }
753
754 let value = value.checked_neg()?;
755 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 fn multi(&self, vec: &[Expr<'_>]) -> Option<Vec<Constant>> {
767 vec.iter().map(|elem| self.expr(elem)).collect::<Option<_>>()
768 }
769
770 #[expect(clippy::too_many_lines)]
772 fn fetch_path(&self, qpath: &QPath<'_>, id: HirId) -> Option<ConstValue> {
773 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 _ 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 let args = self.typeck.node_args(id);
889
890 if !args.is_empty() {
891 let owner_def_id = self.typeck.hir_owner.def_id.to_def_id();
892 let identity_args = ty::GenericArgs::identity_for_item(self.tcx, owner_def_id);
893 if self
895 .tcx
896 .instantiate_and_check_impossible_predicates((owner_def_id, identity_args))
897 {
898 return None;
899 }
900 }
901
902 self.tcx
903 .const_eval_resolve(self.typing_env, mir::UnevaluatedConst::new(did, args), qpath.span())
904 .ok()
905 }
906
907 fn index(&self, lhs: &'_ Expr<'_>, index: &'_ Expr<'_>) -> Option<Constant> {
908 let lhs = self.expr(lhs);
909 let index = self.expr(index);
910
911 match (lhs, index) {
912 (Some(Constant::Vec(vec)), Some(Constant::Int(index))) => match vec.get(index as usize) {
913 Some(Constant::F16(x)) => Some(Constant::F16(*x)),
914 Some(Constant::F32(x)) => Some(Constant::F32(*x)),
915 Some(Constant::F64(x)) => Some(Constant::F64(*x)),
916 Some(Constant::F128(x)) => Some(Constant::F128(*x)),
917 _ => None,
918 },
919 (Some(Constant::Vec(vec)), _) => {
920 if !vec.is_empty() && vec.iter().all(|x| *x == vec[0]) {
921 match vec.first() {
922 Some(Constant::F16(x)) => Some(Constant::F16(*x)),
923 Some(Constant::F32(x)) => Some(Constant::F32(*x)),
924 Some(Constant::F64(x)) => Some(Constant::F64(*x)),
925 Some(Constant::F128(x)) => Some(Constant::F128(*x)),
926 _ => None,
927 }
928 } else {
929 None
930 }
931 },
932 _ => None,
933 }
934 }
935
936 fn block(&self, block: &Block<'_>) -> Option<Constant> {
938 if block.stmts.is_empty()
939 && let Some(expr) = block.expr
940 {
941 let span = block.span.data();
943 if span.ctxt == SyntaxContext::root() {
944 if let Some(expr_span) = walk_span_to_context(expr.span, span.ctxt)
945 && let expr_lo = expr_span.lo()
946 && expr_lo >= span.lo
947 && let Some(src) = (span.lo..expr_lo).get_source_range(self.tcx)
948 && let Some(src) = src.as_str()
949 {
950 use rustc_lexer::TokenKind::{BlockComment, LineComment, OpenBrace, Semi, Whitespace};
951 if !tokenize(src, FrontmatterAllowed::No)
952 .map(|t| t.kind)
953 .filter(|t| !matches!(t, Whitespace | LineComment { .. } | BlockComment { .. } | Semi))
954 .eq([OpenBrace])
955 {
956 self.source.set(ConstantSource::NonLocal);
957 }
958 } else {
959 self.source.set(ConstantSource::NonLocal);
961 }
962 }
963
964 self.expr(expr)
965 } else {
966 None
967 }
968 }
969
970 fn ifthenelse(&self, cond: &Expr<'_>, then: &Expr<'_>, otherwise: Option<&Expr<'_>>) -> Option<Constant> {
971 if let Some(Constant::Bool(b)) = self.expr(cond) {
972 if b {
973 self.expr(then)
974 } else {
975 otherwise.as_ref().and_then(|expr| self.expr(expr))
976 }
977 } else {
978 None
979 }
980 }
981
982 fn binop(&self, op: BinOpKind, left: &Expr<'_>, right: &Expr<'_>) -> Option<Constant> {
983 let l = self.expr(left)?;
984 let r = self.expr(right);
985 match (l, r) {
986 (Constant::Int(l), Some(Constant::Int(r))) => match *self.typeck.expr_ty_opt(left)?.kind() {
987 ty::Int(ity) => {
988 let (ty_min_value, _) = ity.min_max()?;
989 let bits = ity.bits();
990 let l = sext(self.tcx, l, ity);
991 let r = sext(self.tcx, r, ity);
992
993 if let BinOpKind::Div | BinOpKind::Rem = op
996 && l == ty_min_value
997 && r == -1
998 {
999 return None;
1000 }
1001
1002 let zext = |n: i128| Constant::Int(unsext(self.tcx, n, ity));
1003 match op {
1004 BinOpKind::Add => l.checked_add(r).and_then(|n| ity.ensure_fits(n)).map(zext),
1007 BinOpKind::Sub => l.checked_sub(r).and_then(|n| ity.ensure_fits(n)).map(zext),
1008 BinOpKind::Mul => l.checked_mul(r).and_then(|n| ity.ensure_fits(n)).map(zext),
1009 BinOpKind::Div if r != 0 => l.checked_div(r).map(zext),
1010 BinOpKind::Rem if r != 0 => l.checked_rem(r).map(zext),
1011 BinOpKind::Shr if r < bits && !r.is_negative() => l.checked_shr(r.try_into().ok()?).map(zext),
1014 BinOpKind::Shl if r < bits && !r.is_negative() => l.checked_shl(r.try_into().ok()?).map(zext),
1015 BinOpKind::BitXor => Some(zext(l ^ r)),
1016 BinOpKind::BitOr => Some(zext(l | r)),
1017 BinOpKind::BitAnd => Some(zext(l & r)),
1018 BinOpKind::Eq => Some(Constant::Bool(l == r)),
1020 BinOpKind::Ne => Some(Constant::Bool(l != r)),
1021 BinOpKind::Lt => Some(Constant::Bool(l < r)),
1022 BinOpKind::Le => Some(Constant::Bool(l <= r)),
1023 BinOpKind::Ge => Some(Constant::Bool(l >= r)),
1024 BinOpKind::Gt => Some(Constant::Bool(l > r)),
1025 _ => None,
1026 }
1027 },
1028 ty::Uint(ity) => {
1029 let bits = ity.bits();
1030
1031 match op {
1032 BinOpKind::Add => l.checked_add(r).and_then(|n| ity.ensure_fits(n)).map(Constant::Int),
1033 BinOpKind::Sub => l.checked_sub(r).and_then(|n| ity.ensure_fits(n)).map(Constant::Int),
1034 BinOpKind::Mul => l.checked_mul(r).and_then(|n| ity.ensure_fits(n)).map(Constant::Int),
1035 BinOpKind::Div => l.checked_div(r).map(Constant::Int),
1036 BinOpKind::Rem => l.checked_rem(r).map(Constant::Int),
1037 BinOpKind::Shr if r < bits => l.checked_shr(r.try_into().ok()?).map(Constant::Int),
1038 BinOpKind::Shl if r < bits => l.checked_shl(r.try_into().ok()?).map(Constant::Int),
1039 BinOpKind::BitXor => Some(Constant::Int(l ^ r)),
1040 BinOpKind::BitOr => Some(Constant::Int(l | r)),
1041 BinOpKind::BitAnd => Some(Constant::Int(l & r)),
1042 BinOpKind::Eq => Some(Constant::Bool(l == r)),
1043 BinOpKind::Ne => Some(Constant::Bool(l != r)),
1044 BinOpKind::Lt => Some(Constant::Bool(l < r)),
1045 BinOpKind::Le => Some(Constant::Bool(l <= r)),
1046 BinOpKind::Ge => Some(Constant::Bool(l >= r)),
1047 BinOpKind::Gt => Some(Constant::Bool(l > r)),
1048 _ => None,
1049 }
1050 },
1051 _ => None,
1052 },
1053 (Constant::F32(l), Some(Constant::F32(r))) => match op {
1055 BinOpKind::Add => Some(Constant::F32(l + r)),
1056 BinOpKind::Sub => Some(Constant::F32(l - r)),
1057 BinOpKind::Mul => Some(Constant::F32(l * r)),
1058 BinOpKind::Div => Some(Constant::F32(l / r)),
1059 BinOpKind::Rem => Some(Constant::F32(l % r)),
1060 BinOpKind::Eq => Some(Constant::Bool(l == r)),
1061 BinOpKind::Ne => Some(Constant::Bool(l != r)),
1062 BinOpKind::Lt => Some(Constant::Bool(l < r)),
1063 BinOpKind::Le => Some(Constant::Bool(l <= r)),
1064 BinOpKind::Ge => Some(Constant::Bool(l >= r)),
1065 BinOpKind::Gt => Some(Constant::Bool(l > r)),
1066 _ => None,
1067 },
1068 (Constant::F64(l), Some(Constant::F64(r))) => match op {
1069 BinOpKind::Add => Some(Constant::F64(l + r)),
1070 BinOpKind::Sub => Some(Constant::F64(l - r)),
1071 BinOpKind::Mul => Some(Constant::F64(l * r)),
1072 BinOpKind::Div => Some(Constant::F64(l / r)),
1073 BinOpKind::Rem => Some(Constant::F64(l % r)),
1074 BinOpKind::Eq => Some(Constant::Bool(l == r)),
1075 BinOpKind::Ne => Some(Constant::Bool(l != r)),
1076 BinOpKind::Lt => Some(Constant::Bool(l < r)),
1077 BinOpKind::Le => Some(Constant::Bool(l <= r)),
1078 BinOpKind::Ge => Some(Constant::Bool(l >= r)),
1079 BinOpKind::Gt => Some(Constant::Bool(l > r)),
1080 _ => None,
1081 },
1082 (l, r) => match (op, l, r) {
1083 (BinOpKind::And, Constant::Bool(false), _) => Some(Constant::Bool(false)),
1084 (BinOpKind::Or, Constant::Bool(true), _) => Some(Constant::Bool(true)),
1085 (BinOpKind::And, Constant::Bool(true), Some(r)) | (BinOpKind::Or, Constant::Bool(false), Some(r)) => {
1086 Some(r)
1087 },
1088 (BinOpKind::BitXor, Constant::Bool(l), Some(Constant::Bool(r))) => Some(Constant::Bool(l ^ r)),
1089 (BinOpKind::BitAnd, Constant::Bool(l), Some(Constant::Bool(r))) => Some(Constant::Bool(l & r)),
1090 (BinOpKind::BitOr, Constant::Bool(l), Some(Constant::Bool(r))) => Some(Constant::Bool(l | r)),
1091 _ => None,
1092 },
1093 }
1094 }
1095}
1096
1097pub fn mir_to_const<'tcx>(tcx: TyCtxt<'tcx>, val: ConstValue, ty: Ty<'tcx>) -> Option<Constant> {
1098 match (val, ty.kind()) {
1099 (_, &ty::Adt(adt_def, _)) if adt_def.is_struct() => Some(Constant::Adt(val)),
1100 (ConstValue::Scalar(Scalar::Int(int)), _) => match ty.kind() {
1101 ty::Bool => Some(Constant::Bool(int == ScalarInt::TRUE)),
1102 ty::Uint(_) | ty::Int(_) => Some(Constant::Int(int.to_bits(int.size()))),
1103 ty::Float(FloatTy::F16) => Some(Constant::F16(int.into())),
1104 ty::Float(FloatTy::F32) => Some(Constant::F32(f32::from_bits(int.into()))),
1105 ty::Float(FloatTy::F64) => Some(Constant::F64(f64::from_bits(int.into()))),
1106 ty::Float(FloatTy::F128) => Some(Constant::F128(int.into())),
1107 ty::RawPtr(_, _) => Some(Constant::RawPtr(int.to_bits(int.size()))),
1108 _ => None,
1109 },
1110 (_, ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Str) => {
1111 let data = val.try_get_slice_bytes_for_diagnostics(tcx)?;
1112 String::from_utf8(data.to_owned()).ok().map(Constant::Str)
1113 },
1114 (ConstValue::Indirect { alloc_id, offset }, ty::Array(sub_type, len)) => {
1115 let alloc = tcx.global_alloc(alloc_id).unwrap_memory().inner();
1116 let len = len.try_to_target_usize(tcx)?;
1117 let ty::Float(flt) = sub_type.kind() else {
1118 return None;
1119 };
1120 let size = Size::from_bits(flt.bit_width());
1121 let mut res = Vec::new();
1122 for idx in 0..len {
1123 let range = alloc_range(offset + size * idx, size);
1124 let val = alloc.read_scalar(&tcx, range, false).ok()?;
1125 res.push(match flt {
1126 FloatTy::F16 => Constant::F16(val.to_u16().discard_err()?),
1127 FloatTy::F32 => Constant::F32(f32::from_bits(val.to_u32().discard_err()?)),
1128 FloatTy::F64 => Constant::F64(f64::from_bits(val.to_u64().discard_err()?)),
1129 FloatTy::F128 => Constant::F128(val.to_u128().discard_err()?),
1130 });
1131 }
1132 Some(Constant::Vec(res))
1133 },
1134 _ => None,
1135 }
1136}
1137
1138fn field_of_struct<'tcx>(
1139 adt_def: ty::AdtDef<'tcx>,
1140 tcx: TyCtxt<'tcx>,
1141 value: ConstValue,
1142 ty: Ty<'tcx>,
1143 field: Symbol,
1144) -> Option<(ConstValue, Ty<'tcx>)> {
1145 if let Some(dc) = tcx.try_destructure_mir_constant_for_user_output(value, ty)
1146 && let Some(dc_variant) = dc.variant
1147 && let Some(variant) = adt_def.variants().get(dc_variant)
1148 && let Some(field_idx) = variant.fields.iter().position(|el| el.name == field)
1149 {
1150 dc.fields.get(field_idx).copied()
1151 } else {
1152 None
1153 }
1154}
1155
1156pub fn integer_const(cx: &LateContext<'_>, expr: &Expr<'_>, ctxt: SyntaxContext) -> Option<u128> {
1163 if let Some(Constant::Int(value)) = ConstEvalCtxt::new(cx).eval_local(expr, ctxt) {
1164 Some(value)
1165 } else {
1166 None
1167 }
1168}
1169
1170#[inline]
1177pub fn is_zero_integer_const(cx: &LateContext<'_>, expr: &Expr<'_>, ctxt: SyntaxContext) -> bool {
1178 integer_const(cx, expr, ctxt) == Some(0)
1179}
1180
1181pub fn const_item_rhs_to_expr<'tcx>(tcx: TyCtxt<'tcx>, ct_rhs: ConstItemRhs<'tcx>) -> Option<&'tcx Expr<'tcx>> {
1182 match ct_rhs {
1183 ConstItemRhs::Body(body_id) => Some(tcx.hir_body(body_id).value),
1184 ConstItemRhs::TypeConst(const_arg) => match const_arg.kind {
1185 ConstArgKind::Anon(anon) => Some(tcx.hir_body(anon.body).value),
1186 ConstArgKind::Struct(..)
1187 | ConstArgKind::Tup(..)
1188 | ConstArgKind::Literal { .. }
1189 | ConstArgKind::TupleCall(..)
1190 | ConstArgKind::Array(..)
1191 | ConstArgKind::Path(_)
1192 | ConstArgKind::Error(..)
1193 | ConstArgKind::Infer(..) => None,
1194 },
1195 }
1196}