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