Skip to content

Commit

Permalink
Rollup merge of #64406 - Mark-Simulacrum:error-unknown-intrinsic, r=C…
Browse files Browse the repository at this point in the history
…entril

Ban non-extern rust intrinsics

Intrinsics can only be defined by the compiler.

Fixes #36979
  • Loading branch information
Centril authored Sep 14, 2019
2 parents d29df01 + 7b3adc2 commit 5f9b9b2
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 97 deletions.
20 changes: 19 additions & 1 deletion src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,8 @@ fn check_fn<'a, 'tcx>(

let span = body.value.span;

fn_maybe_err(fcx.tcx, span, fn_sig.abi);

if body.generator_kind.is_some() && can_be_generator.is_some() {
let yield_ty = fcx.next_ty_var(TypeVariableOrigin {
kind: TypeVariableOriginKind::TypeInference,
Expand Down Expand Up @@ -1439,6 +1441,14 @@ fn check_opaque_for_cycles<'tcx>(
}
}

// Forbid defining intrinsics in Rust code,
// as they must always be defined by the compiler.
fn fn_maybe_err(tcx: TyCtxt<'_>, sp: Span, abi: Abi) {
if let Abi::RustIntrinsic | Abi::PlatformIntrinsic = abi {
tcx.sess.span_err(sp, "intrinsic must be in `extern \"rust-intrinsic\" { ... }` block");
}
}

pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item) {
debug!(
"check_item_type(it.hir_id={}, it.name={})",
Expand Down Expand Up @@ -1475,9 +1485,17 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item) {
check_on_unimplemented(tcx, trait_def_id, it);
}
}
hir::ItemKind::Trait(..) => {
hir::ItemKind::Trait(_, _, _, _, ref items) => {
let def_id = tcx.hir().local_def_id(it.hir_id);
check_on_unimplemented(tcx, def_id, it);

for item in items.iter() {
let item = tcx.hir().trait_item(item.id);
if let hir::TraitItemKind::Method(sig, _) = &item.node {
let abi = sig.header.abi;
fn_maybe_err(tcx, item.ident.span, abi);
}
}
}
hir::ItemKind::Struct(..) => {
check_struct(tcx, it.hir_id, it.span);
Expand Down
12 changes: 0 additions & 12 deletions src/test/incremental/hashes/function_interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


#![allow(warnings)]
#![feature(intrinsics)]
#![feature(linkage)]
#![feature(rustc_attrs)]
#![crate_type = "rlib"]
Expand Down Expand Up @@ -99,17 +98,6 @@ pub fn make_extern() {}
pub extern "C" fn make_extern() {}


// Extern C Extern Rust-Intrinsic ----------------------------------------------

#[cfg(cfail1)]
pub extern "C" fn make_intrinsic() {}

#[cfg(not(cfail1))]
#[rustc_clean(cfg = "cfail2", except = "Hir, HirBody, typeck_tables_of, fn_sig")]
#[rustc_clean(cfg = "cfail3")]
pub extern "rust-intrinsic" fn make_intrinsic() {}


// Type Parameter --------------------------------------------------------------

#[cfg(cfail1)]
Expand Down
5 changes: 2 additions & 3 deletions src/test/incremental/hashes/trait_defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#![feature(rustc_attrs)]
#![crate_type="rlib"]
#![feature(associated_type_defaults)]
#![feature(intrinsics)]


// Change trait visibility
Expand Down Expand Up @@ -318,7 +317,7 @@ trait TraitAddExternModifier {



// Change extern "C" to extern "rust-intrinsic"
// Change extern "C" to extern "stdcall"
#[cfg(cfail1)]
trait TraitChangeExternCToRustIntrinsic {
extern "C" fn method();
Expand All @@ -330,7 +329,7 @@ trait TraitChangeExternCToRustIntrinsic {
trait TraitChangeExternCToRustIntrinsic {
#[rustc_dirty(label="Hir", cfg="cfail2")]
#[rustc_clean(label="Hir", cfg="cfail3")]
extern "rust-intrinsic" fn method();
extern "stdcall" fn method();
}


Expand Down
10 changes: 8 additions & 2 deletions src/test/ui/feature-gates/feature-gate-abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

// Functions
extern "rust-intrinsic" fn f1() {} //~ ERROR intrinsics are subject to change
//~^ ERROR intrinsic must be in
extern "platform-intrinsic" fn f2() {} //~ ERROR platform intrinsics are experimental
//~^ ERROR intrinsic must be in
extern "vectorcall" fn f3() {} //~ ERROR vectorcall is experimental and subject to change
extern "rust-call" fn f4() {} //~ ERROR rust-call ABI is subject to change
extern "msp430-interrupt" fn f5() {} //~ ERROR msp430-interrupt ABI is experimental
Expand All @@ -22,7 +24,9 @@ extern "amdgpu-kernel" fn f9() {} //~ ERROR amdgpu-kernel ABI is experimental an
// Methods in trait definition
trait Tr {
extern "rust-intrinsic" fn m1(); //~ ERROR intrinsics are subject to change
//~^ ERROR intrinsic must be in
extern "platform-intrinsic" fn m2(); //~ ERROR platform intrinsics are experimental
//~^ ERROR intrinsic must be in
extern "vectorcall" fn m3(); //~ ERROR vectorcall is experimental and subject to change
extern "rust-call" fn m4(); //~ ERROR rust-call ABI is subject to change
extern "msp430-interrupt" fn m5(); //~ ERROR msp430-interrupt ABI is experimental
Expand All @@ -31,8 +35,6 @@ trait Tr {
extern "thiscall" fn m8(); //~ ERROR thiscall is experimental and subject to change
extern "amdgpu-kernel" fn m9(); //~ ERROR amdgpu-kernel ABI is experimental and subject to change

extern "rust-intrinsic" fn dm1() {} //~ ERROR intrinsics are subject to change
extern "platform-intrinsic" fn dm2() {} //~ ERROR platform intrinsics are experimental
extern "vectorcall" fn dm3() {} //~ ERROR vectorcall is experimental and subject to change
extern "rust-call" fn dm4() {} //~ ERROR rust-call ABI is subject to change
extern "msp430-interrupt" fn dm5() {} //~ ERROR msp430-interrupt ABI is experimental
Expand All @@ -47,7 +49,9 @@ struct S;
// Methods in trait impl
impl Tr for S {
extern "rust-intrinsic" fn m1() {} //~ ERROR intrinsics are subject to change
//~^ ERROR intrinsic must be in
extern "platform-intrinsic" fn m2() {} //~ ERROR platform intrinsics are experimental
//~^ ERROR intrinsic must be in
extern "vectorcall" fn m3() {} //~ ERROR vectorcall is experimental and subject to change
extern "rust-call" fn m4() {} //~ ERROR rust-call ABI is subject to change
extern "msp430-interrupt" fn m5() {} //~ ERROR msp430-interrupt ABI is experimental
Expand All @@ -60,7 +64,9 @@ impl Tr for S {
// Methods in inherent impl
impl S {
extern "rust-intrinsic" fn im1() {} //~ ERROR intrinsics are subject to change
//~^ ERROR intrinsic must be in
extern "platform-intrinsic" fn im2() {} //~ ERROR platform intrinsics are experimental
//~^ ERROR intrinsic must be in
extern "vectorcall" fn im3() {} //~ ERROR vectorcall is experimental and subject to change
extern "rust-call" fn im4() {} //~ ERROR rust-call ABI is subject to change
extern "msp430-interrupt" fn im5() {} //~ ERROR msp430-interrupt ABI is experimental
Expand Down
Loading

0 comments on commit 5f9b9b2

Please sign in to comment.