Skip to content

Commit cd40aba

Browse files
committed
remove subtyping
1 parent c52b876 commit cd40aba

File tree

35 files changed

+24
-228
lines changed

35 files changed

+24
-228
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2993,7 +2993,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
29932993
}
29942994
ProjectionElem::ConstantIndex { .. }
29952995
| ProjectionElem::Subslice { .. }
2996-
| ProjectionElem::Subtype(_)
29972996
| ProjectionElem::Index(_) => kind,
29982997
},
29992998
place_ty.projection_ty(tcx, elem),

compiler/rustc_borrowck/src/diagnostics/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
243243
ProjectionElem::Downcast(..) if opt.including_downcast => return None,
244244
ProjectionElem::Downcast(..) => (),
245245
ProjectionElem::OpaqueCast(..) => (),
246-
ProjectionElem::Subtype(..) => (),
247246
ProjectionElem::Field(field, _ty) => {
248247
// FIXME(project-rfc_2229#36): print capture precisely here.
249248
if let Some(field) = self.is_upvar_field_projection(PlaceRef {
@@ -324,9 +323,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
324323
PlaceRef { local, projection: proj_base }.ty(self.body, self.infcx.tcx)
325324
}
326325
ProjectionElem::Downcast(..) => place.ty(self.body, self.infcx.tcx),
327-
ProjectionElem::Subtype(ty) | ProjectionElem::OpaqueCast(ty) => {
328-
PlaceTy::from_ty(*ty)
329-
}
326+
ProjectionElem::OpaqueCast(ty) => PlaceTy::from_ty(*ty),
330327
ProjectionElem::Field(_, field_type) => PlaceTy::from_ty(*field_type),
331328
},
332329
};

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
157157
[
158158
..,
159159
ProjectionElem::Index(_)
160-
| ProjectionElem::Subtype(_)
161160
| ProjectionElem::ConstantIndex { .. }
162161
| ProjectionElem::OpaqueCast { .. }
163162
| ProjectionElem::Subslice { .. }

compiler/rustc_borrowck/src/lib.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1695,9 +1695,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
16951695
// `Downcast` : only changes information about a `Place` without moving.
16961696
// `Subtype` : only transmutes the type, so no moves.
16971697
// So it's safe to skip these.
1698-
ProjectionElem::OpaqueCast(_)
1699-
| ProjectionElem::Subtype(_)
1700-
| ProjectionElem::Downcast(_, _) => (),
1698+
ProjectionElem::OpaqueCast(_) | ProjectionElem::Downcast(_, _) => (),
17011699
}
17021700

17031701
place_ty = place_ty.projection_ty(tcx, elem);
@@ -1921,7 +1919,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
19211919
for (place_base, elem) in place.iter_projections().rev() {
19221920
match elem {
19231921
ProjectionElem::Index(_/*operand*/) |
1924-
ProjectionElem::Subtype(_) |
19251922
ProjectionElem::OpaqueCast(_) |
19261923
ProjectionElem::ConstantIndex { .. } |
19271924
// assigning to P[i] requires P to be valid.
@@ -2312,7 +2309,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
23122309
| ProjectionElem::Index(..)
23132310
| ProjectionElem::ConstantIndex { .. }
23142311
| ProjectionElem::Subslice { .. }
2315-
| ProjectionElem::Subtype(..)
23162312
| ProjectionElem::OpaqueCast { .. }
23172313
| ProjectionElem::Downcast(..) => {
23182314
let upvar_field_projection = self.is_upvar_field_projection(place);

compiler/rustc_borrowck/src/places_conflict.rs

-2
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ fn place_components_conflict<'tcx>(
249249
| (ProjectionElem::ConstantIndex { .. }, _, _)
250250
| (ProjectionElem::Subslice { .. }, _, _)
251251
| (ProjectionElem::OpaqueCast { .. }, _, _)
252-
| (ProjectionElem::Subtype(_), _, _)
253252
| (ProjectionElem::Downcast { .. }, _, _) => {
254253
// Recursive case. This can still be disjoint on a
255254
// further iteration if this a shallow access and
@@ -509,7 +508,6 @@ fn place_projection_conflict<'tcx>(
509508
| ProjectionElem::Field(..)
510509
| ProjectionElem::Index(..)
511510
| ProjectionElem::ConstantIndex { .. }
512-
| ProjectionElem::Subtype(_)
513511
| ProjectionElem::OpaqueCast { .. }
514512
| ProjectionElem::Subslice { .. }
515513
| ProjectionElem::Downcast(..),

compiler/rustc_borrowck/src/prefixes.rs

-3
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,6 @@ impl<'cx, 'tcx> Iterator for Prefixes<'cx, 'tcx> {
8989
cursor = cursor_base;
9090
continue 'cursor;
9191
}
92-
ProjectionElem::Subtype(..) => {
93-
panic!("Subtype projection is not allowed before borrow check")
94-
}
9592
ProjectionElem::Deref => {
9693
// (handled below)
9794
}

compiler/rustc_borrowck/src/type_check/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,6 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
719719
}
720720
PlaceTy::from_ty(fty)
721721
}
722-
ProjectionElem::Subtype(_) => {
723-
bug!("ProjectionElem::Subtype shouldn't exist in borrowck")
724-
}
725722
ProjectionElem::OpaqueCast(ty) => {
726723
let ty = self.sanitize_type(place, ty);
727724
let ty = self.cx.normalize(ty, location);
@@ -2592,9 +2589,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
25922589
| ProjectionElem::Subslice { .. } => {
25932590
// other field access
25942591
}
2595-
ProjectionElem::Subtype(_) => {
2596-
bug!("ProjectionElem::Subtype shouldn't exist in borrowck")
2597-
}
25982592
}
25992593
}
26002594
}

