Skip to content

Commit 918d0ac

Browse files
committed
Auto merge of #104986 - compiler-errors:opaques, r=oli-obk
Combine `ty::Projection` and `ty::Opaque` into `ty::Alias` Implements rust-lang/types-team#79. This PR consolidates `ty::Projection` and `ty::Opaque` into a single `ty::Alias`, with an `AliasKind` and `AliasTy` type (renamed from `ty::ProjectionTy`, which is the inner data of `ty::Projection`) defined as so: ``` enum AliasKind { Projection, Opaque, } struct AliasTy<'tcx> { def_id: DefId, substs: SubstsRef<'tcx>, } ``` Since we don't have access to `TyCtxt` in type flags computation, and because repeatedly calling `DefKind` on the def-id is expensive, these two types are distinguished with `ty::AliasKind`, conveniently glob-imported into `ty::{Projection, Opaque}`. For example: ```diff match ty.kind() { - ty::Opaque(..) => + ty::Alias(ty::Opaque, ..) => {} _ => {} } ``` This PR also consolidates match arms that treated `ty::Opaque` and `ty::Projection` identically. r? `@ghost`
2 parents 21ee03e + 99417d5 commit 918d0ac

File tree

115 files changed

+632
-674
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+632
-674
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
697697
.map_bound(|p| p.predicates),
698698
None,
699699
),
700-
ty::Opaque(did, substs) => {
701-
find_fn_kind_from_did(tcx.bound_explicit_item_bounds(*did), Some(*substs))
700+
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs }) => {
701+
find_fn_kind_from_did(tcx.bound_explicit_item_bounds(*def_id), Some(*substs))
702702
}
703703
ty::Closure(_, substs) => match substs.as_closure().kind() {
704704
ty::ClosureKind::Fn => Some(hir::Mutability::Not),

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
504504
let ErrorConstraintInfo { outlived_fr, span, .. } = errci;
505505

506506
let mut output_ty = self.regioncx.universal_regions().unnormalized_output_ty;
507-
if let ty::Opaque(def_id, _) = *output_ty.kind() {
507+
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs: _ }) = *output_ty.kind() {
508508
output_ty = self.infcx.tcx.type_of(def_id)
509509
};
510510

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ fn push_debuginfo_type_name<'tcx>(
235235
let projection_bounds: SmallVec<[_; 4]> = trait_data
236236
.projection_bounds()
237237
.map(|bound| {
238-
let ExistentialProjection { item_def_id, term, .. } =
238+
let ExistentialProjection { def_id: item_def_id, term, .. } =
239239
tcx.erase_late_bound_regions(bound);
240240
// FIXME(associated_const_equality): allow for consts here
241241
(item_def_id, term.ty().unwrap())
@@ -411,9 +411,8 @@ fn push_debuginfo_type_name<'tcx>(
411411
ty::Error(_)
412412
| ty::Infer(_)
413413
| ty::Placeholder(..)
414-
| ty::Projection(..)
414+
| ty::Alias(..)
415415
| ty::Bound(..)
416-
| ty::Opaque(..)
417416
| ty::GeneratorWitness(..) => {
418417
bug!(
419418
"debuginfo: Trying to create type name for \

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,11 @@ pub(crate) fn const_to_valtree_inner<'tcx>(
142142
| ty::Foreign(..)
143143
| ty::Infer(ty::FreshIntTy(_))
144144
| ty::Infer(ty::FreshFloatTy(_))
145-
| ty::Projection(..)
145+
// FIXME(oli-obk): we could look behind opaque types
146+
| ty::Alias(..)
146147
| ty::Param(_)
147148
| ty::Bound(..)
148149
| ty::Placeholder(..)
149-
// FIXME(oli-obk): we could look behind opaque types
150-
| ty::Opaque(..)
151150
| ty::Infer(_)
152151
// FIXME(oli-obk): we can probably encode closures just like structs
153152
| ty::Closure(..)
@@ -307,11 +306,10 @@ pub fn valtree_to_const_value<'tcx>(
307306
| ty::Foreign(..)
308307
| ty::Infer(ty::FreshIntTy(_))
309308
| ty::Infer(ty::FreshFloatTy(_))
310-
| ty::Projection(..)
309+
| ty::Alias(..)
311310
| ty::Param(_)
312311
| ty::Bound(..)
313312
| ty::Placeholder(..)
314-
| ty::Opaque(..)
315313
| ty::Infer(_)
316314
| ty::Closure(..)
317315
| ty::Generator(..)

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,9 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
8282
ty::Adt(ref adt, _) => {
8383
ConstValue::from_machine_usize(adt.variants().len() as u64, &tcx)
8484
}
85-
ty::Projection(_)
86-
| ty::Opaque(_, _)
87-
| ty::Param(_)
88-
| ty::Placeholder(_)
89-
| ty::Infer(_) => throw_inval!(TooGeneric),
85+
ty::Alias(..) | ty::Param(_) | ty::Placeholder(_) | ty::Infer(_) => {
86+
throw_inval!(TooGeneric)
87+
}
9088
ty::Bound(_, _) => bug!("bound ty during ctfe"),
9189
ty::Bool
9290
| ty::Char

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
601601
| ty::Placeholder(..)
602602
| ty::Bound(..)
603603
| ty::Param(..)
604-
| ty::Opaque(..)
605-
| ty::Projection(..)
604+
| ty::Alias(..)
606605
| ty::GeneratorWitness(..) => bug!("Encountered invalid type {:?}", ty),
607606
}
608607
}

compiler/rustc_const_eval/src/transform/validate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
241241
};
242242

243243
let kind = match parent_ty.ty.kind() {
244-
&ty::Opaque(def_id, substs) => {
244+
&ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs }) => {
245245
self.tcx.bound_type_of(def_id).subst(self.tcx, substs).kind()
246246
}
247247
kind => kind,
@@ -652,7 +652,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
652652
self.fail(location, "`SetDiscriminant`is not allowed until deaggregation");
653653
}
654654
let pty = place.ty(&self.body.local_decls, self.tcx).ty.kind();
655-
if !matches!(pty, ty::Adt(..) | ty::Generator(..) | ty::Opaque(..)) {
655+
if !matches!(pty, ty::Adt(..) | ty::Generator(..) | ty::Alias(ty::Opaque, ..)) {
656656
self.fail(
657657
location,
658658
format!(

compiler/rustc_const_eval/src/util/type_name.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
5858
// Types with identity (print the module path).
5959
ty::Adt(ty::AdtDef(Interned(&ty::AdtDefData { did: def_id, .. }, _)), substs)
6060
| ty::FnDef(def_id, substs)
61-
| ty::Opaque(def_id, substs)
62-
| ty::Projection(ty::ProjectionTy { item_def_id: def_id, substs })
61+
| ty::Alias(_, ty::AliasTy { def_id, substs })
6362
| ty::Closure(def_id, substs)
6463
| ty::Generator(def_id, substs, _) => self.print_def_path(def_id, substs),
6564
ty::Foreign(def_id) => self.print_def_path(def_id, &[]),

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,10 +1146,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
11461146

11471147
debug!(?substs_trait_ref_and_assoc_item);
11481148

1149-
ty::ProjectionTy {
1150-
item_def_id: assoc_item.def_id,
1151-
substs: substs_trait_ref_and_assoc_item,
1152-
}
1149+
ty::AliasTy { def_id: assoc_item.def_id, substs: substs_trait_ref_and_assoc_item }
11531150
});
11541151

11551152
if !speculative {
@@ -1195,7 +1192,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
11951192
// the "projection predicate" for:
11961193
//
11971194
// `<T as Iterator>::Item = u32`
1198-
let assoc_item_def_id = projection_ty.skip_binder().item_def_id;
1195+
let assoc_item_def_id = projection_ty.skip_binder().def_id;
11991196
let def_kind = tcx.def_kind(assoc_item_def_id);
12001197
match (def_kind, term.unpack()) {
12011198
(hir::def::DefKind::AssocTy, ty::TermKind::Ty(_))
@@ -1244,7 +1241,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
12441241
//
12451242
// Calling `skip_binder` is okay, because `add_bounds` expects the `param_ty`
12461243
// parameter to have a skipped binder.
1247-
let param_ty = tcx.mk_ty(ty::Projection(projection_ty.skip_binder()));
1244+
let param_ty = tcx.mk_ty(ty::Alias(ty::Projection, projection_ty.skip_binder()));
12481245
self.add_bounds(param_ty, ast_bounds.iter(), bounds, candidate.bound_vars());
12491246
}
12501247
}

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,7 @@ fn opaque_type_cycle_error(tcx: TyCtxt<'_>, def_id: LocalDefId, span: Span) -> E
14401440
impl<'tcx> ty::visit::TypeVisitor<'tcx> for OpaqueTypeCollector {
14411441
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
14421442
match *t.kind() {
1443-
ty::Opaque(def, _) => {
1443+
ty::Alias(ty::Opaque, ty::AliasTy { def_id: def, substs: _ }) => {
14441444
self.0.push(def);
14451445
ControlFlow::CONTINUE
14461446
}

0 commit comments

Comments
 (0)