1
Fork 0

Add a pointer to address cast kind

A pointer to address cast are often special-cased.
Introduce a dedicated cast kind to make them easy distinguishable.
This commit is contained in:
Tomasz Miąsko 2022-05-31 00:00:00 +00:00
parent d35d972e69
commit dff602fc18
15 changed files with 96 additions and 94 deletions

View file

@ -16,7 +16,6 @@ use rustc_hir as hir;
use rustc_middle::mir::traversal::ReversePostorderIter;
use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor};
use rustc_middle::mir::*;
use rustc_middle::ty::cast::CastTy;
use rustc_middle::ty::subst::InternalSubsts;
use rustc_middle::ty::{self, List, TyCtxt, TypeFoldable};
use rustc_span::Span;
@ -502,18 +501,11 @@ impl<'tcx> Validator<'_, 'tcx> {
Rvalue::ThreadLocalRef(_) => return Err(Unpromotable),
Rvalue::Cast(kind, operand, cast_ty) => {
if matches!(kind, CastKind::Misc) {
let operand_ty = operand.ty(self.body, self.tcx);
let cast_in = CastTy::from_ty(operand_ty).expect("bad input type for cast");
let cast_out = CastTy::from_ty(*cast_ty).expect("bad output type for cast");
if let (CastTy::Ptr(_) | CastTy::FnPtr, CastTy::Int(_)) = (cast_in, cast_out) {
// ptr-to-int casts are not possible in consts and thus not promotable
return Err(Unpromotable);
}
// int-to-ptr casts are fine, they just use the integer value at pointer type.
}
// ptr-to-int casts are not possible in consts and thus not promotable
Rvalue::Cast(CastKind::PointerAddress, _, _) => return Err(Unpromotable),
// int-to-ptr casts are fine, they just use the integer value at pointer type.
Rvalue::Cast(_, operand, _) => {
self.validate_operand(operand)?;
}