compiler/rustc_codegen_cranelift/src/base.rs

-1
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,6 @@ pub(crate) fn codegen_place<'tcx>(
864864
cplace = cplace.place_deref(fx);
865865
}
866866
PlaceElem::OpaqueCast(ty) => bug!("encountered OpaqueCast({ty}) in codegen"),
867-
PlaceElem::Subtype(ty) => cplace = cplace.place_transmute_type(fx, fx.monomorphize(ty)),
868867
PlaceElem::Field(field, _ty) => {
869868
cplace = cplace.place_field(fx, field);
870869
}

compiler/rustc_codegen_cranelift/src/value_and_place.rs

-10
Original file line numberDiff line numberDiff line change
@@ -693,16 +693,6 @@ impl<'tcx> CPlace<'tcx> {
693693
}
694694
}
695695

696-
/// Used for `ProjectionElem::Subtype`, `ty` has to be monomorphized before
697-
/// passed on.
698-
pub(crate) fn place_transmute_type(
699-
self,
700-
fx: &mut FunctionCx<'_, '_, 'tcx>,
701-
ty: Ty<'tcx>,
702-
) -> CPlace<'tcx> {
703-
CPlace { inner: self.inner, layout: fx.layout_of(ty) }
704-
}
705-
706696
pub(crate) fn place_field(
707697
self,
708698
fx: &mut FunctionCx<'_, '_, 'tcx>,

compiler/rustc_codegen_ssa/src/mir/place.rs

-1
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
466466
mir::ProjectionElem::OpaqueCast(ty) => {
467467
bug!("encountered OpaqueCast({ty}) in codegen")
468468
}
469-
mir::ProjectionElem::Subtype(ty) => cg_base.project_type(bx, self.monomorphize(ty)),
470469
mir::ProjectionElem::Index(index) => {
471470
let index = &mir::Operand::Copy(mir::Place::from(index));
472471
let index = self.codegen_operand(bx, index);

compiler/rustc_const_eval/src/interpret/eval_context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_middle::ty::layout::{
1313
self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOf, LayoutOfHelpers,
1414
TyAndLayout,
1515
};
16-
use rustc_middle::ty::{self, GenericArgsRef, ParamEnv, Ty, TyCtxt, TypeFoldable, Variance};
16+
use rustc_middle::ty::{self, GenericArgsRef, ParamEnv, Ty, TyCtxt, TypeFoldable};
1717
use rustc_mir_dataflow::storage::always_storage_live_locals;
1818
use rustc_session::Limit;
1919
use rustc_span::Span;
@@ -384,7 +384,7 @@ pub(super) fn mir_assign_valid_types<'tcx>(
384384
// all normal lifetimes are erased, higher-ranked types with their
385385
// late-bound lifetimes are still around and can lead to type
386386
// differences.
387-
if util::relate_types(tcx, param_env, Variance::Covariant, src.ty, dest.ty) {
387+
if util::is_subtype(tcx, param_env, src.ty, dest.ty) {
388388
// Make sure the layout is equal, too -- just to be safe. Miri really
389389
// needs layout equality. For performance reason we skip this check when
390390
// the types are equal. Equal types *can* have different layouts when

compiler/rustc_const_eval/src/interpret/projection.rs

-2
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,6 @@ where
341341
OpaqueCast(ty) => {
342342
span_bug!(self.cur_span(), "OpaqueCast({ty}) encountered after borrowck")
343343
}
344-
// We don't want anything happening here, this is here as a dummy.
345-
Subtype(_) => base.transmute(base.layout(), self)?,
346344
Field(field, _) => self.project_field(base, field.index())?,
347345
Downcast(_, variant) => self.project_downcast(base, variant)?,
348346
Deref => self.deref_pointer(&base.to_op(self)?)?.into(),

compiler/rustc_const_eval/src/transform/check_consts/check.rs

-1
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
670670
| ProjectionElem::Downcast(..)
671671
| ProjectionElem::OpaqueCast(..)
672672
| ProjectionElem::Subslice { .. }
673-
| ProjectionElem::Subtype(..)
674673
| ProjectionElem::Field(..)
675674
| ProjectionElem::Index(_) => {}
676675
}

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ where
306306
ProjectionElem::Index(index) if in_local(index) => return true,
307307

308308
ProjectionElem::Deref
309-
| ProjectionElem::Subtype(_)
310309
| ProjectionElem::Field(_, _)
311310
| ProjectionElem::OpaqueCast(_)
312311
| ProjectionElem::ConstantIndex { .. }

compiler/rustc_const_eval/src/transform/promote_consts.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,7 @@ impl<'tcx> Validator<'_, 'tcx> {
357357
return Err(Unpromotable);
358358
}
359359

360-
ProjectionElem::ConstantIndex { .. }
361-
| ProjectionElem::Subtype(_)
362-
| ProjectionElem::Subslice { .. } => {}
360+
ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {}
363361

364362
ProjectionElem::Index(local) => {
365363
let mut promotable = false;

compiler/rustc_const_eval/src/transform/validate.rs

+2-29
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_infer::traits::Reveal;
77
use rustc_middle::mir::interpret::Scalar;
88
use rustc_middle::mir::visit::{NonUseContext, PlaceContext, Visitor};
99
use rustc_middle::mir::*;
10-
use rustc_middle::ty::{self, InstanceDef, ParamEnv, Ty, TyCtxt, TypeVisitableExt, Variance};
10+
use rustc_middle::ty::{self, InstanceDef, ParamEnv, Ty, TyCtxt, TypeVisitableExt};
1111
use rustc_mir_dataflow::impls::MaybeStorageLive;
1212
use rustc_mir_dataflow::storage::always_storage_live_locals;
1313
use rustc_mir_dataflow::{Analysis, ResultsCursor};
@@ -16,8 +16,6 @@ use rustc_target::spec::abi::Abi;
1616

1717
use crate::util::is_within_packed;
1818

19-
use crate::util::relate_types;
20-
2119
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2220
enum EdgeKind {
2321
Unwind,
@@ -626,15 +624,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
626624
return true;
627625
}
628626

629-
// After borrowck subtyping should be fully explicit via
630-
// `Subtype` projections.
631-
let variance = if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
632-
Variance::Invariant
633-
} else {
634-
Variance::Covariant
635-
};
636-
637-
crate::util::relate_types(self.tcx, self.param_env, variance, src, dest)
627+
crate::util::is_subtype(self.tcx, self.param_env, src, dest)
638628
}
639629
}
640630

