Skip to content

Commit

Permalink
Unrolled build for rust-lang#121039
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#121039 - cjgillot:gvn-adjust, r=compiler-errors

Correctly compute adjustment casts in GVN

Fixes rust-lang#120925

r? `@oli-obk`
  • Loading branch information
rust-timer authored Feb 14, 2024
2 parents 81b757c + a97e4af commit 39875bd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
14 changes: 9 additions & 5 deletions compiler/rustc_mir_transform/src/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,18 +561,22 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
.ok()?;
dest.into()
}
CastKind::FnPtrToPtr
| CastKind::PtrToPtr
| CastKind::PointerCoercion(
CastKind::FnPtrToPtr | CastKind::PtrToPtr => {
let src = self.evaluated[value].as_ref()?;
let src = self.ecx.read_immediate(src).ok()?;
let to = self.ecx.layout_of(to).ok()?;
let ret = self.ecx.ptr_to_ptr(&src, to).ok()?;
ret.into()
}
CastKind::PointerCoercion(
ty::adjustment::PointerCoercion::MutToConstPointer
| ty::adjustment::PointerCoercion::ArrayToPointer
| ty::adjustment::PointerCoercion::UnsafeFnPointer,
) => {
let src = self.evaluated[value].as_ref()?;
let src = self.ecx.read_immediate(src).ok()?;
let to = self.ecx.layout_of(to).ok()?;
let ret = self.ecx.ptr_to_ptr(&src, to).ok()?;
ret.into()
ImmTy::from_immediate(*src, to).into()
}
_ => return None,
},
Expand Down
25 changes: 25 additions & 0 deletions tests/mir-opt/issue_120925_unsafefncast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Verify that we do not ICE when attempting to interpret casts between fn types.
// skip-filecheck

static FOO: fn() = || assert_ne!(42, 43);
static BAR: fn(i32, i32) = |a, b| assert_ne!(a, b);

fn main() {
FOO();

let bar: unsafe fn(i32, i32) = BAR;

let f: fn() = || {};
f();

f();

f();

let g: fn(i32) = |i| assert_eq!(i, 2);
g(2);

g(2);

g(2);
}

0 comments on commit 39875bd

Please sign in to comment.