Skip to content

Commit 29fa325

Browse files
Initial work on the type system layer
1 parent 888211f commit 29fa325

File tree

54 files changed

+284
-29
lines changed

Some content is hidden

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

54 files changed

+284
-29
lines changed

compiler/rustc_borrowck/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
16341634
| ty::Slice(_)
16351635
| ty::FnDef(_, _)
16361636
| ty::FnPtr(..)
1637+
| ty::UnsafeBinder(_)
16371638
| ty::Dynamic(_, _, _)
16381639
| ty::Closure(_, _)
16391640
| ty::CoroutineClosure(_, _)
@@ -1679,6 +1680,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
16791680
| ty::Ref(_, _, _)
16801681
| ty::FnDef(_, _)
16811682
| ty::FnPtr(..)
1683+
| ty::UnsafeBinder(_)
16821684
| ty::Dynamic(_, _, _)
16831685
| ty::CoroutineWitness(..)
16841686
| ty::Never

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+1
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ fn push_debuginfo_type_name<'tcx>(
435435
push_closure_or_coroutine_name(tcx, def_id, args, qualified, output, visited);
436436
}
437437
}
438+
ty::UnsafeBinder(_) => todo!("FIXME(unsafe_binder)"),
438439
// Type parameters from polymorphized functions.
439440
ty::Param(_) => {
440441
write!(output, "{t:?}").unwrap();

compiler/rustc_const_eval/src/const_eval/valtrees.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ fn const_to_valtree_inner<'tcx>(
178178
| ty::Closure(..)
179179
| ty::CoroutineClosure(..)
180180
| ty::Coroutine(..)
181-
| ty::CoroutineWitness(..) => Err(ValTreeCreationError::NonSupportedType(ty)),
181+
| ty::CoroutineWitness(..)
182+
| ty::UnsafeBinder(_) => Err(ValTreeCreationError::NonSupportedType(ty)),
182183
}
183184
}
184185

@@ -356,7 +357,10 @@ pub fn valtree_to_const_value<'tcx>(
356357
| ty::FnPtr(..)
357358
| ty::Str
358359
| ty::Slice(_)
359-
| ty::Dynamic(..) => bug!("no ValTree should have been created for type {:?}", ty.kind()),
360+
| ty::Dynamic(..)
361+
| ty::UnsafeBinder(_) => {
362+
bug!("no ValTree should have been created for type {:?}", ty.kind())
363+
}
360364
}
361365
}
362366

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
8787
| ty::CoroutineClosure(_, _)
8888
| ty::Coroutine(_, _)
8989
| ty::CoroutineWitness(..)
90+
| ty::UnsafeBinder(_)
9091
| ty::Never
9192
| ty::Tuple(_)
9293
| ty::Error(_) => ConstValue::from_target_usize(0u64, &tcx),

compiler/rustc_const_eval/src/interpret/stack.rs

+2
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
505505
// We don't want to do any queries, so there is not much we can do with ADTs.
506506
ty::Adt(..) => false,
507507

508+
ty::UnsafeBinder(_) => false,
509+
508510
ty::Alias(..) | ty::Param(_) | ty::Placeholder(..) => false,
509511

510512
ty::Infer(ty::TyVar(_)) => false,

compiler/rustc_const_eval/src/interpret/validity.rs

+1
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
760760
// Nothing to check.
761761
Ok(true)
762762
}
763+
ty::UnsafeBinder(_) => todo!("FIXME(unsafe_binder)"),
763764
// The above should be all the primitive types. The rest is compound, we
764765
// check them by visiting their fields/variants.
765766
ty::Adt(..)

compiler/rustc_const_eval/src/util/type_name.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
3838
| ty::FnPtr(..)
3939
| ty::Never
4040
| ty::Tuple(_)
41-
| ty::Dynamic(_, _, _) => self.pretty_print_type(ty),
41+
| ty::Dynamic(_, _, _)
42+
| ty::UnsafeBinder(_) => self.pretty_print_type(ty),
4243