@@ -785,23 +775,6 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
785775
}
786776
}
787777
}
788-
ProjectionElem::Subtype(ty) => {
789-
if !relate_types(
790-
self.tcx,
791-
self.param_env,
792-
Variance::Covariant,
793-
ty,
794-
place_ref.ty(&self.body.local_decls, self.tcx).ty,
795-
) {
796-
self.fail(
797-
location,
798-
format!(
799-
"Failed subtyping {ty:#?} and {:#?}",
800-
place_ref.ty(&self.body.local_decls, self.tcx).ty
801-
),
802-
)
803-
}
804-
}
805778
_ => {}
806779
}
807780
self.super_projection_elem(place_ref, elem, context, location);

compiler/rustc_const_eval/src/util/compare_types.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use rustc_infer::infer::TyCtxtInferExt;
77
use rustc_middle::traits::{DefiningAnchor, ObligationCause};
8-
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt, Variance};
8+
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
99
use rustc_trait_selection::traits::ObligationCtxt;
1010

1111
/// Returns whether the two types are equal up to subtyping.
@@ -24,22 +24,16 @@ pub fn is_equal_up_to_subtyping<'tcx>(
2424
}
2525

2626
// Check for subtyping in either direction.
27-
relate_types(tcx, param_env, Variance::Covariant, src, dest)
28-
|| relate_types(tcx, param_env, Variance::Covariant, dest, src)
27+
is_subtype(tcx, param_env, src, dest) || is_subtype(tcx, param_env, dest, src)
2928
}
3029

