|
| 1 | +use rustc_errors::DiagnosticBuilder; |
| 2 | +use rustc_middle::lint::LintDiagnosticBuilder; |
| 3 | +use rustc_middle::mir::visit::Visitor; |
| 4 | +use rustc_middle::mir::*; |
| 5 | +use rustc_middle::ty::TyCtxt; |
| 6 | +use rustc_session::lint::builtin::CONST_ITEM_MUTATION; |
| 7 | +use rustc_span::def_id::DefId; |
| 8 | + |
| 9 | +use crate::transform::{MirPass, MirSource}; |
| 10 | + |
| 11 | +pub struct CheckConstItemMutation; |
| 12 | + |
| 13 | +impl<'tcx> MirPass<'tcx> for CheckConstItemMutation { |
| 14 | + fn run_pass(&self, tcx: TyCtxt<'tcx>, _src: MirSource<'tcx>, body: &mut Body<'tcx>) { |
| 15 | + let mut checker = ConstMutationChecker { body, tcx, target_local: None }; |
| 16 | + checker.visit_body(&body); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +struct ConstMutationChecker<'a, 'tcx> { |
| 21 | + body: &'a Body<'tcx>, |
| 22 | + tcx: TyCtxt<'tcx>, |
| 23 | + target_local: Option<Local>, |
| 24 | +} |
| 25 | + |
| 26 | +impl<'a, 'tcx> ConstMutationChecker<'a, 'tcx> { |
| 27 | + fn is_const_item(&self, local: Local) -> Option<DefId> { |
| 28 | + if let Some(box LocalInfo::ConstRef { def_id }) = self.body.local_decls[local].local_info { |
| 29 | + Some(def_id) |
| 30 | + } else { |
| 31 | + None |
| 32 | + } |
| 33 | + } |
| 34 | + fn lint_const_item_usage( |
| 35 | + &self, |
| 36 | + const_item: DefId, |
| 37 | + location: Location, |
| 38 | + decorate: impl for<'b> FnOnce(LintDiagnosticBuilder<'b>) -> DiagnosticBuilder<'b>, |
| 39 | + ) { |
| 40 | + let source_info = self.body.source_info(location); |
| 41 | + let lint_root = self.body.source_scopes[source_info.scope] |
| 42 | + .local_data |
| 43 | + .as_ref() |
| 44 | + .assert_crate_local() |
| 45 | + .lint_root; |
| 46 | + |
| 47 | + self.tcx.struct_span_lint_hir(CONST_ITEM_MUTATION, lint_root, source_info.span, |lint| { |
| 48 | + decorate(lint) |
| 49 | + .span_note(self.tcx.def_span(const_item), "`const` item defined here") |
| 50 | + .emit() |
| 51 | + }); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl<'a, 'tcx> Visitor<'tcx> for ConstMutationChecker<'a, 'tcx> { |
| 56 | + fn visit_statement(&mut self, stmt: &Statement<'tcx>, loc: Location) { |
| 57 | + if let StatementKind::Assign(box (lhs, _)) = &stmt.kind { |
| 58 | + // Check for assignment to fields of a constant |
| 59 | + // Assigning directly to a constant (e.g. `FOO = true;`) is a hard error, |
| 60 | + // so emitting a lint would be redundant. |
| 61 | + if !lhs.projection.is_empty() { |
| 62 | + if let Some(def_id) = self.is_const_item(lhs.local) { |
| 63 | + self.lint_const_item_usage(def_id, loc, |lint| { |
| 64 | + let mut lint = lint.build("attempting to modify a `const` item"); |
| 65 | + lint.note("each usage of a `const` item creates a new temporary - the original `const` item will not be modified"); |
| 66 | + lint |
| 67 | + }) |
| 68 | + } |
| 69 | + } |
| 70 | + // We are looking for MIR of the form: |
| 71 | + // |
| 72 | + // ``` |
| 73 | + // _1 = const FOO; |
| 74 | + // _2 = &mut _1; |
| 75 | + // method_call(_2, ..) |
| 76 | + // ``` |
| 77 | + // |
| 78 | + // Record our current LHS, so that we can detect this |
| 79 | + // pattern in `visit_rvalue` |
| 80 | + self.target_local = lhs.as_local(); |
| 81 | + } |
| 82 | + self.super_statement(stmt, loc); |
| 83 | + self.target_local = None; |
| 84 | + } |
| 85 | + fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, loc: Location) { |
| 86 | + if let Rvalue::Ref(_, BorrowKind::Mut { .. }, place) = rvalue { |
| 87 | + let local = place.local; |
| 88 | + if let Some(def_id) = self.is_const_item(local) { |
| 89 | + // If this Rvalue is being used as the right-hand side of a |
| 90 | + // `StatementKind::Assign`, see if it ends up getting used as |
| 91 | + // the `self` parameter of a method call (as the terminator of our current |
| 92 | + // BasicBlock). If so, we emit a more specific lint. |
| 93 | + let method_did = self.target_local.and_then(|target_local| { |
| 94 | + crate::util::find_self_call(self.tcx, &self.body, target_local, loc.block) |
| 95 | + }); |
| 96 | + let lint_loc = |
| 97 | + if method_did.is_some() { self.body.terminator_loc(loc.block) } else { loc }; |
| 98 | + self.lint_const_item_usage(def_id, lint_loc, |lint| { |
| 99 | + let mut lint = lint.build("taking a mutable reference to a `const` item"); |
| 100 | + lint |
| 101 | + .note("each usage of a `const` item creates a new temporary") |
| 102 | + .note("the mutable reference will refer to this temporary, not the original `const` item"); |
| 103 | + |
| 104 | + if let Some(method_did) = method_did { |
| 105 | + lint.span_note(self.tcx.def_span(method_did), "mutable reference created due to call to this method"); |
| 106 | + } |
| 107 | + |
| 108 | + lint |
| 109 | + }); |
| 110 | + } |
| 111 | + } |
| 112 | + self.super_rvalue(rvalue, loc); |
| 113 | + } |
| 114 | +} |
0 commit comments