Skip to content

Commit 0bba7ad

Browse files
authored
Rollup merge of rust-lang#64406 - Mark-Simulacrum:error-unknown-intrinsic, r=Centril
Ban non-extern rust intrinsics Intrinsics can only be defined by the compiler. Fixes rust-lang#36979
2 parents 12ee41c + 34d71f1 commit 0bba7ad

9 files changed

+189
-89
lines changed

src/librustc_typeck/check/mod.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,8 @@ fn check_fn<'a, 'tcx>(
10881088

10891089
let span = body.value.span;
10901090

1091+
fn_maybe_err(fcx.tcx, span, fn_sig.abi);
1092+
10911093
if body.generator_kind.is_some() && can_be_generator.is_some() {
10921094
let yield_ty = fcx.next_ty_var(TypeVariableOrigin {
10931095
kind: TypeVariableOriginKind::TypeInference,
@@ -1439,6 +1441,14 @@ fn check_opaque_for_cycles<'tcx>(
14391441
}
14401442
}
14411443

1444+
// Forbid defining intrinsics in Rust code,
1445+
// as they must always be defined by the compiler.
1446+
fn fn_maybe_err(tcx: TyCtxt<'_>, sp: Span, abi: Abi) {
1447+
if let Abi::RustIntrinsic | Abi::PlatformIntrinsic = abi {
1448+
tcx.sess.span_err(sp, "intrinsic must be in `extern \"rust-intrinsic\" { ... }` block");
1449+
}
1450+
}
1451+
14421452
pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item) {
14431453
debug!(
14441454
"check_item_type(it.hir_id={}, it.name={})",
@@ -1475,9 +1485,17 @@ pub fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, it: &'tcx hir::Item) {
14751485
check_on_unimplemented(tcx, trait_def_id, it);
14761486
}
14771487
}
1478-
hir::ItemKind::Trait(..) => {
1488+
hir::ItemKind::Trait(_, _, _, _, ref items) => {
14791489
let def_id = tcx.hir().local_def_id(it.hir_id);
14801490
check_on_unimplemented(tcx, def_id, it);
1491+
1492+
for item in items.iter() {
1493+
let item = tcx.hir().trait_item(item.id);
1494+
if let hir::TraitItemKind::Method(sig, _) = &item.node {
1495+
let abi = sig.header.abi;
1496+
fn_maybe_err(tcx, item.ident.span, abi);
1497+
}
1498+
}
14811499
}
14821500
hir::ItemKind::Struct(..) => {
14831501
check_struct(tcx, it.hir_id, it.span);

src/test/incremental/hashes/function_interfaces.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212

1313
#![allow(warnings)]
14-
#![feature(intrinsics)]
1514
#![feature(linkage)]
1615
#![feature(rustc_attrs)]
1716
#![crate_type = "rlib"]
@@ -99,15 +98,15 @@ pub fn make_extern() {}
9998
pub extern "C" fn make_extern() {}
10099

101100

102-
// Extern C Extern Rust-Intrinsic ----------------------------------------------
101+
// Extern C Extern stdcall ----------------------------------------------
103102

104103
#[cfg(cfail1)]
105-
pub extern "C" fn make_intrinsic() {}
104+
pub extern "C" fn make_stdcall() {}
106105

107106
#[cfg(not(cfail1))]
108107
#[rustc_clean(cfg = "cfail2", except = "Hir, HirBody, typeck_tables_of, fn_sig")]
109108
#[rustc_clean(cfg = "cfail3")]
110-
pub extern "rust-intrinsic" fn make_intrinsic() {}
109+
pub extern "stdcall" fn make_stdcall() {}
111110

112111

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

src/test/incremental/hashes/trait_defs.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#![feature(rustc_attrs)]
1919
#![crate_type="rlib"]
2020
#![feature(associated_type_defaults)]
21-
#![feature(intrinsics)]
2221

2322

2423
// Change trait visibility
@@ -318,7 +317,7 @@ trait TraitAddExternModifier {
318317

319318

320319

321-
// Change extern "C" to extern "rust-intrinsic"
320+
// Change extern "C" to extern "stdcall"
322321
#[cfg(cfail1)]
323322
trait TraitChangeExternCToRustIntrinsic {
324323
extern "C" fn method();
@@ -330,7 +329,7 @@ trait TraitChangeExternCToRustIntrinsic {
330329
trait TraitChangeExternCToRustIntrinsic {
331330
#[rustc_dirty(label="Hir", cfg="cfail2")]
332331
#[rustc_clean(label="Hir", cfg="cfail3")]
333-
extern "rust-intrinsic" fn method();
332+
extern "stdcall" fn method();
334333
}
335334

336335

src/test/ui/feature-gates/feature-gate-abi.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
// Functions
1212
extern "rust-intrinsic" fn f1() {} //~ ERROR intrinsics are subject to change
13+
//~^ ERROR intrinsic must be in
1314
extern "platform-intrinsic" fn f2() {} //~ ERROR platform intrinsics are experimental
15+
//~^ ERROR intrinsic must be in
1416
extern "vectorcall" fn f3() {} //~ ERROR vectorcall is experimental and subject to change
1517
extern "rust-call" fn f4() {} //~ ERROR rust-call ABI is subject to change
1618
extern "msp430-interrupt" fn f5() {} //~ ERROR msp430-interrupt ABI is experimental
@@ -22,7 +24,9 @@ extern "amdgpu-kernel" fn f9() {} //~ ERROR amdgpu-kernel ABI is experimental an
2224
// Methods in trait definition
2325
trait Tr {
2426
extern "rust-intrinsic" fn m1(); //~ ERROR intrinsics are subject to change
27+
//~^ ERROR intrinsic must be in
2528
extern "platform-intrinsic" fn m2(); //~ ERROR platform intrinsics are experimental
29+
//~^ ERROR intrinsic must be in
2630
extern "vectorcall" fn m3(); //~ ERROR vectorcall is experimental and subject to change
2731
extern "rust-call" fn m4(); //~ ERROR rust-call ABI is subject to change
2832
extern "msp430-interrupt" fn m5(); //~ ERROR msp430-interrupt ABI is experimental
@@ -31,8 +35,6 @@ trait Tr {
3135
extern "thiscall" fn m8(); //~ ERROR thiscall is experimental and subject to change
3236
extern "amdgpu-kernel" fn m9(); //~ ERROR amdgpu-kernel ABI is experimental and subject to change
3337

34-
extern "rust-intrinsic" fn dm1() {} //~ ERROR intrinsics are subject to change
35-
extern "platform-intrinsic" fn dm2() {} //~ ERROR platform intrinsics are experimental
3638
extern "vectorcall" fn dm3() {} //~ ERROR vectorcall is experimental and subject to change
3739
extern "rust-call" fn dm4() {} //~ ERROR rust-call ABI is subject to change
3840
extern "msp430-interrupt" fn dm5() {} //~ ERROR msp430-interrupt ABI is experimental
@@ -47,7 +49,9 @@ struct S;
4749
// Methods in trait impl
4850
impl Tr for S {
4951
extern "rust-intrinsic" fn m1() {} //~ ERROR intrinsics are subject to change
52+
//~^ ERROR intrinsic must be in
5053
extern "platform-intrinsic" fn m2() {} //~ ERROR platform intrinsics are experimental
54+
//~^ ERROR intrinsic must be in
5155
extern "vectorcall" fn m3() {} //~ ERROR vectorcall is experimental and subject to change
5256
extern "rust-call" fn m4() {} //~ ERROR rust-call ABI is subject to change
5357
extern "msp430-interrupt" fn m5() {} //~ ERROR msp430-interrupt ABI is experimental
@@ -60,7 +64,9 @@ impl Tr for S {
6064
// Methods in inherent impl
6165
impl S {
6266
extern "rust-intrinsic" fn im1() {} //~ ERROR intrinsics are subject to change
67+
//~^ ERROR intrinsic must be in
6368
extern "platform-intrinsic" fn im2() {} //~ ERROR platform intrinsics are experimental
69+
//~^ ERROR intrinsic must be in
6470
extern "vectorcall" fn im3() {} //~ ERROR vectorcall is experimental and subject to change
6571
extern "rust-call" fn im4() {} //~ ERROR rust-call ABI is subject to change
6672
extern "msp430-interrupt" fn im5() {} //~ ERROR msp430-interrupt ABI is experimental

0 commit comments

Comments
 (0)