Skip to content

Commit 46ecb23

Browse files
author
Lukas Markeffsky
committed
unify dyn* coercions with other pointer coercions
1 parent 67bb749 commit 46ecb23

File tree

23 files changed

+70
-55
lines changed

23 files changed

+70
-55
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2139,7 +2139,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21392139
);
21402140
}
21412141

2142-
CastKind::DynStar => {
2142+
CastKind::PointerCoercion(PointerCoercion::DynStar) => {
21432143
// get the constraints from the target type (`dyn* Clone`)
21442144
//
21452145
// apply them to prove that the source type `Foo` implements `Clone` etc

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,11 @@ fn codegen_stmt<'tcx>(
770770
let operand = codegen_operand(fx, operand);
771771
crate::unsize::coerce_unsized_into(fx, operand, lval);
772772
}
773-
Rvalue::Cast(CastKind::DynStar, ref operand, _) => {
773+
Rvalue::Cast(
774+
CastKind::PointerCoercion(PointerCoercion::DynStar),
775+
ref operand,
776+
_,
777+
) => {
774778
let operand = codegen_operand(fx, operand);
775779
crate::unsize::coerce_dyn_star(fx, operand, lval);
776780
}

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
526526
bug!("unexpected non-pair operand");
527527
}
528528
}
529-
mir::CastKind::DynStar => {
529+
mir::CastKind::PointerCoercion(PointerCoercion::DynStar) => {
530530
let (lldata, llextra) = operand.val.pointer_parts();
531531
let (lldata, llextra) =
532532
base::cast_to_dyn_star(bx, lldata, operand.layout, cast.ty, llextra);

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,12 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
447447
// These are all okay; they only change the type, not the data.
448448
}
449449

450-
Rvalue::Cast(CastKind::PointerCoercion(PointerCoercion::Unsize), _, _) => {
451-
// Unsizing is implemented for CTFE.
450+
Rvalue::Cast(
451+
CastKind::PointerCoercion(PointerCoercion::Unsize | PointerCoercion::DynStar),
452+
_,
453+
_,
454+
) => {
455+
// Unsizing and `dyn*` coercions are implemented for CTFE.
452456
}
453457

454458
Rvalue::Cast(CastKind::PointerExposeProvenance, _, _) => {
@@ -458,10 +462,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
458462
// Since no pointer can ever get exposed (rejected above), this is easy to support.
459463
}
460464

461-
Rvalue::Cast(CastKind::DynStar, _, _) => {
462-
// `dyn*` coercion is implemented for CTFE.
463-
}
464-
465465
Rvalue::Cast(_, _, _) => {}
466466

467467
Rvalue::NullaryOp(

compiler/rustc_const_eval/src/interpret/cast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
125125
}
126126
}
127127

128-
CastKind::DynStar => {
128+
CastKind::PointerCoercion(PointerCoercion::DynStar) => {
129129
if let ty::Dynamic(data, _, ty::DynStar) = cast_ty.kind() {
130130
// Initial cast from sized to dyn trait
131131
let vtable = self.get_vtable_ptr(src.layout.ty, data)?;

compiler/rustc_hir_typeck/src/cast.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ use rustc_errors::codes::*;
3333
use rustc_errors::{Applicability, Diag, ErrorGuaranteed};
3434
use rustc_hir::{self as hir, ExprKind};
3535
use rustc_macros::{TypeFoldable, TypeVisitable};
36-
use rustc_middle::bug;
3736
use rustc_middle::mir::Mutability;
3837
use rustc_middle::ty::adjustment::AllowTwoPhase;
3938
use rustc_middle::ty::cast::{CastKind, CastTy};
4039
use rustc_middle::ty::error::TypeError;
4140
use rustc_middle::ty::{self, Ty, TyCtxt, TypeAndMut, TypeVisitableExt, VariantDef};
41+
use rustc_middle::{bug, span_bug};
4242
use rustc_session::lint;
4343
use rustc_span::def_id::LOCAL_CRATE;
4444
use rustc_span::symbol::sym;
@@ -674,6 +674,16 @@ impl<'a, 'tcx> CastCheck<'tcx> {
674674
use rustc_middle::ty::cast::CastTy::*;
675675
use rustc_middle::ty::cast::IntTy::*;
676676

677+
if self.cast_ty.is_dyn_star() {
678+
if fcx.tcx.features().dyn_star {
679+
span_bug!(self.span, "should be handled by `coerce`");
680+
} else {
681+
// Report "casting is invalid" rather than "non-primitive cast"
682+
// if the feature is not enabled.
683+
return Err(CastError::IllegalCast);
684+
}
685+
}
686+
677687
let (t_from, t_cast) = match (CastTy::from_ty(self.expr_ty), CastTy::from_ty(self.cast_ty))
678688
{
679689
(Some(t_from), Some(t_cast)) => (t_from, t_cast),
@@ -780,16 +790,6 @@ impl<'a, 'tcx> CastCheck<'tcx> {
780790
(Int(Char) | Int(Bool), Int(_)) => Ok(CastKind::PrimIntCast),
781791

782792
(Int(_) | Float, Int(_) | Float) => Ok(CastKind::NumericCast),
783-
784-
(_, DynStar) => {
785-
if fcx.tcx.features().dyn_star {
786-
bug!("should be handled by `coerce`")
787-
} else {
788-
Err(CastError::IllegalCast)
789-
}
790-
}
791-
792-
(DynStar, _) => Err(CastError::IllegalCast),
793793
}
794794
}
795795

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,10 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
769769
));
770770

771771
Ok(InferOk {
772-
value: (vec![Adjustment { kind: Adjust::DynStar, target: b }], b),
772+
value: (
773+
vec![Adjustment { kind: Adjust::Pointer(PointerCoercion::DynStar), target: b }],
774+
b,
775+
),
773776
obligations,
774777
})
775778
}