4344
// Placeholders (all printed as `_` to uniformize them).
4445
ty::Param(_) | ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) | ty::Error(_) => {

compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ impl<'tcx> InherentCollect<'tcx> {
177177
| ty::Ref(..)
178178
| ty::Never
179179
| ty::FnPtr(..)
180-
| ty::Tuple(..) => self.check_primitive_impl(id, self_ty),
180+
| ty::Tuple(..)
181+
| ty::UnsafeBinder(_) => self.check_primitive_impl(id, self_ty),
181182
ty::Alias(ty::Projection | ty::Inherent | ty::Opaque, _) | ty::Param(_) => {
182183
Err(self.tcx.dcx().emit_err(errors::InherentNominal { span: item_span }))
183184
}

compiler/rustc_hir_analysis/src/coherence/orphan.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ pub(crate) fn orphan_check_impl(
225225
| ty::FnDef(..)
226226
| ty::FnPtr(..)
227227
| ty::Never
228-
| ty::Tuple(..) => (LocalImpl::Allow, NonlocalImpl::DisallowOther),
228+
| ty::Tuple(..)
229+
| ty::UnsafeBinder(_) => (LocalImpl::Allow, NonlocalImpl::DisallowOther),
229230

230231
ty::Closure(..)
231232
| ty::CoroutineClosure(..)

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2062,7 +2062,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
20622062
self.lower_fn_ty(hir_ty.hir_id, bf.safety, bf.abi, bf.decl, None, Some(hir_ty)),
20632063
)
20642064
}
2065-
hir::TyKind::UnsafeBinder(_binder) => todo!(),
2065+
hir::TyKind::UnsafeBinder(binder) => Ty::new_unsafe_binder(
2066+
tcx,
2067+
ty::Binder::bind_with_vars(
2068+
self.lower_ty(binder.inner_ty),
2069+
tcx.late_bound_vars(binder.hir_id),
2070+
),
2071+
),
20662072
hir::TyKind::TraitObject(bounds, lifetime, repr) => {
20672073
self.prohibit_or_lint_bare_trait_object_ty(hir_ty);
20682074

compiler/rustc_hir_analysis/src/variance/constraints.rs

+5
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
322322
self.add_constraints_from_sig(current, sig_tys.with(hdr), variance);
323323
}
324324

325+
ty::UnsafeBinder(ty) => {
326+
let invariant = self.invariant(variance);
327+
self.add_constraints_from_ty(current, ty.skip_binder(), invariant);
328+
}
329+
325330
ty::Error(_) => {
326331
// we encounter this when walking the trait references for object
327332
// types, where we use Error as the Self type

compiler/rustc_hir_typeck/src/cast.rs

+2
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
115115
Some(&f) => self.pointer_kind(f, span)?,
116116
},
117117

118+
ty::UnsafeBinder(_) => todo!("FIXME(unsafe_binder)"),
119+
118120
// Pointers to foreign types are thin, despite being unsized
119121
ty::Foreign(..) => Some(PointerKind::Thin),
120122
// We should really try to normalize here.

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

+1
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'cx, 'tcx> {
441441
| ty::FnDef(..)
442442
| ty::FnPtr(..)
443443
| ty::Dynamic(..)
444+
| ty::UnsafeBinder(_)
444445
| ty::Never
445446
| ty::Tuple(..)
446447
| ty::Alias(..)

compiler/rustc_lint/src/types.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,8 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
11541154
FfiSafe
11551155
}
11561156

1157+
ty::UnsafeBinder(_) => todo!("FIXME(unsafe_binder)"),
1158+
11571159
ty::Param(..)
11581160
| ty::Alias(ty::Projection | ty::Inherent | ty::Weak, ..)
11591161
| ty::Infer(..)

compiler/rustc_middle/src/ty/context.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,8 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
426426
| ty::CoroutineClosure(..)
427427
| ty::Coroutine(_, _)
428428
| ty::Never
429-
| ty::Tuple(_) => {
429+
| ty::Tuple(_)
430+
| ty::UnsafeBinder(_) => {
430431
let simp = ty::fast_reject::simplify_type(
431432
tcx,
432433
self_ty,
@@ -2264,6 +2265,7 @@ impl<'tcx> TyCtxt<'tcx> {
22642265
Ref,
22652266
FnDef,
22662267
FnPtr,
2268+
UnsafeBinder,
22672269
Placeholder,
22682270
Coroutine,
22692271
CoroutineWitness,

compiler/rustc_middle/src/ty/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ impl<'tcx> Ty<'tcx> {
195195
_ => "fn item".into(),
196196
},
197197
ty::FnPtr(..) => "fn pointer".into(),
198+
ty::UnsafeBinder(_) => "unsafe binder".into(),
198199
ty::Dynamic(..) => "trait object".into(),
199200
ty::Closure(..) | ty::CoroutineClosure(..) => "closure".into(),
200201
ty::Coroutine(def_id, ..) => {

compiler/rustc_middle/src/ty/flags.rs

+6
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@ impl FlagComputation {
253253
&ty::FnPtr(sig_tys, _) => self.bound_computation(sig_tys, |computation, sig_tys| {
254254
computation.add_tys(sig_tys.inputs_and_output);
255255
}),
256+
257+
&ty::UnsafeBinder(bound_ty) => {
258+
self.bound_computation(bound_ty.into(), |computation, ty| {
259+
computation.add_ty(ty);
260+
})
261+
}
256262
}
257263
}
258264

compiler/rustc_middle/src/ty/layout.rs

+5
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,11 @@ where
785785
bug!("TyAndLayout::field({:?}): not applicable", this)
786786
}
787787

788+
ty::UnsafeBinder(bound_ty) => {
789+
let ty = tcx.instantiate_bound_regions_with_erased(bound_ty.into());
790+
field_ty_or_layout(TyAndLayout { ty, ..this }, cx, i)
791+
}
792+
788793
// Potentially-fat pointers.
789794
ty::Ref(_, pointee, _) | ty::RawPtr(pointee, _) => {
790795
assert!(i < this.fields.count());

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

+1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ fn characteristic_def_id_of_type_cached<'a>(
291291
| ty::Uint(_)
292292
| ty::Str
293293
| ty::FnPtr(..)
294+
| ty::UnsafeBinder(_)
294295
| ty::Alias(..)
295296
| ty::Placeholder(..)
296297
| ty::Param(_)

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

+3
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,9 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
697697
}
698698
}
699699
ty::FnPtr(ref sig_tys, hdr) => p!(print(sig_tys.with(hdr))),
700+
ty::UnsafeBinder(ref bound_ty) => {
701+
self.wrap_binder(bound_ty, |ty, cx| cx.pretty_print_type(*ty))?;
702+
}
700703
ty::Infer(infer_ty) => {
701704
if self.should_print_verbose() {
702705
p!(write("{:?}", ty.kind()));

compiler/rustc_middle/src/ty/structural_impls.rs

+2
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for Ty<'tcx> {
375375
ty::Tuple(ts) => ty::Tuple(ts.try_fold_with(folder)?),
376376
ty::FnDef(def_id, args) => ty::FnDef(def_id, args.try_fold_with(folder)?),
377377
ty::FnPtr(sig_tys, hdr) => ty::FnPtr(sig_tys.try_fold_with(folder)?, hdr),
378+
ty::UnsafeBinder(f) => ty::UnsafeBinder(f.try_fold_with(folder)?),
378379
ty::Ref(r, ty, mutbl) => {
379380
ty::Ref(r.try_fold_with(folder)?, ty.try_fold_with(folder)?, mutbl)
380381
}
@@ -425,6 +426,7 @@ impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for Ty<'tcx> {
425426
ty::Tuple(ts) => ts.visit_with(visitor),
426427
ty::FnDef(_, args) => args.visit_with(visitor),
427428
ty::FnPtr(ref sig_tys, _) => sig_tys.visit_with(visitor),
429+
ty::UnsafeBinder(ref f) => f.visit_with(visitor),
428430
ty::Ref(r, ty, _) => {
429431
try_visit!(r.visit_with(visitor));
430432
ty.visit_with(visitor)

compiler/rustc_middle/src/ty/sty.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,11 @@ impl<'tcx> Ty<'tcx> {
662662
Ty::new(tcx, FnPtr(sig_tys, hdr))
663663
}
664664

665+
#[inline]
666+
pub fn new_unsafe_binder(tcx: TyCtxt<'tcx>, b: Binder<'tcx, Ty<'tcx>>) -> Ty<'tcx> {
667+
Ty::new(tcx, UnsafeBinder(b.into()))
668+
}
669+
665670
#[inline]
666671
pub fn new_dynamic(
667672
tcx: TyCtxt<'tcx>,
@@ -951,6 +956,10 @@ impl<'tcx> rustc_type_ir::inherent::Ty<TyCtxt<'tcx>> for Ty<'tcx> {
951956
Ty::new_pat(interner, ty, pat)
952957
}
953958

959+
fn new_unsafe_binder(interner: TyCtxt<'tcx>, ty: ty::Binder<'tcx, Ty<'tcx>>) -> Self {
960+
Ty::new_unsafe_binder(interner, ty)
961+
}
962+
954963
fn new_unit(interner: TyCtxt<'tcx>) -> Self {
955964
interner.types.unit
956965
}
@@ -1459,6 +1468,7 @@ impl<'tcx> Ty<'tcx> {
14591468
| ty::CoroutineWitness(..)
14601469
| ty::Never
14611470
| ty::Tuple(_)
1471+
| ty::UnsafeBinder(_)
14621472
| ty::Error(_)
14631473
| ty::Infer(IntVar(_) | FloatVar(_)) => tcx.types.u8,
14641474

@@ -1638,6 +1648,8 @@ impl<'tcx> Ty<'tcx> {
16381648
// metadata of `tail`.
16391649
ty::Param(_) | ty::Alias(..) => Err(tail),
16401650

1651+
| ty::UnsafeBinder(_) => todo!("FIXME(unsafe_binder)"),
1652+
16411653
ty::Infer(ty::TyVar(_))
16421654
| ty::Pat(..)
16431655
| ty::Bound(..)
@@ -1798,6 +1810,7 @@ impl<'tcx> Ty<'tcx> {
17981810
| ty::Float(_)
17991811
| ty::FnDef(..)
18001812
| ty::FnPtr(..)
1813+
| ty::UnsafeBinder(_)
18011814
| ty::RawPtr(..)
18021815
| ty::Char
18031816
| ty::Ref(..)
@@ -1877,6 +1890,8 @@ impl<'tcx> Ty<'tcx> {
18771890
// Might be, but not "trivial" so just giving the safe answer.
18781891
ty::Adt(..) | ty::Closure(..) | ty::CoroutineClosure(..) => false,
18791892

1893+
ty::UnsafeBinder(_) => false,
1894+
18801895
// Needs normalization or revealing to determine, so no is the safe answer.
18811896
ty::Alias(..) => false,
18821897

@@ -1954,7 +1969,8 @@ impl<'tcx> Ty<'tcx> {
19541969
| Coroutine(_, _)
19551970
| CoroutineWitness(..)
19561971
| Never
1957-
| Tuple(_) => true,
1972+
| Tuple(_)
1973+
| UnsafeBinder(_) => true,
19581974
Error(_) | Infer(_) | Alias(_, _) | Param(_) | Bound(_, _) | Placeholder(_) => false,
19591975
}
19601976
}

compiler/rustc_middle/src/ty/util.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,7 @@ impl<'tcx> Ty<'tcx> {
12861286
| ty::Foreign(_)
12871287
| ty::Coroutine(..)
12881288
| ty::CoroutineWitness(..)
1289+
| ty::UnsafeBinder(_)
12891290
| ty::Infer(_)
12901291
| ty::Alias(..)
12911292
| ty::Param(_)
@@ -1326,6 +1327,7 @@ impl<'tcx> Ty<'tcx> {
13261327
| ty::Foreign(_)
13271328
| ty::Coroutine(..)
13281329
| ty::CoroutineWitness(..)
1330+
| ty::UnsafeBinder(_)
13291331
| ty::Infer(_)
13301332
| ty::Alias(..)
13311333
| ty::Param(_)
@@ -1358,6 +1360,9 @@ impl<'tcx> Ty<'tcx> {
13581360
| ty::Infer(ty::FreshIntTy(_))
13591361
| ty::Infer(ty::FreshFloatTy(_)) => AsyncDropGlueMorphology::Noop,
13601362

1363+
// FIXME(unsafe_binders):
1364+
ty::UnsafeBinder(_) => todo!(),
1365+
13611366
ty::Tuple(tys) if tys.is_empty() => AsyncDropGlueMorphology::Noop,
13621367
ty::Adt(adt_def, _) if adt_def.is_manually_drop() => AsyncDropGlueMorphology::Noop,
13631368

@@ -1559,7 +1564,7 @@ impl<'tcx> Ty<'tcx> {
15591564
false
15601565
}
15611566

1562-
ty::Foreign(_) | ty::CoroutineWitness(..) | ty::Error(_) => false,
1567+
ty::Foreign(_) | ty::CoroutineWitness(..) | ty::Error(_) | ty::UnsafeBinder(_) => false,
15631568
}
15641569
}
15651570

@@ -1718,7 +1723,8 @@ pub fn needs_drop_components_with_async<'tcx>(
17181723
| ty::Closure(..)
17191724
| ty::CoroutineClosure(..)
17201725
| ty::Coroutine(..)
1721-
| ty::CoroutineWitness(..) => Ok(smallvec![ty]),
1726+
| ty::CoroutineWitness(..)
1727+
| ty::UnsafeBinder(_) => Ok(smallvec![ty]),
17221728
}
17231729
}
17241730

@@ -1753,7 +1759,8 @@ pub fn is_trivially_const_drop(ty: Ty<'_>) -> bool {
17531759
| ty::CoroutineClosure(..)
17541760
| ty::Coroutine(..)
17551761
| ty::CoroutineWitness(..)
1756-
| ty::Adt(..) => false,
1762+
| ty::Adt(..)
1763+
| ty::UnsafeBinder(_) => false,
17571764

17581765
ty::Array(ty, _) | ty::Slice(ty) | ty::Pat(ty, _) => is_trivially_const_drop(ty),
17591766

compiler/rustc_middle/src/ty/walk.rs

+3
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
194194
sig_tys.skip_binder().inputs_and_output.iter().rev().map(|ty| ty.into()),
195195
);
196196
}
197+
ty::UnsafeBinder(bound_ty) => {
198+
stack.push(bound_ty.skip_binder().into());
199+
}
197200
},
198201
GenericArgKind::Lifetime(_) => {}
199202
GenericArgKind::Const(parent_ct) => match parent_ct.kind() {

compiler/rustc_mir_dataflow/src/move_paths/builder.rs

+2
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ impl<'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> MoveDataBuilder<'a, 'tcx, F> {
161161
| ty::Slice(_)
162162
| ty::FnDef(_, _)
163163
| ty::FnPtr(..)
164+
| ty::UnsafeBinder(..)
164165
| ty::Dynamic(_, _, _)
165166
| ty::Closure(..)
166167
| ty::CoroutineClosure(..)
@@ -204,6 +205,7 @@ impl<'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> MoveDataBuilder<'a, 'tcx, F> {
204205
| ty::Ref(_, _, _)
205206
| ty::FnDef(_, _)
206207
| ty::FnPtr(..)
208+
| ty::UnsafeBinder(_)
207209
| ty::Dynamic(_, _, _)
208210
| ty::CoroutineWitness(..)
209211
| ty::Never

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,8 @@ fn try_write_constant<'tcx>(
712712
| ty::Closure(..)
713713
| ty::CoroutineClosure(..)
714714
| ty::Coroutine(..)
715-
| ty::Dynamic(..) => throw_machine_stop_str!("unsupported type"),
715+
| ty::Dynamic(..)
716+
| ty::UnsafeBinder(_) => throw_machine_stop_str!("unsupported type"),
716717

717718
ty::Error(_) | ty::Infer(..) | ty::CoroutineWitness(..) => bug!(),
718719
}

0 commit comments

Comments
 (0)