Skip to content

Commit 1974b6b

Browse files
committed
Introduce GeneratorWitnessMIR.
1 parent 03618d6 commit 1974b6b

File tree

52 files changed

+265
-72
lines changed

Some content is hidden

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

52 files changed

+265
-72
lines changed

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+1
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ fn push_debuginfo_type_name<'tcx>(
414414
| ty::Placeholder(..)
415415
| ty::Alias(..)
416416
| ty::Bound(..)
417+
| ty::GeneratorWitnessMIR(..)
417418
| ty::GeneratorWitness(..) => {
418419
bug!(
419420
"debuginfo: Trying to create type name for \

compiler/rustc_const_eval/src/const_eval/valtrees.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub(crate) fn const_to_valtree_inner<'tcx>(
151151
// FIXME(oli-obk): we can probably encode closures just like structs
152152
| ty::Closure(..)
153153
| ty::Generator(..)
154-
| ty::GeneratorWitness(..) => Err(ValTreeCreationError::NonSupportedType),
154+
| ty::GeneratorWitness(..) |ty::GeneratorWitnessMIR(..)=> Err(ValTreeCreationError::NonSupportedType),
155155
}
156156
}
157157

@@ -314,6 +314,7 @@ pub fn valtree_to_const_value<'tcx>(
314314
| ty::Closure(..)
315315
| ty::Generator(..)
316316
| ty::GeneratorWitness(..)
317+
| ty::GeneratorWitnessMIR(..)
317318
| ty::FnPtr(_)
318319
| ty::RawPtr(_)
319320
| ty::Str

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
101101
| ty::Closure(_, _)
102102
| ty::Generator(_, _, _)
103103
| ty::GeneratorWitness(_)
104+
| ty::GeneratorWitnessMIR(_, _)
104105
| ty::Never
105106
| ty::Tuple(_)
106107
| ty::Error(_) => ConstValue::from_machine_usize(0u64, &tcx),

compiler/rustc_const_eval/src/interpret/validity.rs

+1
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
602602
| ty::Bound(..)
603603
| ty::Param(..)
604604
| ty::Alias(..)
605+
| ty::GeneratorWitnessMIR(..)
605606
| ty::GeneratorWitness(..) => bug!("Encountered invalid type {:?}", ty),
606607
}
607608
}

compiler/rustc_const_eval/src/util/type_name.rs

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
6464
ty::Foreign(def_id) => self.print_def_path(def_id, &[]),
6565

6666
ty::GeneratorWitness(_) => bug!("type_name: unexpected `GeneratorWitness`"),
67+
ty::GeneratorWitnessMIR(..) => bug!("type_name: unexpected `GeneratorWitnessMIR`"),
6768
}
6869
}
6970

compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs

+1
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ impl<'tcx> InherentCollect<'tcx> {
240240
| ty::Closure(..)
241241
| ty::Generator(..)
242242
| ty::GeneratorWitness(..)
243+
| ty::GeneratorWitnessMIR(..)
243244
| ty::Bound(..)
244245
| ty::Placeholder(_)
245246
| ty::Infer(_) => {

compiler/rustc_hir_analysis/src/variance/constraints.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,12 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
295295
// types, where we use Error as the Self type
296296
}
297297

298-
ty::Placeholder(..) | ty::GeneratorWitness(..) | ty::Bound(..) | ty::Infer(..) => {
299-
bug!(
300-
"unexpected type encountered in \
301-
variance inference: {}",
302-
ty
303-
);
298+
ty::Placeholder(..)
299+
| ty::GeneratorWitness(..)
300+
| ty::GeneratorWitnessMIR(..)
301+
| ty::Bound(..)
302+
| ty::Infer(..) => {
303+
bug!("unexpected type encountered in variance inference: {}", ty);
304304
}
305305
}
306306
}

compiler/rustc_hir_typeck/src/cast.rs

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
130130
| ty::Float(_)
131131
| ty::Array(..)
132132
| ty::GeneratorWitness(..)
133+
| ty::GeneratorWitnessMIR(..)
133134
| ty::RawPtr(_)
134135
| ty::Ref(..)
135136
| ty::FnDef(..)

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

+1
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
435435
ty::Closure(..)
436436
| ty::Generator(..)
437437
| ty::GeneratorWitness(..)
438+
| ty::GeneratorWitnessMIR(..)
438439
| ty::Bool
439440
| ty::Char
440441
| ty::Int(..)

compiler/rustc_infer/src/infer/freshen.rs

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
209209
| ty::Foreign(..)
210210
| ty::Param(..)
211211
| ty::Closure(..)
212+
| ty::GeneratorWitnessMIR(..)
212213
| ty::GeneratorWitness(..) => t.super_fold_with(self),
213214

