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
use std::collections::HashMap;

use rustdoc_types as types;
use serde::Serialize;
use thiserror::Error;

use crate::{
    compare::{Compare, Similarities},
    query::Query,
    Index,
};

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Hit {
    pub name: String,
    pub path: Vec<String>,
    pub link: String,
    pub docs: Option<String>,
    #[serde(skip)]
    similarities: Similarities,
}

impl Hit {
    pub fn similarities(&self) -> &Similarities {
        &self.similarities
    }
}

impl PartialOrd for Hit {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.similarities.partial_cmp(&other.similarities)
    }
}

#[derive(Error, Debug)]
pub enum SearchError {
    #[error("crate `{0}` is not present in the index")]
    CrateNotFound(String),

    #[error("item with id `{0}` is not present in crate `{1}`")]
    ItemNotFound(String, String),
}

pub type Result<T> = std::result::Result<T, SearchError>;

/// Represents a scope to search in.
#[derive(Debug, Clone)]
pub enum Scope {
    /// Represetns a single crate.
    Crate(String),

    /// Represents multiple crates.
    ///
    /// For example:
    /// - `rustc_ast`, `rustc_ast_lowering`, `rustc_passes` and `rustc_ast_pretty`
    /// - `std`, `core` and `alloc`
    Set(Vec<String>),
}

impl Scope {
    pub fn flatten(self) -> Vec<String> {
        match self {
            Scope::Crate(krate) => vec![krate],
            Scope::Set(krates) => krates,
        }
    }
}

