Skip to content

Commit

Permalink
Rollup merge of rust-lang#84404 - tmiasko:intrinsics-in-coercion-lub,…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
Dylan-DPC authored Apr 22, 2021
2 parents aac5125 + 75732dd commit f180c1e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
8 changes: 8 additions & 0 deletions compiler/rustc_typeck/src/check/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
};
if let (Some(a_sig), Some(b_sig)) = (a_sig, b_sig) {
// Intrinsics are not coercible to function pointers.
if a_sig.abi() == Abi::RustIntrinsic
|| a_sig.abi() == Abi::PlatformIntrinsic
|| b_sig.abi() == Abi::RustIntrinsic
|| b_sig.abi() == Abi::PlatformIntrinsic
{
return Err(TypeError::IntrinsicCast);
}
// The signature must match.
let a_sig = self.normalize_associated_types_in(new.span, a_sig);
let b_sig = self.normalize_associated_types_in(new.span, b_sig);
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/reify-intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ fn b() {
//~^ ERROR casting
}

fn c() {
let _ = [
std::intrinsics::copy_nonoverlapping::<i32>,
std::intrinsics::copy::<i32>,
//~^ ERROR cannot coerce
];
}

fn main() {}
11 changes: 10 additions & 1 deletion src/test/ui/reify-intrinsic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ error[E0606]: casting `unsafe extern "rust-intrinsic" fn(_) -> _ {transmute::<_,
LL | let _ = std::mem::transmute as unsafe extern "rust-intrinsic" fn(isize) -> usize;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

error: aborting due to 3 previous errors

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

0 comments on commit f180c1e

Please sign in to comment.