214215
ty::Placeholder(..) | ty::Bound(..) => bug!("unexpected type {:?}", t),

compiler/rustc_infer/src/infer/outlives/components.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn compute_components<'tcx>(
112112
}
113113

114114
// All regions are bound inside a witness
115-
ty::GeneratorWitness(..) => (),
115+
ty::GeneratorWitness(..) | ty::GeneratorWitnessMIR(..) => (),
116116

117117
// OutlivesTypeParameterEnv -- the actual checking that `X:'a`
118118
// is implied by the environment is done in regionck.

compiler/rustc_lint/src/types.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
11071107
| ty::Closure(..)
11081108
| ty::Generator(..)
11091109
| ty::GeneratorWitness(..)
1110+
| ty::GeneratorWitnessMIR(..)
11101111
| ty::Placeholder(..)
11111112
| ty::FnDef(..) => bug!("unexpected type in foreign function: {:?}", ty),
11121113
}

compiler/rustc_middle/src/ty/context.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,7 @@ impl<'tcx> TyCtxt<'tcx> {
13061306
Placeholder,
13071307
Generator,
13081308
GeneratorWitness,
1309+
GeneratorWitnessMIR,
13091310
Dynamic,
13101311
Closure,
13111312
Tuple,
@@ -1815,6 +1816,11 @@ impl<'tcx> TyCtxt<'tcx> {
18151816
self.mk_mut_ref(self.lifetimes.re_erased, context_ty)
18161817
}
18171818

1819+
#[inline]
1820+
pub fn mk_generator_witness_mir(self, id: DefId, substs: SubstsRef<'tcx>) -> Ty<'tcx> {
1821+
self.mk_ty(GeneratorWitnessMIR(id, substs))
1822+
}
1823+
18181824
#[inline]
18191825
pub fn mk_ty_var(self, v: TyVid) -> Ty<'tcx> {
18201826
self.mk_ty_infer(TyVar(v))

compiler/rustc_middle/src/ty/error.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ impl<'tcx> Ty<'tcx> {
325325
ty::Dynamic(..) => "trait object".into(),
326326
ty::Closure(..) => "closure".into(),
327327
ty::Generator(def_id, ..) => tcx.generator_kind(def_id).unwrap().descr().into(),
328-
ty::GeneratorWitness(..) => "generator witness".into(),
328+
ty::GeneratorWitness(..) |
329+
ty::GeneratorWitnessMIR(..) => "generator witness".into(),
329330
ty::Tuple(..) => "tuple".into(),
330331
ty::Infer(ty::TyVar(_)) => "inferred type".into(),
331332
ty::Infer(ty::IntVar(_)) => "integer".into(),
@@ -373,7 +374,7 @@ impl<'tcx> Ty<'tcx> {
373374
ty::Dynamic(..) => "trait object".into(),
374375
ty::Closure(..) => "closure".into(),
375376
ty::Generator(def_id, ..) => tcx.generator_kind(def_id).unwrap().descr().into(),
376-
ty::GeneratorWitness(..) => "generator witness".into(),
377+
ty::GeneratorWitness(..) | ty::GeneratorWitnessMIR(..) => "generator witness".into(),
377378
ty::Tuple(..) => "tuple".into(),
378379
ty::Placeholder(..) => "higher-ranked type".into(),
379380
ty::Bound(..) => "bound type variable".into(),

compiler/rustc_middle/src/ty/fast_reject.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub enum SimplifiedType {
3232
ClosureSimplifiedType(DefId),
3333
GeneratorSimplifiedType(DefId),
3434
GeneratorWitnessSimplifiedType(usize),
35+
GeneratorWitnessMIRSimplifiedType(DefId),
3536
FunctionSimplifiedType(usize),
3637
PlaceholderSimplifiedType,
3738
}
@@ -108,6 +109,7 @@ pub fn simplify_type<'tcx>(
108109
ty::FnDef(def_id, _) | ty::Closure(def_id, _) => Some(ClosureSimplifiedType(def_id)),
109110
ty::Generator(def_id, _, _) => Some(GeneratorSimplifiedType(def_id)),
110111
ty::GeneratorWitness(tys) => Some(GeneratorWitnessSimplifiedType(tys.skip_binder().len())),
112+
ty::GeneratorWitnessMIR(def_id, _) => Some(GeneratorWitnessMIRSimplifiedType(def_id)),
111113
ty::Never => Some(NeverSimplifiedType),
112114
ty::Tuple(tys) => Some(TupleSimplifiedType(tys.len())),
113115
ty::FnPtr(f) => Some(FunctionSimplifiedType(f.skip_binder().inputs().len())),
@@ -139,7 +141,8 @@ impl SimplifiedType {
139141
| ForeignSimplifiedType(d)
140142
| TraitSimplifiedType(d)
141143
| ClosureSimplifiedType(d)
142-
| GeneratorSimplifiedType(d) => Some(d),
144+
| GeneratorSimplifiedType(d)
145+
| GeneratorWitnessMIRSimplifiedType(d) => Some(d),
143146
_ => None,
144147
}
145148
}
@@ -208,6 +211,7 @@ impl DeepRejectCtxt {
208211
| ty::Closure(..)
209212
| ty::Generator(..)
210213
| ty::GeneratorWitness(..)
214+
| ty::GeneratorWitnessMIR(..)
211215
| ty::Placeholder(..)
212216
| ty::Bound(..)
213217
| ty::Infer(_) => bug!("unexpected impl_ty: {impl_ty}"),
@@ -306,7 +310,7 @@ impl DeepRejectCtxt {
306310

307311
ty::Error(_) => true,
308312

309-
ty::GeneratorWitness(..) | ty::Bound(..) => {
313+
ty::GeneratorWitness(..) | ty::GeneratorWitnessMIR(..) | ty::Bound(..) => {
310314
bug!("unexpected obligation type: {:?}", obligation_ty)
311315
}
312316
}

compiler/rustc_middle/src/ty/flags.rs

+10
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@ impl FlagComputation {
125125
self.bound_computation(ts, |flags, ts| flags.add_tys(ts));
126126
}
127127

128+
&ty::GeneratorWitnessMIR(_, ref substs) => {
129+
let should_remove_further_specializable =
130+
!self.flags.contains(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
131+
self.add_substs(substs);
132+
if should_remove_further_specializable {
133+
self.flags -= TypeFlags::STILL_FURTHER_SPECIALIZABLE;
134+
}
135+
self.add_flags(TypeFlags::HAS_TY_GENERATOR);
136+
}
137+
128138
&ty::Closure(_, substs) => {
129139
let substs = substs.as_closure();
130140
let should_remove_further_specializable =

compiler/rustc_middle/src/ty/layout.rs

+1
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ where
645645
| ty::Never
646646
| ty::FnDef(..)
647647
| ty::GeneratorWitness(..)
648+
| ty::GeneratorWitnessMIR(..)
648649
| ty::Foreign(..)
649650
| ty::Dynamic(_, _, ty::Dyn) => {
650651
bug!("TyAndLayout::field({:?}): not applicable", this)

compiler/rustc_middle/src/ty/opaque_types.rs

+49-46
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::ty::fold::{TypeFolder, TypeSuperFoldable};
33
use crate::ty::subst::{GenericArg, GenericArgKind};
44
use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
55
use rustc_data_structures::fx::FxHashMap;
6+
use rustc_span::def_id::DefId;
67
use rustc_span::Span;
78

89
/// Converts generic params of a TypeFoldable from one
@@ -47,6 +48,47 @@ impl<'tcx> ReverseMapper<'tcx> {
4748
assert!(!self.do_not_error);
4849
kind.fold_with(self)
4950
}
51+
52+
fn fold_closure_substs(
53+
&mut self,
54+
def_id: DefId,
55+
substs: ty::SubstsRef<'tcx>,
56+
) -> ty::SubstsRef<'tcx> {
57+
// I am a horrible monster and I pray for death. When
58+
// we encounter a closure here, it is always a closure
59+
// from within the function that we are currently
60+
// type-checking -- one that is now being encapsulated
61+
// in an opaque type. Ideally, we would
62+
// go through the types/lifetimes that it references
63+
// and treat them just like we would any other type,
64+
// which means we would error out if we find any
65+
// reference to a type/region that is not in the
66+
// "reverse map".
67+
//
68+
// **However,** in the case of closures, there is a
69+
// somewhat subtle (read: hacky) consideration. The
70+
// problem is that our closure types currently include
71+
// all the lifetime parameters declared on the
72+
// enclosing function, even if they are unused by the
73+
// closure itself. We can't readily filter them out,
74+
// so here we replace those values with `'empty`. This
75+
// can't really make a difference to the rest of the
76+
// compiler; those regions are ignored for the
77+
// outlives relation, and hence don't affect trait
78+
// selection or auto traits, and they are erased
79+
// during codegen.
80+
81+
let generics = self.tcx.generics_of(def_id);
82+
self.tcx.mk_substs(substs.iter().enumerate().map(|(index, kind)| {
83+
if index < generics.parent_count {
84+
// Accommodate missing regions in the parent kinds...
85+
self.fold_kind_no_missing_regions_error(kind)
86+
} else {
87+
// ...but not elsewhere.
88+
self.fold_kind_normally(kind)
89+
}
90+
}))
91+
}
5092
}
5193

5294
impl<'tcx> TypeFolder<'tcx> for ReverseMapper<'tcx> {
@@ -104,59 +146,20 @@ impl<'tcx> TypeFolder<'tcx> for ReverseMapper<'tcx> {
104146
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
105147
match *ty.kind() {
106148
ty::Closure(def_id, substs) => {
107-
// I am a horrible monster and I pray for death. When
108-
// we encounter a closure here, it is always a closure
109-
// from within the function that we are currently
110-
// type-checking -- one that is now being encapsulated
111-
// in an opaque type. Ideally, we would
112-
// go through the types/lifetimes that it references
113-
// and treat them just like we would any other type,
114-
// which means we would error out if we find any
115-
// reference to a type/region that is not in the
116-
// "reverse map".
117-
//
118-
// **However,** in the case of closures, there is a
119-
// somewhat subtle (read: hacky) consideration. The
120-
// problem is that our closure types currently include
121-
// all the lifetime parameters declared on the
122-
// enclosing function, even if they are unused by the
123-
// closure itself. We can't readily filter them out,
124-
// so here we replace those values with `'empty`. This
125-
// can't really make a difference to the rest of the
126-
// compiler; those regions are ignored for the
127-
// outlives relation, and hence don't affect trait
128-
// selection or auto traits, and they are erased
129-
// during codegen.
130-
131-
let generics = self.tcx.generics_of(def_id);
132-
let substs = self.tcx.mk_substs(substs.iter().enumerate().map(|(index, kind)| {
133-
if index < generics.parent_count {
134-
// Accommodate missing regions in the parent kinds...
135-
self.fold_kind_no_missing_regions_error(kind)
136-
} else {
137-
// ...but not elsewhere.
138-
self.fold_kind_normally(kind)
139-
}
140-
}));
141-
149+
let substs = self.fold_closure_substs(def_id, substs);
142150
self.tcx.mk_closure(def_id, substs)
143151
}
144152

145153
ty::Generator(def_id, substs, movability) => {
146-
let generics = self.tcx.generics_of(def_id);
147-
let substs = self.tcx.mk_substs(substs.iter().enumerate().map(|(index, kind)| {
148-
if index < generics.parent_count {
149-
// Accommodate missing regions in the parent kinds...
150-
self.fold_kind_no_missing_regions_error(kind)
151-
} else {
152-
// ...but not elsewhere.
153-
self.fold_kind_normally(kind)
154-
}
155-
}));
156-
154+
let substs = self.fold_closure_substs(def_id, substs);
157155
self.tcx.mk_generator(def_id, substs, movability)
158156
}
159157

158+
ty::GeneratorWitnessMIR(def_id, substs) => {
159+
let substs = self.fold_closure_substs(def_id, substs);
160+
self.tcx.mk_generator_witness_mir(def_id, substs)
161+
}
162+
160163
ty::Param(param) => {
161164
// Look it up in the substitution list.
162165
match self.map.get(&ty.into()).map(|k| k.unpack()) {

compiler/rustc_middle/src/ty/print/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ fn characteristic_def_id_of_type_cached<'a>(
265265
ty::FnDef(def_id, _)
266266
| ty::Closure(def_id, _)
267267
| ty::Generator(def_id, _, _)
268+
| ty::GeneratorWitnessMIR(def_id, _)
268269
| ty::Foreign(def_id) => Some(def_id),
269270

270271
ty::Bool

compiler/rustc_middle/src/ty/print/pretty.rs

+22
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,28 @@ pub trait PrettyPrinter<'tcx>:
811811
ty::GeneratorWitness(types) => {
812812
p!(in_binder(&types));
813813
}
814+
ty::GeneratorWitnessMIR(did, substs) => {
815+
p!(write("["));
816+
if !self.tcx().sess.verbose() {
817+
p!("generator witness");
818+
// FIXME(eddyb) should use `def_span`.
819+
if let Some(did) = did.as_local() {
820+
let span = self.tcx().def_span(did);
821+
p!(write(
822+
"@{}",
823+
// This may end up in stderr diagnostics but it may also be emitted
824+
// into MIR. Hence we use the remapped path if available
825+
self.tcx().sess.source_map().span_to_embeddable_string(span)
826+
));
827+
} else {
828+
p!(write("@"), print_def_path(did, substs));
829+
}
830+
} else {
831+
p!(print_def_path(did, substs));
832+
}
833+
834+
p!("]")
835+
}
814836
ty::Closure(did, substs) => {
815837
p!(write("["));
816838
if !self.should_print_verbose() {

0 commit comments

Comments
 (0)