compiler/rustc_hir_typeck/src/expr_use_visitor.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -759,9 +759,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
759759
for adjustment in adjustments {
760760
debug!("walk_adjustment expr={:?} adj={:?}", expr, adjustment);
761761
match adjustment.kind {
762-
adjustment::Adjust::NeverToAny
763-
| adjustment::Adjust::Pointer(_)
764-
| adjustment::Adjust::DynStar => {
762+
adjustment::Adjust::NeverToAny | adjustment::Adjust::Pointer(_) => {
765763
// Creating a closure/fn-pointer or unsizing consumes
766764
// the input and stores it into the resulting rvalue.
767765
self.consume_or_copy(&place_with_id, place_with_id.hir_id);
@@ -1296,8 +1294,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
12961294
adjustment::Adjust::NeverToAny
12971295
| adjustment::Adjust::Pointer(_)
12981296
| adjustment::Adjust::Borrow(_)
1299-
| adjustment::Adjust::ReborrowPin(..)
1300-
| adjustment::Adjust::DynStar => {
1297+
| adjustment::Adjust::ReborrowPin(..) => {
13011298
// Result is an rvalue.
13021299
Ok(self.cat_rvalue(expr.hir_id, target))
13031300
}

compiler/rustc_middle/src/mir/statement.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,6 @@ impl<'tcx> Rvalue<'tcx> {
434434
| CastKind::PtrToPtr
435435
| CastKind::PointerCoercion(_)
436436
| CastKind::PointerWithExposedProvenance
437-
| CastKind::DynStar
438437
| CastKind::Transmute,
439438
_,
440439
_,

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,8 +1404,6 @@ pub enum CastKind {
14041404
///
14051405
/// Both are runtime nops, so should be [`CastKind::PtrToPtr`] instead in runtime MIR.
14061406
PointerCoercion(PointerCoercion),
1407-
/// Cast into a dyn* object.
1408-
DynStar,
14091407
IntToInt,
14101408
FloatToInt,
14111409
FloatToFloat,

0 commit comments

Comments
 (0)