make some intrinsics safe.

This commit is contained in:
Mazdak Farrokhzad 2018-12-31 04:11:17 +01:00
parent e4c47f9c60
commit fedfb61f26
3 changed files with 15 additions and 9 deletions

View file

@ -66,6 +66,17 @@ fn equate_intrinsic_type<'a, 'tcx>(
require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(def_id)), fty);
}
/// Returns whether the given intrinsic is unsafe to call or not.
pub fn intrisic_operation_unsafety(intrinsic: &str) -> hir::Unsafety {
match intrinsic {
"size_of" | "min_align_of" | "needs_drop" |
"overflowing_add" | "overflowing_sub" | "overflowing_mul" |
"rotate_left" | "rotate_right"
=> hir::Unsafety::Normal,
_ => hir::Unsafety::Unsafe,
}
}
/// Remember to add all intrinsics here, in librustc_codegen_llvm/intrinsic.rs,
/// and in libcore/intrinsics.rs
pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
@ -117,10 +128,7 @@ pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
} else if &name[..] == "abort" || &name[..] == "unreachable" {
(0, Vec::new(), tcx.types.never, hir::Unsafety::Unsafe)
} else {
let unsafety = match &name[..] {
"size_of" | "min_align_of" | "needs_drop" => hir::Unsafety::Normal,
_ => hir::Unsafety::Unsafe,
};
let unsafety = intrisic_operation_unsafety(&name[..]);
let (n_tps, inputs, output) = match &name[..] {
"breakpoint" => (0, Vec::new(), tcx.mk_unit()),
"size_of" |

View file

@ -80,7 +80,7 @@ mod closure;
mod callee;
mod compare_method;
mod generator_interior;
mod intrinsic;
pub mod intrinsic;
mod op;
use astconv::{AstConv, PathSeg};

View file

@ -16,6 +16,7 @@
use astconv::{AstConv, Bounds};
use constrained_type_params as ctp;
use check::intrinsic::intrisic_operation_unsafety;
use lint;
use middle::lang_items::SizedTraitLangItem;
use middle::resolve_lifetime as rl;
@ -2076,10 +2077,7 @@ fn compute_sig_of_foreign_fn_decl<'a, 'tcx>(
abi: abi::Abi,
) -> ty::PolyFnSig<'tcx> {
let unsafety = if abi == abi::Abi::RustIntrinsic {
match &*tcx.item_name(def_id).as_str() {
"size_of" | "min_align_of" | "needs_drop" => hir::Unsafety::Normal,
_ => hir::Unsafety::Unsafe,
}
intrisic_operation_unsafety(&*tcx.item_name(def_id).as_str())
} else {
hir::Unsafety::Unsafe
};