Skip to main content

rustc_mir_transform/
known_panics_lint.rs

1//! A lint that checks for known panics like overflows, division by zero,
2//! out-of-bound access etc. Uses const propagation to determine the values of
3//! operands during checks.
4
5use std::fmt::Debug;
6
7use rustc_abi::{BackendRepr, FieldIdx, HasDataLayout, Size, TargetDataLayout, VariantIdx};
8use rustc_attr_ir::find_attr;
9use rustc_const_eval::const_eval::DummyMachine;
10use rustc_const_eval::interpret::{ImmTy, InterpCx, InterpResult, Projectable, Scalar, interp_ok};
11use rustc_data_structures::fx::FxHashSet;
12use rustc_hir::HirId;
13use rustc_hir::def::DefKind;
14use rustc_index::IndexVec;
15use rustc_index::bit_set::DenseBitSet;
16use rustc_lint_defs::builtin::UNCONDITIONAL_PANIC;
17use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
18use rustc_middle::mir::*;
19use rustc_middle::ty::consts::ConstExt;
20use rustc_middle::ty::layout::{LayoutError, LayoutOf, LayoutOfHelpers, TyAndLayout};
21use rustc_middle::ty::{
22    self, ConstInt, GenericArgKind, GenericParamDefKind, ScalarInt, Ty, TyCtxt, TypeVisitableExt,
23    Unnormalized,
24};
25use rustc_span::{Span, bug};
26use tracing::{debug, instrument, trace};
27
28use crate::diagnostics::{AssertLint, AssertLintKind, ConstNIsZero};
29
30pub(super) struct KnownPanicsLint;
31
32impl<'tcx> crate::MirLint<'tcx> for KnownPanicsLint {
33    fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
34        if body.tainted_by_errors.is_some() {
35            return;
36        }
37
38        let def_id = body.source.def_id().expect_local();
39        let def_kind = tcx.def_kind(def_id);
40        let is_fn_like = def_kind.is_fn_like();
41        let is_assoc_const = def_kind == DefKind::AssocConst;
42
43        // Only run const prop on functions, methods, closures and associated constants
44        if !is_fn_like && !is_assoc_const {
45            // skip anon_const/statics/consts because they'll be evaluated by miri anyway
46            trace!("KnownPanicsLint skipped for {:?}", def_id);
47            return;
48        }
49
50        // FIXME(welseywiser) const prop doesn't work on coroutines because of query cycles
51        // computing their layout.
52        if tcx.is_coroutine(def_id.to_def_id()) {
53            trace!("KnownPanicsLint skipped for coroutine {:?}", def_id);
54            return;
55        }
56
57        trace!("KnownPanicsLint starting for {:?}", def_id);
58
59        let mut linter = ConstPropagator::new(body, tcx);
60        linter.visit_body(body);
61
62        trace!("KnownPanicsLint done for {:?}", def_id);
63    }
64}
65
66/// Visits MIR nodes, performs const propagation
67/// and runs lint checks as it goes
68struct ConstPropagator<'mir, 'tcx> {
69    ecx: InterpCx<'tcx, DummyMachine>,
70    tcx: TyCtxt<'tcx>,
71    typing_env: ty::TypingEnv<'tcx>,
72    worklist: Vec<BasicBlock>,
73    visited_blocks: DenseBitSet<BasicBlock>,
74    locals: IndexVec<Local, Value<'tcx>>,
75    body: &'mir Body<'tcx>,
76    written_only_inside_own_block_locals: FxHashSet<Local>,
77    can_const_prop: IndexVec<Local, ConstPropMode>,
78}
79
80#[derive(Debug, Clone)]
81enum Value<'tcx> {
82    Immediate(ImmTy<'tcx>),
83    Aggregate { variant: VariantIdx, fields: IndexVec<FieldIdx, Value<'tcx>> },
84    Uninit,
85}
86
87impl<'tcx> From<ImmTy<'tcx>> for Value<'tcx> {
88    fn from(v: ImmTy<'tcx>) -> Self {
89        Self::Immediate(v)
90    }
91}
92
93impl<'tcx> Value<'tcx> {
94    fn project(
95        &self,
96        proj: &[PlaceElem<'tcx>],
97        prop: &ConstPropagator<'_, 'tcx>,
98    ) -> Option<&Value<'tcx>> {
99        let mut this = self;
100        for proj in proj {
101            this = match (*proj, this) {
102                (PlaceElem::Field(idx, _), Value::Aggregate { fields, .. }) => {
103                    fields.get(idx).unwrap_or(&Value::Uninit)
104                }
105                (PlaceElem::Index(idx), Value::Aggregate { fields, .. }) => {
106                    let idx = prop.get_const(idx.into())?.immediate()?;
107                    let idx = prop.ecx.read_target_usize(idx).discard_err()?.try_into().ok()?;
108                    if idx <= FieldIdx::MAX_AS_U32 {
109                        fields.get(FieldIdx::from_u32(idx)).unwrap_or(&Value::Uninit)
110                    } else {
111                        return None;
112                    }
113                }
114                (
115                    PlaceElem::ConstantIndex { offset, min_length: _, from_end: false },
116                    Value::Aggregate { fields, .. },
117                ) => fields
118                    .get(FieldIdx::from_u32(offset.try_into().ok()?))
119                    .unwrap_or(&Value::Uninit),
120                _ => return None,
121            };
122        }
123        Some(this)
124    }
125
126    fn project_mut(&mut self, proj: &[PlaceElem<'_>]) -> Option<&mut Value<'tcx>> {
127        let mut this = self;
128        for proj in proj {
129            this = match (proj, this) {
130                (PlaceElem::Field(idx, _), Value::Aggregate { fields, .. }) => {
131                    fields.ensure_contains_elem(*idx, || Value::Uninit)
132                }
133                (PlaceElem::Field(..), val @ Value::Uninit) => {
134                    *val =
135                        Value::Aggregate { variant: VariantIdx::ZERO, fields: Default::default() };
136                    val.project_mut(&[*proj])?
137                }
138                _ => return None,
139            };
140        }
141        Some(this)
142    }
143
144    fn immediate(&self) -> Option<&ImmTy<'tcx>> {
145        match self {
146            Value::Immediate(op) => Some(op),
147            _ => None,
148        }
149    }
150}
151
152impl<'tcx> LayoutOfHelpers<'tcx> for ConstPropagator<'_, 'tcx> {
153    type LayoutOfResult = Result<TyAndLayout<'tcx>, LayoutError<'tcx>>;
154
155    #[inline]
156    fn handle_layout_err(&self, err: LayoutError<'tcx>, _: Span, _: Ty<'tcx>) -> LayoutError<'tcx> {
157        err
158    }
159}
160
161impl HasDataLayout for ConstPropagator<'_, '_> {
162    #[inline]
163    fn data_layout(&self) -> &TargetDataLayout {
164        &self.tcx.data_layout
165    }
166}
167
168impl<'tcx> ty::layout::HasTyCtxt<'tcx> for ConstPropagator<'_, 'tcx> {
169    #[inline]
170    fn tcx(&self) -> TyCtxt<'tcx> {
171        self.tcx
172    }
173}
174
175impl<'tcx> ty::layout::HasTypingEnv<'tcx> for ConstPropagator<'_, 'tcx> {
176    #[inline]
177    fn typing_env(&self) -> ty::TypingEnv<'tcx> {
178        self.typing_env
179    }
180}
181
182impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
183    fn new(body: &'mir Body<'tcx>, tcx: TyCtxt<'tcx>) -> ConstPropagator<'mir, 'tcx> {
184        let def_id = body.source.def_id();
185        // FIXME(#132279): This is used during the phase transition from analysis
186        // to runtime, so we have to manually specify the correct typing mode.
187        let typing_env = ty::TypingEnv::post_analysis(tcx, body.source.def_id());
188        let can_const_prop = CanConstProp::check(tcx, typing_env, body);
189        let ecx = InterpCx::new(tcx, tcx.def_span(def_id), typing_env, DummyMachine);
190
191        ConstPropagator {
192            ecx,
193            tcx,
194            typing_env,
195            worklist: vec![START_BLOCK],
196            visited_blocks: DenseBitSet::new_empty(body.basic_blocks.len()),
197            locals: IndexVec::from_elem_n(Value::Uninit, body.local_decls.len()),
198            body,
199            can_const_prop,
200            written_only_inside_own_block_locals: Default::default(),
201        }
202    }
203
204    fn local_decls(&self) -> &'mir LocalDecls<'tcx> {
205        &self.body.local_decls
206    }
207
208    fn get_const(&self, place: Place<'tcx>) -> Option<&Value<'tcx>> {
209        self.locals[place.local].project(&place.projection, self)
210    }
211
212    /// Remove `local` from the pool of `Locals`. Allows writing to them,
213    /// but not reading from them anymore.
214    fn remove_const(&mut self, local: Local) {
215        self.locals[local] = Value::Uninit;
216        self.written_only_inside_own_block_locals.remove(&local);
217    }
218
219    fn access_mut(&mut self, place: &Place<'_>) -> Option<&mut Value<'tcx>> {
220        match self.can_const_prop[place.local] {
221            ConstPropMode::NoPropagation => return None,
222            ConstPropMode::OnlyInsideOwnBlock => {
223                self.written_only_inside_own_block_locals.insert(place.local);
224            }
225            ConstPropMode::FullConstProp => {}
226        }
227        self.locals[place.local].project_mut(place.projection)
228    }
229
230    fn lint_root(&self, source_info: SourceInfo) -> Option<HirId> {
231        source_info.scope.lint_root(&self.body.source_scopes)
232    }
233
234    fn use_ecx<F, T>(&mut self, f: F) -> Option<T>
235    where
236        F: FnOnce(&mut Self) -> InterpResult<'tcx, T>,
237    {
238        f(self)
239            .inspect_err_info(|err| {
240                trace!("InterpCx operation failed: {:?}", err);
241                // Some errors shouldn't come up because creating them causes
242                // an allocation, which we should avoid. When that happens,
243                // dedicated error variants should be introduced instead.
244                assert!(
245                    !err.kind().formatted_string(),
246                    "known panics lint encountered formatting error: {}",
247                    err.to_string(),
248                );
249            })
250            .discard_err()
251    }
252
253    /// Returns the value, if any, of evaluating `c`.
254    fn eval_constant(&mut self, c: &ConstOperand<'tcx>) -> Option<ImmTy<'tcx>> {
255        // FIXME we need to revisit this for #67176
256        if c.has_param() {
257            return None;
258        }
259
260        // Normalization needed b/c known panics lint runs in
261        // `mir_drops_elaborated_and_const_checked`, which happens before
262        // optimized MIR. Only after optimizing the MIR can we guarantee
263        // that the `PostAnalysisNormalize` pass has happened and that the body's consts
264        // are normalized, so any call to resolve before that needs to be
265        // manually normalized.
266        let val = self
267            .tcx
268            .try_normalize_erasing_regions(self.typing_env, Unnormalized::new_wip(c.const_))
269            .ok()?;
270
271        self.use_ecx(|this| this.ecx.eval_mir_constant(&val, c.span, None))?
272            .as_mplace_or_imm()
273            .right()
274    }
275
276    /// Returns the value, if any, of evaluating `place`.
277    #[instrument(level = "trace", skip(self), ret)]
278    fn eval_place(&mut self, place: Place<'tcx>) -> Option<ImmTy<'tcx>> {
279        match self.get_const(place)? {
280            Value::Immediate(imm) => Some(imm.clone()),
281            Value::Aggregate { .. } => None,
282            Value::Uninit => None,
283        }
284    }
285
286    /// Returns the value, if any, of evaluating `op`. Calls upon `eval_constant`
287    /// or `eval_place`, depending on the variant of `Operand` used.
288    fn eval_operand(&mut self, op: &Operand<'tcx>) -> Option<ImmTy<'tcx>> {
289        match *op {
290            Operand::RuntimeChecks(_) => None,
291            Operand::Constant(ref c) => self.eval_constant(c),
292            Operand::Move(place) | Operand::Copy(place) => self.eval_place(place),
293        }
294    }
295
296    fn report_assert_as_lint(
297        &self,
298        location: Location,
299        lint_kind: AssertLintKind,
300        assert_kind: AssertKind<impl Debug>,
301    ) {
302        let source_info = self.body.source_info(location);
303        if let Some(lint_root) = self.lint_root(*source_info) {
304            let span = source_info.span;
305            self.tcx.emit_node_span_lint(
306                lint_kind.lint(),
307                lint_root,
308                span,
309                AssertLint { span, assert_kind, lint_kind },
310            );
311        }
312    }
313
314    fn check_unary_op(&mut self, op: UnOp, arg: &Operand<'tcx>, location: Location) -> Option<()> {
315        let arg = self.eval_operand(arg)?;
316        // The only operator that can overflow is `Neg`.
317        if op == UnOp::Neg && arg.layout.ty.is_integral() {
318            // Compute this as `0 - arg` so we can use `SubWithOverflow` to check for overflow.
319            let (arg, overflow) = self.use_ecx(|this| {
320                let arg = this.ecx.read_immediate(&arg)?;
321                let (_res, overflow) = this
322                    .ecx
323                    .binary_op(BinOp::SubWithOverflow, &ImmTy::from_int(0, arg.layout), &arg)?
324                    .to_scalar_pair();
325                interp_ok((arg, overflow.to_bool()?))
326            })?;
327            if overflow {
328                self.report_assert_as_lint(
329                    location,
330                    AssertLintKind::ArithmeticOverflow,
331                    AssertKind::OverflowNeg(arg.to_const_int()),
332                );
333                return None;
334            }
335        }
336
337        Some(())
338    }
339
340    fn check_binary_op(
341        &mut self,
342        op: BinOp,
343        left: &Operand<'tcx>,
344        right: &Operand<'tcx>,
345        location: Location,
346    ) -> Option<()> {
347        let r =
348            self.eval_operand(right).and_then(|r| self.use_ecx(|this| this.ecx.read_immediate(&r)));
349        let l =
350            self.eval_operand(left).and_then(|l| self.use_ecx(|this| this.ecx.read_immediate(&l)));
351        // Check for exceeding shifts *even if* we cannot evaluate the LHS.
352        if matches!(op, BinOp::Shr | BinOp::Shl) {
353            let r = r.clone()?;
354            // We need the type of the LHS. We cannot use `place_layout` as that is the type
355            // of the result, which for checked binops is not the same!
356            let left_ty = left.ty(self.local_decls(), self.tcx);
357            let left_size = self.ecx.layout_of(left_ty).ok()?.size;
358            let right_size = r.layout.size;
359            let r_bits = r.to_scalar().to_bits(right_size).discard_err();
360            if r_bits.is_some_and(|b| b >= left_size.bits() as u128) {
361                debug!("check_binary_op: reporting assert for {:?}", location);
362                let panic = AssertKind::Overflow(
363                    op,
364                    // Invent a dummy value, the diagnostic ignores it anyway
365                    ConstInt::new(
366                        ScalarInt::try_from_uint(1_u8, left_size).unwrap(),
367                        left_ty.is_signed(),
368                        left_ty.is_ptr_sized_integral(),
369                    ),
370                    r.to_const_int(),
371                );
372                self.report_assert_as_lint(location, AssertLintKind::ArithmeticOverflow, panic);
373                return None;
374            }
375        }
376
377        // Div/Rem are handled via the assertions they trigger.
378        // But for Add/Sub/Mul, those assertions only exist in debug builds, and we want to
379        // lint in release builds as well, so we check on the operation instead.
380        // So normalize to the "overflowing" operator, and then ensure that it
381        // actually is an overflowing operator.
382        let op = op.wrapping_to_overflowing().unwrap_or(op);
383        // The remaining operators are handled through `wrapping_to_overflowing`.
384        if let (Some(l), Some(r)) = (l, r)
385            && l.layout.ty.is_integral()
386            && op.is_overflowing()
387            && self.use_ecx(|this| {
388                let (_res, overflow) = this.ecx.binary_op(op, &l, &r)?.to_scalar_pair();
389                overflow.to_bool()
390            })?
391        {
392            self.report_assert_as_lint(
393                location,
394                AssertLintKind::ArithmeticOverflow,
395                AssertKind::Overflow(op, l.to_const_int(), r.to_const_int()),
396            );
397            return None;
398        }
399
400        Some(())
401    }
402
403    fn check_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) -> Option<()> {
404        // Perform any special handling for specific Rvalue types.
405        // Generally, checks here fall into one of two categories:
406        //   1. Additional checking to provide useful lints to the user
407        //        - In this case, we will do some validation and then fall through to the
408        //          end of the function which evals the assignment.
409        //   2. Working around bugs in other parts of the compiler
410        //        - In this case, we'll return `None` from this function to stop evaluation.
411        match rvalue {
412            // Additional checking: give lints to the user if an overflow would occur.
413            // We do this here and not in the `Assert` terminator as that terminator is
414            // only sometimes emitted (overflow checks can be disabled), but we want to always
415            // lint.
416            Rvalue::UnaryOp(op, arg) => {
417                trace!("checking UnaryOp(op = {:?}, arg = {:?})", op, arg);
418                self.check_unary_op(*op, arg, location)?;
419            }
420            Rvalue::BinaryOp(op, (left, right)) => {
421                trace!("checking BinaryOp(op = {:?}, left = {:?}, right = {:?})", op, left, right);
422                self.check_binary_op(*op, left, right, location)?;
423            }
424
425            // Do not try creating references (#67862)
426            Rvalue::RawPtr(_, place) | Rvalue::Ref(_, _, place) | Rvalue::Reborrow(_, _, place) => {
427                trace!("skipping RawPtr | Ref | Reborrow for {:?}", place);
428
429                // This may be creating mutable references or immutable references to cells.
430                // If that happens, the pointed to value could be mutated via that reference.
431                // Since we aren't tracking references, the const propagator loses track of what
432                // value the local has right now.
433                // Thus, all locals that have their reference taken
434                // must not take part in propagation.
435                self.remove_const(place.local);
436
437                return None;
438            }
439            Rvalue::ThreadLocalRef(def_id) => {
440                trace!("skipping ThreadLocalRef({:?})", def_id);
441
442                return None;
443            }
444
445            // There's no other checking to do at this time.
446            Rvalue::Aggregate(..)
447            | Rvalue::Use(..)
448            | Rvalue::CopyForDeref(..)
449            | Rvalue::Repeat(..)
450            | Rvalue::Cast(..)
451            | Rvalue::Discriminant(..)
452            | Rvalue::WrapUnsafeBinder(..) => {}
453        }
454
455        // FIXME we need to revisit this for #67176
456        if rvalue.has_param() {
457            return None;
458        }
459        if !rvalue.ty(self.local_decls(), self.tcx).is_sized(self.tcx, self.typing_env) {
460            // the interpreter doesn't support unsized locals (only unsized arguments),
461            // but rustc does (in a kinda broken way), so we have to skip them here
462            return None;
463        }
464
465        Some(())
466    }
467
468    fn check_assertion(
469        &mut self,
470        expected: bool,
471        msg: &AssertKind<Operand<'tcx>>,
472        cond: &Operand<'tcx>,
473        location: Location,
474    ) {
475        let Some(value) = &self.eval_operand(cond) else { return };
476        trace!("assertion on {:?} should be {:?}", value, expected);
477
478        let expected = Scalar::from_bool(expected);
479        let Some(value_const) = self.use_ecx(|this| this.ecx.read_scalar(value)) else { return };
480
481        if expected != value_const {
482            // Poison all places this operand references so that further code
483            // doesn't use the invalid value
484            if let Some(place) = cond.place() {
485                self.remove_const(place.local);
486            }
487
488            enum DbgVal<T> {
489                Val(T),
490                Underscore,
491            }
492            impl<T: std::fmt::Debug> std::fmt::Debug for DbgVal<T> {
493                fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
494                    match self {
495                        Self::Val(val) => val.fmt(fmt),
496                        Self::Underscore => fmt.write_str("_"),
497                    }
498                }
499            }
500            let mut eval_to_int = |op| {
501                // This can be `None` if the lhs wasn't const propagated and we just
502                // triggered the assert on the value of the rhs.
503                self.eval_operand(op)
504                    .and_then(|op| self.ecx.read_immediate(&op).discard_err())
505                    .map_or(DbgVal::Underscore, |op| DbgVal::Val(op.to_const_int()))
506            };
507            let msg = match msg {
508                AssertKind::DivisionByZero(op) => AssertKind::DivisionByZero(eval_to_int(op)),
509                AssertKind::RemainderByZero(op) => AssertKind::RemainderByZero(eval_to_int(op)),
510                AssertKind::Overflow(bin_op @ (BinOp::Div | BinOp::Rem), op1, op2) => {
511                    // Division overflow is *UB* in the MIR, and different than the
512                    // other overflow checks.
513                    AssertKind::Overflow(*bin_op, eval_to_int(op1), eval_to_int(op2))
514                }
515                AssertKind::BoundsCheck { len, index } => {
516                    let len = eval_to_int(len);
517                    let index = eval_to_int(index);
518                    AssertKind::BoundsCheck { len, index }
519                }
520                // Remaining overflow errors are already covered by checks on the binary operators.
521                AssertKind::Overflow(..) | AssertKind::OverflowNeg(_) => return,
522                // Need proper const propagator for these.
523                _ => return,
524            };
525            self.report_assert_as_lint(location, AssertLintKind::UnconditionalPanic, msg);
526        }
527    }
528
529    fn ensure_not_propagated(&self, local: Local) {
530        if cfg!(debug_assertions) {
531            let val = self.get_const(local.into());
532            assert!(
533                matches!(val, Some(Value::Uninit))
534                    || self
535                        .layout_of(self.local_decls()[local].ty)
536                        .map_or(true, |layout| layout.is_zst()),
537                "failed to remove values for `{local:?}`, value={val:?}",
538            )
539        }
540    }
541
542    #[instrument(level = "trace", skip(self), ret)]
543    fn eval_rvalue(&mut self, rvalue: &Rvalue<'tcx>, dest: &Place<'tcx>) -> Option<()> {
544        if !dest.projection.is_empty() {
545            return None;
546        }
547        use rustc_middle::mir::Rvalue::*;
548        let layout = self.ecx.layout_of(dest.ty(self.body, self.tcx).ty).ok()?;
549        trace!(?layout);
550
551        let val: Value<'_> = match *rvalue {
552            ThreadLocalRef(_) => return None,
553
554            Use(ref operand, _) | WrapUnsafeBinder(ref operand, _) => {
555                self.eval_operand(operand)?.into()
556            }
557
558            CopyForDeref(place) | Reborrow(_, _, place) => self.eval_place(place)?.into(),
559
560            BinaryOp(bin_op, (ref left, ref right)) => {
561                let left = self.eval_operand(left)?;
562                let left = self.use_ecx(|this| this.ecx.read_immediate(&left))?;
563
564                let right = self.eval_operand(right)?;
565                let right = self.use_ecx(|this| this.ecx.read_immediate(&right))?;
566
567                let val = self.use_ecx(|this| this.ecx.binary_op(bin_op, &left, &right))?;
568                if matches!(val.layout.backend_repr, BackendRepr::ScalarPair { .. }) {
569                    // FIXME `Value` should properly support pairs in `Immediate`... but currently
570                    // it does not.
571                    let (val, overflow) = val.to_pair(&self.ecx);
572                    Value::Aggregate {
573                        variant: VariantIdx::ZERO,
574                        fields: [val.into(), overflow.into()].into_iter().collect(),
575                    }
576                } else {
577                    val.into()
578                }
579            }
580
581            UnaryOp(un_op, ref operand) => {
582                let operand = self.eval_operand(operand)?;
583                let val = self.use_ecx(|this| this.ecx.read_immediate(&operand))?;
584
585                let val = self.use_ecx(|this| this.ecx.unary_op(un_op, &val))?;
586                val.into()
587            }
588
589            Aggregate(ref kind, ref fields) => Value::Aggregate {
590                fields: fields
591                    .iter()
592                    .map(|field| self.eval_operand(field).map_or(Value::Uninit, Value::Immediate))
593                    .collect(),
594                variant: match **kind {
595                    AggregateKind::Adt(_, variant, _, _, _) => variant,
596                    AggregateKind::Array(_)
597                    | AggregateKind::Tuple
598                    | AggregateKind::RawPtr(_, _)
599                    | AggregateKind::Closure(_, _)
600                    | AggregateKind::Coroutine(_, _)
601                    | AggregateKind::CoroutineClosure(_, _) => VariantIdx::ZERO,
602                },
603            },
604
605            Repeat(ref op, n) => {
606                trace!(?op, ?n);
607                return None;
608            }
609
610            Ref(..) | RawPtr(..) => return None,
611
612            Cast(ref kind, ref value, to) => match kind {
613                CastKind::IntToInt | CastKind::IntToFloat => {
614                    let value = self.eval_operand(value)?;
615                    let value = self.ecx.read_immediate(&value).discard_err()?;
616                    let to = self.ecx.layout_of(to).ok()?;
617                    let res = self.ecx.int_to_int_or_float(&value, to).discard_err()?;
618                    res.into()
619                }
620                CastKind::FloatToFloat | CastKind::FloatToInt => {
621                    let value = self.eval_operand(value)?;
622                    let value = self.ecx.read_immediate(&value).discard_err()?;
623                    let to = self.ecx.layout_of(to).ok()?;
624                    let res = self.ecx.float_to_float_or_int(&value, to).discard_err()?;
625                    res.into()
626                }
627                CastKind::Transmute | CastKind::Subtype => {
628                    let value = self.eval_operand(value)?;
629                    let to = self.ecx.layout_of(to).ok()?;
630                    // `offset` for immediates only supports scalar/scalar-pair ABIs,
631                    // so bail out if the target is not one.
632                    match (value.layout.backend_repr, to.backend_repr) {
633                        (BackendRepr::Scalar(..), BackendRepr::Scalar(..)) => {}
634                        (BackendRepr::ScalarPair { .. }, BackendRepr::ScalarPair { .. }) => {}
635                        _ => return None,
636                    }
637
638                    value.offset(Size::ZERO, to, &self.ecx).discard_err()?.into()
639                }
640                _ => return None,
641            },
642
643            Discriminant(place) => {
644                let variant = match self.get_const(place)? {
645                    Value::Immediate(op) => {
646                        let op = op.clone();
647                        self.use_ecx(|this| this.ecx.read_discriminant(&op))?
648                    }
649                    Value::Aggregate { variant, .. } => *variant,
650                    Value::Uninit => return None,
651                };
652                let imm = self.use_ecx(|this| {
653                    this.ecx.discriminant_for_variant(
654                        place.ty(this.local_decls(), this.tcx).ty,
655                        variant,
656                    )
657                })?;
658                imm.into()
659            }
660        };
661        trace!(?val);
662
663        *self.access_mut(dest)? = val;
664
665        Some(())
666    }
667}
668
669impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
670    fn visit_body(&mut self, body: &Body<'tcx>) {
671        while let Some(bb) = self.worklist.pop() {
672            if !self.visited_blocks.insert(bb) {
673                continue;
674            }
675
676            let data = &body.basic_blocks[bb];
677            self.visit_basic_block_data(bb, data);
678        }
679    }
680
681    fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
682        self.super_operand(operand, location);
683    }
684
685    fn visit_const_operand(&mut self, constant: &ConstOperand<'tcx>, location: Location) {
686        trace!("visit_const_operand: {:?}", constant);
687        self.super_const_operand(constant, location);
688        self.eval_constant(constant);
689    }
690
691    fn visit_assign(&mut self, place: &Place<'tcx>, rvalue: &Rvalue<'tcx>, location: Location) {
692        self.super_assign(place, rvalue, location);
693
694        let Some(()) = self.check_rvalue(rvalue, location) else { return };
695
696        match self.can_const_prop[place.local] {
697            // Do nothing if the place is indirect.
698            _ if place.is_indirect() => {}
699            ConstPropMode::NoPropagation => self.ensure_not_propagated(place.local),
700            ConstPropMode::OnlyInsideOwnBlock | ConstPropMode::FullConstProp => {
701                if self.eval_rvalue(rvalue, place).is_none() {
702                    // Const prop failed, so erase the destination, ensuring that whatever happens
703                    // from here on, does not know about the previous value.
704                    // This is important in case we have
705                    // ```rust
706                    // let mut x = 42;
707                    // x = SOME_MUTABLE_STATIC;
708                    // // x must now be uninit
709                    // ```
710                    // FIXME: we overzealously erase the entire local, because that's easier to
711                    // implement.
712                    trace!(
713                        "propagation into {:?} failed.
714                        Nuking the entire site from orbit, it's the only way to be sure",
715                        place,
716                    );
717                    self.remove_const(place.local);
718                }
719            }
720        }
721    }
722
723    fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
724        trace!("visit_statement: {:?}", statement);
725
726        // We want to evaluate operands before any change to the assigned-to value,
727        // so we recurse first.
728        self.super_statement(statement, location);
729
730        match statement.kind {
731            StatementKind::SetDiscriminant { ref place, variant_index } => {
732                match self.can_const_prop[place.local] {
733                    // Do nothing if the place is indirect.
734                    _ if place.is_indirect() => {}
735                    ConstPropMode::NoPropagation => self.ensure_not_propagated(place.local),
736                    ConstPropMode::FullConstProp | ConstPropMode::OnlyInsideOwnBlock => {
737                        match self.access_mut(place) {
738                            Some(Value::Aggregate { variant, .. }) => *variant = variant_index,
739                            _ => self.remove_const(place.local),
740                        }
741                    }
742                }
743            }
744            StatementKind::StorageLive(local) => {
745                self.remove_const(local);
746            }
747            StatementKind::StorageDead(local) => {
748                self.remove_const(local);
749            }
750            _ => {}
751        }
752    }
753
754    fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
755        self.super_terminator(terminator, location);
756        match &terminator.kind {
757            TerminatorKind::Assert { expected, msg, cond, .. } => {
758                self.check_assertion(*expected, msg, cond, location);
759            }
760            TerminatorKind::SwitchInt { discr, targets } => {
761                if let Some(ref value) = self.eval_operand(discr)
762                    && let Some(value_const) = self.use_ecx(|this| this.ecx.read_scalar(value))
763                    && let Some(constant) = value_const.to_bits(value_const.size()).discard_err()
764                {
765                    // We managed to evaluate the discriminant, so we know we only need to visit
766                    // one target.
767                    let target = targets.target_for_value(constant);
768                    self.worklist.push(target);
769                    return;
770                }
771                // We failed to evaluate the discriminant, fallback to visiting all successors.
772            }
773            TerminatorKind::Call { func, args: _, .. } => {
774                if let Some((def_id, generic_args)) = func.const_fn_def() {
775                    for (index, arg) in generic_args.iter().enumerate() {
776                        if let GenericArgKind::Const(ct) = arg.kind() {
777                            let generics = self.tcx.generics_of(def_id);
778                            let param_def = generics.param_at(index, self.tcx);
779
780                            if let GenericParamDefKind::Const { .. } = param_def.kind
781                                && find_attr!(self.tcx, param_def.def_id, RustcPanicsWhenZero)
782                                && let Some(0) = ct.try_to_target_usize(self.tcx)
783                            {
784                                // We managed to figure-out that the value of a
785                                // `#[rustc_panics_when_zero]` const-generic parameter is zero.
786                                //
787                                // Let's report it as an unconditional panic.
788                                let source_info = self.body.source_info(location);
789                                if let Some(lint_root) = self.lint_root(*source_info) {
790                                    self.tcx.emit_node_span_lint(
791                                        UNCONDITIONAL_PANIC,
792                                        lint_root,
793                                        source_info.span,
794                                        ConstNIsZero {
795                                            const_param_span: source_info.span,
796                                            const_param_name: param_def.name,
797                                        },
798                                    );
799                                }
800                            }
801                        }
802                    }
803                }
804            }
805            // None of these have Operands to const-propagate.
806            TerminatorKind::Goto { .. }
807            | TerminatorKind::UnwindResume
808            | TerminatorKind::UnwindTerminate(_)
809            | TerminatorKind::Return
810            | TerminatorKind::TailCall { .. }
811            | TerminatorKind::Unreachable
812            | TerminatorKind::Drop { .. }
813            | TerminatorKind::Yield { .. }
814            | TerminatorKind::CoroutineDrop
815            | TerminatorKind::FalseEdge { .. }
816            | TerminatorKind::FalseUnwind { .. }
817            | TerminatorKind::InlineAsm { .. } => {}
818        }
819
820        self.worklist.extend(terminator.successors());
821    }
822
823    fn visit_basic_block_data(&mut self, block: BasicBlock, data: &BasicBlockData<'tcx>) {
824        self.super_basic_block_data(block, data);
825
826        // We remove all Locals which are restricted in propagation to their containing blocks and
827        // which were modified in the current block.
828        // Take it out of the ecx so we can get a mutable reference to the ecx for `remove_const`.
829        let mut written_only_inside_own_block_locals =
830            std::mem::take(&mut self.written_only_inside_own_block_locals);
831
832        // This loop can get very hot for some bodies: it check each local in each bb.
833        // To avoid this quadratic behaviour, we only clear the locals that were modified inside
834        // the current block.
835        // The order in which we remove consts does not matter.
836        #[allow(rustc::potential_query_instability)]
837        for local in written_only_inside_own_block_locals.drain() {
838            debug_assert_eq!(self.can_const_prop[local], ConstPropMode::OnlyInsideOwnBlock);
839            self.remove_const(local);
840        }
841        self.written_only_inside_own_block_locals = written_only_inside_own_block_locals;
842
843        if cfg!(debug_assertions) {
844            for (local, &mode) in self.can_const_prop.iter_enumerated() {
845                match mode {
846                    ConstPropMode::FullConstProp => {}
847                    ConstPropMode::NoPropagation | ConstPropMode::OnlyInsideOwnBlock => {
848                        self.ensure_not_propagated(local);
849                    }
850                }
851            }
852        }
853    }
854}
855
856/// The maximum number of bytes that we'll allocate space for a local or the return value.
857/// Needed for #66397, because otherwise we eval into large places and that can cause OOM or just
858/// Severely regress performance.
859const MAX_ALLOC_LIMIT: u64 = 1024;
860
861/// The mode that `ConstProp` is allowed to run in for a given `Local`.
862#[derive(Clone, Copy, Debug, PartialEq)]
863enum ConstPropMode {
864    /// The `Local` can be propagated into and reads of this `Local` can also be propagated.
865    FullConstProp,
866    /// The `Local` can only be propagated into and from its own block.
867    OnlyInsideOwnBlock,
868    /// The `Local` cannot be part of propagation at all. Any statement
869    /// referencing it either for reading or writing will not get propagated.
870    NoPropagation,
871}
872
873/// A visitor that determines locals in a MIR body
874/// that can be const propagated
875struct CanConstProp {
876    can_const_prop: IndexVec<Local, ConstPropMode>,
877    // False at the beginning. Once set, no more assignments are allowed to that local.
878    found_assignment: DenseBitSet<Local>,
879}
880
881impl CanConstProp {
882    /// Returns true if `local` can be propagated
883    fn check<'tcx>(
884        tcx: TyCtxt<'tcx>,
885        typing_env: ty::TypingEnv<'tcx>,
886        body: &Body<'tcx>,
887    ) -> IndexVec<Local, ConstPropMode> {
888        let mut cpv = CanConstProp {
889            can_const_prop: IndexVec::from_elem(ConstPropMode::FullConstProp, &body.local_decls),
890            found_assignment: DenseBitSet::new_empty(body.local_decls.len()),
891        };
892        for (local, val) in cpv.can_const_prop.iter_enumerated_mut() {
893            let ty = body.local_decls[local].ty;
894            if ty.is_async_drop_in_place_coroutine(tcx) {
895                // No const propagation for async drop coroutine (AsyncDropGlue).
896                // Otherwise, tcx.layout_of(typing_env.as_query_input(ty)) will be called
897                // (early layout request for async drop coroutine) to calculate layout size.
898                // Layout for `async_drop_in_place<T>::{closure}` may only be known with known T.
899                *val = ConstPropMode::NoPropagation;
900                continue;
901            } else if ty.is_union() {
902                // Unions are incompatible with the current implementation of
903                // const prop because Rust has no concept of an active
904                // variant of a union
905                *val = ConstPropMode::NoPropagation;
906            } else {
907                match tcx.layout_of(typing_env.as_query_input(ty)) {
908                    Ok(layout) if layout.size < Size::from_bytes(MAX_ALLOC_LIMIT) => {}
909                    // Either the layout fails to compute, then we can't use this local anyway
910                    // or the local is too large, then we don't want to.
911                    _ => {
912                        *val = ConstPropMode::NoPropagation;
913                        continue;
914                    }
915                }
916            }
917        }
918        // Consider that arguments are assigned on entry.
919        for arg in body.args_iter() {
920            cpv.found_assignment.insert(arg);
921        }
922        cpv.visit_body(body);
923        cpv.can_const_prop
924    }
925}
926
927impl<'tcx> Visitor<'tcx> for CanConstProp {
928    fn visit_place(&mut self, place: &Place<'tcx>, mut context: PlaceContext, loc: Location) {
929        use rustc_middle::mir::visit::PlaceContext::*;
930
931        // Dereferencing just read the address of `place.local`.
932        if place.projection.first() == Some(&PlaceElem::Deref) {
933            context = NonMutatingUse(NonMutatingUseContext::Copy);
934        }
935
936        self.visit_local(place.local, context, loc);
937        self.visit_projection(place.as_ref(), context, loc);
938    }
939
940    fn visit_local(&mut self, local: Local, context: PlaceContext, _: Location) {
941        use rustc_middle::mir::visit::PlaceContext::*;
942        match context {
943            // These are just stores, where the storing is not propagatable, but there may be later
944            // mutations of the same local via `Store`
945            | MutatingUse(MutatingUseContext::Call)
946            | MutatingUse(MutatingUseContext::AsmOutput)
947            // Actual store that can possibly even propagate a value
948            | MutatingUse(MutatingUseContext::Store)
949            | MutatingUse(MutatingUseContext::SetDiscriminant) => {
950                if !self.found_assignment.insert(local) {
951                    match &mut self.can_const_prop[local] {
952                        // If the local can only get propagated in its own block, then we don't have
953                        // to worry about multiple assignments, as we'll nuke the const state at the
954                        // end of the block anyway, and inside the block we overwrite previous
955                        // states as applicable.
956                        ConstPropMode::OnlyInsideOwnBlock => {}
957                        ConstPropMode::NoPropagation => {}
958                        other @ ConstPropMode::FullConstProp => {
959                            trace!(
960                                "local {:?} can't be propagated because of multiple assignments. Previous state: {:?}",
961                                local, other,
962                            );
963                            *other = ConstPropMode::OnlyInsideOwnBlock;
964                        }
965                    }
966                }
967            }
968            // Reading constants is allowed an arbitrary number of times
969            NonMutatingUse(NonMutatingUseContext::Copy)
970            | NonMutatingUse(NonMutatingUseContext::Move)
971            | NonMutatingUse(NonMutatingUseContext::Inspect)
972            | NonMutatingUse(NonMutatingUseContext::PlaceMention)
973            | NonUse(_) => {}
974
975            // These could be propagated with a smarter analysis or just some careful thinking about
976            // whether they'd be fine right now.
977            MutatingUse(MutatingUseContext::Yield)
978            | MutatingUse(MutatingUseContext::Drop)
979            // These can't ever be propagated under any scheme, as we can't reason about indirect
980            // mutation.
981            | NonMutatingUse(NonMutatingUseContext::SharedBorrow)
982            | NonMutatingUse(NonMutatingUseContext::FakeBorrow)
983            | NonMutatingUse(NonMutatingUseContext::RawBorrow)
984            | MutatingUse(MutatingUseContext::Borrow)
985            | MutatingUse(MutatingUseContext::RawBorrow) => {
986                trace!("local {:?} can't be propagated because it's used: {:?}", local, context);
987                self.can_const_prop[local] = ConstPropMode::NoPropagation;
988            }
989            MutatingUse(MutatingUseContext::Projection)
990            | NonMutatingUse(NonMutatingUseContext::Projection) => {
991                bug!("visit_place should not pass {context:?} for {local:?}")
992            }
993        }
994    }
995}