3130
/// Returns whether `src` is a subtype of `dest`, i.e. `src <: dest`.
3231
///
33-
/// When validating assignments, the variance should be `Covariant`. When checking
34-
/// during `MirPhase` >= `MirPhase::Runtime(RuntimePhase::Initial)` variance should be `Invariant`
35-
/// because we want to check for type equality.
36-
///
3732
/// This mostly ignores opaque types as it can be used in constraining contexts
3833
/// while still computing the final underlying type.
39-
pub fn relate_types<'tcx>(
34+
pub fn is_subtype<'tcx>(
4035
tcx: TyCtxt<'tcx>,
4136
param_env: ParamEnv<'tcx>,
42-
variance: Variance,
4337
src: Ty<'tcx>,
4438
dest: Ty<'tcx>,
4539
) -> bool {
@@ -54,7 +48,7 @@ pub fn relate_types<'tcx>(
5448
let cause = ObligationCause::dummy();
5549
let src = ocx.normalize(&cause, param_env, src);
5650
let dest = ocx.normalize(&cause, param_env, dest);
57-
match ocx.relate(&cause, param_env, variance, src, dest) {
51+
match ocx.sub(&cause, param_env, src, dest) {
5852
Ok(()) => {}
5953
Err(_) => return false,
6054
};

compiler/rustc_const_eval/src/util/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod type_name;
88

99
pub use self::alignment::{is_disaligned, is_within_packed};
1010
pub use self::check_validity_requirement::check_validity_requirement;
11-
pub use self::compare_types::{is_equal_up_to_subtyping, relate_types};
11+
pub use self::compare_types::{is_equal_up_to_subtyping, is_subtype};
1212
pub use self::type_name::type_name;
1313

1414
/// Classify whether an operator is "left-homogeneous", i.e., the LHS has the

compiler/rustc_middle/src/mir/pretty.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,6 @@ fn pre_fmt_projection(projection: &[PlaceElem<'_>], fmt: &mut Formatter<'_>) ->
11211121
for &elem in projection.iter().rev() {
11221122
match elem {
11231123
ProjectionElem::OpaqueCast(_)
1124-
| ProjectionElem::Subtype(_)
11251124
| ProjectionElem::Downcast(_, _)
11261125
| ProjectionElem::Field(_, _) => {
11271126
write!(fmt, "(").unwrap();
@@ -1144,9 +1143,6 @@ fn post_fmt_projection(projection: &[PlaceElem<'_>], fmt: &mut Formatter<'_>) ->
11441143
ProjectionElem::OpaqueCast(ty) => {
11451144
write!(fmt, " as {ty})")?;
11461145
}
1147-
ProjectionElem::Subtype(ty) => {
1148-
write!(fmt, " as subtype {ty})")?;
1149-
}
11501146
ProjectionElem::Downcast(Some(name), _index) => {
11511147
write!(fmt, " as {name})")?;
11521148
}

compiler/rustc_middle/src/mir/statement.rs

-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ impl<V, T> ProjectionElem<V, T> {
5757
Self::Field(_, _)
5858
| Self::Index(_)
5959
| Self::OpaqueCast(_)
60-
| Self::Subtype(_)
6160
| Self::ConstantIndex { .. }
6261
| Self::Subslice { .. }
6362
| Self::Downcast(_, _) => false,
@@ -71,7 +70,6 @@ impl<V, T> ProjectionElem<V, T> {
7170
Self::Deref | Self::Index(_) => false,
7271
Self::Field(_, _)
7372
| Self::OpaqueCast(_)
74-
| Self::Subtype(_)
7573
| Self::ConstantIndex { .. }
7674
| Self::Subslice { .. }
7775
| Self::Downcast(_, _) => true,
@@ -97,7 +95,6 @@ impl<V, T> ProjectionElem<V, T> {
9795
| Self::Field(_, _) => true,
9896
Self::ConstantIndex { from_end: true, .. }
9997
| Self::Index(_)
100-
| Self::Subtype(_)
10198
| Self::OpaqueCast(_)
10299
| Self::Subslice { .. } => false,
103100
}

compiler/rustc_middle/src/mir/syntax.rs

-12
Original file line numberDiff line numberDiff line change
@@ -1076,18 +1076,6 @@ pub enum ProjectionElem<V, T> {
10761076
/// Like an explicit cast from an opaque type to a concrete type, but without
10771077
/// requiring an intermediate variable.
10781078
OpaqueCast(T),
1079-
1080-
/// A `Subtype(T)` projection is applied to any `StatementKind::Assign` where
1081-
/// type of lvalue doesn't match the type of rvalue, the primary goal is making subtyping
1082-
/// explicit during optimizations and codegen.
1083-
///
1084-
/// This projection doesn't impact the runtime behavior of the program except for potentially changing
1085-
/// some type metadata of the interpreter or codegen backend.
1086-
///
1087-
/// This goal is achieved with mir_transform pass `Subtyper`, which runs right after
1088-
/// borrowchecker, as we only care about subtyping that can affect trait selection and
1089-
/// `TypeId`.
1090-
Subtype(T),
10911079
}
10921080

10931081
/// Alias for projections as they appear in places, where the base is a place

compiler/rustc_middle/src/mir/tcx.rs

-3
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,6 @@ impl<'tcx> PlaceTy<'tcx> {
111111
ProjectionElem::OpaqueCast(ty) => {
112112
PlaceTy::from_ty(handle_opaque_cast_and_subtype(&self, ty))
113113
}
114-
ProjectionElem::Subtype(ty) => {
115-
PlaceTy::from_ty(handle_opaque_cast_and_subtype(&self, ty))
116-
}
117114
};
118115
debug!("projection_ty self: {:?} elem: {:?} yields: {:?}", self, elem, answer);
119116
answer

compiler/rustc_middle/src/mir/visit.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -1110,11 +1110,6 @@ macro_rules! visit_place_fns {
11101110
self.visit_ty(&mut new_ty, TyContext::Location(location));
11111111
if ty != new_ty { Some(PlaceElem::OpaqueCast(new_ty)) } else { None }
11121112
}
1113-
PlaceElem::Subtype(ty) => {
1114-
let mut new_ty = ty;
1115-
self.visit_ty(&mut new_ty, TyContext::Location(location));
1116-
if ty != new_ty { Some(PlaceElem::Subtype(new_ty)) } else { None }
1117-
}
11181113
PlaceElem::Deref
11191114
| PlaceElem::ConstantIndex { .. }
11201115
| PlaceElem::Subslice { .. }
@@ -1181,9 +1176,7 @@ macro_rules! visit_place_fns {
11811176
location: Location,
11821177
) {
11831178
match elem {
1184-
ProjectionElem::OpaqueCast(ty)
1185-
| ProjectionElem::Subtype(ty)
1186-
| ProjectionElem::Field(_, ty) => {
1179+
ProjectionElem::OpaqueCast(ty) | ProjectionElem::Field(_, ty) => {
11871180
self.visit_ty(ty, TyContext::Location(location));
11881181
}
11891182
ProjectionElem::Index(local) => {

0 commit comments

Comments
 (0)