1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use rustc_data_structures::stable_map::FxHashMap;
use rustc_hir as hir;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::Span;
use std::cmp;
#[derive(Clone, PartialOrd, Ord, Eq, PartialEq, Debug)]
pub enum Representability {
Representable,
ContainsRecursive,
SelfRecursive(Vec<Span>),
}
pub fn ty_is_representable<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, sp: Span) -> Representability {
debug!("is_type_representable: {:?}", ty);
let mut seen: Vec<Ty<'_>> = Vec::new();
let mut representable_cache = FxHashMap::default();
let r = is_type_structurally_recursive(tcx, sp, &mut seen, &mut representable_cache, ty);
debug!("is_type_representable: {:?} is {:?}", ty, r);
r
}
fn fold_repr<It: Iterator<Item = Representability>>(iter: It) -> Representability {
iter.fold(Representability::Representable, |r1, r2| match (r1, r2) {
(Representability::SelfRecursive(v1), Representability::SelfRecursive(v2)) => {
Representability::SelfRecursive(v1.into_iter().chain(v2).collect())
}
(r1, r2) => cmp::max(r1, r2),
})
}
fn are_inner_types_recursive<'tcx>(
tcx: TyCtxt<'tcx>,
sp: Span,
seen: &mut Vec<Ty<'tcx>>,
representable_cache: &mut FxHashMap<Ty<'tcx>, Representability>,
ty: Ty<'tcx>,
) -> Representability {
match ty.kind() {
ty::Tuple(..) => {
fold_repr(
ty.tuple_fields().map(|ty| {
is_type_structurally_recursive(tcx, sp, seen, representable_cache, ty)
}),
)
}
ty::Array(ty, _) => is_type_structurally_recursive(tcx, sp, seen, representable_cache, ty),
ty::Adt(def, substs) => {
fold_repr(def.all_fields().map(|field| {
let ty = field.ty(tcx, substs);
let span = match field
.did
.as_local()
.map(|id| tcx.hir().local_def_id_to_hir_id(id))
.and_then(|id| tcx.hir().find(id))
{
Some(hir::Node::Field(field)) => field.ty.span,
_ => sp,
};
match is_type_structurally_recursive(tcx, span, seen, representable_cache, ty) {
Representability::SelfRecursive(_) => {
Representability::SelfRecursive(vec![span])
}
x => x,
}
}))
}
ty::Closure(..) => {
bug!("requires check invoked on inapplicable type: {:?}", ty)
}
_ => Representability::Representable,
}
}
fn same_adt<'tcx>(ty: Ty<'tcx>, def: &'tcx ty::AdtDef) -> bool {
match *ty.kind() {
ty::Adt(ty_def, _) => ty_def == def,
_ => false,
}
}
fn is_type_structurally_recursive<'tcx>(
tcx: TyCtxt<'tcx>,
sp: Span,
seen: &mut Vec<Ty<'tcx>>,
representable_cache: &mut FxHashMap<Ty<'tcx>, Representability>,
ty: Ty<'tcx>,
) -> Representability {
debug!("is_type_structurally_recursive: {:?} {:?}", ty, sp);
if let Some(representability) = representable_cache.get(ty) {
debug!(
"is_type_structurally_recursive: {:?} {:?} - (cached) {:?}",
ty, sp, representability
);
return representability.clone();
}
let representability =
is_type_structurally_recursive_inner(tcx, sp, seen, representable_cache, ty);
representable_cache.insert(ty, representability.clone());
representability
}
fn is_type_structurally_recursive_inner<'tcx>(
tcx: TyCtxt<'tcx>,
sp: Span,
seen: &mut Vec<Ty<'tcx>>,
representable_cache: &mut FxHashMap<Ty<'tcx>, Representability>,
ty: Ty<'tcx>,
) -> Representability {
match ty.kind() {
ty::Adt(def, _) => {
{
let mut iter = seen.iter();
if let Some(&seen_adt) = iter.next() {
if same_adt(seen_adt, *def) {
debug!("SelfRecursive: {:?} contains {:?}", seen_adt, ty);
return Representability::SelfRecursive(vec![sp]);
}
}
for &seen_adt in iter {
if ty::TyS::same_type(ty, seen_adt) {
debug!("ContainsRecursive: {:?} contains {:?}", seen_adt, ty);
return Representability::ContainsRecursive;
}
}
}
seen.push(ty);
let out = are_inner_types_recursive(tcx, sp, seen, representable_cache, ty);
seen.pop();
out
}
_ => {
are_inner_types_recursive(tcx, sp, seen, representable_cache, ty)
}
}
}