unify dyn* coercions with other pointer coercions
This commit is contained in:
parent
67bb749c2e
commit
46ecb23198
@ -2139,7 +2139,7 @@ fn check_rvalue(&mut self, body: &Body<'tcx>, rvalue: &Rvalue<'tcx>, location: L
|
||||
);
|
||||
}
|
||||
|
||||
CastKind::DynStar => {
|
||||
CastKind::PointerCoercion(PointerCoercion::DynStar) => {
|
||||
// get the constraints from the target type (`dyn* Clone`)
|
||||
//
|
||||
// apply them to prove that the source type `Foo` implements `Clone` etc
|
||||
|
@ -770,7 +770,11 @@ fn is_fat_ptr<'tcx>(fx: &FunctionCx<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool {
|
||||
let operand = codegen_operand(fx, operand);
|
||||
crate::unsize::coerce_unsized_into(fx, operand, lval);
|
||||
}
|
||||
Rvalue::Cast(CastKind::DynStar, ref operand, _) => {
|
||||
Rvalue::Cast(
|
||||
CastKind::PointerCoercion(PointerCoercion::DynStar),
|
||||
ref operand,
|
||||
_,
|
||||
) => {
|
||||
let operand = codegen_operand(fx, operand);
|
||||
crate::unsize::coerce_dyn_star(fx, operand, lval);
|
||||
}
|
||||
|
@ -526,7 +526,7 @@ pub(crate) fn codegen_rvalue_operand(
|
||||
bug!("unexpected non-pair operand");
|
||||
}
|
||||
}
|
||||
mir::CastKind::DynStar => {
|
||||
mir::CastKind::PointerCoercion(PointerCoercion::DynStar) => {
|
||||
let (lldata, llextra) = operand.val.pointer_parts();
|
||||
let (lldata, llextra) =
|
||||
base::cast_to_dyn_star(bx, lldata, operand.layout, cast.ty, llextra);
|
||||
|
@ -447,8 +447,12 @@ fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
|
||||
// These are all okay; they only change the type, not the data.
|
||||
}
|
||||
|
||||
Rvalue::Cast(CastKind::PointerCoercion(PointerCoercion::Unsize), _, _) => {
|
||||
// Unsizing is implemented for CTFE.
|
||||
Rvalue::Cast(
|
||||
CastKind::PointerCoercion(PointerCoercion::Unsize | PointerCoercion::DynStar),
|
||||
_,
|
||||
_,
|
||||
) => {
|
||||
// Unsizing and `dyn*` coercions are implemented for CTFE.
|
||||
}
|
||||
|
||||
Rvalue::Cast(CastKind::PointerExposeProvenance, _, _) => {
|
||||
@ -458,10 +462,6 @@ fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
|
||||
// Since no pointer can ever get exposed (rejected above), this is easy to support.
|
||||
}
|
||||
|
||||
Rvalue::Cast(CastKind::DynStar, _, _) => {
|
||||
// `dyn*` coercion is implemented for CTFE.
|
||||
}
|
||||
|
||||
Rvalue::Cast(_, _, _) => {}
|
||||
|
||||
Rvalue::NullaryOp(
|
||||
|
@ -125,7 +125,7 @@ pub fn cast(
|
||||
}
|
||||
}
|
||||
|
||||
CastKind::DynStar => {
|
||||
CastKind::PointerCoercion(PointerCoercion::DynStar) => {
|
||||
if let ty::Dynamic(data, _, ty::DynStar) = cast_ty.kind() {
|
||||
// Initial cast from sized to dyn trait
|
||||
let vtable = self.get_vtable_ptr(src.layout.ty, data)?;
|
||||
|
@ -33,12 +33,12 @@
|
||||
use rustc_errors::{Applicability, Diag, ErrorGuaranteed};
|
||||
use rustc_hir::{self as hir, ExprKind};
|
||||
use rustc_macros::{TypeFoldable, TypeVisitable};
|
||||
use rustc_middle::bug;
|
||||
use rustc_middle::mir::Mutability;
|
||||
use rustc_middle::ty::adjustment::AllowTwoPhase;
|
||||
use rustc_middle::ty::cast::{CastKind, CastTy};
|
||||
use rustc_middle::ty::error::TypeError;
|
||||
use rustc_middle::ty::{self, Ty, TyCtxt, TypeAndMut, TypeVisitableExt, VariantDef};
|
||||
use rustc_middle::{bug, span_bug};
|
||||
use rustc_session::lint;
|
||||
use rustc_span::def_id::LOCAL_CRATE;
|
||||
use rustc_span::symbol::sym;
|
||||
@ -674,6 +674,16 @@ fn do_check(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<CastKind, CastError> {
|
||||
use rustc_middle::ty::cast::CastTy::*;
|
||||
use rustc_middle::ty::cast::IntTy::*;
|
||||
|
||||
if self.cast_ty.is_dyn_star() {
|
||||
if fcx.tcx.features().dyn_star {
|
||||
span_bug!(self.span, "should be handled by `coerce`");
|
||||
} else {
|
||||
// Report "casting is invalid" rather than "non-primitive cast"
|
||||
// if the feature is not enabled.
|
||||
return Err(CastError::IllegalCast);
|
||||
}
|
||||
}
|
||||
|
||||
let (t_from, t_cast) = match (CastTy::from_ty(self.expr_ty), CastTy::from_ty(self.cast_ty))
|
||||
{
|
||||
(Some(t_from), Some(t_cast)) => (t_from, t_cast),
|
||||
@ -780,16 +790,6 @@ fn do_check(&self, fcx: &FnCtxt<'a, 'tcx>) -> Result<CastKind, CastError> {
|
||||
(Int(Char) | Int(Bool), Int(_)) => Ok(CastKind::PrimIntCast),
|
||||
|
||||
(Int(_) | Float, Int(_) | Float) => Ok(CastKind::NumericCast),
|
||||
|
||||
(_, DynStar) => {
|
||||
if fcx.tcx.features().dyn_star {
|
||||
bug!("should be handled by `coerce`")
|
||||
} else {
|
||||
Err(CastError::IllegalCast)
|
||||
}
|
||||
}
|
||||
|
||||
(DynStar, _) => Err(CastError::IllegalCast),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -769,7 +769,10 @@ fn coerce_dyn_star(
|
||||
));
|
||||
|
||||
Ok(InferOk {
|
||||
value: (vec![Adjustment { kind: Adjust::DynStar, target: b }], b),
|
||||
value: (
|
||||
vec![Adjustment { kind: Adjust::Pointer(PointerCoercion::DynStar), target: b }],
|
||||
b,
|
||||
),
|
||||
obligations,
|
||||
})
|
||||
}
|
||||
|
@ -759,9 +759,7 @@ fn walk_adjustment(&self, expr: &hir::Expr<'_>) -> Result<(), Cx::Error> {
|
||||
for adjustment in adjustments {
|
||||
debug!("walk_adjustment expr={:?} adj={:?}", expr, adjustment);
|
||||
match adjustment.kind {
|
||||
adjustment::Adjust::NeverToAny
|
||||
| adjustment::Adjust::Pointer(_)
|
||||
| adjustment::Adjust::DynStar => {
|
||||
adjustment::Adjust::NeverToAny | adjustment::Adjust::Pointer(_) => {
|
||||
// Creating a closure/fn-pointer or unsizing consumes
|
||||
// the input and stores it into the resulting rvalue.
|
||||
self.consume_or_copy(&place_with_id, place_with_id.hir_id);
|
||||
@ -1296,8 +1294,7 @@ fn cat_expr_adjusted_with<F>(
|
||||
adjustment::Adjust::NeverToAny
|
||||
| adjustment::Adjust::Pointer(_)
|
||||
| adjustment::Adjust::Borrow(_)
|
||||
| adjustment::Adjust::ReborrowPin(..)
|
||||
| adjustment::Adjust::DynStar => {
|
||||
| adjustment::Adjust::ReborrowPin(..) => {
|
||||
// Result is an rvalue.
|
||||
Ok(self.cat_rvalue(expr.hir_id, target))
|
||||
}
|
||||
|
@ -434,7 +434,6 @@ pub fn is_safe_to_remove(&self) -> bool {
|
||||
| CastKind::PtrToPtr
|
||||
| CastKind::PointerCoercion(_)
|
||||
| CastKind::PointerWithExposedProvenance
|
||||
| CastKind::DynStar
|
||||
| CastKind::Transmute,
|
||||
_,
|
||||
_,
|
||||
|
@ -1404,8 +1404,6 @@ pub enum CastKind {
|
||||
///
|
||||
/// Both are runtime nops, so should be [`CastKind::PtrToPtr`] instead in runtime MIR.
|
||||
PointerCoercion(PointerCoercion),
|
||||
/// Cast into a dyn* object.
|
||||
DynStar,
|
||||
IntToInt,
|
||||
FloatToInt,
|
||||
FloatToFloat,
|
||||
|
@ -35,6 +35,9 @@ pub enum PointerCoercion {
|
||||
/// type. Codegen backends and miri figure out what has to be done
|
||||
/// based on the precise source/target type at hand.
|
||||
Unsize,
|
||||
|
||||
/// Go from a pointer-like type to a `dyn*` object.
|
||||
DynStar,
|
||||
}
|
||||
|
||||
/// Represents coercing a value to a different type of value.
|
||||
@ -102,9 +105,6 @@ pub enum Adjust<'tcx> {
|
||||
|
||||
Pointer(PointerCoercion),
|
||||
|
||||
/// Cast into a dyn* object.
|
||||
DynStar,
|
||||
|
||||
/// Take a pinned reference and reborrow as a `Pin<&mut T>` or `Pin<&T>`.
|
||||
ReborrowPin(ty::Region<'tcx>, hir::Mutability),
|
||||
}
|
||||
|
@ -34,15 +34,12 @@ pub enum CastTy<'tcx> {
|
||||
FnPtr,
|
||||
/// Raw pointers.
|
||||
Ptr(ty::TypeAndMut<'tcx>),
|
||||
/// Casting into a `dyn*` value.
|
||||
DynStar,
|
||||
}
|
||||
|
||||
/// Cast Kind. See [RFC 401](https://rust-lang.github.io/rfcs/0401-coercions.html)
|
||||
/// (or rustc_hir_analysis/check/cast.rs).
|
||||
#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable)]
|
||||
pub enum CastKind {
|
||||
CoercionCast,
|
||||
PtrPtrCast,
|
||||
PtrAddrCast,
|
||||
AddrPtrCast,
|
||||
@ -53,7 +50,6 @@ pub enum CastKind {
|
||||
ArrayPtrCast,
|
||||
FnPtrPtrCast,
|
||||
FnPtrAddrCast,
|
||||
DynStarCast,
|
||||
}
|
||||
|
||||
impl<'tcx> CastTy<'tcx> {
|
||||
@ -71,7 +67,6 @@ pub fn from_ty(t: Ty<'tcx>) -> Option<CastTy<'tcx>> {
|
||||
ty::Adt(d, _) if d.is_enum() && d.is_payloadfree() => Some(CastTy::Int(IntTy::CEnum)),
|
||||
ty::RawPtr(ty, mutbl) => Some(CastTy::Ptr(ty::TypeAndMut { ty, mutbl })),
|
||||
ty::FnPtr(..) => Some(CastTy::FnPtr),
|
||||
ty::Dynamic(_, _, ty::DynStar) => Some(CastTy::DynStar),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@ -86,7 +81,6 @@ pub fn mir_cast_kind<'tcx>(from_ty: Ty<'tcx>, cast_ty: Ty<'tcx>) -> mir::CastKin
|
||||
mir::CastKind::PointerExposeProvenance
|
||||
}
|
||||
(Some(CastTy::Int(_)), Some(CastTy::Ptr(_))) => mir::CastKind::PointerWithExposedProvenance,
|
||||
(_, Some(CastTy::DynStar)) => mir::CastKind::DynStar,
|
||||
(Some(CastTy::Int(_)), Some(CastTy::Int(_))) => mir::CastKind::IntToInt,
|
||||
(Some(CastTy::FnPtr), Some(CastTy::Ptr(_))) => mir::CastKind::FnPtrToPtr,
|
||||
|
||||
|
@ -146,7 +146,6 @@ fn apply_adjustment(
|
||||
Adjust::Borrow(AutoBorrow::RawPtr(mutability)) => {
|
||||
ExprKind::RawBorrow { mutability, arg: self.thir.exprs.push(expr) }
|
||||
}
|
||||
Adjust::DynStar => ExprKind::Cast { source: self.thir.exprs.push(expr) },
|
||||
Adjust::ReborrowPin(region, mutbl) => {
|
||||
debug!("apply ReborrowPin adjustment");
|
||||
// Rewrite `$expr` as `Pin { __pointer: &(mut)? *($expr).__pointer }`
|
||||
|
@ -70,11 +70,11 @@ fn visit_rvalue(&mut self, rvalue: &mir::Rvalue<'tcx>, location: Location) {
|
||||
match *rvalue {
|
||||
// We need to detect unsizing casts that required vtables.
|
||||
mir::Rvalue::Cast(
|
||||
mir::CastKind::PointerCoercion(PointerCoercion::Unsize),
|
||||
mir::CastKind::PointerCoercion(PointerCoercion::Unsize)
|
||||
| mir::CastKind::PointerCoercion(PointerCoercion::DynStar),
|
||||
ref operand,
|
||||
target_ty,
|
||||
)
|
||||
| mir::Rvalue::Cast(mir::CastKind::DynStar, ref operand, target_ty) => {
|
||||
) => {
|
||||
// This isn't monomorphized yet so we can't tell what the actual types are -- just
|
||||
// add everything that may involve a vtable.
|
||||
let source_ty = operand.ty(self.body, self.tcx);
|
||||
|
@ -1128,9 +1128,6 @@ macro_rules! check_kinds {
|
||||
Rvalue::Cast(kind, operand, target_type) => {
|
||||
let op_ty = operand.ty(self.body, self.tcx);
|
||||
match kind {
|
||||
CastKind::DynStar => {
|
||||
// FIXME(dyn-star): make sure nothing needs to be done here.
|
||||
}
|
||||
// FIXME: Add Checks for these
|
||||
CastKind::PointerWithExposedProvenance | CastKind::PointerExposeProvenance => {}
|
||||
CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer) => {
|
||||
@ -1208,6 +1205,9 @@ macro_rules! check_kinds {
|
||||
// This is used for all `CoerceUnsized` types,
|
||||
// not just pointers/references, so is hard to check.
|
||||
}
|
||||
CastKind::PointerCoercion(PointerCoercion::DynStar) => {
|
||||
// FIXME(dyn-star): make sure nothing needs to be done here.
|
||||
}
|
||||
CastKind::IntToInt | CastKind::IntToFloat => {
|
||||
let input_valid = op_ty.is_integral() || op_ty.is_char() || op_ty.is_bool();
|
||||
let target_valid = target_type.is_numeric() || target_type.is_char();
|
||||
|
@ -665,11 +665,11 @@ fn visit_rvalue(&mut self, rvalue: &mir::Rvalue<'tcx>, location: Location) {
|
||||
// have to instantiate all methods of the trait being cast to, so we
|
||||
// can build the appropriate vtable.
|
||||
mir::Rvalue::Cast(
|
||||
mir::CastKind::PointerCoercion(PointerCoercion::Unsize),
|
||||
mir::CastKind::PointerCoercion(PointerCoercion::Unsize)
|
||||
| mir::CastKind::PointerCoercion(PointerCoercion::DynStar),
|
||||
ref operand,
|
||||
target_ty,
|
||||
)
|
||||
| mir::Rvalue::Cast(mir::CastKind::DynStar, ref operand, target_ty) => {
|
||||
) => {
|
||||
let source_ty = operand.ty(self.body, self.tcx);
|
||||
// *Before* monomorphizing, record that we already handled this mention.
|
||||
self.used_mentioned_items
|
||||
|
@ -282,11 +282,12 @@ impl<'tcx> Stable<'tcx> for mir::CastKind {
|
||||
type T = stable_mir::mir::CastKind;
|
||||
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
|
||||
use rustc_middle::mir::CastKind::*;
|
||||
use rustc_middle::ty::adjustment::PointerCoercion;
|
||||
match self {
|
||||
PointerExposeProvenance => stable_mir::mir::CastKind::PointerExposeAddress,
|
||||
PointerWithExposedProvenance => stable_mir::mir::CastKind::PointerWithExposedProvenance,
|
||||
PointerCoercion(PointerCoercion::DynStar) => stable_mir::mir::CastKind::DynStar,
|
||||
PointerCoercion(c) => stable_mir::mir::CastKind::PointerCoercion(c.stable(tables)),
|
||||
DynStar => stable_mir::mir::CastKind::DynStar,
|
||||
IntToInt => stable_mir::mir::CastKind::IntToInt,
|
||||
FloatToInt => stable_mir::mir::CastKind::FloatToInt,
|
||||
FloatToFloat => stable_mir::mir::CastKind::FloatToFloat,
|
||||
|
@ -119,6 +119,7 @@ fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
|
||||
}
|
||||
PointerCoercion::ArrayToPointer => stable_mir::mir::PointerCoercion::ArrayToPointer,
|
||||
PointerCoercion::Unsize => stable_mir::mir::PointerCoercion::Unsize,
|
||||
PointerCoercion::DynStar => unreachable!("represented as `CastKind::DynStar` in smir"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -960,6 +960,7 @@ pub enum CastKind {
|
||||
PointerExposeAddress,
|
||||
PointerWithExposedProvenance,
|
||||
PointerCoercion(PointerCoercion),
|
||||
// FIXME(smir-rename): change this to PointerCoercion(DynStar)
|
||||
DynStar,
|
||||
IntToInt,
|
||||
FloatToInt,
|
||||
|
@ -154,7 +154,7 @@ fn check_rvalue<'tcx>(
|
||||
Rvalue::Cast(CastKind::PointerExposeProvenance, _, _) => {
|
||||
Err((span, "casting pointers to ints is unstable in const fn".into()))
|
||||
},
|
||||
Rvalue::Cast(CastKind::DynStar, _, _) => {
|
||||
Rvalue::Cast(CastKind::PointerCoercion(PointerCoercion::DynStar), _, _) => {
|
||||
// FIXME(dyn-star)
|
||||
unimplemented!()
|
||||
},
|
||||
|
@ -5,7 +5,7 @@ trait Tr {}
|
||||
|
||||
fn f(x: dyn* Tr) -> usize {
|
||||
x as usize
|
||||
//~^ ERROR casting `(dyn* Tr + 'static)` as `usize` is invalid
|
||||
//~^ ERROR non-primitive cast: `(dyn* Tr + 'static)` as `usize`
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
@ -1,9 +1,9 @@
|
||||
error[E0606]: casting `(dyn* Tr + 'static)` as `usize` is invalid
|
||||
error[E0605]: non-primitive cast: `(dyn* Tr + 'static)` as `usize`
|
||||
--> $DIR/dyn-to-rigid.rs:7:5
|
||||
|
|
||||
LL | x as usize
|
||||
| ^^^^^^^^^^
|
||||
| ^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0606`.
|
||||
For more information about this error, try `rustc --explain E0605`.
|
||||
|
18
tests/ui/dyn-star/enum-cast.rs
Normal file
18
tests/ui/dyn-star/enum-cast.rs
Normal file
@ -0,0 +1,18 @@
|
||||
//@ check-pass
|
||||
|
||||
// This used to ICE, because the compiler confused a pointer-like to dyn* coercion
|
||||
// with a c-like enum to integer cast.
|
||||
|
||||
#![feature(dyn_star)]
|
||||
#![expect(incomplete_features)]
|
||||
|
||||
enum E {
|
||||
Num(usize),
|
||||
}
|
||||
|
||||
trait Trait {}
|
||||
impl Trait for E {}
|
||||
|
||||
fn main() {
|
||||
let _ = E::Num(42) as dyn* Trait;
|
||||
}
|
Loading…
Reference in New Issue
Block a user