Rollup merge of #121039 - cjgillot:gvn-adjust, r=compiler-errors

Correctly compute adjustment casts in GVN

Fixes https://github.com/rust-lang/rust/issues/120925

r? `@oli-obk`
This commit is contained in:
Oli Scherer 2024-02-14 11:53:41 +01:00 committed by GitHub
commit 93bc34073e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 5 deletions

View file

@ -561,9 +561,14 @@ fn eval_to_const(&mut self, value: VnIndex) -> Option<OpTy<'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,
@ -571,8 +576,7 @@ fn eval_to_const(&mut self, value: VnIndex) -> Option<OpTy<'tcx>> {
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,
},

View file

@ -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);
}