Skip to content

Commit

Permalink
make some intrinsics safe.
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Dec 31, 2018
1 parent e4c47f9 commit fedfb61
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
16 changes: 12 additions & 4 deletions src/librustc_typeck/check/intrinsic.rs
Expand Up @@ -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>,
Expand Down Expand Up @@ -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" |
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/mod.rs
Expand Up @@ -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};
Expand Down
6 changes: 2 additions & 4 deletions src/librustc_typeck/collect.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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
};
Expand Down

0 comments on commit fedfb61

Please sign in to comment.