impl Index {
    /// Perform search with given query and scope.
    ///
    /// Returns [`Hit`]s whose similarity score outperforms given `threshold`.
    pub fn search(&self, query: &Query, scope: Scope, threshold: f32) -> Result<Vec<Hit>> {
        let mut hits = vec![];

        let krates = scope.flatten();
        for krate_name in krates {
            let krate = self
                .crates
                .get(&krate_name)
                .ok_or(SearchError::CrateNotFound(krate_name.clone()))?;
            for item in krate.index.values() {
                match item.inner {
                    types::ItemEnum::Function(_) => {
                        let (path, link) = Self::path_and_link(krate, &krate_name, item, None)?;
                        let sims = self.compare(query, item, krate, None);

                        if sims.score() < threshold {
                            hits.push(Hit {
                                name: item.name.clone().unwrap(), // SAFETY: all functions has its name.
                                path,
                                link,
                                docs: item.docs.clone(),
                                similarities: sims,
                            });
                        }
                    }
                    types::ItemEnum::Impl(ref impl_) if impl_.trait_.is_none() => {
                        let assoc_items = impl_
                            .items
                            .iter()
                            .map(|id| {
                                krate.index.get(id).ok_or(SearchError::ItemNotFound(
                                    id.0.clone(),
                                    krate_name.clone(),
                                ))
                            })
                            .collect::<Result<Vec<_>>>()?;
                        for assoc_item in assoc_items {
                            if let types::ItemEnum::Method(_) = assoc_item.inner {
                                let (path, link) = Self::path_and_link(
                                    krate,
                                    &krate_name,
                                    assoc_item,
                                    Some(impl_),
                                )?;
                                let sims = self.compare(query, assoc_item, krate, Some(impl_));

                                if sims.score() < threshold {
                                    hits.push(Hit {
                                        name: assoc_item.name.clone().unwrap(), // SAFETY: all methods has its name.
                                        path,
                                        link,
                                        docs: assoc_item.docs.clone(),
                                        similarities: sims,
                                    })
                                }
                            }
                        }
                    }
                    // TODO(hkmatsumoto): Acknowledge trait method as well.
                    _ => {}
                }
            }
        }

        hits.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());
        Ok(hits)
    }

    #[tracing::instrument(skip(self, krate))]
    fn compare(
        &self,
        query: &Query,
        item: &types::Item,
        krate: &types::Crate,
        impl_: Option<&types::Impl>,
    ) -> Similarities {
        let mut generics;
        if let Some(impl_) = impl_ {
            generics = impl_.generics.clone();
            generics
                .where_predicates
                .push(types::WherePredicate::EqPredicate {
                    lhs: types::Type::Generic("Self".to_owned()),
                    rhs: impl_.for_.clone(),
                });
        } else {
            generics = types::Generics::default()
        }
        let mut substs = HashMap::default();

        let sims = query.compare(item, krate, &mut generics, &mut substs);
        Similarities(sims)
    }

    /// Given `item` and optional `impl_`, compute its path and rustdoc link to `item`.
    ///
    /// `item` must be a function or a method, otherwise assertions will fail.
    fn path_and_link(
        krate: &types::Crate,
        krate_name: &str,
        item: &types::Item,
        impl_: Option<&types::Impl>,
    ) -> Result<(Vec<String>, String)> {
        assert!(matches!(
            item.inner,
            types::ItemEnum::Function(_) | types::ItemEnum::Method(_)
        ));

        use types::Type;

        let get_path = |id: &types::Id| -> Result<Vec<String>> {
            let path = krate
                .paths
                .get(id)
                .ok_or(SearchError::ItemNotFound(
                    id.0.clone(),
                    krate_name.to_owned(),
                ))?
                .path
                .clone();

            Ok(path)
        };

        // If `item` is a associated item, replace the last segment of the path for the link of the ADT
        // it is binded to.
        let mut path;
        let mut link;
        if let Some(impl_) = impl_ {
            let recv;
            match (&impl_.for_, &impl_.trait_) {
                (_, Some(ref t)) => {
                    if let Type::ResolvedPath { name, id, .. } = t {
                        path = get_path(id)?;
                        recv = format!("trait.{}.html", name);
                    } else {
                        // SAFETY: All traits are represented by `ResolvedPath`.
                        unreachable!()
                    }
                }
                (
                    Type::ResolvedPath {
                        ref name, ref id, ..
                    },
                    _,
                ) => {
                    path = get_path(id)?;
                    let summary = krate.paths.get(id).ok_or(SearchError::ItemNotFound(
                        id.0.clone(),
                        krate_name.to_owned(),
                    ))?;
                    match summary.kind {
                        types::ItemKind::Union => recv = format!("union.{}.html", name),
                        types::ItemKind::Enum => recv = format!("enum.{}.html", name),
                        types::ItemKind::Struct => recv = format!("struct.{}.html", name),
                        // SAFETY: ADTs are either unions or enums or structs.
                        _ => unreachable!(),
                    }
                }
                (Type::Primitive(ref prim), _) => {
                    path = vec![prim.clone()];
                    recv = format!("primitive.{}.html", prim);
                }
                (Type::Tuple(_), _) => {
                    path = vec!["tuple".to_owned()];
                    recv = "primitive.tuple.html".to_owned();
                }
                (Type::Slice(_), _) => {
                    path = vec!["slice".to_owned()];
                    recv = "primitive.slice.html".to_owned();
                }
                (Type::Array { .. }, _) => {
                    path = vec!["array".to_owned()];
                    recv = "primitive.array.html".to_owned();
                }
                (Type::RawPointer { .. }, _) => {
                    path = vec!["pointer".to_owned()];
                    recv = "primitive.pointer.html".to_owned();
                }
                (Type::BorrowedRef { .. }, _) => {
                    path = vec!["reference".to_owned()];
                    recv = "primitive.reference.html".to_owned();
                }
                _ => unreachable!(),
            }
            link = path.clone();
            if let Some(l) = link.last_mut() {
                *l = recv;
            }
        } else {
            path = get_path(&item.id)?;
            link = path.clone();
        }

        match item.inner {
            types::ItemEnum::Function(_) => {
                if let Some(l) = link.last_mut() {
                    *l = format!("fn.{}.html", l);
                }
                Ok((path.clone(), link.join("/")))
            }
            types::ItemEnum::Method(_) => {
                path.push(item.name.clone().unwrap()); // SAFETY: all methods has its name.
                link.push(item.name.clone().unwrap());
                if let Some(l) = link.last_mut() {
                    *l = format!("#method.{}", l);
                }
                Ok((path.clone(), link.join("/")))
            }
            // SAFETY: Already asserted at the beginning of this function.
            _ => unreachable!(),
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use smallvec::{smallvec, SmallVec};

    use super::*;
    use crate::compare::{
        DiscreteSimilarity::*,
        Similarity::{self, *},
    };
    use crate::query::{FnDecl, FnRetTy, Function};

    fn krate() -> types::Crate {
        types::Crate {
            root: types::Id("0:0".to_owned()),
            crate_version: Some("0.0.0".to_owned()),
            includes_private: false,
            index: Default::default(),
            paths: Default::default(),
            external_crates: Default::default(),
            format_version: 0,
        }
    }

    fn item(name: String, inner: types::ItemEnum) -> types::Item {
        types::Item {
            id: types::Id("test".to_owned()),
            crate_id: 0,
            name: Some(name),
            span: None,
            visibility: types::Visibility::Public,
            docs: None,
            links: HashMap::default(),
            attrs: vec![],
            deprecation: None,
            inner,
        }
    }

    /// Returns a function which will be expressed as `fn foo() -> ()`.
    fn foo() -> types::Function {
        types::Function {
            decl: types::FnDecl {
                inputs: vec![],
                output: None,
                c_variadic: false,
            },
            generics: types::Generics {
                params: vec![],
                where_predicates: vec![],
            },
            header: HashSet::default(),
            abi: "rust".to_owned(),
        }
    }

    #[test]
    fn compare_symbol() {
        let query = Query {
            name: Some("foo".to_owned()),
            kind: None,
        };

        let function = foo();
        let item = item("foo".to_owned(), types::ItemEnum::Function(function));
        let krate = krate();
        let mut generics = types::Generics::default();
        let mut substs = HashMap::default();

        let expected: SmallVec<[_; 10]> = smallvec![Continuous(0.0)];
        assert_eq!(
            query.compare(&item, &krate, &mut generics, &mut substs),
            expected
        )
    }

    #[test]
    fn compare_function() {
        let q = Function {
            decl: FnDecl {
                inputs: Some(vec![]),
                output: Some(FnRetTy::DefaultReturn),
            },
        };

        let i = foo();

        let krate = krate();
        let mut generics = types::Generics::default();
        let mut substs = HashMap::default();

        let expected: SmallVec<[Similarity; 10]> =
            smallvec![Discrete(Equivalent), Discrete(Equivalent)];
        assert_eq!(q.compare(&i, &krate, &mut generics, &mut substs), expected)
    }
}