diff --git a/src/librustc_typeck/check/intrinsic.rs b/src/librustc_typeck/check/intrinsic.rs index a36b21921436e..f2d69eb7f7456 100644 --- a/src/librustc_typeck/check/intrinsic.rs +++ b/src/librustc_typeck/check/intrinsic.rs @@ -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" | diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 39beb2832851b..30019feb960aa 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -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}; diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 9fc2f11b19738..5d6fe3f384535 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -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 };