Skip to content

Commit e5c98fd

Browse files
author
Lukas Markeffsky
committed
unify dyn* coercions with other pointer coercions
1 parent 8c2c9a9 commit e5c98fd

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
@@ -2141,7 +2141,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21412141
);
21422142
}
21432143

2144-
CastKind::DynStar => {
2144+
CastKind::PointerCoercion(PointerCoercion::DynStar) => {
21452145
// get the constraints from the target type (`dyn* Clone`)
21462146
//
21472147
// 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
@@ -525,7 +525,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
525525
bug!("unexpected non-pair operand");
526526
}
527527
}
528-
mir::CastKind::DynStar => {
528+
mir::CastKind::PointerCoercion(PointerCoercion::DynStar) => {
529529
let (lldata, llextra) = operand.val.pointer_parts();
530530
let (lldata, llextra) =
531531
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.principal())?;

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;
@@ -678,6 +678,16 @@ impl<'a, 'tcx> CastCheck<'tcx> {
678678
use rustc_middle::ty::cast::CastTy::*;
679679
use rustc_middle::ty::cast::IntTy::*;
680680

681+
if self.cast_ty.is_dyn_star() {
682+
if fcx.tcx.features().dyn_star {
683+
span_bug!(self.span, "should be handled by `coerce`");
684+
} else {
685+
// Report "casting is invalid" rather than "non-primitive cast"
686+
// if the feature is not enabled.
687+
return Err(CastError::IllegalCast);
688+
}
689+
}
690+
681691
let (t_from, t_cast) = match (CastTy::from_ty(self.expr_ty), CastTy::from_ty(self.cast_ty))
682692
{
683693
(Some(t_from), Some(t_cast)) => (t_from, t_cast),
@@ -784,16 +794,6 @@ impl<'a, 'tcx> CastCheck<'tcx> {
784794
(Int(Char) | Int(Bool), Int(_)) => Ok(CastKind::PrimIntCast),
785795

786796
(Int(_) | Float, Int(_) | Float) => Ok(CastKind::NumericCast),
787-
788-
(_, DynStar) => {
789-
if fcx.tcx.features().dyn_star {
790-
bug!("should be handled by `coerce`")
791-
} else {
792-
Err(CastError::IllegalCast)
793-
}
794-
}
795-
796-
(DynStar, _) => Err(CastError::IllegalCast),
797797
}
798798
}
799799

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
@@ -757,9 +757,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
757757
for adjustment in adjustments {
758758
debug!("walk_adjustment expr={:?} adj={:?}", expr, adjustment);
759759
match adjustment.kind {
760-
adjustment::Adjust::NeverToAny
761-
| adjustment::Adjust::Pointer(_)
762-
| adjustment::Adjust::DynStar => {
760+
adjustment::Adjust::NeverToAny | adjustment::Adjust::Pointer(_) => {
763761
// Creating a closure/fn-pointer or unsizing consumes
764762
// the input and stores it into the resulting rvalue.
765763
self.consume_or_copy(&place_with_id, place_with_id.hir_id);
@@ -1283,8 +1281,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
12831281

12841282
adjustment::Adjust::NeverToAny
12851283
| adjustment::Adjust::Pointer(_)
1286-
| adjustment::Adjust::Borrow(_)
1287-
| adjustment::Adjust::DynStar => {
1284+
| adjustment::Adjust::Borrow(_) => {
12881285
// Result is an rvalue.
12891286
Ok(self.cat_rvalue(expr.hir_id, target))
12901287
}

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)