|
| 1 | +use rustc_hir::def_id::DefId; |
| 2 | +use rustc_middle::mir::visit::Visitor; |
| 3 | +use rustc_middle::mir::*; |
| 4 | +use rustc_middle::ty::{self, Ty, TyCtxt}; |
| 5 | +use rustc_session::lint::builtin::FUNCTION_REFERENCES; |
| 6 | +use rustc_span::Span; |
| 7 | +use rustc_target::spec::abi::Abi; |
| 8 | + |
| 9 | +use crate::transform::{MirPass, MirSource}; |
| 10 | + |
| 11 | +pub struct FunctionReferences; |
| 12 | + |
| 13 | +impl<'tcx> MirPass<'tcx> for FunctionReferences { |
| 14 | + fn run_pass(&self, tcx: TyCtxt<'tcx>, _src: MirSource<'tcx>, body: &mut Body<'tcx>) { |
| 15 | + let source_info = SourceInfo::outermost(body.span); |
| 16 | + let mut checker = FunctionRefChecker { |
| 17 | + tcx, |
| 18 | + body, |
| 19 | + potential_lints: Vec::new(), |
| 20 | + casts: Vec::new(), |
| 21 | + calls: Vec::new(), |
| 22 | + source_info, |
| 23 | + }; |
| 24 | + checker.visit_body(&body); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +struct FunctionRefChecker<'a, 'tcx> { |
| 29 | + tcx: TyCtxt<'tcx>, |
| 30 | + body: &'a Body<'tcx>, |
| 31 | + potential_lints: Vec<FunctionRefLint>, |
| 32 | + casts: Vec<Span>, |
| 33 | + calls: Vec<Span>, |
| 34 | + source_info: SourceInfo, |
| 35 | +} |
| 36 | + |
| 37 | +impl<'a, 'tcx> Visitor<'tcx> for FunctionRefChecker<'a, 'tcx> { |
| 38 | + fn visit_basic_block_data(&mut self, block: BasicBlock, data: &BasicBlockData<'tcx>) { |
| 39 | + self.super_basic_block_data(block, data); |
| 40 | + for cast_span in self.casts.drain(..) { |
| 41 | + self.potential_lints.retain(|lint| lint.source_info.span != cast_span); |
| 42 | + } |
| 43 | + for call_span in self.calls.drain(..) { |
| 44 | + self.potential_lints.retain(|lint| lint.source_info.span != call_span); |
| 45 | + } |
| 46 | + for lint in self.potential_lints.drain(..) { |
| 47 | + lint.emit(self.tcx, self.body); |
| 48 | + } |
| 49 | + } |
| 50 | + fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) { |
| 51 | + self.source_info = statement.source_info; |
| 52 | + self.super_statement(statement, location); |
| 53 | + } |
| 54 | + fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) { |
| 55 | + self.source_info = terminator.source_info; |
| 56 | + if let TerminatorKind::Call { |
| 57 | + func, |
| 58 | + args: _, |
| 59 | + destination: _, |
| 60 | + cleanup: _, |
| 61 | + from_hir_call: _, |
| 62 | + fn_span: _, |
| 63 | + } = &terminator.kind |
| 64 | + { |
| 65 | + let span = match func { |
| 66 | + Operand::Copy(place) | Operand::Move(place) => { |
| 67 | + self.body.local_decls[place.local].source_info.span |
| 68 | + } |
| 69 | + Operand::Constant(constant) => constant.span, |
| 70 | + }; |
| 71 | + self.calls.push(span); |
| 72 | + }; |
| 73 | + self.super_terminator(terminator, location); |
| 74 | + } |
| 75 | + fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) { |
| 76 | + match rvalue { |
| 77 | + Rvalue::Ref(_, _, place) | Rvalue::AddressOf(_, place) => { |
| 78 | + let decl = &self.body.local_decls[place.local]; |
| 79 | + if let ty::FnDef(def_id, _) = decl.ty.kind { |
| 80 | + let ident = self |
| 81 | + .body |
| 82 | + .var_debug_info |
| 83 | + .iter() |
| 84 | + .find(|info| info.source_info.span == decl.source_info.span) |
| 85 | + .map(|info| info.name.to_ident_string()) |
| 86 | + .unwrap_or(self.tcx.def_path_str(def_id)); |
| 87 | + let lint = FunctionRefLint { ident, def_id, source_info: self.source_info }; |
| 88 | + self.potential_lints.push(lint); |
| 89 | + } |
| 90 | + } |
| 91 | + Rvalue::Cast(_, op, _) => { |
| 92 | + let op_ty = op.ty(self.body, self.tcx); |
| 93 | + if self.is_fn_ref(op_ty) { |
| 94 | + self.casts.push(self.source_info.span); |
| 95 | + } |
| 96 | + } |
| 97 | + _ => {} |
| 98 | + } |
| 99 | + self.super_rvalue(rvalue, location); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl<'a, 'tcx> FunctionRefChecker<'a, 'tcx> { |
| 104 | + fn is_fn_ref(&self, ty: Ty<'tcx>) -> bool { |
| 105 | + let referent_ty = match ty.kind { |
| 106 | + ty::Ref(_, referent_ty, _) => Some(referent_ty), |
| 107 | + ty::RawPtr(ty_and_mut) => Some(ty_and_mut.ty), |
| 108 | + _ => None, |
| 109 | + }; |
| 110 | + referent_ty |
| 111 | + .map(|ref_ty| if let ty::FnDef(..) = ref_ty.kind { true } else { false }) |
| 112 | + .unwrap_or(false) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +struct FunctionRefLint { |
| 117 | + ident: String, |
| 118 | + def_id: DefId, |
| 119 | + source_info: SourceInfo, |
| 120 | +} |
| 121 | + |
| 122 | +impl<'tcx> FunctionRefLint { |
| 123 | + fn emit(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) { |
| 124 | + let def_id = self.def_id; |
| 125 | + let source_info = self.source_info; |
| 126 | + let lint_root = body.source_scopes[source_info.scope] |
| 127 | + .local_data |
| 128 | + .as_ref() |
| 129 | + .assert_crate_local() |
| 130 | + .lint_root; |
| 131 | + let fn_sig = tcx.fn_sig(def_id); |
| 132 | + let unsafety = fn_sig.unsafety().prefix_str(); |
| 133 | + let abi = match fn_sig.abi() { |
| 134 | + Abi::Rust => String::from(""), |
| 135 | + other_abi => { |
| 136 | + let mut s = String::from("extern \""); |
| 137 | + s.push_str(other_abi.name()); |
| 138 | + s.push_str("\" "); |
| 139 | + s |
| 140 | + } |
| 141 | + }; |
| 142 | + let num_args = fn_sig.inputs().map_bound(|inputs| inputs.len()).skip_binder(); |
| 143 | + let variadic = if fn_sig.c_variadic() { ", ..." } else { "" }; |
| 144 | + let ret = if fn_sig.output().skip_binder().is_unit() { "" } else { " -> _" }; |
| 145 | + tcx.struct_span_lint_hir(FUNCTION_REFERENCES, lint_root, source_info.span, |lint| { |
| 146 | + lint.build(&format!( |
| 147 | + "cast `{}` with `as {}{}fn({}{}){}` to use it as a pointer", |
| 148 | + self.ident, |
| 149 | + unsafety, |
| 150 | + abi, |
| 151 | + vec!["_"; num_args].join(", "), |
| 152 | + variadic, |
| 153 | + ret, |
| 154 | + )) |
| 155 | + .emit() |
| 156 | + }); |
| 157 | + } |
| 158 | +} |
0 commit comments