Skip to content

Commit f180c1e

Browse files
authored
Rollup merge of rust-lang#84404 - tmiasko:intrinsics-in-coercion-lub, r=Mark-Simulacrum
Check for intrinsics before coercing to a function pointer Return an error if coercing function items / non-capturing closures to a common function pointer type would require reifying an intrinsic. Turns ICE reported in rust-lang#84297 into a proper error.
2 parents aac5125 + 75732dd commit f180c1e

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

compiler/rustc_typeck/src/check/coercion.rs

+8
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
973973
}
974974
};
975975
if let (Some(a_sig), Some(b_sig)) = (a_sig, b_sig) {
976+
// Intrinsics are not coercible to function pointers.
977+
if a_sig.abi() == Abi::RustIntrinsic
978+
|| a_sig.abi() == Abi::PlatformIntrinsic
979+
|| b_sig.abi() == Abi::RustIntrinsic
980+
|| b_sig.abi() == Abi::PlatformIntrinsic
981+
{
982+
return Err(TypeError::IntrinsicCast);
983+
}
976984
// The signature must match.
977985
let a_sig = self.normalize_associated_types_in(new.span, a_sig);
978986
let b_sig = self.normalize_associated_types_in(new.span, b_sig);

src/test/ui/reify-intrinsic.rs

+8
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,12 @@ fn b() {
1212
//~^ ERROR casting
1313
}
1414

15+
fn c() {
16+
let _ = [
17+
std::intrinsics::copy_nonoverlapping::<i32>,
18+
std::intrinsics::copy::<i32>,
19+
//~^ ERROR cannot coerce
20+
];
21+
}
22+
1523
fn main() {}

src/test/ui/reify-intrinsic.stderr

+10-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,16 @@ error[E0606]: casting `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_,
1919
LL | let _ = std::mem::transmute as unsafe extern "rust-intrinsic" fn(isize) -> usize;
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2121

22-
error: aborting due to 2 previous errors
22+
error[E0308]: cannot coerce intrinsics to function pointers
23+
--> $DIR/reify-intrinsic.rs:18:9
24+
|
25+
LL | std::intrinsics::copy::<i32>,
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot coerce intrinsics to function pointers
27+
|
28+
= note: expected type `unsafe extern "rust-intrinsic" fn(_, _, _) {copy_nonoverlapping::<i32>}`
29+
found fn item `unsafe extern "rust-intrinsic" fn(_, _, _) {std::intrinsics::copy::<i32>}`
30+
31+
error: aborting due to 3 previous errors
2332

2433
Some errors have detailed explanations: E0308, E0606.
2534
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)