]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/variance/mod.rs
Rollup merge of #59432 - phansch:compiletest_docs, r=alexcrichton
[rust.git] / src / librustc_typeck / variance / mod.rs
1 //! Module for inferring the variance of type and lifetime parameters. See the [rustc guide]
2 //! chapter for more info.
3 //!
4 //! [rustc guide]: https://rust-lang.github.io/rustc-guide/variance.html
5
6 use arena;
7 use rustc::hir;
8 use hir::Node;
9 use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
10 use rustc::ty::{self, CrateVariancesMap, TyCtxt};
11 use rustc::ty::query::Providers;
12 use rustc_data_structures::sync::Lrc;
13
14 /// Defines the `TermsContext` basically houses an arena where we can
15 /// allocate terms.
16 mod terms;
17
18 /// Code to gather up constraints.
19 mod constraints;
20
21 /// Code to solve constraints and write out the results.
22 mod solve;
23
24 /// Code to write unit tests of variance.
25 pub mod test;
26
27 /// Code for transforming variances.
28 mod xform;
29
30 pub fn provide(providers: &mut Providers<'_>) {
31     *providers = Providers {
32         variances_of,
33         crate_variances,
34         ..*providers
35     };
36 }
37
38 fn crate_variances<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum)
39                              -> Lrc<CrateVariancesMap> {
40     assert_eq!(crate_num, LOCAL_CRATE);
41     let mut arena = arena::TypedArena::default();
42     let terms_cx = terms::determine_parameters_to_be_inferred(tcx, &mut arena);
43     let constraints_cx = constraints::add_constraints_from_crate(terms_cx);
44     Lrc::new(solve::solve_constraints(constraints_cx))
45 }
46
47 fn variances_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_def_id: DefId)
48                           -> Lrc<Vec<ty::Variance>> {
49     let id = tcx.hir().as_local_hir_id(item_def_id).expect("expected local def-id");
50     let unsupported = || {
51         // Variance not relevant.
52         span_bug!(tcx.hir().span_by_hir_id(id), "asked to compute variance for wrong kind of item")
53     };
54     match tcx.hir().get_by_hir_id(id) {
55         Node::Item(item) => match item.node {
56             hir::ItemKind::Enum(..) |
57             hir::ItemKind::Struct(..) |
58             hir::ItemKind::Union(..) |
59             hir::ItemKind::Fn(..) => {}
60
61             _ => unsupported()
62         },
63
64         Node::TraitItem(item) => match item.node {
65             hir::TraitItemKind::Method(..) => {}
66
67             _ => unsupported()
68         },
69
70         Node::ImplItem(item) => match item.node {
71             hir::ImplItemKind::Method(..) => {}
72
73             _ => unsupported()
74         },
75
76         Node::ForeignItem(item) => match item.node {
77             hir::ForeignItemKind::Fn(..) => {}
78
79             _ => unsupported()
80         },
81
82         Node::Variant(_) | Node::Ctor(..) => {}
83
84         _ => unsupported()
85     }
86
87     // Everything else must be inferred.
88
89     let crate_map = tcx.crate_variances(LOCAL_CRATE);
90     crate_map.variances.get(&item_def_id)
91                        .unwrap_or(&crate_map.empty_variance)
92                        .clone()
93 }