rustc_ty_utils/
representability.rs1use rustc_hir::def::DefKind;
2use rustc_index::bit_set::DenseBitSet;
3use rustc_middle::bug;
4use rustc_middle::query::Providers;
5use rustc_middle::ty::{self, Ty, TyCtxt};
6use rustc_span::def_id::LocalDefId;
7
8pub(crate) fn provide(providers: &mut Providers) {
9 *providers = Providers {
10 check_representability,
11 check_representability_adt_ty,
12 params_in_repr,
13 ..*providers
14 };
15}
16
17fn check_representability(tcx: TyCtxt<'_>, def_id: LocalDefId) {
18 match tcx.def_kind(def_id) {
19 DefKind::Struct | DefKind::Union | DefKind::Enum => {
20 for variant in tcx.adt_def(def_id).variants() {
21 for field in variant.fields.iter() {
22 tcx.ensure_ok().check_representability(field.did.expect_local());
23 }
24 }
25 }
26 DefKind::Field => {
27 check_representability_ty(tcx, tcx.type_of(def_id).instantiate_identity());
28 }
29 def_kind => ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected {0:?}", def_kind))bug!("unexpected {def_kind:?}"),
30 }
31}
32
33fn check_representability_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) {
34 match *ty.kind() {
35 ty::Adt(..) => {
38 tcx.ensure_ok().check_representability_adt_ty(ty);
39 }
40 ty::Array(ty, _) => {
42 check_representability_ty(tcx, ty);
43 }
44 ty::Tuple(tys) => {
45 for ty in tys {
46 check_representability_ty(tcx, ty);
47 }
48 }
49 _ => {}
50 }
51}
52
53fn check_representability_adt_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) {
70 let ty::Adt(adt, args) = ty.kind() else { ::rustc_middle::util::bug::bug_fmt(format_args!("expected adt"))bug!("expected adt") };
71 if let Some(def_id) = adt.did().as_local() {
72 tcx.ensure_ok().check_representability(def_id);
73 }
74 let params_in_repr = tcx.params_in_repr(adt.did());
77 for (i, arg) in args.iter().enumerate() {
78 if let ty::GenericArgKind::Type(ty) = arg.kind() {
79 if params_in_repr.contains(i as u32) {
80 check_representability_ty(tcx, ty);
81 }
82 }
83 }
84}
85
86fn params_in_repr(tcx: TyCtxt<'_>, def_id: LocalDefId) -> DenseBitSet<u32> {
87 let adt_def = tcx.adt_def(def_id);
88 let generics = tcx.generics_of(def_id);
89 let mut params_in_repr = DenseBitSet::new_empty(generics.own_params.len());
90 for variant in adt_def.variants() {
91 for field in variant.fields.iter() {
92 params_in_repr_ty(
93 tcx,
94 tcx.type_of(field.did).instantiate_identity(),
95 &mut params_in_repr,
96 );
97 }
98 }
99 params_in_repr
100}
101
102fn params_in_repr_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, params_in_repr: &mut DenseBitSet<u32>) {
103 match *ty.kind() {
104 ty::Adt(adt, args) => {
105 let inner_params_in_repr = tcx.params_in_repr(adt.did());
106 for (i, arg) in args.iter().enumerate() {
107 if let ty::GenericArgKind::Type(ty) = arg.kind() {
108 if inner_params_in_repr.contains(i as u32) {
109 params_in_repr_ty(tcx, ty, params_in_repr);
110 }
111 }
112 }
113 }
114 ty::Array(ty, _) => params_in_repr_ty(tcx, ty, params_in_repr),
115 ty::Tuple(tys) => tys.iter().for_each(|ty| params_in_repr_ty(tcx, ty, params_in_repr)),
116 ty::Param(param) => {
117 params_in_repr.insert(param.index);
118 }
119 _ => {}
120 }
121}