2020-09-10 21:53:14 -05:00
|
|
|
use rustc_hir::def_id::DefId;
|
|
|
|
use rustc_middle::mir::visit::Visitor;
|
|
|
|
use rustc_middle::mir::*;
|
2020-10-06 16:55:46 -05:00
|
|
|
use rustc_middle::ty::{
|
|
|
|
self,
|
|
|
|
subst::{GenericArgKind, Subst},
|
|
|
|
PredicateAtom, Ty, TyCtxt, TyS,
|
|
|
|
};
|
2020-10-06 08:51:10 -05:00
|
|
|
use rustc_session::lint::builtin::FUNCTION_ITEM_REFERENCES;
|
|
|
|
use rustc_span::{symbol::sym, Span};
|
2020-09-10 21:53:14 -05:00
|
|
|
use rustc_target::spec::abi::Abi;
|
|
|
|
|
2020-10-06 08:51:10 -05:00
|
|
|
use crate::transform::MirPass;
|
2020-09-10 21:53:14 -05:00
|
|
|
|
2020-10-06 08:51:10 -05:00
|
|
|
pub struct FunctionItemReferences;
|
2020-09-10 21:53:14 -05:00
|
|
|
|
2020-10-06 08:51:10 -05:00
|
|
|
impl<'tcx> MirPass<'tcx> for FunctionItemReferences {
|
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
|
|
|
let mut checker = FunctionItemRefChecker { tcx, body };
|
2020-09-10 21:53:14 -05:00
|
|
|
checker.visit_body(&body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 08:51:10 -05:00
|
|
|
struct FunctionItemRefChecker<'a, 'tcx> {
|
2020-09-10 21:53:14 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
body: &'a Body<'tcx>,
|
|
|
|
}
|
|
|
|
|
2020-10-06 08:51:10 -05:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for FunctionItemRefChecker<'a, 'tcx> {
|
2020-09-10 21:53:14 -05:00
|
|
|
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
|
|
|
|
if let TerminatorKind::Call {
|
|
|
|
func,
|
2020-10-06 08:51:10 -05:00
|
|
|
args,
|
2020-09-10 21:53:14 -05:00
|
|
|
destination: _,
|
|
|
|
cleanup: _,
|
|
|
|
from_hir_call: _,
|
|
|
|
fn_span: _,
|
|
|
|
} = &terminator.kind
|
|
|
|
{
|
2020-10-06 16:55:46 -05:00
|
|
|
let source_info = *self.body.source_info(location);
|
|
|
|
//this handles all function calls outside macros
|
|
|
|
if !source_info.span.from_expansion() {
|
|
|
|
let func_ty = func.ty(self.body, self.tcx);
|
|
|
|
if let ty::FnDef(def_id, substs_ref) = *func_ty.kind() {
|
|
|
|
//handle `std::mem::transmute`
|
|
|
|
if self.tcx.is_diagnostic_item(sym::transmute, def_id) {
|
|
|
|
let arg_ty = args[0].ty(self.body, self.tcx);
|
|
|
|
for generic_inner_ty in arg_ty.walk() {
|
|
|
|
if let GenericArgKind::Type(inner_ty) = generic_inner_ty.unpack() {
|
|
|
|
if let Some(fn_id) = FunctionItemRefChecker::is_fn_ref(inner_ty) {
|
|
|
|
let ident = self.tcx.item_name(fn_id).to_ident_string();
|
|
|
|
let span = self.nth_arg_span(&args, 0);
|
|
|
|
self.emit_lint(ident, fn_id, source_info, span);
|
|
|
|
}
|
2020-10-06 08:51:10 -05:00
|
|
|
}
|
|
|
|
}
|
2020-10-06 16:55:46 -05:00
|
|
|
} else {
|
|
|
|
//handle any function call with `std::fmt::Pointer` as a bound trait
|
|
|
|
//this includes calls to `std::fmt::Pointer::fmt` outside of macros
|
|
|
|
let param_env = self.tcx.param_env(def_id);
|
|
|
|
let bounds = param_env.caller_bounds();
|
|
|
|
for bound in bounds {
|
|
|
|
if let Some(bound_ty) = self.is_pointer_trait(&bound.skip_binders()) {
|
|
|
|
//get the argument types as they appear in the function signature
|
|
|
|
let arg_defs = self.tcx.fn_sig(def_id).skip_binder().inputs();
|
|
|
|
for (arg_num, arg_def) in arg_defs.iter().enumerate() {
|
|
|
|
//for all types reachable from the argument type in the fn sig
|
|
|
|
for generic_inner_ty in arg_def.walk() {
|
|
|
|
if let GenericArgKind::Type(inner_ty) =
|
|
|
|
generic_inner_ty.unpack()
|
|
|
|
{
|
|
|
|
//if the inner type matches the type bound by `Pointer`
|
|
|
|
if TyS::same_type(inner_ty, bound_ty) {
|
|
|
|
//do a substitution using the parameters from the callsite
|
|
|
|
let subst_ty = inner_ty.subst(self.tcx, substs_ref);
|
|
|
|
if let Some(fn_id) =
|
|
|
|
FunctionItemRefChecker::is_fn_ref(subst_ty)
|
|
|
|
{
|
|
|
|
let ident =
|
|
|
|
self.tcx.item_name(fn_id).to_ident_string();
|
|
|
|
let span = self.nth_arg_span(&args, arg_num);
|
|
|
|
self.emit_lint(ident, fn_id, source_info, span);
|
|
|
|
}
|
2020-10-06 08:51:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 21:53:14 -05:00
|
|
|
}
|
2020-10-06 08:51:10 -05:00
|
|
|
}
|
|
|
|
}
|
2020-09-10 21:53:14 -05:00
|
|
|
self.super_terminator(terminator, location);
|
|
|
|
}
|
2020-10-06 16:55:46 -05:00
|
|
|
//This handles `std::fmt::Pointer::fmt` when it's used in the formatting macros.
|
|
|
|
//It's handled as an operand instead of a Call terminator so it won't depend on
|
|
|
|
//whether the formatting macros call `fmt` directly, transmute it first or other
|
|
|
|
//internal fmt details.
|
2020-10-06 08:51:10 -05:00
|
|
|
fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
|
2020-10-06 16:55:46 -05:00
|
|
|
let source_info = *self.body.source_info(location);
|
|
|
|
if source_info.span.from_expansion() {
|
|
|
|
let op_ty = operand.ty(self.body, self.tcx);
|
|
|
|
if let ty::FnDef(def_id, substs_ref) = *op_ty.kind() {
|
|
|
|
if self.tcx.is_diagnostic_item(sym::pointer_trait_fmt, def_id) {
|
|
|
|
let param_ty = substs_ref.type_at(0);
|
|
|
|
if let Some(fn_id) = FunctionItemRefChecker::is_fn_ref(param_ty) {
|
|
|
|
//the operand's ctxt wouldn't display the lint since it's inside a macro
|
|
|
|
//so we have to use the callsite's ctxt
|
|
|
|
let callsite_ctxt = source_info.span.source_callsite().ctxt();
|
|
|
|
let span = source_info.span.with_ctxt(callsite_ctxt);
|
|
|
|
let ident = self.tcx.item_name(fn_id).to_ident_string();
|
|
|
|
self.emit_lint(ident, fn_id, source_info, span);
|
|
|
|
}
|
2020-09-10 21:53:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 08:51:10 -05:00
|
|
|
self.super_operand(operand, location);
|
2020-09-10 21:53:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 08:51:10 -05:00
|
|
|
impl<'a, 'tcx> FunctionItemRefChecker<'a, 'tcx> {
|
|
|
|
//return the bound parameter type if the trait is `std::fmt::Pointer`
|
|
|
|
fn is_pointer_trait(&self, bound: &PredicateAtom<'tcx>) -> Option<Ty<'tcx>> {
|
|
|
|
if let ty::PredicateAtom::Trait(predicate, _) = bound {
|
|
|
|
if self.tcx.is_diagnostic_item(sym::pointer_trait, predicate.def_id()) {
|
|
|
|
Some(predicate.trait_ref.self_ty())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn is_fn_ref(ty: Ty<'tcx>) -> Option<DefId> {
|
|
|
|
let referent_ty = match ty.kind() {
|
2020-09-10 21:53:14 -05:00
|
|
|
ty::Ref(_, referent_ty, _) => Some(referent_ty),
|
2020-10-06 08:51:10 -05:00
|
|
|
ty::RawPtr(ty_and_mut) => Some(&ty_and_mut.ty),
|
2020-09-10 21:53:14 -05:00
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
referent_ty
|
2020-10-06 08:51:10 -05:00
|
|
|
.map(
|
|
|
|
|ref_ty| {
|
|
|
|
if let ty::FnDef(def_id, _) = *ref_ty.kind() { Some(def_id) } else { None }
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap_or(None)
|
2020-09-10 21:53:14 -05:00
|
|
|
}
|
2020-10-06 08:51:10 -05:00
|
|
|
fn nth_arg_span(&self, args: &Vec<Operand<'tcx>>, n: usize) -> Span {
|
|
|
|
match &args[n] {
|
|
|
|
Operand::Copy(place) | Operand::Move(place) => {
|
|
|
|
self.body.local_decls[place.local].source_info.span
|
|
|
|
}
|
|
|
|
Operand::Constant(constant) => constant.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn emit_lint(&self, ident: String, fn_id: DefId, source_info: SourceInfo, span: Span) {
|
|
|
|
let lint_root = self.body.source_scopes[source_info.scope]
|
2020-09-10 21:53:14 -05:00
|
|
|
.local_data
|
|
|
|
.as_ref()
|
|
|
|
.assert_crate_local()
|
|
|
|
.lint_root;
|
2020-10-06 08:51:10 -05:00
|
|
|
let fn_sig = self.tcx.fn_sig(fn_id);
|
2020-09-10 21:53:14 -05:00
|
|
|
let unsafety = fn_sig.unsafety().prefix_str();
|
|
|
|
let abi = match fn_sig.abi() {
|
|
|
|
Abi::Rust => String::from(""),
|
|
|
|
other_abi => {
|
|
|
|
let mut s = String::from("extern \"");
|
|
|
|
s.push_str(other_abi.name());
|
|
|
|
s.push_str("\" ");
|
|
|
|
s
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let num_args = fn_sig.inputs().map_bound(|inputs| inputs.len()).skip_binder();
|
|
|
|
let variadic = if fn_sig.c_variadic() { ", ..." } else { "" };
|
|
|
|
let ret = if fn_sig.output().skip_binder().is_unit() { "" } else { " -> _" };
|
2020-10-06 08:51:10 -05:00
|
|
|
self.tcx.struct_span_lint_hir(FUNCTION_ITEM_REFERENCES, lint_root, span, |lint| {
|
2020-09-10 21:53:14 -05:00
|
|
|
lint.build(&format!(
|
2020-10-06 10:59:14 -05:00
|
|
|
"cast `{}` with `as {}{}fn({}{}){}` to obtain a function pointer",
|
2020-10-06 08:51:10 -05:00
|
|
|
ident,
|
2020-09-10 21:53:14 -05:00
|
|
|
unsafety,
|
|
|
|
abi,
|
|
|
|
vec!["_"; num_args].join(", "),
|
|
|
|
variadic,
|
|
|
|
ret,
|
|
|
|
))
|
2020-10-06 08:51:10 -05:00
|
|
|
.emit();
|
2020-09-10 21:53:14 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|