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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use std::collections::HashMap;
use log::{debug, info, trace};
use log_derive::logfn;
use rustdoc_types as types;
use crate::types::*;
pub trait Approximate<Destination> {
fn approx(
&self,
dest: &Destination,
generics: &types::Generics,
substs: &mut HashMap<String, Type>,
) -> Vec<Similarity>;
}
trait GenericsExt {
fn compose(&self, other: &types::Generics) -> types::Generics;
}
impl GenericsExt for types::Generics {
fn compose(&self, other: &types::Generics) -> types::Generics {
let mut params = self.params.clone();
params.append(&mut other.params.clone());
let mut where_predicates = self.where_predicates.clone();
where_predicates.append(&mut other.where_predicates.clone());
types::Generics {
params,
where_predicates,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Similarity {
Different,
Subequal,
Equivalent,
}
use Similarity::*;
impl Similarity {
fn degrade(&self) -> Similarity {
match self {
Equivalent | Subequal => Subequal,
Different => Different,
}
}
}
impl Approximate<types::Item> for Query {
#[logfn(info, fmt = "Approximating `Query` to `Item` finished: {:?}")]
fn approx(
&self,
item: &types::Item,
generics: &types::Generics,
substs: &mut HashMap<String, Type>,
) -> Vec<Similarity> {
info!("-------------------------------");
info!("Approximating `Query` to `Item`");
trace!("approx(lhs={:?}, rhs={:?})", self, item);
let mut sims = Vec::new();
if let Some(ref name) = self.name {
match item.name {
Some(ref item_name) => sims.append(&mut name.approx(item_name, generics, substs)),
None => sims.push(Different),
}
}
if let Some(ref kind) = self.kind {
sims.append(&mut kind.approx(&item.inner, generics, substs))
}
sims
}
}
impl Approximate<String> for Symbol {
#[logfn(info, fmt = "Approximating `Symbol` to `String` finished: {:?}")]
fn approx(
&self,
string: &String,
_: &types::Generics,
_: &mut HashMap<String, Type>,
) -> Vec<Similarity> {
info!("Approximating `Symbol` to `String`");
trace!("approx(lhs: {:?}, rhs: {:?})", self, string);
if self == string {
vec![Equivalent]
} else {
vec![Different]
}
}
}
impl Approximate<types::ItemEnum> for QueryKind {
#[logfn(info, fmt = "Approximating `QueryKind` to `ItemEnum` finished: {:?}")]
fn approx(
&self,
kind: &types::ItemEnum,
generics: &types::Generics,
substs: &mut HashMap<String, Type>,
) -> Vec<Similarity> {
info!("Approximating `QueryKind` to `ItemEnum`");
trace!("approx(lhs: {:?}, rhs: {:?})", self, kind);
use types::ItemEnum::*;
use QueryKind::*;
match (self, kind) {
(FunctionQuery(q), Function(i)) => q.approx(i, generics, substs),
(FunctionQuery(q), Method(i)) => q.approx(i, generics, substs),
_ => vec![Different],
}
}
}
impl Approximate<types::Function> for Function {
#[logfn(info, fmt = "Approximating `Function` to `Function` finished: {:?}")]
fn approx(
&self,
function: &types::Function,
generics: &types::Generics,
substs: &mut HashMap<String, Type>,
) -> Vec<Similarity> {
info!("Approximating `Function` to `Function`");
trace!("approx(lhs: {:?}, rhs: {:?})", self, function);
let generics = generics.compose(&function.generics);
self.decl.approx(&function.decl, &generics, substs)
}
}
impl Approximate<types::Method> for Function {
#[logfn(info, fmt = "Approximating `Function` to `Method` finished: {:?}")]
fn approx(
&self,
method: &types::Method,
generics: &types::Generics,
substs: &mut HashMap<String, Type>,
) -> Vec<Similarity> {
info!("Approximating `Function` to `Method`");
trace!(
"approx(lhs: {:?}, rhs: {:?}, generics: {:?})",
self,
method,
generics
);
let generics = generics.compose(&method.generics);
self.decl.approx(&method.decl, &generics, substs)
}
}
impl Approximate<types::FnDecl> for FnDecl {
#[logfn(info, fmt = "Approximating `FnDecl` to `FnDecl` finished: {:?}")]
fn approx(
&self,
decl: &types::FnDecl,
generics: &types::Generics,
substs: &mut HashMap<String, Type>,
) -> Vec<Similarity> {
info!("Approximating `FnDecl` to `FnDecl`");
trace!("approx(lhs: {:?}, rhs: {:?})", self, decl);
let mut sims = Vec::new();
if let Some(ref inputs) = self.inputs {
inputs
.iter()
.enumerate()
.for_each(|(idx, input)| match decl.inputs.get(idx) {
Some(arg) => sims.append(&mut input.approx(arg, generics, substs)),
None => sims.push(Different),
});
if decl.inputs.len() > inputs.len() {
let extra = decl.inputs.len() - inputs.len();
sims.append(&mut vec![Different; extra])
}
}
if let Some(ref output) = self.output {
sims.append(&mut output.approx(&decl.output, generics, substs))
}
sims
}
}
impl Approximate<(String, types::Type)> for Argument {
#[logfn(
info,
fmt = "Approximating `Argument` to `(String, Type)` finished: {:?}"
)]
fn approx(
&self,
arg: &(String, types::Type),
generics: &types::Generics,
substs: &mut HashMap<String, Type>,
) -> Vec<Similarity> {
info!("Approximating `Argument` to `(String, Type)`");
trace!("approx(lhs: {:?}, rhs: {:?})", self, arg);
let mut sims = Vec::new();
if let Some(ref type_) = self.ty {
sims.append(&mut type_.approx(&arg.1, generics, substs));
}
if let Some(ref name) = self.name {
sims.append(&mut name.approx(&arg.0, generics, substs));
}
sims
}
}
impl Approximate<Option<types::Type>> for FnRetTy {
#[logfn(info, fmt = "Approximating `FnRetTy` to `Option<Type>` finished: {:?}")]
fn approx(
&self,
ret_ty: &Option<types::Type>,
generics: &types::Generics,
substs: &mut HashMap<String, Type>,
) -> Vec<Similarity> {
info!("Approximating `FnRetTy` to `Option<Type>`");
trace!("approx(lhs: {:?}, rhs: {:?})", self, ret_ty);
match (self, ret_ty) {
(FnRetTy::Return(q), Some(i)) => q.approx(i, generics, substs),
(FnRetTy::DefaultReturn, None) => vec![Equivalent],
_ => vec![Different],
}
}
}
impl Approximate<types::Type> for Type {
#[logfn(info, fmt = "Approximating `Type` to `Type` finished: {:?}")]
fn approx(
&self,
type_: &types::Type,
generics: &types::Generics,
substs: &mut HashMap<String, Type>,
) -> Vec<Similarity> {
info!("Approximating `Type` to `Type`");
trace!(
"approx(lhs: {:?}, rhs: {:?}, generics: {:?}, substs: {:?})",
self,
type_,
generics,
substs
);
use Type::*;
match (self, type_) {
(q, types::Type::Generic(i)) => {
if i == "Self" {
for where_predicate in &generics.where_predicates {
if let types::WherePredicate::EqPredicate { lhs, rhs } = where_predicate {
if lhs == &types::Type::Generic("Self".to_owned()) {
return q.approx(rhs, generics, substs);
}
}
}
}
match substs.get(i) {
Some(i) => {
if q == i {
vec![Subequal]
} else {
vec![Different]
}
}
None => {
substs.insert(i.clone(), q.clone());
vec![Subequal]
}
}
}
(Tuple(q), types::Type::Tuple(i)) => {
let mut sims: Vec<_> = q
.iter()
.zip(i.iter())
.filter_map(|(q, i)| q.as_ref().map(|q| q.approx(i, generics, substs)))
.flatten()
.collect();
sims.push(Equivalent);
if i.len() > q.len() {
sims.append(&mut vec![Different; i.len() - q.len()]);
}
sims
}
(Slice(q), types::Type::Slice(i)) => {
let mut sims = vec![Equivalent];
if let Some(q) = q {
sims.append(&mut q.approx(i, generics, substs))
}
sims
}
(Never, types::Type::Never) => vec![Equivalent],
(
RawPointer {
mutable: q_mut,
type_: q,
},
types::Type::RawPointer {
mutable: i_mut,
type_: i,
},
) => {
if q_mut == i_mut {
q.approx(i, generics, substs)
} else {
q.approx(i, generics, substs)
.iter()
.map(|sim| sim.degrade())
.collect()
}
}
(
BorrowedRef {
mutable: q_mut,
type_: q,
},
types::Type::BorrowedRef {
mutable: i_mut,
type_: i,
..
},
) => {
if q_mut == i_mut {
q.approx(i, generics, substs)
} else {
q.approx(i, generics, substs)
.iter()
.map(|sim| sim.degrade())
.collect()
}
}
(q, types::Type::BorrowedRef { type_: i, .. }) => q
.approx(i, generics, substs)
.iter()
.map(|sim| sim.degrade())
.collect(),
(
UnresolvedPath {
name: q,
args: q_args,
},
types::Type::ResolvedPath {
name: i,
args: i_args,
..
},
) => {
let mut sims = q.approx(i, generics, substs);
if sims == vec![Equivalent] {
match (q_args, i_args) {
(Some(q), Some(i)) => {
if let (
GenericArgs::AngleBracketed { args: q },
types::GenericArgs::AngleBracketed { args: i, .. },
) = (&**q, &**i)
{
let q = q.iter().map(|q| {
q.as_ref().map(|q| match q {
GenericArg::Type(q) => q,
})
});
let i = i.iter().filter_map(|i| match i {
types::GenericArg::Type(t) => Some(t),
_ => None,
});
for (q, i) in q.zip(i) {
if let Some(q) = q {
sims.append(&mut q.approx(i, generics, substs))
}
}
}
}
(Some(_), None) => sims.push(Different),
(None, _) => {}
}
}
sims
}
(Primitive(q), types::Type::Primitive(i)) => q.approx(i, generics, substs),
(q, i) => {
debug!(
"Potentially unimplemented approximation: approx(lhs: {:?}, rhs: {:?})",
q, i
);
vec![Different]
}
}
}
}
impl Approximate<String> for PrimitiveType {
#[logfn(
info,
fmt = "Approximating `PrimitiveType` to `PrimitiveType` finished: {:?}"
)]
fn approx(
&self,
prim_ty: &String,
_: &types::Generics,
_: &mut HashMap<String, Type>,
) -> Vec<Similarity> {
info!("Approximating `PrimitiveType` to `String`");
trace!("approx(lhs: {:?}, rhs: {:?})", self, prim_ty);
if self.as_str() == prim_ty {
vec![Equivalent]
} else {
vec![Different]
}
}
}